In display-buffer-record-window record selected window if necessary.
[emacs.git] / lisp / kmacro.el
blob327cf455e656f5d5a07dc145f499fda58e661aab
1 ;;; kmacro.el --- enhanced keyboard macros
3 ;; Copyright (C) 2002-2015 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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; The kmacro package provides the user interface to emacs' basic
26 ;; keyboard macro functionality. With kmacro, two function keys are
27 ;; dedicated to keyboard macros, by default F3 and F4.
29 ;; Note: The traditional bindings C-x (, C-x ), and C-x e are still
30 ;; supported, but for some users these bindings are too hard to type
31 ;; to be really useful for doing small repeated tasks.
33 ;; To start defining a keyboard macro, use F3. To end the macro,
34 ;; use F4, and to call the macro also use F4. This makes it very
35 ;; easy to repeat a macro immediately after defining it.
37 ;; You can call the macro repeatedly by pressing F4 multiple times, or
38 ;; you can give a numeric prefix argument specifying the number of
39 ;; times to repeat the macro. Macro execution automatically
40 ;; terminates when point reaches the end of the buffer or if an error
41 ;; is signaled by ringing the bell.
43 ;; When you define a macro with F3/F4, it is automatically added to
44 ;; the head of the "keyboard macro ring", and F4 actually executes the
45 ;; first element of the macro ring.
47 ;; Note: an empty macro is never added to the macro ring.
49 ;; You can execute the second element on the macro ring with C-u F4 or
50 ;; C-x C-k C-l, you can use C-x C-k C-p and C-x C-k C-n to cycle
51 ;; through the macro ring, and you can swap the first and second
52 ;; elements with C-x C-k C-t. To delete the first element in the
53 ;; macro ring, use C-x C-k C-d.
55 ;; You can also use C-x C-k C-s to start a macro, and C-x C-k C-k to
56 ;; end it; then use C-k to execute it immediately, or C-x C-k C-k to
57 ;; execute it later.
59 ;; In general, immediately after using C-x C-k followed by one of C-k,
60 ;; C-l, C-p, or C-n, you can further cycle the macro ring using C-p or
61 ;; C-n, execute the first or second macro using C-k or C-l, delete
62 ;; the head macro with C-d, or edit the current macro with C-e without
63 ;; repeating the C-x C-k prefix.
65 ;; If you enter F3 while defining the macro, the numeric value of
66 ;; `kmacro-counter' is inserted using the `kmacro-counter-format', and
67 ;; `kmacro-counter' is incremented by 1 (or the numeric prefix value
68 ;; of F3).
70 ;; The initial value of `kmacro-counter' is 0, or the numeric prefix
71 ;; value given to F3 when starting the macro.
73 ;; Now, each time you call the macro using F4, the current
74 ;; value of `kmacro-counter' is inserted and incremented, making it
75 ;; easy to insert incremental numbers in the buffer.
77 ;; Example:
79 ;; The following sequence: M-5 F3 x M-2 F3 y F4 F4 F4 F4
80 ;; inserts the following string: x5yx7yx9yx11y
82 ;; A macro can also be called using a mouse click, default S-mouse-3.
83 ;; This calls the macro at the point where you click the mouse.
85 ;; You can edit the last macro using C-x C-k C-e.
87 ;; You can append to the last macro using C-u F3.
89 ;; You can set the macro counter using C-x C-k C-c, add to it using C-x C-k C-a,
90 ;; and you can set the macro counter format with C-x C-k C-f.
92 ;; The following key bindings are performed:
94 ;; Normal While defining macro
95 ;; --------------------------- ------------------------------
96 ;; f3 Define macro Insert current counter value
97 ;; Prefix arg specifies initial and increase counter by prefix
98 ;; counter value (default 0) (default increment: 1)
100 ;; C-u f3 APPENDs to last macro
102 ;; f4 Call last macro End macro
103 ;; Prefix arg specifies number
104 ;; of times to execute macro.
106 ;; C-u f4 Swap last and head of macro ring.
108 ;; S-mouse-3 Set point at click and End macro and execute macro at
109 ;; execute last macro. click.
111 ;;; Code:
113 ;; Customization:
115 (defgroup kmacro nil
116 "Simplified keyboard macro user interface."
117 :group 'keyboard
118 :group 'convenience
119 :version "22.1"
120 :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
121 :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
123 (defcustom kmacro-call-mouse-event 'S-mouse-3
124 "The mouse event used by kmacro to call a macro.
125 Set to nil if no mouse binding is desired."
126 :type 'symbol
127 :group 'kmacro)
129 (defcustom kmacro-ring-max 8
130 "Maximum number of keyboard macros to save in macro ring."
131 :type 'integer
132 :group 'kmacro)
135 (defcustom kmacro-execute-before-append t
136 "Controls whether appending to a macro starts by executing the macro.
137 If non-nil, using a single \\[universal-argument] prefix executes the macro
138 before appending, while more than one \\[universal-argument] prefix does not
139 execute the macro.
140 Otherwise, a single \\[universal-argument] prefix does not execute the
141 macro, while more than one \\[universal-argument] prefix causes the
142 macro to be executed before appending to it."
143 :type 'boolean
144 :group 'kmacro)
147 (defcustom kmacro-repeat-no-prefix t
148 "Allow repeating certain macro commands without entering the C-x C-k prefix."
149 :type 'boolean
150 :group 'kmacro)
152 (defcustom kmacro-call-repeat-key t
153 "Allow repeating macro call using last key or a specific key."
154 :type '(choice (const :tag "Disabled" nil)
155 (const :tag "Last key" t)
156 (character :tag "Character" :value ?e)
157 (symbol :tag "Key symbol" :value RET))
158 :group 'kmacro)
160 (defcustom kmacro-call-repeat-with-arg nil
161 "Repeat macro call with original arg when non-nil; repeat once if nil."
162 :type 'boolean
163 :group 'kmacro)
165 (defcustom kmacro-step-edit-mini-window-height 0.75
166 "Override `max-mini-window-height' when step edit keyboard macro."
167 :type 'number
168 :group 'kmacro)
170 ;; Keymap
172 (defvar kmacro-keymap
173 (let ((map (make-sparse-keymap)))
174 ;; Start, end, execute macros
175 (define-key map "s" 'kmacro-start-macro)
176 (define-key map "\C-s" 'kmacro-start-macro)
177 (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
178 (define-key map "r" 'apply-macro-to-region-lines)
179 (define-key map "q" 'kbd-macro-query) ;; Like C-x q
181 ;; macro ring
182 (define-key map "\C-n" 'kmacro-cycle-ring-next)
183 (define-key map "\C-p" 'kmacro-cycle-ring-previous)
184 (define-key map "\C-v" 'kmacro-view-macro-repeat)
185 (define-key map "\C-d" 'kmacro-delete-ring-head)
186 (define-key map "\C-t" 'kmacro-swap-ring)
187 (define-key map "\C-l" 'kmacro-call-ring-2nd-repeat)
189 ;; macro counter
190 (define-key map "\C-f" 'kmacro-set-format)
191 (define-key map "\C-c" 'kmacro-set-counter)
192 (define-key map "\C-i" 'kmacro-insert-counter)
193 (define-key map "\C-a" 'kmacro-add-counter)
195 ;; macro editing
196 (define-key map "\C-e" 'kmacro-edit-macro-repeat)
197 (define-key map "\r" 'kmacro-edit-macro)
198 (define-key map "e" 'edit-kbd-macro)
199 (define-key map "l" 'kmacro-edit-lossage)
200 (define-key map " " 'kmacro-step-edit-macro)
202 ;; naming and binding
203 (define-key map "b" 'kmacro-bind-to-key)
204 (define-key map "n" 'kmacro-name-last-macro)
205 (define-key map "x" 'kmacro-to-register)
206 map)
207 "Keymap for keyboard macro commands.")
208 (defalias 'kmacro-keymap kmacro-keymap)
210 ;;; Provide some binding for startup:
211 ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
212 ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
213 ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
214 ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
215 ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
216 ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
217 ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
219 (if kmacro-call-mouse-event
220 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
223 ;;; Called from keyboard-quit
225 (defun kmacro-keyboard-quit ()
226 (or (not defining-kbd-macro)
227 (eq defining-kbd-macro 'append)
228 (kmacro-ring-empty-p)
229 (kmacro-pop-ring)))
232 ;;; Keyboard macro counter
234 (defvar kmacro-counter 0
235 "Current keyboard macro counter.")
237 (defvar kmacro-default-counter-format "%d")
239 (defvar kmacro-counter-format "%d"
240 "Current keyboard macro counter format.")
242 (defvar kmacro-counter-format-start kmacro-counter-format
243 "Macro format at start of macro execution.")
245 (defvar kmacro-counter-value-start kmacro-counter
246 "Macro counter at start of macro execution.")
248 (defvar kmacro-last-counter 0
249 "Last counter inserted by key macro.")
251 (defvar kmacro-initial-counter-value nil
252 "Initial counter value for the next keyboard macro to be defined.")
255 (defun kmacro-insert-counter (arg)
256 "Insert macro counter, then increment it by ARG.
257 Interactively, ARG defaults to 1. With \\[universal-argument], insert
258 previous `kmacro-counter', and do not modify counter."
259 (interactive "P")
260 (if kmacro-initial-counter-value
261 (setq kmacro-counter kmacro-initial-counter-value
262 kmacro-initial-counter-value nil))
263 (if (and arg (listp arg))
264 (insert (format kmacro-counter-format kmacro-last-counter))
265 (insert (format kmacro-counter-format kmacro-counter))
266 (kmacro-add-counter (prefix-numeric-value arg))))
269 (defun kmacro-set-format (format)
270 "Set macro counter FORMAT."
271 (interactive "sMacro Counter Format: ")
272 (setq kmacro-counter-format
273 (if (equal format "") "%d" format))
274 ;; redefine initial macro counter if we are not executing a macro.
275 (if (not (or defining-kbd-macro executing-kbd-macro))
276 (setq kmacro-default-counter-format kmacro-counter-format)))
279 (defun kmacro-display-counter (&optional value)
280 "Display current counter value."
281 (unless value (setq value kmacro-counter))
282 (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value))
285 (defun kmacro-set-counter (arg)
286 "Set `kmacro-counter' to ARG or prompt if missing.
287 With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
288 (interactive "NMacro counter value: ")
289 (if (not (or defining-kbd-macro executing-kbd-macro))
290 (kmacro-display-counter (setq kmacro-initial-counter-value arg))
291 (setq kmacro-last-counter kmacro-counter
292 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
293 kmacro-counter-value-start
294 arg))
295 (unless executing-kbd-macro
296 (kmacro-display-counter))))
299 (defun kmacro-add-counter (arg)
300 "Add numeric prefix arg (prompt if missing) to macro counter.
301 With \\[universal-argument], restore previous counter value."
302 (interactive "NAdd to macro counter: ")
303 (if kmacro-initial-counter-value
304 (setq kmacro-counter kmacro-initial-counter-value
305 kmacro-initial-counter-value nil))
306 (let ((last kmacro-last-counter))
307 (setq kmacro-last-counter kmacro-counter
308 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
309 last
310 kmacro-counter (+ kmacro-counter arg))))
311 (unless executing-kbd-macro
312 (kmacro-display-counter)))
315 (defun kmacro-loop-setup-function ()
316 "Function called prior to each iteration of macro."
317 ;; Restore macro counter format to initial format, so it is ok to change
318 ;; counter format in the macro without restoring it.
319 (setq kmacro-counter-format kmacro-counter-format-start)
320 ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
321 (setq kmacro-counter-value-start kmacro-counter)
322 ;; Return non-nil to continue execution.
326 ;;; Keyboard macro ring
328 (defvar kmacro-ring nil
329 "The keyboard macro ring.
330 Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
331 the macro ring (when defining or executing) is not stored in the ring;
332 instead it is available in the variables `last-kbd-macro', `kmacro-counter',
333 and `kmacro-counter-format'.")
335 ;; Remember what we are currently looking at with kmacro-view-macro.
337 (defvar kmacro-view-last-item nil)
338 (defvar kmacro-view-item-no 0)
341 (defun kmacro-ring-head ()
342 "Return pseudo head element in macro ring."
343 (and last-kbd-macro
344 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
347 (defun kmacro-push-ring (&optional elt)
348 "Push ELT or current macro onto `kmacro-ring'."
349 (when (setq elt (or elt (kmacro-ring-head)))
350 (let ((history-delete-duplicates nil))
351 (add-to-history 'kmacro-ring elt kmacro-ring-max))))
354 (defun kmacro-split-ring-element (elt)
355 (setq last-kbd-macro (car elt)
356 kmacro-counter (nth 1 elt)
357 kmacro-counter-format-start (nth 2 elt)))
360 (defun kmacro-pop-ring1 (&optional raw)
361 "Pop head element off macro ring (no check).
362 Non-nil arg RAW means just return raw first element."
363 (prog1 (car kmacro-ring)
364 (unless raw
365 (kmacro-split-ring-element (car kmacro-ring)))
366 (setq kmacro-ring (cdr kmacro-ring))))
369 (defun kmacro-pop-ring (&optional raw)
370 "Pop head element off macro ring.
371 Non-nil arg RAW means just return raw first element."
372 (unless (kmacro-ring-empty-p)
373 (kmacro-pop-ring1 raw)))
376 (defun kmacro-ring-empty-p (&optional none)
377 "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
378 Check only `last-kbd-macro' if optional arg NONE is non-nil."
379 (while (and (null last-kbd-macro) kmacro-ring)
380 (kmacro-pop-ring1))
381 (cond
382 ((null last-kbd-macro)
383 (message "No keyboard macro defined.")
385 ((and (null none) (null kmacro-ring))
386 (message "Only one keyboard macro defined.")
388 (t nil)))
391 (defun kmacro-display (macro &optional trunc descr empty)
392 "Display a keyboard MACRO.
393 Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
394 Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
395 Optional arg EMPTY is message to print if no macros are defined."
396 (if macro
397 (let* ((x 60)
398 (m (format-kbd-macro macro))
399 (l (length m))
400 (z (and trunc (> l x))))
401 (message "%s%s: %s%s" (or descr "Macro")
402 (if (= kmacro-counter 0) ""
403 (format " [%s]"
404 (format kmacro-counter-format-start kmacro-counter)))
405 (if z (substring m 0 (1- x)) m) (if z "..." "")))
406 (message "%s" (or empty "No keyboard macros defined"))))
409 (defun kmacro-repeat-on-last-key (keys)
410 "Process kmacro commands keys immediately after cycling the ring."
411 (setq keys (vconcat keys))
412 (let ((n (1- (length keys)))
413 cmd done repeat)
414 (while (and last-kbd-macro
415 (not done)
416 (aset keys n (read-event))
417 (setq cmd (key-binding keys t))
418 (setq repeat (get cmd 'kmacro-repeat)))
419 (clear-this-command-keys t)
420 (cond
421 ((eq repeat 'ring)
422 (if kmacro-ring
423 (let ((kmacro-repeat-no-prefix nil))
424 (funcall cmd nil))
425 (kmacro-display last-kbd-macro t)))
426 ((eq repeat 'head)
427 (let ((kmacro-repeat-no-prefix nil))
428 (funcall cmd nil)))
429 ((eq repeat 'stop)
430 (funcall cmd nil)
431 (setq done t)))
432 (setq last-input-event nil)))
433 (when last-input-event
434 (clear-this-command-keys t)
435 (setq unread-command-events (list last-input-event))))
438 (defun kmacro-get-repeat-prefix ()
439 (let (keys)
440 (and kmacro-repeat-no-prefix
441 (setq keys (this-single-command-keys))
442 (> (length keys) 1)
443 keys)))
446 ;;;###autoload
447 (defun kmacro-exec-ring-item (item arg)
448 "Execute item ITEM from the macro ring.
449 ARG is the number of times to execute the item."
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 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 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 The ARG parameter is unused."
488 (interactive)
489 (unless (kmacro-ring-empty-p)
490 (kmacro-push-ring)
491 (let* ((keys (kmacro-get-repeat-prefix))
492 (len (length kmacro-ring))
493 (tail (nthcdr (- len 2) kmacro-ring))
494 (elt (car (cdr tail))))
495 (setcdr tail nil)
496 (kmacro-split-ring-element elt)
497 (kmacro-display last-kbd-macro t)
498 (if keys
499 (kmacro-repeat-on-last-key keys)))))
501 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
504 (defun kmacro-cycle-ring-previous (&optional _arg)
505 "Move to previous keyboard macro in keyboard macro ring.
506 Displays the selected macro in the echo area.
507 The ARG parameter is unused."
508 (interactive)
509 (unless (kmacro-ring-empty-p)
510 (let ((keys (kmacro-get-repeat-prefix))
511 (cur (kmacro-ring-head)))
512 (kmacro-pop-ring1)
513 (if kmacro-ring
514 (nconc kmacro-ring (list cur))
515 (setq kmacro-ring (list cur)))
516 (kmacro-display last-kbd-macro t)
517 (if keys
518 (kmacro-repeat-on-last-key keys)))))
520 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
523 (defun kmacro-swap-ring ()
524 "Swap first two elements on keyboard macro ring."
525 (interactive)
526 (unless (kmacro-ring-empty-p)
527 (let ((cur (kmacro-ring-head)))
528 (kmacro-pop-ring1)
529 (kmacro-push-ring cur))
530 (kmacro-display last-kbd-macro t)))
533 (defun kmacro-delete-ring-head (&optional _arg)
534 "Delete current macro from keyboard macro ring.
535 The ARG parameter is unused."
536 (interactive)
537 (unless (kmacro-ring-empty-p t)
538 (if (null kmacro-ring)
539 (setq last-kbd-macro nil)
540 (kmacro-pop-ring))
541 (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
543 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
545 ;;; Traditional bindings:
548 ;;;###autoload
549 (defun kmacro-start-macro (arg)
550 "Record subsequent keyboard input, defining a keyboard macro.
551 The commands are recorded even as they are executed.
552 Use \\[kmacro-end-macro] to finish recording and make the macro available.
553 Use \\[kmacro-end-and-call-macro] to execute the macro.
555 Non-nil arg (prefix arg) means append to last macro defined.
557 With \\[universal-argument] prefix, append to last keyboard macro
558 defined. Depending on `kmacro-execute-before-append', this may begin
559 by re-executing the last macro as if you typed it again.
561 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
562 defining the macro.
564 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
565 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
566 The format of the counter can be modified via \\[kmacro-set-format].
568 Use \\[kmacro-name-last-macro] to give it a permanent name.
569 Use \\[kmacro-bind-to-key] to bind it to a key sequence."
570 (interactive "P")
571 (if (or defining-kbd-macro executing-kbd-macro)
572 (message "Already defining keyboard macro.")
573 (let ((append (and arg (listp arg))))
574 (unless append
575 (if last-kbd-macro
576 (kmacro-push-ring
577 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
578 (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
579 kmacro-initial-counter-value
581 kmacro-initial-counter-value nil
582 kmacro-counter-value-start kmacro-counter
583 kmacro-last-counter kmacro-counter
584 kmacro-counter-format kmacro-default-counter-format
585 kmacro-counter-format-start kmacro-default-counter-format))
587 (start-kbd-macro append
588 (and append
589 (if kmacro-execute-before-append
590 (> (car arg) 4)
591 (= (car arg) 4))))
592 (if (and defining-kbd-macro append)
593 (setq defining-kbd-macro 'append)))))
596 ;;;###autoload
597 (defun kmacro-end-macro (arg)
598 "Finish defining a keyboard macro.
599 The definition was started by \\[kmacro-start-macro].
600 The macro is now available for use via \\[kmacro-call-macro],
601 or it can be given a name with \\[kmacro-name-last-macro] and then invoked
602 under that name.
604 With numeric arg, repeat macro now that many times,
605 counting the definition just completed as the first repetition.
606 An argument of zero means repeat until error."
607 (interactive "P")
608 ;; Isearch may push the kmacro-end-macro key sequence onto the macro.
609 ;; Just ignore it when executing the macro.
610 (unless executing-kbd-macro
611 (end-kbd-macro arg #'kmacro-loop-setup-function)
612 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
613 (setq last-kbd-macro nil)
614 (message "Ignore empty macro")
615 ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
616 (while (and (null last-kbd-macro) kmacro-ring)
617 (kmacro-pop-ring1)))))
620 ;;;###autoload
621 (defun kmacro-call-macro (arg &optional no-repeat end-macro macro)
622 "Call the keyboard MACRO that you defined with \\[kmacro-start-macro].
623 A prefix argument serves as a repeat count. Zero means repeat until error.
624 MACRO defaults to `last-kbd-macro'.
626 When you call the macro, you can call the macro again by repeating
627 just the last key in the key sequence that you used to call this
628 command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
629 for details on how to adjust or disable this behavior.
631 To make a macro permanent so you can call it even after defining
632 others, use \\[kmacro-name-last-macro]."
633 (interactive "p")
634 (let ((repeat-key (and (or (and (null no-repeat)
635 (> (length (this-single-command-keys)) 1))
636 ;; Used when we're in the process of repeating.
637 (eq no-repeat 'repeating))
638 last-input-event)))
639 (if end-macro
640 (kmacro-end-macro arg) ; modifies last-kbd-macro
641 (let ((last-kbd-macro (or macro last-kbd-macro)))
642 (call-last-kbd-macro arg #'kmacro-loop-setup-function)))
643 (when (consp arg)
644 (setq arg (car arg)))
645 (when (and (or (null arg) (> arg 0))
646 (setq repeat-key
647 (if (eq kmacro-call-repeat-key t)
648 repeat-key
649 kmacro-call-repeat-key)))
650 ;; Issue a hint to the user, if the echo area isn't in use.
651 (unless (current-message)
652 (message "(Type %s to repeat macro%s)"
653 (format-kbd-macro (vector repeat-key) nil)
654 (if (and kmacro-call-repeat-with-arg
655 arg (> arg 1))
656 (format " %d times" arg) "")))
657 ;; Can't use the `keep-pred' arg because this overlay keymap
658 ;; needs to be removed during the next run of the kmacro
659 ;; (i.e. we must add and remove this map at each repetition).
660 (set-transient-map
661 (let ((map (make-sparse-keymap)))
662 (define-key map (vector repeat-key)
663 `(lambda () (interactive)
664 (kmacro-call-macro ,(and kmacro-call-repeat-with-arg arg)
665 'repeating nil ,(if end-macro
666 last-kbd-macro
667 (or macro last-kbd-macro)))))
668 map)))))
671 ;;; Combined function key bindings:
673 ;;;###autoload
674 (defun kmacro-start-macro-or-insert-counter (arg)
675 "Record subsequent keyboard input, defining a keyboard macro.
676 The commands are recorded even as they are executed.
678 Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
679 macro.
681 With \\[universal-argument], appends to current keyboard macro (keeping
682 the current value of `kmacro-counter').
684 When defining/executing macro, inserts macro counter and increments
685 the counter with ARG or 1 if missing. With \\[universal-argument],
686 inserts previous `kmacro-counter' (but do not modify counter).
688 The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
689 The format of the counter can be modified via \\[kmacro-set-format]."
690 (interactive "P")
691 (if (or defining-kbd-macro executing-kbd-macro)
692 (kmacro-insert-counter arg)
693 (kmacro-start-macro arg)))
696 ;;;###autoload
697 (defun kmacro-end-or-call-macro (arg &optional no-repeat)
698 "End kbd macro if currently being defined; else call last kbd macro.
699 With numeric prefix ARG, repeat macro that many times.
700 With \\[universal-argument], call second macro in macro ring."
701 (interactive "P")
702 (cond
703 (defining-kbd-macro
704 (if kmacro-call-repeat-key
705 (kmacro-call-macro arg no-repeat t)
706 (kmacro-end-macro arg)))
707 ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
708 kmacro-view-last-item)
709 (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
710 ((and arg (listp arg))
711 (kmacro-call-ring-2nd 1))
713 (kmacro-call-macro arg no-repeat))))
716 (defun kmacro-end-or-call-macro-repeat (arg)
717 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
718 (interactive "P")
719 (let ((keys (kmacro-get-repeat-prefix)))
720 (kmacro-end-or-call-macro arg t)
721 (if keys
722 (kmacro-repeat-on-last-key keys))))
724 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
727 ;;;###autoload
728 (defun kmacro-end-and-call-macro (arg &optional no-repeat)
729 "Call last keyboard macro, ending it first if currently being defined.
730 With numeric prefix ARG, repeat macro that many times.
731 Zero argument means repeat until there is an error.
733 To give a macro a permanent name, so you can call it
734 even after defining other macros, use \\[kmacro-name-last-macro]."
735 (interactive "P")
736 (if defining-kbd-macro
737 (kmacro-end-macro nil))
738 (kmacro-call-macro arg no-repeat))
741 ;;;###autoload
742 (defun kmacro-end-call-mouse (event)
743 "Move point to the position clicked with the mouse and call last kbd macro.
744 If kbd macro currently being defined end it before activating it."
745 (interactive "e")
746 (when defining-kbd-macro
747 (end-kbd-macro))
748 (mouse-set-point event)
749 (kmacro-call-macro nil t))
752 ;;; Misc. commands
754 ;; An idea for macro bindings:
755 ;; Create a separate keymap installed as a minor-mode keymap (e.g. in
756 ;; the emulation-mode-map-alists) in which macro bindings are made
757 ;; independent of any other bindings. When first binding is made,
758 ;; the keymap is created, installed, and enabled. Key seq. C-x C-k +
759 ;; can then be used to toggle the use of this keymap on and off.
760 ;; This means that it would be safe(r) to bind ordinary keys like
761 ;; letters and digits, provided that we inhibit the keymap while
762 ;; executing the macro later on (but that's controversial...)
764 (defun kmacro-lambda-form (mac &optional counter format)
765 "Create lambda form for macro bound to symbol or key."
766 (if counter
767 (setq mac (list mac counter format)))
768 `(lambda (&optional arg)
769 "Keyboard macro."
770 (interactive "p")
771 (kmacro-exec-ring-item ',mac arg)))
773 (defun kmacro-extract-lambda (mac)
774 "Extract kmacro from a kmacro lambda form."
775 (and (consp mac)
776 (eq (car mac) 'lambda)
777 (setq mac (assoc 'kmacro-exec-ring-item mac))
778 (consp (cdr mac))
779 (consp (car (cdr mac)))
780 (consp (cdr (car (cdr mac))))
781 (setq mac (car (cdr (car (cdr mac)))))
782 (listp mac)
783 (= (length mac) 3)
784 (arrayp (car mac))
785 mac))
788 (defun kmacro-bind-to-key (_arg)
789 "When not defining or executing a macro, offer to bind last macro to a key.
790 The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
791 through [C-x C-k Z] are reserved for user bindings, and to bind to
792 one of these sequences, just enter the digit or letter, rather than
793 the whole sequence.
795 You can bind to any valid key sequence, but if you try to bind to
796 a key with an existing command binding, you will be asked for
797 confirmation whether to replace that binding. Note that the
798 binding is made in the `global-map' keymap, so the macro binding
799 may be shaded by a local key binding.
800 The ARG parameter is unused."
801 (interactive "p")
802 (if (or defining-kbd-macro executing-kbd-macro)
803 (if defining-kbd-macro
804 (message "Cannot save macro while defining it."))
805 (unless last-kbd-macro
806 (error "No keyboard macro defined"))
807 (let ((key-seq (read-key-sequence "Bind last macro to key: "))
808 ok cmd)
809 (when (= (length key-seq) 1)
810 (let ((ch (aref key-seq 0)))
811 (if (and (integerp ch)
812 (or (and (>= ch ?0) (<= ch ?9))
813 (and (>= ch ?A) (<= ch ?Z))))
814 (setq key-seq (concat "\C-x\C-k" key-seq)
815 ok t))))
816 (when (and (not (equal key-seq "\a"))
817 (or ok
818 (not (setq cmd (key-binding key-seq)))
819 (stringp cmd)
820 (vectorp cmd)
821 (yes-or-no-p (format "%s runs command %S. Bind anyway? "
822 (format-kbd-macro key-seq)
823 cmd))))
824 (define-key global-map key-seq
825 (kmacro-lambda-form (kmacro-ring-head)))
826 (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
829 (defun kmacro-name-last-macro (symbol)
830 "Assign a name to the last keyboard macro defined.
831 Argument SYMBOL is the name to define.
832 The symbol's function definition becomes the keyboard macro string.
833 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
834 (interactive "SName for last kbd macro: ")
835 (or last-kbd-macro
836 (error "No keyboard macro defined"))
837 (and (fboundp symbol)
838 (not (get symbol 'kmacro))
839 (not (stringp (symbol-function symbol)))
840 (not (vectorp (symbol-function symbol)))
841 (error "Function %s is already defined and not a keyboard macro"
842 symbol))
843 (if (string-equal symbol "")
844 (error "No command name given"))
845 (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
846 (put symbol 'kmacro t))
849 (defun kmacro-execute-from-register (k)
850 (kmacro-call-macro current-prefix-arg nil nil k))
852 (defun kmacro-to-register (r)
853 "Store the last keyboard macro in register R.
855 Interactively, reads the register using `register-read-with-preview'."
856 (interactive
857 (progn
858 (or last-kbd-macro (error "No keyboard macro defined"))
859 (list (register-read-with-preview "Save to register: "))))
860 (set-register r (registerv-make
861 last-kbd-macro
862 :jump-func 'kmacro-execute-from-register
863 :print-func (lambda (k)
864 (princ (format "a keyboard macro:\n %s"
865 (format-kbd-macro k))))
866 :insert-func (lambda (k)
867 (insert (format-kbd-macro k))))))
870 (defun kmacro-view-macro (&optional _arg)
871 "Display the last keyboard macro.
872 If repeated, it shows previous elements in the macro ring.
873 The ARG parameter is unused."
874 (interactive)
875 (cond
876 ((or (kmacro-ring-empty-p)
877 (not (eq last-command 'kmacro-view-macro)))
878 (setq kmacro-view-last-item nil))
879 ((null kmacro-view-last-item)
880 (setq kmacro-view-last-item kmacro-ring
881 kmacro-view-item-no 2))
882 ((consp kmacro-view-last-item)
883 (setq kmacro-view-last-item (cdr kmacro-view-last-item)
884 kmacro-view-item-no (1+ kmacro-view-item-no)))
886 (setq kmacro-view-last-item nil)))
887 (setq this-command 'kmacro-view-macro
888 last-command this-command) ;; in case we repeat
889 (kmacro-display (if kmacro-view-last-item
890 (car (car kmacro-view-last-item))
891 last-kbd-macro)
893 (if kmacro-view-last-item
894 (concat (cond ((= kmacro-view-item-no 2) "2nd")
895 ((= kmacro-view-item-no 3) "3nd")
896 (t (format "%dth" kmacro-view-item-no)))
897 " previous macro")
898 "Last macro")))
900 (defun kmacro-view-macro-repeat (&optional arg)
901 "Display the last keyboard macro.
902 If repeated, it shows previous elements in the macro ring.
903 To execute the displayed macro ring item without changing the macro ring,
904 just enter C-k.
905 This is like `kmacro-view-macro', but allows repeating macro commands
906 without repeating the prefix."
907 (interactive)
908 (let ((keys (kmacro-get-repeat-prefix)))
909 (kmacro-view-macro arg)
910 (if (and last-kbd-macro keys)
911 (kmacro-repeat-on-last-key keys))))
913 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
916 (defun kmacro-edit-macro-repeat (&optional arg)
917 "Edit last keyboard macro."
918 (interactive "P")
919 (edit-kbd-macro "\r" arg))
921 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
924 (defun kmacro-edit-macro (&optional arg)
925 "As edit last keyboard macro, but without kmacro-repeat property."
926 (interactive "P")
927 (edit-kbd-macro "\r" arg))
930 (defun kmacro-edit-lossage ()
931 "Edit most recent 300 keystrokes as a keyboard macro."
932 (interactive)
933 (kmacro-push-ring)
934 (edit-kbd-macro "\C-hl"))
937 ;;; Single-step editing of keyboard macros
939 (defvar kmacro-step-edit-active) ;; step-editing active
940 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
941 (defvar kmacro-step-edit-inserting) ;; inserting into macro
942 (defvar kmacro-step-edit-appending) ;; append to end of macro
943 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
944 (defvar kmacro-step-edit-prefix-index) ;; index of first prefix arg key
945 (defvar kmacro-step-edit-key-index) ;; index of current key
946 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
947 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
948 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
950 (defvar kmacro-step-edit-map
951 (let ((map (make-sparse-keymap)))
952 ;; query-replace-map answers include: `act', `skip', `act-and-show',
953 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
954 ;; `automatic', `backup', `exit-prefix', and `help'.")
955 ;; Also: `quit', `edit-replacement'
957 (set-keymap-parent map query-replace-map)
959 (define-key map "\t" 'act-repeat)
960 (define-key map [tab] 'act-repeat)
961 (define-key map "\C-k" 'skip-rest)
962 (define-key map "c" 'automatic)
963 (define-key map "f" 'skip-keep)
964 (define-key map "q" 'quit)
965 (define-key map "d" 'skip)
966 (define-key map "\C-d" 'skip)
967 (define-key map "i" 'insert)
968 (define-key map "I" 'insert-1)
969 (define-key map "r" 'replace)
970 (define-key map "R" 'replace-1)
971 (define-key map "a" 'append)
972 (define-key map "A" 'append-end)
973 map)
974 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
975 This keymap is an extension to the `query-replace-map', allowing the
976 following additional answers: `insert', `insert-1', `replace', `replace-1',
977 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
979 (defvar kmacro-step-edit-prefix-commands
980 '(universal-argument universal-argument-more universal-argument-minus
981 digit-argument negative-argument)
982 "Commands which build up a prefix arg for the current command.")
984 (defun kmacro-step-edit-prompt (macro index)
985 ;; Show step-edit prompt
986 (let ((keys (and (not kmacro-step-edit-appending)
987 index (substring macro index executing-kbd-macro-index)))
988 (future (and (not kmacro-step-edit-appending)
989 (substring macro executing-kbd-macro-index)))
990 (message-log-max nil)
991 (curmsg (current-message)))
993 ;; TODO: Scroll macro if max-mini-window-height is too small.
994 (message "%s"
995 (concat
996 (format "Macro: %s%s%s%s%s\n"
997 (format-kbd-macro kmacro-step-edit-new-macro 1)
998 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
999 (propertize (if keys (format-kbd-macro keys)
1000 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
1001 (if future " " "")
1002 (if future (format-kbd-macro future) ""))
1003 (cond
1004 ((minibufferp)
1005 (format "%s\n%s\n"
1006 (propertize "\
1007 minibuffer " 'face 'header-line)
1008 (buffer-substring (point-min) (point-max))))
1009 (curmsg
1010 (format "%s\n%s\n"
1011 (propertize "\
1012 echo area " 'face 'header-line)
1013 curmsg))
1014 (t ""))
1015 (if keys
1016 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
1017 (propertize "\
1018 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
1019 (if kmacro-step-edit-help "\
1020 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
1021 TAB: execute while same, ?: toggle help
1022 Edit: i: insert, r: replace, a: append, A: append at end,
1023 I/R: insert/replace with one sequence,
1024 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
1025 ----------------------------------------------------------------
1026 " "")
1027 (propertize "Next command:" 'face 'bold)
1028 this-command)
1029 (propertize
1030 (format "Type key sequence%s to insert and execute%s: "
1031 (if (numberp kmacro-step-edit-inserting) "" "s")
1032 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
1033 'face 'bold))))))
1035 (defun kmacro-step-edit-query ()
1036 ;; Pre-command hook function for step-edit in "command" mode
1037 (let ((resize-mini-windows t)
1038 (max-mini-window-height kmacro-step-edit-mini-window-height)
1039 act restore-index next-index)
1041 ;; Handle commands which reads additional input using read-char.
1042 (cond
1043 ((and (eq this-command 'quoted-insert)
1044 (not (eq kmacro-step-edit-action t)))
1045 ;; Find the actual end of this key sequence.
1046 ;; Must be able to backtrack in case we actually execute it.
1047 (setq restore-index executing-kbd-macro-index)
1048 (let (unread-command-events)
1049 (quoted-insert 0)
1050 (when unread-command-events
1051 (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1052 next-index executing-kbd-macro-index)))))
1054 ;; Query the user; stop macro execution temporarily.
1055 (let ((macro executing-kbd-macro)
1056 (executing-kbd-macro nil)
1057 (defining-kbd-macro nil))
1059 ;; Any action requested by previous command
1060 (cond
1061 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
1062 (cond
1063 ((eq this-command 'quoted-insert)
1064 (clear-this-command-keys) ;; recent-keys actually
1065 (let (unread-command-events)
1066 (quoted-insert (prefix-numeric-value current-prefix-arg))
1067 (setq kmacro-step-edit-new-macro
1068 (vconcat kmacro-step-edit-new-macro (recent-keys)))
1069 (when unread-command-events
1070 (setq kmacro-step-edit-new-macro
1071 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
1072 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
1073 (setq current-prefix-arg nil
1074 prefix-arg nil)
1075 (setq act 'ignore))
1077 (setq act 'act)))
1078 (setq kmacro-step-edit-action nil))
1079 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
1080 (setq act 'act))
1082 (setq kmacro-step-edit-action nil)))
1084 ;; Handle prefix arg, or query user
1085 (cond
1086 (act act) ;; set above
1087 ((memq this-command kmacro-step-edit-prefix-commands)
1088 (unless kmacro-step-edit-prefix-index
1089 (setq kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1090 (setq act 'universal-argument))
1091 ((eq this-command 'universal-argument-other-key)
1092 (setq act 'universal-argument))
1094 (kmacro-step-edit-prompt macro (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1095 (setq act (lookup-key kmacro-step-edit-map
1096 (vector (with-current-buffer (current-buffer) (read-event))))))))
1098 ;; Resume macro execution and perform the action
1099 (cond
1100 ((eq act 'universal-argument)
1101 nil)
1102 ((cond
1103 ((eq act 'act)
1105 ((eq act 'act-repeat)
1106 (setq kmacro-step-edit-action this-command)
1108 ((eq act 'quit)
1109 (setq kmacro-step-edit-replace nil)
1110 (setq kmacro-step-edit-active 'ignore)
1111 nil)
1112 ((eq act 'skip)
1113 (setq kmacro-step-edit-prefix-index nil)
1114 nil)
1115 ((eq act 'skip-keep)
1116 (setq this-command 'ignore)
1118 ((eq act 'skip-rest)
1119 (setq kmacro-step-edit-active 'ignore)
1120 nil)
1121 ((memq act '(automatic exit))
1122 (setq kmacro-step-edit-active nil)
1123 (setq act t)
1125 ((member act '(insert-1 insert))
1126 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1127 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
1128 nil)
1129 ((member act '(replace-1 replace))
1130 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
1131 (setq kmacro-step-edit-prefix-index nil)
1132 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1133 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1134 kmacro-step-edit-appending t))
1135 nil)
1136 ((eq act 'append)
1137 (setq kmacro-step-edit-inserting t)
1138 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1139 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1140 kmacro-step-edit-appending t))
1142 ((eq act 'append-end)
1143 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1144 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1145 kmacro-step-edit-inserting t
1146 kmacro-step-edit-appending t)
1147 (setq kmacro-step-edit-active 'append-end))
1148 (setq act t)
1150 ((eq act 'help)
1151 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1152 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
1153 nil)
1154 (t ;; Ignore unknown responses
1155 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1156 nil))
1157 (if (> executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1158 (setq kmacro-step-edit-new-macro
1159 (vconcat kmacro-step-edit-new-macro
1160 (substring executing-kbd-macro
1161 (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
1162 (if (eq act t) nil executing-kbd-macro-index)))
1163 kmacro-step-edit-prefix-index nil))
1164 (if restore-index
1165 (setq executing-kbd-macro-index restore-index)))
1167 (setq this-command 'ignore)))
1168 (setq kmacro-step-edit-key-index next-index)))
1170 (defun kmacro-step-edit-insert ()
1171 ;; Pre-command hook function for step-edit in "insert" mode
1172 (let ((resize-mini-windows t)
1173 (max-mini-window-height kmacro-step-edit-mini-window-height)
1174 (macro executing-kbd-macro)
1175 (executing-kbd-macro nil)
1176 (defining-kbd-macro nil)
1177 cmd keys next-index)
1178 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
1179 kmacro-step-edit-prefix-index nil)
1180 (kmacro-step-edit-prompt macro nil)
1181 ;; Now, we have read a key sequence from the macro, but we don't want
1182 ;; to execute it yet. So push it back and read another sequence.
1183 (reset-this-command-lengths)
1184 (setq keys (read-key-sequence nil nil nil nil t))
1185 (setq cmd (key-binding keys t nil))
1186 (if (cond
1187 ((null cmd)
1189 ((eq cmd 'quoted-insert)
1190 (clear-this-command-keys) ;; recent-keys actually
1191 (quoted-insert (prefix-numeric-value current-prefix-arg))
1192 (setq current-prefix-arg nil
1193 prefix-arg nil)
1194 (setq keys (vconcat keys (recent-keys)))
1195 (when (numberp kmacro-step-edit-inserting)
1196 (setq kmacro-step-edit-inserting nil)
1197 (when unread-command-events
1198 (setq keys (substring keys 0 (- (length unread-command-events)))
1199 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1200 next-index executing-kbd-macro-index
1201 unread-command-events nil)))
1202 (setq cmd 'ignore)
1203 nil)
1204 ((memq cmd kmacro-step-edit-prefix-commands)
1205 (reset-this-command-lengths)
1206 nil)
1207 ((eq cmd 'universal-argument-other-key)
1208 (setq kmacro-step-edit-action t)
1209 (reset-this-command-lengths)
1210 (if (numberp kmacro-step-edit-inserting)
1211 (setq kmacro-step-edit-inserting nil))
1212 nil)
1213 ((numberp kmacro-step-edit-inserting)
1214 (setq kmacro-step-edit-inserting nil)
1215 nil)
1216 ((equal keys "\C-j")
1217 (setq kmacro-step-edit-inserting nil)
1218 (setq kmacro-step-edit-action nil)
1219 ;; Forget any (partial) prefix arg from next command
1220 (setq kmacro-step-edit-prefix-index nil)
1221 (reset-this-command-lengths)
1222 (setq overriding-terminal-local-map nil)
1223 (setq next-index kmacro-step-edit-key-index)
1225 (t nil))
1226 (setq this-command 'ignore)
1227 (setq this-command cmd)
1228 (if (memq this-command '(self-insert-command digit-argument))
1229 (setq last-command-event (aref keys (1- (length keys)))))
1230 (if keys
1231 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1232 (setq kmacro-step-edit-key-index next-index)))
1234 (defun kmacro-step-edit-pre-command ()
1235 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1236 (when kmacro-step-edit-active
1237 (cond
1238 ((eq kmacro-step-edit-active 'ignore)
1239 (setq this-command 'ignore))
1240 ((eq kmacro-step-edit-active 'append-end)
1241 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1242 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1243 kmacro-step-edit-inserting t
1244 kmacro-step-edit-appending t
1245 kmacro-step-edit-active t)))
1246 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1247 (if kmacro-step-edit-inserting
1248 (kmacro-step-edit-insert)
1249 (kmacro-step-edit-query))
1250 (setq kmacro-step-edit-num-input-keys num-input-keys)
1251 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1252 (setq kmacro-step-edit-appending nil
1253 kmacro-step-edit-active 'ignore)))))
1254 (when (eq kmacro-step-edit-active t)
1255 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1257 (defun kmacro-step-edit-minibuf-setup ()
1258 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1259 (when kmacro-step-edit-active
1260 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1262 (defun kmacro-step-edit-post-command ()
1263 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1264 (when kmacro-step-edit-active
1265 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1266 (if kmacro-step-edit-key-index
1267 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1268 (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
1271 (defun kmacro-step-edit-macro ()
1272 "Step edit and execute last keyboard macro.
1274 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1275 (interactive)
1276 (let ((kmacro-step-edit-active t)
1277 (kmacro-step-edit-new-macro "")
1278 (kmacro-step-edit-inserting nil)
1279 (kmacro-step-edit-appending nil)
1280 (kmacro-step-edit-replace t)
1281 (kmacro-step-edit-prefix-index nil)
1282 (kmacro-step-edit-key-index 0)
1283 (kmacro-step-edit-action nil)
1284 (kmacro-step-edit-help nil)
1285 (kmacro-step-edit-num-input-keys num-input-keys)
1286 (pre-command-hook pre-command-hook)
1287 (post-command-hook post-command-hook)
1288 (minibuffer-setup-hook minibuffer-setup-hook))
1289 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1290 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1291 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1292 (call-last-kbd-macro nil nil)
1293 (when (and kmacro-step-edit-replace
1294 kmacro-step-edit-new-macro
1295 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1296 (kmacro-push-ring)
1297 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1299 (provide 'kmacro)
1301 ;;; kmacro.el ends here