(with-current-buffer): Doc fix.
[emacs.git] / lisp / kmacro.el
blobd3db76fcc8a8f4323dae64d471e03163d497f618
1 ;;; kmacro.el --- enhanced keyboard macros
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard convenience
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; The kmacro package is an alternative user interface to emacs'
28 ;; keyboard macro functionality. This functionality is normally bound
29 ;; to C-x (, C-x ), and C-x e, but these bindings are too hard to
30 ;; type to be really useful for doing small repeated tasks.
32 ;; With kmacro, two function keys are dedicated to keyboard macros,
33 ;; by default F3 and F4. Personally, I prefer F1 and F2, but those
34 ;; keys already have default bindings.
36 ;; To start defining a keyboard macro, use F3. To end the macro,
37 ;; use F4, and to call the macro also use F4. This makes it very
38 ;; easy to repeat a macro immediately after defining it.
40 ;; You can call the macro repeatedly by pressing F4 multiple times, or
41 ;; you can give a numeric prefix argument specifying the number of
42 ;; times to repeat the macro. Macro execution automatically
43 ;; terminates when point reaches the end of the buffer or if an error
44 ;; is signalled by ringing the bell.
46 ;; When you define a macro with F3/F4, it is automatically added to
47 ;; the head of the "keyboard macro ring", and F4 actually executes the
48 ;; first element of the macro ring.
50 ;; Note: an empty macro is never added to the macro ring.
52 ;; You can execute the second element on the macro ring with C-u F4 or
53 ;; C-x C-k C-l, you can use C-x C-k C-p and C-x C-k C-n to cycle
54 ;; through the macro ring, and you can swap the first and second
55 ;; elements with C-x C-k C-t. To delete the first element in the
56 ;; macro ring, use C-x C-k C-d.
59 ;; You can also use C-x C-k C-s to start a macro, and C-x C-k C-k to
60 ;; end it; then use C-k to execute it immediately, or C-x C-k C-k to
61 ;; execute it later.
63 ;; In general, immediately after using C-x C-k followed by one of C-k,
64 ;; C-l, C-p, or C-n, you can further cycle the macro ring using C-p or
65 ;; C-n, execute the first or second macro using C-k or C-l, delete
66 ;; the head macro with C-d, or edit the current macro with C-e without
67 ;; repeating the C-x C-k prefix.
69 ;; If you enter F3 while defining the macro, the numeric value of
70 ;; `kmacro-counter' is inserted using the `kmacro-counter-format', and
71 ;; `kmacro-counter' is incremented by 1 (or the numeric prefix value
72 ;; of F3).
74 ;; The initial value of `kmacro-counter' is 0, or the numeric prefix
75 ;; value given to F3 when starting the macro.
77 ;; Now, each time you call the macro using F4, the current
78 ;; value of `kmacro-counter' is inserted and incremented, making it
79 ;; easy to insert incremental numbers in the buffer.
81 ;; Example:
83 ;; The following sequence: M-5 F3 x M-2 F3 y F4 F4 F4 F4
84 ;; inserts the following string: x5yx7yx9yx11y
86 ;; A macro can also be called using a mouse click, default S-mouse-3.
87 ;; This calls the macro at the point where you click the mouse.
89 ;; You can edit the last macro using C-x C-k C-e.
91 ;; You can append to the last macro using C-u F3.
93 ;; You can set the macro counter using C-x C-k C-c, add to it using C-x C-k C-a,
94 ;; and you can set the macro counter format with C-x C-k C-f.
96 ;; The following key bindings are performed:
98 ;; Normal While defining macro
99 ;; --------------------------- ------------------------------
100 ;; f3 Define macro Insert current counter value
101 ;; Prefix arg specifies initial and increase counter by prefix
102 ;; counter value (default 0) (default increment: 1)
104 ;; C-u f3 APPENDs to last macro
106 ;; f4 Call last macro End macro
107 ;; Prefix arg specifies number
108 ;; of times to execute macro.
110 ;; C-u f4 Swap last and head of macro ring.
112 ;; S-mouse-3 Set point at click and End macro and execute macro at
113 ;; execute last macro. click.
115 ;;; Code:
117 ;; Customization:
119 (defgroup kmacro nil
120 "Simplified keyboard macro user interface."
121 :group 'keyboard
122 :group 'convenience
123 :version "22.1"
124 :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
125 :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
127 (defcustom kmacro-call-mouse-event 'S-mouse-3
128 "The mouse event used by kmacro to call a macro.
129 Set to nil if no mouse binding is desired."
130 :type 'symbol
131 :group 'kmacro)
133 (defcustom kmacro-ring-max 8
134 "Maximum number of keyboard macros to save in macro ring."
135 :type 'integer
136 :group 'kmacro)
139 (defcustom kmacro-execute-before-append t
140 "Controls whether appending to a macro starts by executing the macro.
141 If non-nil, using a single \\[universal-argument] prefix executes the macro
142 before appending, while more than one \\[universal-argument] prefix does not
143 execute the macro.
144 Otherwise, a single \\[universal-argument] prefix does not execute the
145 macro, while more than one \\[universal-argument] prefix causes the
146 macro to be executed before appending to it."
147 :type 'boolean
148 :group 'kmacro)
151 (defcustom kmacro-repeat-no-prefix t
152 "Allow repeating certain macro commands without entering the C-x C-k prefix."
153 :type 'boolean
154 :group 'kmacro)
156 (defcustom kmacro-call-repeat-key t
157 "Allow repeating macro call using last key or a specific key."
158 :type '(choice (const :tag "Disabled" nil)
159 (const :tag "Last key" t)
160 (character :tag "Character" :value ?e)
161 (symbol :tag "Key symbol" :value RET))
162 :group 'kmacro)
164 (defcustom kmacro-call-repeat-with-arg nil
165 "Repeat macro call with original arg when non-nil; repeat once if nil."
166 :type 'boolean
167 :group 'kmacro)
169 (defcustom kmacro-step-edit-mini-window-height 0.75
170 "Override `max-mini-window-height' when step edit keyboard macro."
171 :type 'number
172 :group 'kmacro)
174 ;; Keymap
176 (defvar kmacro-keymap
177 (let ((map (make-sparse-keymap)))
178 ;; Start, end, execute macros
179 (define-key map "s" 'kmacro-start-macro)
180 (define-key map "\C-s" 'kmacro-start-macro)
181 (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
182 (define-key map "r" 'apply-macro-to-region-lines)
183 (define-key map "q" 'kbd-macro-query) ;; Like C-x q
185 ;; macro ring
186 (define-key map "\C-n" 'kmacro-cycle-ring-next)
187 (define-key map "\C-p" 'kmacro-cycle-ring-previous)
188 (define-key map "\C-v" 'kmacro-view-macro-repeat)
189 (define-key map "\C-d" 'kmacro-delete-ring-head)
190 (define-key map "\C-t" 'kmacro-swap-ring)
191 (define-key map "\C-l" 'kmacro-call-ring-2nd-repeat)
193 ;; macro counter
194 (define-key map "\C-f" 'kmacro-set-format)
195 (define-key map "\C-c" 'kmacro-set-counter)
196 (define-key map "\C-i" 'kmacro-insert-counter)
197 (define-key map "\C-a" 'kmacro-add-counter)
199 ;; macro editing
200 (define-key map "\C-e" 'kmacro-edit-macro-repeat)
201 (define-key map "\r" 'kmacro-edit-macro)
202 (define-key map "e" 'edit-kbd-macro)
203 (define-key map "l" 'kmacro-edit-lossage)
204 (define-key map " " 'kmacro-step-edit-macro)
206 ;; naming and binding
207 (define-key map "b" 'kmacro-bind-to-key)
208 (define-key map "n" 'kmacro-name-last-macro)
209 map)
210 "Keymap for keyboard macro commands.")
211 (defalias 'kmacro-keymap kmacro-keymap)
213 ;;; Provide some binding for startup:
214 ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
215 ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
216 ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
217 ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
218 ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
219 ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
220 ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
222 (if kmacro-call-mouse-event
223 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
226 ;;; Called from keyboard-quit
228 (defun kmacro-keyboard-quit ()
229 (or (not defining-kbd-macro)
230 (eq defining-kbd-macro 'append)
231 (kmacro-ring-empty-p)
232 (kmacro-pop-ring)))
235 ;;; Keyboard macro counter
237 (defvar kmacro-counter 0
238 "*Current keyboard macro counter.")
240 (defvar kmacro-default-counter-format "%d")
242 (defvar kmacro-counter-format "%d"
243 "*Current keyboard macro counter format.")
245 (defvar kmacro-counter-format-start kmacro-counter-format
246 "Macro format at start of macro execution.")
248 (defvar kmacro-counter-value-start kmacro-counter
249 "Macro counter at start of macro execution.")
251 (defvar kmacro-last-counter 0
252 "Last counter inserted by key macro.")
254 (defvar kmacro-initial-counter-value nil
255 "Initial counter value for the next keyboard macro to be defined.")
258 (defun kmacro-insert-counter (arg)
259 "Insert macro counter and increment with ARG or 1 if missing.
260 With \\[universal-argument], insert previous kmacro-counter (but do not modify counter)."
261 (interactive "P")
262 (if kmacro-initial-counter-value
263 (setq kmacro-counter kmacro-initial-counter-value
264 kmacro-initial-counter-value nil))
265 (if (and arg (listp arg))
266 (insert (format kmacro-counter-format kmacro-last-counter))
267 (insert (format kmacro-counter-format kmacro-counter))
268 (kmacro-add-counter (prefix-numeric-value arg))))
271 (defun kmacro-set-format (format)
272 "Set macro counter FORMAT."
273 (interactive "sMacro Counter Format: ")
274 (setq kmacro-counter-format
275 (if (equal format "") "%d" format))
276 ;; redefine initial macro counter if we are not executing a macro.
277 (if (not (or defining-kbd-macro executing-kbd-macro))
278 (setq kmacro-default-counter-format kmacro-counter-format)))
281 (defun kmacro-display-counter (&optional value)
282 "Display current counter value."
283 (unless value (setq value kmacro-counter))
284 (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value))
287 (defun kmacro-set-counter (arg)
288 "Set kmacro-counter to ARG or prompt if missing.
289 With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
290 (interactive "NMacro counter value: ")
291 (if (not (or defining-kbd-macro executing-kbd-macro))
292 (kmacro-display-counter (setq kmacro-initial-counter-value arg))
293 (setq kmacro-last-counter kmacro-counter
294 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
295 kmacro-counter-value-start
296 arg))
297 (unless executing-kbd-macro
298 (kmacro-display-counter))))
301 (defun kmacro-add-counter (arg)
302 "Add numeric prefix arg (prompt if missing) to macro counter.
303 With \\[universal-argument], restore previous counter value."
304 (interactive "NAdd to macro counter: ")
305 (if kmacro-initial-counter-value
306 (setq kmacro-counter kmacro-initial-counter-value
307 kmacro-initial-counter-value nil))
308 (let ((last kmacro-last-counter))
309 (setq kmacro-last-counter kmacro-counter
310 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
311 last
312 kmacro-counter (+ kmacro-counter arg))))
313 (unless executing-kbd-macro
314 (kmacro-display-counter)))
317 (defun kmacro-loop-setup-function ()
318 "Function called prior to each iteration of macro."
319 ;; Restore macro counter format to initial format, so it is ok to change
320 ;; counter format in the macro without restoring it.
321 (setq kmacro-counter-format kmacro-counter-format-start)
322 ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
323 (setq kmacro-counter-value-start kmacro-counter)
324 ;; Return non-nil to continue execution.
328 ;;; Keyboard macro ring
330 (defvar kmacro-ring nil
331 "The keyboard macro ring.
332 Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
333 the macro ring (when defining or executing) is not stored in the ring;
334 instead it is available in the variables `last-kbd-macro', `kmacro-counter',
335 and `kmacro-counter-format'.")
337 ;; Remember what we are currently looking at with kmacro-view-macro.
339 (defvar kmacro-view-last-item nil)
340 (defvar kmacro-view-item-no 0)
343 (defun kmacro-ring-head ()
344 "Return pseudo head element in macro ring."
345 (and last-kbd-macro
346 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
349 (defun kmacro-push-ring (&optional elt)
350 "Push ELT or current macro onto `kmacro-ring'."
351 (when (setq elt (or elt (kmacro-ring-head)))
352 (let ((history-delete-duplicates nil))
353 (add-to-history 'kmacro-ring elt kmacro-ring-max))))
356 (defun kmacro-split-ring-element (elt)
357 (setq last-kbd-macro (car elt)
358 kmacro-counter (nth 1 elt)
359 kmacro-counter-format-start (nth 2 elt)))
362 (defun kmacro-pop-ring1 (&optional raw)
363 "Pop head element off macro ring (no check).
364 Non-nil arg RAW means just return raw first element."
365 (prog1 (car kmacro-ring)
366 (unless raw
367 (kmacro-split-ring-element (car kmacro-ring)))
368 (setq kmacro-ring (cdr kmacro-ring))))
371 (defun kmacro-pop-ring (&optional raw)
372 "Pop head element off macro ring.
373 Non-nil arg RAW means just return raw first element."
374 (unless (kmacro-ring-empty-p)
375 (kmacro-pop-ring1 raw)))
378 (defun kmacro-ring-empty-p (&optional none)
379 "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
380 Check only `last-kbd-macro' if optional arg NONE is non-nil."
381 (while (and (null last-kbd-macro) kmacro-ring)
382 (kmacro-pop-ring1))
383 (cond
384 ((null last-kbd-macro)
385 (message "No keyboard macro defined.")
387 ((and (null none) (null kmacro-ring))
388 (message "Only one keyboard macro defined.")
390 (t nil)))
393 (defun kmacro-display (macro &optional trunc descr empty)
394 "Display a keyboard MACRO.
395 Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
396 Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
397 Optional arg EMPTY is message to print if no macros are defined."
398 (if macro
399 (let* ((x 60)
400 (m (format-kbd-macro macro))
401 (l (length m))
402 (z (and trunc (> l x))))
403 (message "%s%s: %s%s" (or descr "Macro")
404 (if (= kmacro-counter 0) ""
405 (format " [%s]"
406 (format kmacro-counter-format-start kmacro-counter)))
407 (if z (substring m 0 (1- x)) m) (if z "..." "")))
408 (message (or empty "No keyboard macros defined"))))
411 (defun kmacro-repeat-on-last-key (keys)
412 "Process kmacro commands keys immidiately after cycling the ring."
413 (setq keys (vconcat keys))
414 (let ((n (1- (length keys)))
415 cmd done repeat)
416 (while (and last-kbd-macro
417 (not done)
418 (aset keys n (read-event))
419 (setq cmd (key-binding keys t))
420 (setq repeat (get cmd 'kmacro-repeat)))
421 (clear-this-command-keys t)
422 (cond
423 ((eq repeat 'ring)
424 (if kmacro-ring
425 (let ((kmacro-repeat-no-prefix nil))
426 (funcall cmd nil))
427 (kmacro-display last-kbd-macro t)))
428 ((eq repeat 'head)
429 (let ((kmacro-repeat-no-prefix nil))
430 (funcall cmd nil)))
431 ((eq repeat 'stop)
432 (funcall cmd nil)
433 (setq done t)))
434 (setq last-input-event nil)))
435 (when last-input-event
436 (clear-this-command-keys t)
437 (setq unread-command-events (list last-input-event))))
440 (defun kmacro-get-repeat-prefix ()
441 (let (keys)
442 (and kmacro-repeat-no-prefix
443 (setq keys (this-single-command-keys))
444 (> (length keys) 1)
445 keys)))
448 (defun kmacro-exec-ring-item (item arg)
449 "Execute item ITEM from the macro ring."
450 ;; Use counter and format specific to the macro on the ring!
451 (let ((kmacro-counter (nth 1 item))
452 (kmacro-counter-format-start (nth 2 item)))
453 (execute-kbd-macro (car item) arg #'kmacro-loop-setup-function)
454 (setcar (cdr item) kmacro-counter)))
457 (defun kmacro-call-ring-2nd (arg)
458 "Execute second keyboard macro at in macro ring."
459 (interactive "P")
460 (unless (kmacro-ring-empty-p)
461 (kmacro-exec-ring-item (car kmacro-ring) arg)))
464 (defun kmacro-call-ring-2nd-repeat (arg)
465 "Execute second keyboard macro at in macro ring.
466 This is like `kmacro-call-ring-2nd', but allows repeating macro commands
467 without repeating the prefix."
468 (interactive "P")
469 (let ((keys (kmacro-get-repeat-prefix)))
470 (kmacro-call-ring-2nd arg)
471 (if (and kmacro-ring keys)
472 (kmacro-repeat-on-last-key keys))))
474 (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
477 (defun kmacro-view-ring-2nd ()
478 "Display the current head of the keyboard macro ring."
479 (interactive)
480 (unless (kmacro-ring-empty-p)
481 (kmacro-display (car (car kmacro-ring)) "2nd macro")))
484 (defun kmacro-cycle-ring-next (&optional arg)
485 "Move to next keyboard macro in keyboard macro ring.
486 Displays the selected macro in the echo area."
487 (interactive)
488 (unless (kmacro-ring-empty-p)
489 (kmacro-push-ring)
490 (let* ((keys (kmacro-get-repeat-prefix))
491 (len (length kmacro-ring))
492 (tail (nthcdr (- len 2) kmacro-ring))
493 (elt (car (cdr tail))))
494 (setcdr tail nil)
495 (kmacro-split-ring-element elt)
496 (kmacro-display last-kbd-macro t)
497 (if keys
498 (kmacro-repeat-on-last-key keys)))))
500 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
503 (defun kmacro-cycle-ring-previous (&optional arg)
504 "Move to previous keyboard macro in keyboard macro ring.
505 Displays the selected macro in the echo area."
506 (interactive)
507 (unless (kmacro-ring-empty-p)
508 (let ((keys (kmacro-get-repeat-prefix))
509 (cur (kmacro-ring-head)))
510 (kmacro-pop-ring1)
511 (if kmacro-ring
512 (nconc kmacro-ring (list cur))
513 (setq kmacro-ring (list cur)))
514 (kmacro-display last-kbd-macro t)
515 (if keys
516 (kmacro-repeat-on-last-key keys)))))
518 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
521 (defun kmacro-swap-ring ()
522 "Swap first two elements on keyboard macro ring."
523 (interactive)
524 (unless (kmacro-ring-empty-p)
525 (let ((cur (kmacro-ring-head)))
526 (kmacro-pop-ring1)
527 (kmacro-push-ring cur))
528 (kmacro-display last-kbd-macro t)))
531 (defun kmacro-delete-ring-head (&optional arg)
532 "Delete current macro from keyboard macro ring."
533 (interactive)
534 (unless (kmacro-ring-empty-p t)
535 (if (null kmacro-ring)
536 (setq last-kbd-macro nil)
537 (kmacro-pop-ring))
538 (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
540 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
542 ;;; Traditional bindings:
545 ;;;###autoload
546 (defun kmacro-start-macro (arg)
547 "Record subsequent keyboard input, defining a keyboard macro.
548 The commands are recorded even as they are executed.
549 Use \\[kmacro-end-macro] to finish recording and make the macro available.
550 Use \\[kmacro-end-and-call-macro] to execute the macro.
552 Non-nil arg (prefix arg) means append to last macro defined.
554 With \\[universal-argument] prefix, append to last keyboard macro
555 defined. Depending on `kmacro-execute-before-append', this may begin
556 by re-executing the last macro as if you typed it again.
558 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
559 defining the macro.
561 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
562 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
563 The format of the counter can be modified via \\[kmacro-set-format].
565 Use \\[kmacro-name-last-macro] to give it a permanent name.
566 Use \\[kmacro-bind-to-key] to bind it to a key sequence."
567 (interactive "P")
568 (if (or defining-kbd-macro executing-kbd-macro)
569 (message "Already defining keyboard macro.")
570 (let ((append (and arg (listp arg))))
571 (unless append
572 (if last-kbd-macro
573 (kmacro-push-ring
574 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
575 (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
576 kmacro-initial-counter-value
578 kmacro-initial-counter-value nil
579 kmacro-counter-value-start kmacro-counter
580 kmacro-last-counter kmacro-counter
581 kmacro-counter-format kmacro-default-counter-format
582 kmacro-counter-format-start kmacro-default-counter-format))
584 (start-kbd-macro append
585 (and append
586 (if kmacro-execute-before-append
587 (> (car arg) 4)
588 (= (car arg) 4))))
589 (if (and defining-kbd-macro append)
590 (setq defining-kbd-macro 'append)))))
593 ;;;###autoload
594 (defun kmacro-end-macro (arg)
595 "Finish defining a keyboard macro.
596 The definition was started by \\[kmacro-start-macro].
597 The macro is now available for use via \\[kmacro-call-macro],
598 or it can be given a name with \\[kmacro-name-last-macro] and then invoked
599 under that name.
601 With numeric arg, repeat macro now that many times,
602 counting the definition just completed as the first repetition.
603 An argument of zero means repeat until error."
604 (interactive "P")
605 ;; Isearch may push the kmacro-end-macro key sequence onto the macro.
606 ;; Just ignore it when executing the macro.
607 (unless executing-kbd-macro
608 (end-kbd-macro arg #'kmacro-loop-setup-function)
609 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
610 (message "Ignore empty macro")
611 (kmacro-pop-ring))))
614 ;;;###autoload
615 (defun kmacro-call-macro (arg &optional no-repeat end-macro)
616 "Call the last keyboard macro that you defined with \\[kmacro-start-macro].
617 A prefix argument serves as a repeat count. Zero means repeat until error.
619 When you call the macro, you can call the macro again by repeating
620 just the last key in the key sequence that you used to call this
621 command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
622 for details on how to adjust or disable this behavior.
624 To make a macro permanent so you can call it even after defining
625 others, use \\[kmacro-name-last-macro]."
626 (interactive "p")
627 (let ((repeat-key (and (null no-repeat)
628 (> (length (this-single-command-keys)) 1)
629 last-input-event))
630 repeat-key-str)
631 (if end-macro
632 (kmacro-end-macro arg)
633 (call-last-kbd-macro arg #'kmacro-loop-setup-function))
634 (when (consp arg)
635 (setq arg (car arg)))
636 (when (and (or (null arg) (> arg 0))
637 (setq repeat-key
638 (if (eq kmacro-call-repeat-key t)
639 repeat-key
640 kmacro-call-repeat-key)))
641 (setq repeat-key-str (format-kbd-macro (vector repeat-key) nil))
642 (while repeat-key
643 (message "(Type %s to repeat macro%s)"
644 repeat-key-str
645 (if (and kmacro-call-repeat-with-arg
646 arg (> arg 1))
647 (format " %d times" arg) ""))
648 (if (equal repeat-key (read-event))
649 (progn
650 (clear-this-command-keys t)
651 (call-last-kbd-macro (and kmacro-call-repeat-with-arg arg)
652 #'kmacro-loop-setup-function)
653 (setq last-input-event nil))
654 (setq repeat-key nil)))
655 (when last-input-event
656 (clear-this-command-keys t)
657 (setq unread-command-events (list last-input-event))))))
660 ;;; Combined function key bindings:
662 ;;;###autoload
663 (defun kmacro-start-macro-or-insert-counter (arg)
664 "Record subsequent keyboard input, defining a keyboard macro.
665 The commands are recorded even as they are executed.
667 Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
668 macro.
670 With \\[universal-argument], appends to current keyboard macro (keeping
671 the current value of `kmacro-counter').
673 When defining/executing macro, inserts macro counter and increments
674 the counter with ARG or 1 if missing. With \\[universal-argument],
675 inserts previous kmacro-counter (but do not modify counter).
677 The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
678 The format of the counter can be modified via \\[kmacro-set-format]."
679 (interactive "P")
680 (if (or defining-kbd-macro executing-kbd-macro)
681 (kmacro-insert-counter arg)
682 (kmacro-start-macro arg)))
685 ;;;###autoload
686 (defun kmacro-end-or-call-macro (arg &optional no-repeat)
687 "End kbd macro if currently being defined; else call last kbd macro.
688 With numeric prefix ARG, repeat macro that many times.
689 With \\[universal-argument], call second macro in macro ring."
690 (interactive "P")
691 (cond
692 (defining-kbd-macro
693 (if kmacro-call-repeat-key
694 (kmacro-call-macro arg no-repeat t)
695 (kmacro-end-macro arg)))
696 ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
697 kmacro-view-last-item)
698 (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
699 ((and arg (listp arg))
700 (kmacro-call-ring-2nd 1))
702 (kmacro-call-macro arg no-repeat))))
705 (defun kmacro-end-or-call-macro-repeat (arg)
706 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
707 (interactive "P")
708 (let ((keys (kmacro-get-repeat-prefix)))
709 (kmacro-end-or-call-macro arg t)
710 (if keys
711 (kmacro-repeat-on-last-key keys))))
713 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
716 ;;;###autoload
717 (defun kmacro-end-and-call-macro (arg &optional no-repeat)
718 "Call last keyboard macro, ending it first if currently being defined.
719 With numeric prefix ARG, repeat macro that many times.
720 Zero argument means repeat until there is an error.
722 To give a macro a permanent name, so you can call it
723 even after defining other macros, use \\[kmacro-name-last-macro]."
724 (interactive "P")
725 (if defining-kbd-macro
726 (kmacro-end-macro nil))
727 (kmacro-call-macro arg no-repeat))
730 ;;;###autoload
731 (defun kmacro-end-call-mouse (event)
732 "Move point to the position clicked with the mouse and call last kbd macro.
733 If kbd macro currently being defined end it before activating it."
734 (interactive "e")
735 (when defining-kbd-macro
736 (end-kbd-macro))
737 (mouse-set-point event)
738 (kmacro-call-macro nil t))
741 ;;; Misc. commands
743 ;; An idea for macro bindings:
744 ;; Create a separate keymap installed as a minor-mode keymap (e.g. in
745 ;; the emulation-mode-map-alists) in which macro bindings are made
746 ;; independent of any other bindings. When first binding is made,
747 ;; the kemap is created, installed, and enabled. Key seq. C-x C-k +
748 ;; can then be used to toggle the use of this keymap on and off.
749 ;; This means that it would be safe(r) to bind ordinary keys like
750 ;; letters and digits, provided that we inhibit the keymap while
751 ;; executing the macro later on (but that's controversial...)
753 (defun kmacro-lambda-form (mac &optional counter format)
754 "Create lambda form for macro bound to symbol or key."
755 (if counter
756 (setq mac (list mac counter format)))
757 `(lambda (&optional arg)
758 "Keyboard macro."
759 (interactive "p")
760 (kmacro-exec-ring-item ',mac arg)))
762 (defun kmacro-extract-lambda (mac)
763 "Extract kmacro from a kmacro lambda form."
764 (and (consp mac)
765 (eq (car mac) 'lambda)
766 (setq mac (assoc 'kmacro-exec-ring-item mac))
767 (consp (cdr mac))
768 (consp (car (cdr mac)))
769 (consp (cdr (car (cdr mac))))
770 (setq mac (car (cdr (car (cdr mac)))))
771 (listp mac)
772 (= (length mac) 3)
773 (arrayp (car mac))
774 mac))
777 (defun kmacro-bind-to-key (arg)
778 "When not defining or executing a macro, offer to bind last macro to a key.
779 The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
780 through [C-x C-k Z] are reserved for user bindings, and to bind to
781 one of these sequences, just enter the digit or letter, rather than
782 the whole sequence.
784 You can bind to any valid key sequence, but if you try to bind to
785 a key with an existing command binding, you will be asked for
786 confirmation whether to replace that binding. Note that the
787 binding is made in the `global-map' keymap, so the macro binding
788 may be shaded by a local key binding."
789 (interactive "p")
790 (if (or defining-kbd-macro executing-kbd-macro)
791 (if defining-kbd-macro
792 (message "Cannot save macro while defining it."))
793 (unless last-kbd-macro
794 (error "No keyboard macro defined"))
795 (let ((key-seq (read-key-sequence "Bind last macro to key: "))
796 ok cmd)
797 (when (= (length key-seq) 1)
798 (let ((ch (aref key-seq 0)))
799 (if (or (and (>= ch ?0) (<= ch ?9))
800 (and (>= ch ?A) (<= ch ?Z)))
801 (setq key-seq (concat "\C-x\C-k" key-seq)
802 ok t))))
803 (when (and (not (equal key-seq "\a"))
804 (or ok
805 (not (setq cmd (key-binding key-seq)))
806 (stringp cmd)
807 (vectorp cmd)
808 (yes-or-no-p (format "%s runs command %S. Bind anyway? "
809 (format-kbd-macro key-seq)
810 cmd))))
811 (define-key global-map key-seq
812 (kmacro-lambda-form (kmacro-ring-head)))
813 (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
816 (defun kmacro-name-last-macro (symbol)
817 "Assign a name to the last keyboard macro defined.
818 Argument SYMBOL is the name to define.
819 The symbol's function definition becomes the keyboard macro string.
820 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
821 (interactive "SName for last kbd macro: ")
822 (or last-kbd-macro
823 (error "No keyboard macro defined"))
824 (and (fboundp symbol)
825 (not (get symbol 'kmacro))
826 (not (stringp (symbol-function symbol)))
827 (not (vectorp (symbol-function symbol)))
828 (error "Function %s is already defined and not a keyboard macro"
829 symbol))
830 (if (string-equal symbol "")
831 (error "No command name given"))
832 (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
833 (put symbol 'kmacro t))
836 (defun kmacro-view-macro (&optional arg)
837 "Display the last keyboard macro.
838 If repeated, it shows previous elements in the macro ring."
839 (interactive)
840 (cond
841 ((or (kmacro-ring-empty-p)
842 (not (eq last-command 'kmacro-view-macro)))
843 (setq kmacro-view-last-item nil))
844 ((null kmacro-view-last-item)
845 (setq kmacro-view-last-item kmacro-ring
846 kmacro-view-item-no 2))
847 ((consp kmacro-view-last-item)
848 (setq kmacro-view-last-item (cdr kmacro-view-last-item)
849 kmacro-view-item-no (1+ kmacro-view-item-no)))
851 (setq kmacro-view-last-item nil)))
852 (setq this-command 'kmacro-view-macro
853 last-command this-command) ;; in case we repeat
854 (kmacro-display (if kmacro-view-last-item
855 (car (car kmacro-view-last-item))
856 last-kbd-macro)
858 (if kmacro-view-last-item
859 (concat (cond ((= kmacro-view-item-no 2) "2nd")
860 ((= kmacro-view-item-no 3) "3nd")
861 (t (format "%dth" kmacro-view-item-no)))
862 " previous macro")
863 "Last macro")))
865 (defun kmacro-view-macro-repeat (&optional arg)
866 "Display the last keyboard macro.
867 If repeated, it shows previous elements in the macro ring.
868 To execute the displayed macro ring item without changing the macro ring,
869 just enter C-k.
870 This is like `kmacro-view-macro', but allows repeating macro commands
871 without repeating the prefix."
872 (interactive)
873 (let ((keys (kmacro-get-repeat-prefix)))
874 (kmacro-view-macro arg)
875 (if (and last-kbd-macro keys)
876 (kmacro-repeat-on-last-key keys))))
878 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
881 (defun kmacro-edit-macro-repeat (&optional arg)
882 "Edit last keyboard macro."
883 (interactive "P")
884 (edit-kbd-macro "\r" arg))
886 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
889 (defun kmacro-edit-macro (&optional arg)
890 "As edit last keyboard macro, but without kmacro-repeat property."
891 (interactive "P")
892 (edit-kbd-macro "\r" arg))
895 (defun kmacro-edit-lossage ()
896 "Edit most recent 100 keystrokes as a keyboard macro."
897 (interactive)
898 (kmacro-push-ring)
899 (edit-kbd-macro "\C-hl"))
902 ;;; Single-step editing of keyboard macros
904 (defvar kmacro-step-edit-active) ;; step-editing active
905 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
906 (defvar kmacro-step-edit-inserting) ;; inserting into macro
907 (defvar kmacro-step-edit-appending) ;; append to end of macro
908 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
909 (defvar kmacro-step-edit-prefix-index) ;; index of first prefix arg key
910 (defvar kmacro-step-edit-key-index) ;; index of current key
911 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
912 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
913 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
915 (defvar kmacro-step-edit-map (make-sparse-keymap)
916 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
917 This keymap is an extension to the `query-replace-map', allowing the
918 following additional answers: `insert', `insert-1', `replace', `replace-1',
919 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
921 ;; query-replace-map answers include: `act', `skip', `act-and-show',
922 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
923 ;; `automatic', `backup', `exit-prefix', and `help'.")
924 ;; Also: `quit', `edit-replacement'
926 (set-keymap-parent kmacro-step-edit-map query-replace-map)
928 (define-key kmacro-step-edit-map "\t" 'act-repeat)
929 (define-key kmacro-step-edit-map [tab] 'act-repeat)
930 (define-key kmacro-step-edit-map "\C-k" 'skip-rest)
931 (define-key kmacro-step-edit-map "c" 'automatic)
932 (define-key kmacro-step-edit-map "f" 'skip-keep)
933 (define-key kmacro-step-edit-map "q" 'quit)
934 (define-key kmacro-step-edit-map "d" 'skip)
935 (define-key kmacro-step-edit-map "\C-d" 'skip)
936 (define-key kmacro-step-edit-map "i" 'insert)
937 (define-key kmacro-step-edit-map "I" 'insert-1)
938 (define-key kmacro-step-edit-map "r" 'replace)
939 (define-key kmacro-step-edit-map "R" 'replace-1)
940 (define-key kmacro-step-edit-map "a" 'append)
941 (define-key kmacro-step-edit-map "A" 'append-end)
943 (defvar kmacro-step-edit-prefix-commands
944 '(universal-argument universal-argument-more universal-argument-minus
945 digit-argument negative-argument)
946 "Commands which builds up a prefix arg for the current command")
948 (defun kmacro-step-edit-prompt (macro index)
949 ;; Show step-edit prompt
950 (let ((keys (and (not kmacro-step-edit-appending)
951 index (substring macro index executing-kbd-macro-index)))
952 (future (and (not kmacro-step-edit-appending)
953 (substring macro executing-kbd-macro-index)))
954 (message-log-max nil)
955 (curmsg (current-message)))
957 ;; TODO: Scroll macro if max-mini-window-height is too small.
958 (message "%s"
959 (concat
960 (format "Macro: %s%s%s%s%s\n"
961 (format-kbd-macro kmacro-step-edit-new-macro 1)
962 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
963 (propertize (if keys (format-kbd-macro keys)
964 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
965 (if future " " "")
966 (if future (format-kbd-macro future) ""))
967 (cond
968 ((minibufferp)
969 (format "%s\n%s\n"
970 (propertize "\
971 minibuffer " 'face 'header-line)
972 (buffer-substring (point-min) (point-max))))
973 (curmsg
974 (format "%s\n%s\n"
975 (propertize "\
976 echo area " 'face 'header-line)
977 curmsg))
978 (t ""))
979 (if keys
980 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
981 (propertize "\
982 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
983 (if kmacro-step-edit-help "\
984 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
985 TAB: execute while same, ?: toggle help
986 Edit: i: insert, r: replace, a: append, A: append at end,
987 I/R: insert/replace with one sequence,
988 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
989 ----------------------------------------------------------------
990 " "")
991 (propertize "Next command:" 'face 'bold)
992 this-command)
993 (propertize
994 (format "Type key sequence%s to insert and execute%s: "
995 (if (numberp kmacro-step-edit-inserting) "" "s")
996 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
997 'face 'bold))))))
999 (defun kmacro-step-edit-query ()
1000 ;; Pre-command hook function for step-edit in "command" mode
1001 (let ((resize-mini-windows t)
1002 (max-mini-window-height kmacro-step-edit-mini-window-height)
1003 act restore-index next-index)
1005 ;; Handle commands which reads additional input using read-char.
1006 (cond
1007 ((and (eq this-command 'quoted-insert)
1008 (not (eq kmacro-step-edit-action t)))
1009 ;; Find the actual end of this key sequence.
1010 ;; Must be able to backtrack in case we actually execute it.
1011 (setq restore-index executing-kbd-macro-index)
1012 (let (unread-command-events)
1013 (quoted-insert 0)
1014 (when unread-command-events
1015 (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1016 next-index executing-kbd-macro-index)))))
1018 ;; Query the user; stop macro exection temporarily
1019 (let ((macro executing-kbd-macro)
1020 (executing-kbd-macro nil)
1021 (defining-kbd-macro nil))
1023 ;; Any action requested by previous command
1024 (cond
1025 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
1026 (cond
1027 ((eq this-command 'quoted-insert)
1028 (clear-this-command-keys) ;; recent-keys actually
1029 (let (unread-command-events)
1030 (quoted-insert (prefix-numeric-value current-prefix-arg))
1031 (setq kmacro-step-edit-new-macro
1032 (vconcat kmacro-step-edit-new-macro (recent-keys)))
1033 (when unread-command-events
1034 (setq kmacro-step-edit-new-macro
1035 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
1036 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
1037 (setq current-prefix-arg nil
1038 prefix-arg nil)
1039 (setq act 'ignore))
1041 (setq act 'act)))
1042 (setq kmacro-step-edit-action nil))
1043 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
1044 (setq act 'act))
1046 (setq kmacro-step-edit-action nil)))
1048 ;; Handle prefix arg, or query user
1049 (cond
1050 (act act) ;; set above
1051 ((memq this-command kmacro-step-edit-prefix-commands)
1052 (unless kmacro-step-edit-prefix-index
1053 (setq kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1054 (setq act 'universal-argument))
1055 ((eq this-command 'universal-argument-other-key)
1056 (setq act 'universal-argument))
1058 (kmacro-step-edit-prompt macro (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1059 (setq act (lookup-key kmacro-step-edit-map
1060 (vector (with-current-buffer (current-buffer) (read-event))))))))
1062 ;; Resume macro execution and perform the action
1063 (cond
1064 ((eq act 'universal-argument)
1065 nil)
1066 ((cond
1067 ((eq act 'act)
1069 ((eq act 'act-repeat)
1070 (setq kmacro-step-edit-action this-command)
1072 ((eq act 'quit)
1073 (setq kmacro-step-edit-replace nil)
1074 (setq kmacro-step-edit-active 'ignore)
1075 nil)
1076 ((eq act 'skip)
1077 (setq kmacro-step-edit-prefix-index nil)
1078 nil)
1079 ((eq act 'skip-keep)
1080 (setq this-command 'ignore)
1082 ((eq act 'skip-rest)
1083 (setq kmacro-step-edit-active 'ignore)
1084 nil)
1085 ((memq act '(automatic exit))
1086 (setq kmacro-step-edit-active nil)
1087 (setq act t)
1089 ((member act '(insert-1 insert))
1090 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1091 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
1092 nil)
1093 ((member act '(replace-1 replace))
1094 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
1095 (setq kmacro-step-edit-prefix-index nil)
1096 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1097 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1098 kmacro-step-edit-appending t))
1099 nil)
1100 ((eq act 'append)
1101 (setq kmacro-step-edit-inserting t)
1102 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1103 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1104 kmacro-step-edit-appending t))
1106 ((eq act 'append-end)
1107 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1108 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1109 kmacro-step-edit-inserting t
1110 kmacro-step-edit-appending t)
1111 (setq kmacro-step-edit-active 'append-end))
1112 (setq act t)
1114 ((eq act 'help)
1115 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1116 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
1117 nil)
1118 (t ;; Ignore unknown responses
1119 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1120 nil))
1121 (if (> executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1122 (setq kmacro-step-edit-new-macro
1123 (vconcat kmacro-step-edit-new-macro
1124 (substring executing-kbd-macro
1125 (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
1126 (if (eq act t) nil executing-kbd-macro-index)))
1127 kmacro-step-edit-prefix-index nil))
1128 (if restore-index
1129 (setq executing-kbd-macro-index restore-index)))
1131 (setq this-command 'ignore)))
1132 (setq kmacro-step-edit-key-index next-index)))
1134 (defun kmacro-step-edit-insert ()
1135 ;; Pre-command hook function for step-edit in "insert" mode
1136 (let ((resize-mini-windows t)
1137 (max-mini-window-height kmacro-step-edit-mini-window-height)
1138 (macro executing-kbd-macro)
1139 (executing-kbd-macro nil)
1140 (defining-kbd-macro nil)
1141 cmd keys next-index)
1142 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
1143 kmacro-step-edit-prefix-index nil)
1144 (kmacro-step-edit-prompt macro nil)
1145 ;; Now, we have read a key sequence from the macro, but we don't want
1146 ;; to execute it yet. So push it back and read another sequence.
1147 (reset-this-command-lengths)
1148 (setq keys (read-key-sequence nil nil nil nil t))
1149 (setq cmd (key-binding keys t nil))
1150 (if (cond
1151 ((null cmd)
1153 ((eq cmd 'quoted-insert)
1154 (clear-this-command-keys) ;; recent-keys actually
1155 (quoted-insert (prefix-numeric-value current-prefix-arg))
1156 (setq current-prefix-arg nil
1157 prefix-arg nil)
1158 (setq keys (vconcat keys (recent-keys)))
1159 (when (numberp kmacro-step-edit-inserting)
1160 (setq kmacro-step-edit-inserting nil)
1161 (when unread-command-events
1162 (setq keys (substring keys 0 (- (length unread-command-events)))
1163 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1164 next-index executing-kbd-macro-index
1165 unread-command-events nil)))
1166 (setq cmd 'ignore)
1167 nil)
1168 ((memq cmd kmacro-step-edit-prefix-commands)
1169 (setq universal-argument-num-events 0)
1170 (reset-this-command-lengths)
1171 nil)
1172 ((eq cmd 'universal-argument-other-key)
1173 (setq kmacro-step-edit-action t)
1174 (setq universal-argument-num-events 0)
1175 (reset-this-command-lengths)
1176 (if (numberp kmacro-step-edit-inserting)
1177 (setq kmacro-step-edit-inserting nil))
1178 nil)
1179 ((numberp kmacro-step-edit-inserting)
1180 (setq kmacro-step-edit-inserting nil)
1181 nil)
1182 ((equal keys "\C-j")
1183 (setq kmacro-step-edit-inserting nil)
1184 (setq kmacro-step-edit-action nil)
1185 ;; Forget any (partial) prefix arg from next command
1186 (setq kmacro-step-edit-prefix-index nil)
1187 (reset-this-command-lengths)
1188 (setq overriding-terminal-local-map nil)
1189 (setq universal-argument-num-events nil)
1190 (setq next-index kmacro-step-edit-key-index)
1192 (t nil))
1193 (setq this-command 'ignore)
1194 (setq this-command cmd)
1195 (if (memq this-command '(self-insert-command digit-argument))
1196 (setq last-command-char (aref keys (1- (length keys)))))
1197 (if keys
1198 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1199 (setq kmacro-step-edit-key-index next-index)))
1201 (defun kmacro-step-edit-pre-command ()
1202 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1203 (when kmacro-step-edit-active
1204 (cond
1205 ((eq kmacro-step-edit-active 'ignore)
1206 (setq this-command 'ignore))
1207 ((eq kmacro-step-edit-active 'append-end)
1208 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1209 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1210 kmacro-step-edit-inserting t
1211 kmacro-step-edit-appending t
1212 kmacro-step-edit-active t)))
1213 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1214 (if kmacro-step-edit-inserting
1215 (kmacro-step-edit-insert)
1216 (kmacro-step-edit-query))
1217 (setq kmacro-step-edit-num-input-keys num-input-keys)
1218 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1219 (setq kmacro-step-edit-appending nil
1220 kmacro-step-edit-active 'ignore)))))
1221 (when (eq kmacro-step-edit-active t)
1222 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1224 (defun kmacro-step-edit-minibuf-setup ()
1225 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1226 (when kmacro-step-edit-active
1227 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1229 (defun kmacro-step-edit-post-command ()
1230 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1231 (when kmacro-step-edit-active
1232 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1233 (if kmacro-step-edit-key-index
1234 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1235 (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
1238 (defun kmacro-step-edit-macro ()
1239 "Step edit and execute last keyboard macro.
1241 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1242 (interactive)
1243 (let ((kmacro-step-edit-active t)
1244 (kmacro-step-edit-new-macro "")
1245 (kmacro-step-edit-inserting nil)
1246 (kmacro-step-edit-appending nil)
1247 (kmacro-step-edit-replace t)
1248 (kmacro-step-edit-prefix-index nil)
1249 (kmacro-step-edit-key-index 0)
1250 (kmacro-step-edit-action nil)
1251 (kmacro-step-edit-help nil)
1252 (kmacro-step-edit-num-input-keys num-input-keys)
1253 (pre-command-hook pre-command-hook)
1254 (post-command-hook post-command-hook)
1255 (minibuffer-setup-hook minibuffer-setup-hook))
1256 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1257 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1258 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1259 (call-last-kbd-macro nil nil)
1260 (when (and kmacro-step-edit-replace
1261 kmacro-step-edit-new-macro
1262 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1263 (kmacro-push-ring)
1264 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1266 (provide 'kmacro)
1268 ;;; arch-tag: d3fe0b24-ae41-47de-a4d6-41a77d5559f0
1269 ;;; kmacro.el ends here