* erc-stamp.el (erc-echo-timestamp):
[bpt/emacs.git] / lisp / kmacro.el
index 2b4cbca..ebb3a4a 100644 (file)
@@ -1,6 +1,7 @@
 ;;; kmacro.el --- enhanced keyboard macros
 
-;; Copyright (C) 2002  Free Software Foundation, Inc.
+;; Copyright (C) 2002, 2003, 2004, 2005, 2006,
+;;   2007 Free Software Foundation, Inc.
 
 ;; Author: Kim F. Storm <storm@cua.dk>
 ;; Keywords: keyboard convenience
@@ -9,7 +10,7 @@
 
 ;; GNU Emacs 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 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
-;; The kmacro package is an alternative user interface to emacs'
-;; keyboard macro functionality.  This functionality is normally bound
-;; to C-x (, C-x ), and C-x e, but these bindings are too hard to
-;; type to be really useful for doing small repeated tasks.
+;; The kmacro package provides the user interface to emacs' basic
+;; keyboard macro functionality.  With kmacro, two function keys are
+;; dedicated to keyboard macros, by default F3 and F4.
+
+;; Note: The traditional bindings C-x (, C-x ), and C-x e are still
+;; supported, but for some users these bindings are too hard to type
+;; to be really useful for doing small repeated tasks.
 
-;; With kmacro, two function keys are dedicated to keyboard macros,
-;; by default F3 and F4.  Personally, I prefer F1 and F2, but those
-;; keys already have default bindings.
-;;
 ;; To start defining a keyboard macro, use F3.  To end the macro,
 ;; use F4, and to call the macro also use F4.  This makes it very
 ;; easy to repeat a macro immediately after defining it.
@@ -55,7 +55,6 @@
 ;; elements with C-x C-k C-t.  To delete the first element in the
 ;; macro ring, use C-x C-k C-d.
 ;;
-;;
 ;; You can also use C-x C-k C-s to start a macro, and C-x C-k C-k to
 ;; end it; then use C-k to execute it immediately, or C-x C-k C-k to
 ;; execute it later.
   "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 +222,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.")
 
@@ -246,7 +256,7 @@ macro to be executed before appending to it."
 
 (defun kmacro-insert-counter (arg)
   "Insert macro counter and increment with ARG or 1 if missing.
-With \\[universal-argument], insert previous kmacro-counter (but do not modify counter)."
+With \\[universal-argument], insert previous `kmacro-counter' (but do not modify counter)."
   (interactive "P")
   (if kmacro-initial-counter-value
       (setq kmacro-counter kmacro-initial-counter-value
@@ -259,12 +269,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)
@@ -274,7 +284,7 @@ With \\[universal-argument], insert previous kmacro-counter (but do not modify c
 
 
 (defun kmacro-set-counter (arg)
-  "Set kmacro-counter to ARG or prompt if missing.
+  "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: ")
   (if (not (or defining-kbd-macro executing-kbd-macro))
@@ -338,10 +348,8 @@ and `kmacro-counter-format'.")
 (defun kmacro-push-ring (&optional elt)
   "Push ELT or current macro onto `kmacro-ring'."
   (when (setq elt (or elt (kmacro-ring-head)))
-    (let ((len (length kmacro-ring)))
-      (setq kmacro-ring (cons elt kmacro-ring))
-      (if (>= len kmacro-ring-max)
-         (setcdr (nthcdr len kmacro-ring) nil)))))
+    (let ((history-delete-duplicates nil))
+      (add-to-history 'kmacro-ring elt kmacro-ring-max))))
 
 
 (defun kmacro-split-ring-element (elt)
@@ -366,11 +374,6 @@ Non-nil arg RAW means just return raw first element."
     (kmacro-pop-ring1 raw)))
 
 
-(defun kmacro-ring-length ()
-  "Return length of macro ring, including pseudo head."
-  (+ (if last-kbd-macro 1 0) (length kmacro-ring)))
-
-
 (defun kmacro-ring-empty-p (&optional none)
   "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
 Check only `last-kbd-macro' if optional arg NONE is non-nil."
@@ -395,17 +398,17 @@ 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%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"))))
+            (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 "%s" (or empty "No keyboard macros defined"))))
 
 
 (defun kmacro-repeat-on-last-key (keys)
-  "Process kmacro commands keys immidiately after cycling the ring."
+  "Process kmacro commands keys immediately after cycling the ring."
   (setq keys (vconcat keys))
   (let ((n (1- (length keys)))
        cmd done repeat)
@@ -451,14 +454,14 @@ Optional arg EMPTY is message to print if no macros are defined."
 
 
 (defun kmacro-call-ring-2nd (arg)
-  "Execute second keyboard macro at in macro ring."
+  "Execute second keyboard macro in macro ring."
   (interactive "P")
   (unless (kmacro-ring-empty-p)
     (kmacro-exec-ring-item (car kmacro-ring) arg)))
 
 
 (defun kmacro-call-ring-2nd-repeat (arg)
-  "Execute second keyboard macro at in macro ring.
+  "Execute second keyboard macro in macro ring.
 This is like `kmacro-call-ring-2nd', but allows repeating macro commands
 without repeating the prefix."
   (interactive "P")
@@ -566,26 +569,24 @@ Use \\[kmacro-bind-to-key] to bind it to a key sequence."
     (let ((append (and arg (listp arg))))
       (unless append
        (if last-kbd-macro
-           (let ((len (length kmacro-ring)))
-             (setq kmacro-ring
-                   (cons
-                    (list last-kbd-macro kmacro-counter kmacro-counter-format-start)
-                    kmacro-ring))
-             (if (>= len kmacro-ring-max)
-                 (setcdr (nthcdr len kmacro-ring) nil))))
+           (kmacro-push-ring
+            (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
        (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
                                 kmacro-initial-counter-value
                                 0)
              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
@@ -600,10 +601,16 @@ 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))
+      (setq last-kbd-macro nil)
+      (message "Ignore empty macro")
+      ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
+      (while (and (null last-kbd-macro) kmacro-ring)
+       (kmacro-pop-ring1)))))
 
 
 ;;;###autoload
@@ -614,7 +621,7 @@ A prefix argument serves as a repeat count.  Zero means repeat until error.
 When you call the macro, you can call the macro again by repeating
 just the last key in the key sequence that you used to call this
 command.  See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
-for details on how to adjust or disable this behaviour.
+for details on how to adjust or disable this behavior.
 
 To make a macro permanent so you can call it even after defining
 others, use \\[kmacro-name-last-macro]."
@@ -667,7 +674,7 @@ the current value of `kmacro-counter').
 
 When defining/executing macro, inserts macro counter and increments
 the counter with ARG or 1 if missing.  With \\[universal-argument],
-inserts previous kmacro-counter (but do not modify counter).
+inserts previous `kmacro-counter' (but do not modify counter).
 
 The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
 The format of the counter can be modified via \\[kmacro-set-format]."
@@ -791,8 +798,9 @@ may be shaded by a local key binding."
          ok cmd)
       (when (= (length key-seq) 1)
        (let ((ch (aref key-seq 0)))
-         (if (or (and (>= ch ?0) (<= ch ?9))
-                 (and (>= ch ?A) (<= ch ?Z)))
+         (if (and (integerp ch)
+                  (or (and (>= ch ?0) (<= ch ?9))
+                      (and (>= ch ?A) (<= ch ?Z))))
              (setq key-seq (concat "\C-x\C-k" key-seq)
                    ok t))))
       (when (and (not (equal key-seq "\a"))
@@ -938,14 +946,14 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
 (defvar kmacro-step-edit-prefix-commands
   '(universal-argument universal-argument-more universal-argument-minus
                       digit-argument negative-argument)
-  "Commands which builds up a prefix arg for the current command")
+  "Commands which build up a prefix arg for the current command.")
 
 (defun kmacro-step-edit-prompt (macro index)
   ;; Show step-edit prompt
   (let ((keys (and (not kmacro-step-edit-appending)
-                  index (substring macro index executing-macro-index)))
+                  index (substring macro index executing-kbd-macro-index)))
        (future (and (not kmacro-step-edit-appending)
-                    (substring macro executing-macro-index)))
+                    (substring macro executing-kbd-macro-index)))
        (message-log-max nil)
        (curmsg (current-message)))
 
@@ -1003,12 +1011,12 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
           (not (eq kmacro-step-edit-action t)))
       ;; Find the actual end of this key sequence.
       ;; Must be able to backtrack in case we actually execute it.
-      (setq restore-index executing-macro-index)
+      (setq restore-index executing-kbd-macro-index)
       (let (unread-command-events)
        (quoted-insert 0)
        (when unread-command-events
-         (setq executing-macro-index (- executing-macro-index (length unread-command-events))
-               next-index executing-macro-index)))))
+         (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
+               next-index executing-kbd-macro-index)))))
 
     ;; Query the user; stop macro exection temporarily
     (let ((macro executing-kbd-macro)
@@ -1028,7 +1036,7 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
            (when unread-command-events
              (setq kmacro-step-edit-new-macro
                    (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
-                   executing-macro-index (- executing-macro-index (length unread-command-events)))))
+                   executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
          (setq current-prefix-arg nil
                prefix-arg nil)
          (setq act 'ignore))
@@ -1082,24 +1090,24 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
        (setq act t)
        t)
        ((member act '(insert-1 insert))
-       (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
+       (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
        (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
        nil)
        ((member act '(replace-1 replace))
        (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
        (setq kmacro-step-edit-prefix-index nil)
-       (if (= executing-macro-index (length executing-kbd-macro))
+       (if (= executing-kbd-macro-index (length executing-kbd-macro))
            (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
                  kmacro-step-edit-appending t))
        nil)
        ((eq act 'append)
        (setq kmacro-step-edit-inserting t)
-       (if (= executing-macro-index (length executing-kbd-macro))
+       (if (= executing-kbd-macro-index (length executing-kbd-macro))
            (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
                  kmacro-step-edit-appending t))
        t)
        ((eq act 'append-end)
-       (if (= executing-macro-index (length executing-kbd-macro))
+       (if (= executing-kbd-macro-index (length executing-kbd-macro))
            (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
                  kmacro-step-edit-inserting t
                  kmacro-step-edit-appending t)
@@ -1107,21 +1115,21 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
        (setq act t)
        t)
        ((eq act 'help)
-       (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
+       (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
        (setq kmacro-step-edit-help (not kmacro-step-edit-help))
        nil)
        (t ;; Ignore unknown responses
-       (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
+       (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
        nil))
-      (if (> executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
+      (if (> executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
          (setq kmacro-step-edit-new-macro
                (vconcat kmacro-step-edit-new-macro
                         (substring executing-kbd-macro
                                    (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
-                                   (if (eq act t) nil executing-macro-index)))
+                                   (if (eq act t) nil executing-kbd-macro-index)))
                kmacro-step-edit-prefix-index nil))
       (if restore-index
-         (setq executing-macro-index restore-index)))
+         (setq executing-kbd-macro-index restore-index)))
      (t
       (setq this-command 'ignore)))
     (setq kmacro-step-edit-key-index next-index)))
@@ -1134,7 +1142,7 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
        (executing-kbd-macro nil)
        (defining-kbd-macro nil)
        cmd keys next-index)
-    (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
+    (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
          kmacro-step-edit-prefix-index nil)
     (kmacro-step-edit-prompt macro nil)
     ;; Now, we have read a key sequence from the macro, but we don't want
@@ -1155,8 +1163,8 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
            (setq kmacro-step-edit-inserting nil)
            (when unread-command-events
              (setq keys (substring keys 0 (- (length unread-command-events)))
-                   executing-macro-index (- executing-macro-index (length unread-command-events))
-                   next-index executing-macro-index
+                   executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
+                   next-index executing-kbd-macro-index
                    unread-command-events nil)))
          (setq cmd 'ignore)
          nil)
@@ -1200,7 +1208,7 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
      ((eq kmacro-step-edit-active 'ignore)
       (setq this-command 'ignore))
      ((eq kmacro-step-edit-active 'append-end)
-      (if (= executing-macro-index (length executing-kbd-macro))
+      (if (= executing-kbd-macro-index (length executing-kbd-macro))
          (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
                kmacro-step-edit-inserting t
                kmacro-step-edit-appending t
@@ -1226,8 +1234,8 @@ following additional answers: `insert', `insert-1', `replace', `replace-1',
   (when kmacro-step-edit-active
     (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
     (if kmacro-step-edit-key-index
-       (setq executing-macro-index kmacro-step-edit-key-index)
-      (setq kmacro-step-edit-key-index executing-macro-index))))
+       (setq executing-kbd-macro-index kmacro-step-edit-key-index)
+      (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
 
 
 (defun kmacro-step-edit-macro ()