]> code.delx.au - gnu-emacs/blobdiff - lisp/kmacro.el
*** empty log message ***
[gnu-emacs] / lisp / kmacro.el
index 5aefe46625d8742a2578312cf6521d5de83b7e9e..20816fc7feacb609c1ecac10b88eb40c077784ea 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 "22.1"
   :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
   :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
 
@@ -222,12 +223,22 @@ 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
 
 (defvar kmacro-counter 0
   "*Current keyboard macro counter.")
 
+(defvar kmacro-default-counter-format "%d")
+
 (defvar kmacro-counter-format "%d"
   "*Current keyboard macro counter format.")
 
@@ -248,7 +259,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))
@@ -257,12 +270,12 @@ With \\[universal-argument], insert previous kmacro-counter (but do not modify c
 
 (defun kmacro-set-format (format)
   "Set macro counter FORMAT."
-  (interactive "sMacro Counter Format (printf format): ")
+  (interactive "sMacro Counter Format: ")
   (setq kmacro-counter-format
        (if (equal format "") "%d" format))
   ;; redefine initial macro counter if we are not executing a macro.
   (if (not (or defining-kbd-macro executing-kbd-macro))
-      (setq kmacro-counter-format-start kmacro-counter-format)))
+      (setq kmacro-default-counter-format kmacro-counter-format)))
 
 
 (defun kmacro-display-counter (&optional value)
@@ -275,23 +288,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))
@@ -393,9 +406,12 @@ Optional arg EMPTY is message to print if no macros are defined."
       (let* ((x 60)
             (m (format-kbd-macro macro))
             (l (length m))
-            (z (and nil trunc (> l x))))
-       (message (format "%s: %s%s" (or descr "Macro")
-                        (if z (substring m 0 (1- x)) m) (if z "..." ""))))
+            (z (and trunc (> l x))))
+       (message "%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"))))
 
 
@@ -574,13 +590,16 @@ Use \\[kmacro-bind-to-key] to bind it to a key sequence."
              kmacro-initial-counter-value nil
              kmacro-counter-value-start kmacro-counter
              kmacro-last-counter kmacro-counter
-             kmacro-counter-format-start kmacro-counter-format))
+             kmacro-counter-format kmacro-default-counter-format
+             kmacro-counter-format-start kmacro-default-counter-format))
 
       (start-kbd-macro append
                       (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
@@ -595,10 +614,13 @@ With numeric arg, repeat macro now that many times,
 counting the definition just completed as the first repetition.
 An argument of zero means repeat until error."
   (interactive "P")
-  (end-kbd-macro arg #'kmacro-loop-setup-function)
-  (when (and last-kbd-macro (= (length last-kbd-macro) 0))
-    (message "Ignore empty macro")
-    (kmacro-pop-ring)))
+   ;; Isearch may push the kmacro-end-macro key sequence onto the macro.
+   ;; Just ignore it when executing the macro.
+  (unless executing-kbd-macro
+    (end-kbd-macro arg #'kmacro-loop-setup-function)
+    (when (and last-kbd-macro (= (length last-kbd-macro) 0))
+      (message "Ignore empty macro")
+      (kmacro-pop-ring))))
 
 
 ;;;###autoload
@@ -740,6 +762,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]
@@ -775,10 +821,7 @@ may be shaded by a local key binding."
                                          (format-kbd-macro key-seq)
                                          cmd))))
        (define-key global-map key-seq
-         `(lambda (&optional arg)
-            "Keyboard macro."
-            (interactive "p")
-            (kmacro-exec-ring-item ',(kmacro-ring-head) arg)))
+         (kmacro-lambda-form (kmacro-ring-head)))
        (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
 
 
@@ -798,11 +841,7 @@ Such a \"function\" cannot be called from Lisp, but it is a valid editor command
              symbol))
   (if (string-equal symbol "")
       (error "No command name given"))
-  (fset symbol
-       `(lambda (&optional arg)
-          "Keyboard macro."
-          (interactive "p")
-          (kmacro-exec-ring-item ',(kmacro-ring-head) arg)))
+  (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
   (put symbol 'kmacro t))