Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 115 lines (96 sloc) 4.05 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
;;; repeat-macro-def.el --- make interactive funcitons repeatable by final key-chord

;; this file is not part of Emacs

;; Copyright (C) 2012 Le Wang
;; Author: Le Wang
;; Maintainer: Le Wang
;; Description: make interactive funcitons repeatable by final key-chord
;; Author: Le Wang
;; Maintainer: Le Wang

;; Created: Tue Mar 20 19:41:11 2012 (+0800)
;; Version: 0.1
;; Last-Updated: Wed Mar 21 08:02:36 2012 (+0800)
;; By: Le Wang
;; Update #: 3
;; URL:
;; Keywords:
;; Compatibility:

;;; Installation:

;; (require 'repeat-macro-def)
;; (defun-repeatable enlarge-window)
;; (defun-repeatable enlarge-window-horizontally)
;; (global-set-key [remap enlarge-window] 'enlarge-window-repeatable)
;; (global-set-key [remap enlarge-window-horizontally] 'enlarge-window-horizontally-repeatable)
;;

;;; Commentary:

;; Default binding for `enlarge-window' is "C-x ^", calling
;;
;; (defun-repeatable enlarge-window-horizontally)
;;
;; defines
;;
;; `enlarge-window-repeatable', which is repeatable by "^".
;;
;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A previous version of this function used `repeat', but setting ;;
;; `last-repeatable-command' caused the help text to show "&rest --cl-end" ;;
;; instead of the formal arguments. This is more functional as well. ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(eval-when-compile (require 'cl))

(provide 'repeat-macro-def)

(defmacro defun-repeatable-int (name args &rest body)
  "Define interactive function that can be repeated by pressing last key cord."
  (declare (indent defun))
  (let* ((res body)
         (doc-string (when (stringp (car res))
                       (list (pop res))))
         (interactive-form (if (eq 'interactive (caar res))
                               (pop res)
                             (error "definition is not interactive"))))
    `(defun ,name ,args
       ,@doc-string
       ,interactive-form
       (let* ((repeat-key (and (> (length (this-single-command-keys)) 1)
                               last-input-event))
              (repeat-key-str (format-kbd-macro (vector repeat-key) nil))
              event)
         (loop do ,(cons 'progn res)
               while (progn
                       (message "(Type %s to repeat)" repeat-key-str)
                       (setq event (read-event))
                       (clear-this-command-keys t)
                       (if (equal event repeat-key)
                           (progn
                             (setq last-input-event nil)
                             t)
                         (push last-input-event unread-command-events)
                         nil)))))))


(defmacro defun-repeatable (func)
  (let ((new-func (intern (concat (symbol-name func) "-repeatable"))))
     (progn
     `(defun-repeatable-int ,new-func ()
        ,(format "repeatable version of `%s'" func)
        (interactive)
        (call-interactively (quote ,func))))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; repeat-macro-def.el ends here

(provide 'repeat-macro-def)
Something went wrong with that request. Please try again.