]> code.delx.au - gnu-emacs/blobdiff - lisp/kmacro.el
Add a provide statement.
[gnu-emacs] / lisp / kmacro.el
index a129ab53cefbf07cb3807cb9517b21968d36bdf8..9ee34a8432cf241f65ecc19de9956da9f90539a7 100644 (file)
@@ -1,6 +1,6 @@
 ;;; kmacro.el --- enhanced keyboard macros
 
-;; Copyright (C) 2002  Free Software Foundation, Inc.
+;; Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 ;; Author: Kim F. Storm <storm@cua.dk>
 ;; Keywords: keyboard convenience
   "Simplified keyboard macro user interface."
   :group 'keyboard
   :group 'convenience
+  :version "21.4"
   :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
   :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
 
@@ -204,7 +205,7 @@ macro to be executed before appending to it."
 
     ;; naming and binding
     (define-key map "b"    'kmacro-bind-to-key)
-    (define-key map "n"    'name-last-kbd-macro)
+    (define-key map "n"    'kmacro-name-last-macro)
     map)
   "Keymap for keyboard macro commands.")
 (defalias 'kmacro-keymap kmacro-keymap)
@@ -222,6 +223,14 @@ macro to be executed before appending to it."
   (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
 
 
+;;; Called from keyboard-quit
+
+(defun kmacro-keyboard-quit ()
+  (or (not defining-kbd-macro)
+      (eq defining-kbd-macro 'append)
+      (kmacro-ring-empty-p)
+      (kmacro-pop-ring)))
+
 
 ;;; Keyboard macro counter
 
@@ -248,7 +257,9 @@ macro to be executed before appending to it."
   "Insert macro counter and increment with ARG or 1 if missing.
 With \\[universal-argument], insert previous kmacro-counter (but do not modify counter)."
   (interactive "P")
-  (setq kmacro-initial-counter-value nil)
+  (if kmacro-initial-counter-value
+      (setq kmacro-counter kmacro-initial-counter-value
+           kmacro-initial-counter-value nil))
   (if (and arg (listp arg))
       (insert (format kmacro-counter-format kmacro-last-counter))
     (insert (format kmacro-counter-format kmacro-counter))
@@ -275,23 +286,23 @@ With \\[universal-argument], insert previous kmacro-counter (but do not modify c
   "Set kmacro-counter to ARG or prompt if missing.
 With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
   (interactive "NMacro counter value: ")
-  (setq kmacro-last-counter kmacro-counter
-       kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
-                          kmacro-counter-value-start
-                        arg))
-  ;; setup initial macro counter value if we are not executing a macro.
-  (setq kmacro-initial-counter-value
-       (and (not (or defining-kbd-macro executing-kbd-macro))
-            kmacro-counter))
-  (unless executing-kbd-macro
-    (kmacro-display-counter)))
+  (if (not (or defining-kbd-macro executing-kbd-macro))
+      (kmacro-display-counter (setq kmacro-initial-counter-value arg))
+    (setq kmacro-last-counter kmacro-counter
+         kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
+                            kmacro-counter-value-start
+                          arg))
+    (unless executing-kbd-macro
+      (kmacro-display-counter))))
 
 
 (defun kmacro-add-counter (arg)
   "Add numeric prefix arg (prompt if missing) to macro counter.
 With \\[universal-argument], restore previous counter value."
   (interactive "NAdd to macro counter: ")
-  (setq kmacro-initial-counter-value nil)
+  (if kmacro-initial-counter-value
+      (setq kmacro-counter kmacro-initial-counter-value
+           kmacro-initial-counter-value nil))
   (let ((last kmacro-last-counter))
     (setq kmacro-last-counter kmacro-counter
          kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
@@ -394,7 +405,10 @@ Optional arg EMPTY is message to print if no macros are defined."
             (m (format-kbd-macro macro))
             (l (length m))
             (z (and nil trunc (> l x))))
-       (message (format "%s: %s%s" (or descr "Macro")
+       (message (format "%s%s: %s%s" (or descr "Macro")
+                        (if (= kmacro-counter 0) ""
+                          (format " [%s]"
+                                  (format kmacro-counter-format-start kmacro-counter)))
                         (if z (substring m 0 (1- x)) m) (if z "..." ""))))
     (message (or empty "No keyboard macros defined"))))
 
@@ -539,8 +553,8 @@ Displays the selected macro in the echo area."
 The commands are recorded even as they are executed.
 Use \\[kmacro-end-macro] to finish recording and make the macro available.
 Use \\[kmacro-end-and-call-macro] to execute the macro.
-Use \\[name-last-kbd-macro] to give it a permanent name.
-Non-nil arg (prefix arg) means append to last macro defined;
+
+Non-nil arg (prefix arg) means append to last macro defined.
 
 With \\[universal-argument] prefix, append to last keyboard macro
 defined.  Depending on `kmacro-execute-before-append', this may begin
@@ -551,7 +565,10 @@ defining the macro.
 
 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
-The format of the counter can be modified via \\[kmacro-set-format]."
+The format of the counter can be modified via \\[kmacro-set-format].
+
+Use \\[kmacro-name-last-macro] to give it a permanent name.
+Use \\[kmacro-bind-to-key] to bind it to a key sequence."
   (interactive "P")
   (if (or defining-kbd-macro executing-kbd-macro)
       (message "Already defining keyboard macro.")
@@ -577,7 +594,9 @@ The format of the counter can be modified via \\[kmacro-set-format]."
                       (and append
                            (if kmacro-execute-before-append
                                (> (car arg) 4)
-                             (= (car arg) 4)))))))
+                             (= (car arg) 4))))
+      (if (and defining-kbd-macro append)
+         (setq defining-kbd-macro 'append)))))
 
 
 ;;;###autoload
@@ -585,7 +604,7 @@ The format of the counter can be modified via \\[kmacro-set-format]."
   "Finish defining a keyboard macro.
 The definition was started by \\[kmacro-start-macro].
 The macro is now available for use via \\[kmacro-call-macro],
-or it can be given a name with \\[name-last-kbd-macro] and then invoked
+or it can be given a name with \\[kmacro-name-last-macro] and then invoked
 under that name.
 
 With numeric arg, repeat macro now that many times,
@@ -609,7 +628,7 @@ command.  See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
 for details on how to adjust or disable this behaviour.
 
 To make a macro permanent so you can call it even after defining
-others, use \\[name-last-kbd-macro]."
+others, use \\[kmacro-name-last-macro]."
   (interactive "p")
   (let ((repeat-key (and (null no-repeat)
                         (> (length (this-single-command-keys)) 1)
@@ -707,7 +726,7 @@ With numeric prefix ARG, repeat macro that many times.
 Zero argument means repeat until there is an error.
 
 To give a macro a permanent name, so you can call it
-even after defining other macros, use \\[name-last-kbd-macro]."
+even after defining other macros, use \\[kmacro-name-last-macro]."
   (interactive "P")
   (if defining-kbd-macro
       (kmacro-end-macro nil))
@@ -737,6 +756,30 @@ If kbd macro currently being defined end it before activating it."
 ;; letters and digits, provided that we inhibit the keymap while
 ;; executing the macro later on (but that's controversial...)
 
+(defun kmacro-lambda-form (mac &optional counter format)
+  "Create lambda form for macro bound to symbol or key."
+  (if counter
+      (setq mac (list mac counter format)))
+  `(lambda (&optional arg)
+     "Keyboard macro."
+     (interactive "p")
+     (kmacro-exec-ring-item ',mac arg)))
+
+(defun kmacro-extract-lambda (mac)
+  "Extract kmacro from a kmacro lambda form."
+  (and (consp mac)
+       (eq (car mac) 'lambda)
+       (setq mac (assoc 'kmacro-exec-ring-item mac))
+       (consp (cdr mac))
+       (consp (car (cdr mac)))
+       (consp (cdr (car (cdr mac))))
+       (setq mac (car (cdr (car (cdr mac)))))
+       (listp mac)
+       (= (length mac) 3)
+       (arrayp (car mac))
+       mac))
+
+
 (defun kmacro-bind-to-key (arg)
   "When not defining or executing a macro, offer to bind last macro to a key.
 The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
@@ -771,10 +814,31 @@ may be shaded by a local key binding."
                     (yes-or-no-p (format "%s runs command %S.  Bind anyway? "
                                          (format-kbd-macro key-seq)
                                          cmd))))
-       (define-key global-map key-seq last-kbd-macro)
+       (define-key global-map key-seq
+         (kmacro-lambda-form (kmacro-ring-head)))
        (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
 
 
+(defun kmacro-name-last-macro (symbol)
+  "Assign a name to the last keyboard macro defined.
+Argument SYMBOL is the name to define.
+The symbol's function definition becomes the keyboard macro string.
+Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
+  (interactive "SName for last kbd macro: ")
+  (or last-kbd-macro
+      (error "No keyboard macro defined"))
+  (and (fboundp symbol)
+       (not (get symbol 'kmacro))
+       (not (stringp (symbol-function symbol)))
+       (not (vectorp (symbol-function symbol)))
+       (error "Function %s is already defined and not a keyboard macro"
+             symbol))
+  (if (string-equal symbol "")
+      (error "No command name given"))
+  (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
+  (put symbol 'kmacro t))
+
+
 (defun kmacro-view-macro (&optional arg)
   "Display the last keyboard macro.
 If repeated, it shows previous elements in the macro ring."
@@ -897,7 +961,8 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
        (curmsg (current-message)))
 
     ;; TODO: Scroll macro if max-mini-window-height is too small.
-    (message (concat
+    (message "%s"
+            (concat
              (format "Macro: %s%s%s%s%s\n"
                      (format-kbd-macro kmacro-step-edit-new-macro 1)
                      (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")