Improve responsiveness while in 'replace-buffer-contents'
[emacs.git] / lisp / kmacro.el
blob7abd8aed79afe7a43c685fe0f37016a245fd037c
1 ;;; kmacro.el --- enhanced keyboard macros -*- lexical-binding: t -*-
3 ;; Copyright (C) 2002-2018 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 <https://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:
114 (require 'replace)
116 (defgroup kmacro nil
117 "Simplified keyboard macro user interface."
118 :group 'keyboard
119 :group 'convenience
120 :version "22.1"
121 :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
122 :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
124 (defcustom kmacro-call-mouse-event 'S-mouse-3
125 "The mouse event used by kmacro to call a macro.
126 Set to nil if no mouse binding is desired."
127 :type 'symbol
128 :group 'kmacro)
130 (defcustom kmacro-ring-max 8
131 "Maximum number of keyboard macros to save in macro ring."
132 :type 'integer
133 :group 'kmacro)
136 (defcustom kmacro-execute-before-append t
137 "Controls whether appending to a macro starts by executing the macro.
138 If non-nil, using a single \\[universal-argument] prefix executes the macro
139 before appending, while more than one \\[universal-argument] prefix does not
140 execute the macro.
141 Otherwise, a single \\[universal-argument] prefix does not execute the
142 macro, while more than one \\[universal-argument] prefix causes the
143 macro to be executed before appending to it."
144 :type 'boolean
145 :group 'kmacro)
148 (defcustom kmacro-repeat-no-prefix t
149 "Allow repeating certain macro commands without entering the C-x C-k prefix."
150 :type 'boolean
151 :group 'kmacro)
153 (defcustom kmacro-call-repeat-key t
154 "Allow repeating macro call using last key or a specific key."
155 :type '(choice (const :tag "Disabled" nil)
156 (const :tag "Last key" t)
157 (character :tag "Character" :value ?e)
158 (symbol :tag "Key symbol" :value RET))
159 :group 'kmacro)
161 (defcustom kmacro-call-repeat-with-arg nil
162 "Repeat macro call with original arg when non-nil; repeat once if nil."
163 :type 'boolean
164 :group 'kmacro)
166 (defcustom kmacro-step-edit-mini-window-height 0.75
167 "Override `max-mini-window-height' when step edit keyboard macro."
168 :type 'number
169 :group 'kmacro)
171 ;; Keymap
173 (defvar kmacro-keymap
174 (let ((map (make-sparse-keymap)))
175 ;; Start, end, execute macros
176 (define-key map "s" 'kmacro-start-macro)
177 (define-key map "\C-s" 'kmacro-start-macro)
178 (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
179 (define-key map "r" 'apply-macro-to-region-lines)
180 (define-key map "q" 'kbd-macro-query) ;; Like C-x q
182 ;; macro ring
183 (define-key map "\C-n" 'kmacro-cycle-ring-next)
184 (define-key map "\C-p" 'kmacro-cycle-ring-previous)
185 (define-key map "\C-v" 'kmacro-view-macro-repeat)
186 (define-key map "\C-d" 'kmacro-delete-ring-head)
187 (define-key map "\C-t" 'kmacro-swap-ring)
188 (define-key map "\C-l" 'kmacro-call-ring-2nd-repeat)
190 ;; macro counter
191 (define-key map "\C-f" 'kmacro-set-format)
192 (define-key map "\C-c" 'kmacro-set-counter)
193 (define-key map "\C-i" 'kmacro-insert-counter)
194 (define-key map "\C-a" 'kmacro-add-counter)
196 ;; macro editing
197 (define-key map "\C-e" 'kmacro-edit-macro-repeat)
198 (define-key map "\r" 'kmacro-edit-macro)
199 (define-key map "e" 'edit-kbd-macro)
200 (define-key map "l" 'kmacro-edit-lossage)
201 (define-key map " " 'kmacro-step-edit-macro)
203 ;; naming and binding
204 (define-key map "b" 'kmacro-bind-to-key)
205 (define-key map "n" 'kmacro-name-last-macro)
206 (define-key map "x" 'kmacro-to-register)
207 map)
208 "Keymap for keyboard macro commands.")
209 (defalias 'kmacro-keymap kmacro-keymap)
211 ;;; Provide some binding for startup:
212 ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
213 ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
214 ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
215 ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
216 ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
217 ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
218 ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
220 (if kmacro-call-mouse-event
221 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
224 ;;; Called from keyboard-quit
226 (defun kmacro-keyboard-quit ()
227 (or (not defining-kbd-macro)
228 (eq defining-kbd-macro 'append)
229 (kmacro-ring-empty-p)
230 (kmacro-pop-ring)))
233 ;;; Keyboard macro counter
235 (defvar kmacro-counter 0
236 "Current keyboard macro counter.
238 This is normally initialized to zero when the macro is defined,
239 and incremented each time the value of the counter is inserted
240 into a buffer. See `kmacro-start-macro-or-insert-counter' for
241 more details.")
243 (defvar kmacro-default-counter-format "%d")
245 (defvar kmacro-counter-format "%d"
246 "Current keyboard macro counter format.
248 Can be set directly via `kmacro-set-format', which see.")
250 (defvar kmacro-counter-format-start kmacro-counter-format
251 "Macro format at start of macro execution.")
253 (defvar kmacro-counter-value-start kmacro-counter
254 "Macro counter at start of macro execution.")
256 (defvar kmacro-last-counter 0
257 "Last counter inserted by key macro.")
259 (defvar kmacro-initial-counter-value nil
260 "Initial counter value for the next keyboard macro to be defined.")
263 (defun kmacro-insert-counter (arg)
264 "Insert current value of `kmacro-counter', then increment it by ARG.
265 Interactively, ARG defaults to 1. With \\[universal-argument], insert
266 current value of `kmacro-counter', but do not increment it."
267 (interactive "P")
268 (if kmacro-initial-counter-value
269 (setq kmacro-counter kmacro-initial-counter-value
270 kmacro-initial-counter-value nil))
271 (if (and arg (listp arg))
272 (insert (format kmacro-counter-format kmacro-last-counter))
273 (insert (format kmacro-counter-format kmacro-counter))
274 (kmacro-add-counter (prefix-numeric-value arg))))
277 (defun kmacro-set-format (format)
278 "Set the format of `kmacro-counter' to FORMAT."
279 (interactive "sMacro Counter Format: ")
280 (setq kmacro-counter-format
281 (if (equal format "") "%d" format))
282 ;; redefine initial macro counter if we are not executing a macro.
283 (if (not (or defining-kbd-macro executing-kbd-macro))
284 (setq kmacro-default-counter-format kmacro-counter-format)))
287 (defun kmacro-display-counter (&optional value)
288 "Display current counter value."
289 (unless value (setq value kmacro-counter))
290 (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value))
293 (defun kmacro-set-counter (arg)
294 "Set the value of `kmacro-counter' to ARG, or prompt for value if no argument.
295 With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
296 (interactive "NMacro counter value: ")
297 (if (not (or defining-kbd-macro executing-kbd-macro))
298 (kmacro-display-counter (setq kmacro-initial-counter-value arg))
299 (setq kmacro-last-counter kmacro-counter
300 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
301 kmacro-counter-value-start
302 arg))
303 (unless executing-kbd-macro
304 (kmacro-display-counter))))
307 (defun kmacro-add-counter (arg)
308 "Add the value of numeric prefix arg (prompt if missing) to `kmacro-counter'.
309 With \\[universal-argument], restore previous counter value."
310 (interactive "NAdd to macro counter: ")
311 (if kmacro-initial-counter-value
312 (setq kmacro-counter kmacro-initial-counter-value
313 kmacro-initial-counter-value nil))
314 (let ((last kmacro-last-counter))
315 (setq kmacro-last-counter kmacro-counter
316 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
317 last
318 kmacro-counter (+ kmacro-counter arg))))
319 (unless executing-kbd-macro
320 (kmacro-display-counter)))
323 (defun kmacro-loop-setup-function ()
324 "Function called prior to each iteration of macro."
325 ;; Restore macro counter format to initial format, so it is ok to change
326 ;; counter format in the macro without restoring it.
327 (setq kmacro-counter-format kmacro-counter-format-start)
328 ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
329 (setq kmacro-counter-value-start kmacro-counter)
330 ;; Return non-nil to continue execution.
334 ;;; Keyboard macro ring
336 (defvar kmacro-ring nil
337 "The keyboard macro ring.
338 Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
339 the macro ring (when defining or executing) is not stored in the ring;
340 instead it is available in the variables `last-kbd-macro', `kmacro-counter',
341 and `kmacro-counter-format'.")
343 ;; Remember what we are currently looking at with kmacro-view-macro.
345 (defvar kmacro-view-last-item nil)
346 (defvar kmacro-view-item-no 0)
349 (defun kmacro-ring-head ()
350 "Return pseudo head element in macro ring."
351 (and last-kbd-macro
352 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
355 (defun kmacro-push-ring (&optional elt)
356 "Push ELT or current macro onto `kmacro-ring'."
357 (when (setq elt (or elt (kmacro-ring-head)))
358 (let ((history-delete-duplicates nil))
359 (add-to-history 'kmacro-ring elt kmacro-ring-max))))
362 (defun kmacro-split-ring-element (elt)
363 (setq last-kbd-macro (car elt)
364 kmacro-counter (nth 1 elt)
365 kmacro-counter-format-start (nth 2 elt)))
368 (defun kmacro-pop-ring1 (&optional raw)
369 "Pop head element off macro ring (no check).
370 Non-nil arg RAW means just return raw first element."
371 (prog1 (car kmacro-ring)
372 (unless raw
373 (kmacro-split-ring-element (car kmacro-ring)))
374 (setq kmacro-ring (cdr kmacro-ring))))
377 (defun kmacro-pop-ring (&optional raw)
378 "Pop head element off macro ring.
379 Non-nil arg RAW means just return raw first element."
380 (unless (kmacro-ring-empty-p)
381 (kmacro-pop-ring1 raw)))
384 (defun kmacro-ring-empty-p (&optional none)
385 "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
386 Check only `last-kbd-macro' if optional arg NONE is non-nil."
387 (while (and (null last-kbd-macro) kmacro-ring)
388 (kmacro-pop-ring1))
389 (cond
390 ((null last-kbd-macro)
391 (message "No keyboard macro defined.")
393 ((and (null none) (null kmacro-ring))
394 (message "Only one keyboard macro defined.")
396 (t nil)))
399 (defun kmacro-display (macro &optional trunc descr empty)
400 "Display a keyboard MACRO.
401 Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
402 Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
403 Optional arg EMPTY is message to print if no macros are defined."
404 (if macro
405 (let* ((x 60)
406 (m (format-kbd-macro macro))
407 (l (length m))
408 (z (and trunc (> l x))))
409 (message "%s%s: %s%s" (or descr "Macro")
410 (if (= kmacro-counter 0) ""
411 (format " [%s]"
412 (format kmacro-counter-format-start kmacro-counter)))
413 (if z (substring m 0 (1- x)) m) (if z "..." "")))
414 (message "%s" (or empty "No keyboard macros defined"))))
417 (defun kmacro-repeat-on-last-key (keys)
418 "Process kmacro commands keys immediately after cycling the ring."
419 (setq keys (vconcat keys))
420 (let ((n (1- (length keys)))
421 cmd done repeat)
422 (while (and last-kbd-macro
423 (not done)
424 (aset keys n (read-event))
425 (setq cmd (key-binding keys t))
426 (setq repeat (get cmd 'kmacro-repeat)))
427 (clear-this-command-keys t)
428 (cond
429 ((eq repeat 'ring)
430 (if kmacro-ring
431 (let ((kmacro-repeat-no-prefix nil))
432 (funcall cmd nil))
433 (kmacro-display last-kbd-macro t)))
434 ((eq repeat 'head)
435 (let ((kmacro-repeat-no-prefix nil))
436 (funcall cmd nil)))
437 ((eq repeat 'stop)
438 (funcall cmd nil)
439 (setq done t)))
440 (setq last-input-event nil)))
441 (when last-input-event
442 (clear-this-command-keys t)
443 (push last-input-event unread-command-events)))
446 (defun kmacro-get-repeat-prefix ()
447 (let (keys)
448 (and kmacro-repeat-no-prefix
449 (setq keys (this-single-command-keys))
450 (> (length keys) 1)
451 keys)))
454 ;;;###autoload
455 (defun kmacro-exec-ring-item (item arg)
456 "Execute item ITEM from the macro ring.
457 ARG is the number of times to execute the item."
458 ;; Use counter and format specific to the macro on the ring!
459 (let ((kmacro-counter (nth 1 item))
460 (kmacro-counter-format-start (nth 2 item)))
461 (execute-kbd-macro (car item) arg #'kmacro-loop-setup-function)
462 (setcar (cdr item) kmacro-counter)))
465 (defun kmacro-call-ring-2nd (arg)
466 "Execute second keyboard macro in macro ring."
467 (interactive "P")
468 (unless (kmacro-ring-empty-p)
469 (kmacro-exec-ring-item (car kmacro-ring) arg)))
472 (defun kmacro-call-ring-2nd-repeat (arg)
473 "Execute second keyboard macro in macro ring.
474 This is like `kmacro-call-ring-2nd', but allows repeating macro commands
475 without repeating the prefix."
476 (interactive "P")
477 (let ((keys (kmacro-get-repeat-prefix)))
478 (kmacro-call-ring-2nd arg)
479 (if (and kmacro-ring keys)
480 (kmacro-repeat-on-last-key keys))))
482 (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
485 (defun kmacro-view-ring-2nd ()
486 "Display the current head of the keyboard macro ring."
487 (interactive)
488 (unless (kmacro-ring-empty-p)
489 (kmacro-display (car (car kmacro-ring)) nil "2nd macro")))
492 (defun kmacro-cycle-ring-next (&optional _arg)
493 "Move to next keyboard macro in keyboard macro ring.
494 Displays the selected macro in the echo area.
495 The ARG parameter is unused."
496 (interactive)
497 (unless (kmacro-ring-empty-p)
498 (kmacro-push-ring)
499 (let* ((keys (kmacro-get-repeat-prefix))
500 (len (length kmacro-ring))
501 (tail (nthcdr (- len 2) kmacro-ring))
502 (elt (car (cdr tail))))
503 (setcdr tail nil)
504 (kmacro-split-ring-element elt)
505 (kmacro-display last-kbd-macro t)
506 (if keys
507 (kmacro-repeat-on-last-key keys)))))
509 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
512 (defun kmacro-cycle-ring-previous (&optional _arg)
513 "Move to previous keyboard macro in keyboard macro ring.
514 Displays the selected macro in the echo area.
515 The ARG parameter is unused."
516 (interactive)
517 (unless (kmacro-ring-empty-p)
518 (let ((keys (kmacro-get-repeat-prefix))
519 (cur (kmacro-ring-head)))
520 (kmacro-pop-ring1)
521 (if kmacro-ring
522 (nconc kmacro-ring (list cur))
523 (setq kmacro-ring (list cur)))
524 (kmacro-display last-kbd-macro t)
525 (if keys
526 (kmacro-repeat-on-last-key keys)))))
528 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
531 (defun kmacro-swap-ring ()
532 "Swap first two elements on keyboard macro ring."
533 (interactive)
534 (unless (kmacro-ring-empty-p)
535 (let ((cur (kmacro-ring-head)))
536 (kmacro-pop-ring1)
537 (kmacro-push-ring cur))
538 (kmacro-display last-kbd-macro t)))
541 (defun kmacro-delete-ring-head (&optional _arg)
542 "Delete current macro from keyboard macro ring.
543 The ARG parameter is unused."
544 (interactive)
545 (unless (kmacro-ring-empty-p t)
546 (if (null kmacro-ring)
547 (setq last-kbd-macro nil)
548 (kmacro-pop-ring))
549 (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
551 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
553 ;;; Traditional bindings:
556 ;;;###autoload
557 (defun kmacro-start-macro (arg)
558 "Record subsequent keyboard input, defining a keyboard macro.
559 The commands are recorded even as they are executed.
560 Use \\[kmacro-end-macro] to finish recording and make the macro available.
561 Use \\[kmacro-end-and-call-macro] to execute the macro.
563 Non-nil arg (prefix arg) means append to last macro defined.
565 With \\[universal-argument] prefix, append to last keyboard macro
566 defined. Depending on `kmacro-execute-before-append', this may begin
567 by re-executing the last macro as if you typed it again.
569 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
570 defining the macro.
572 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
573 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
574 The format of the counter can be modified via \\[kmacro-set-format].
576 Use \\[kmacro-name-last-macro] to give it a name that will remain valid even
577 after another macro is defined.
578 Use \\[kmacro-bind-to-key] to bind it to a key sequence."
579 (interactive "P")
580 (if (or defining-kbd-macro executing-kbd-macro)
581 (message "Already defining keyboard macro.")
582 (let ((append (and arg (listp arg))))
583 (unless append
584 (if last-kbd-macro
585 (kmacro-push-ring
586 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
587 (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
588 kmacro-initial-counter-value
590 kmacro-initial-counter-value nil
591 kmacro-counter-value-start kmacro-counter
592 kmacro-last-counter kmacro-counter
593 kmacro-counter-format kmacro-default-counter-format
594 kmacro-counter-format-start kmacro-default-counter-format))
596 (start-kbd-macro append
597 (and append
598 (if kmacro-execute-before-append
599 (> (car arg) 4)
600 (= (car arg) 4))))
601 (if (and defining-kbd-macro append)
602 (setq defining-kbd-macro 'append)))))
605 ;;;###autoload
606 (defun kmacro-end-macro (arg)
607 "Finish defining a keyboard macro.
608 The definition was started by \\[kmacro-start-macro].
609 The macro is now available for use via \\[kmacro-call-macro],
610 or it can be given a name with \\[kmacro-name-last-macro] and then invoked
611 under that name.
613 With numeric arg, repeat macro now that many times,
614 counting the definition just completed as the first repetition.
615 An argument of zero means repeat until error."
616 (interactive "P")
617 ;; Isearch may push the kmacro-end-macro key sequence onto the macro.
618 ;; Just ignore it when executing the macro.
619 (unless executing-kbd-macro
620 (end-kbd-macro arg #'kmacro-loop-setup-function)
621 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
622 (setq last-kbd-macro nil)
623 (message "Ignore empty macro")
624 ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
625 (while (and (null last-kbd-macro) kmacro-ring)
626 (kmacro-pop-ring1)))))
629 ;;;###autoload
630 (defun kmacro-call-macro (arg &optional no-repeat end-macro macro)
631 "Call the keyboard MACRO that you defined with \\[kmacro-start-macro].
632 A prefix argument serves as a repeat count. Zero means repeat until error.
633 MACRO defaults to `last-kbd-macro'.
635 When you call the macro, you can call the macro again by repeating
636 just the last key in the key sequence that you used to call this
637 command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
638 for details on how to adjust or disable this behavior.
640 To give a macro a name so you can call it even after defining others,
641 use \\[kmacro-name-last-macro]."
642 (interactive "p")
643 (let ((repeat-key (and (or (and (null no-repeat)
644 (> (length (this-single-command-keys)) 1))
645 ;; Used when we're in the process of repeating.
646 (eq no-repeat 'repeating))
647 last-input-event)))
648 (if end-macro
649 (kmacro-end-macro arg) ; modifies last-kbd-macro
650 (let ((last-kbd-macro (or macro last-kbd-macro)))
651 (call-last-kbd-macro arg #'kmacro-loop-setup-function)))
652 (when (consp arg)
653 (setq arg (car arg)))
654 (when (and (or (null arg) (> arg 0))
655 (setq repeat-key
656 (if (eq kmacro-call-repeat-key t)
657 repeat-key
658 kmacro-call-repeat-key)))
659 ;; Issue a hint to the user, if the echo area isn't in use.
660 (unless (current-message)
661 (message "(Type %s to repeat macro%s)"
662 (format-kbd-macro (vector repeat-key) nil)
663 (if (and kmacro-call-repeat-with-arg
664 arg (> arg 1))
665 (format " %d times" arg) "")))
666 ;; Can't use the `keep-pred' arg because this overlay keymap
667 ;; needs to be removed during the next run of the kmacro
668 ;; (i.e. we must add and remove this map at each repetition).
669 (set-transient-map
670 (let ((map (make-sparse-keymap)))
671 (define-key map (vector repeat-key)
672 `(lambda () (interactive)
673 (kmacro-call-macro ,(and kmacro-call-repeat-with-arg arg)
674 'repeating nil ,(if end-macro
675 last-kbd-macro
676 (or macro last-kbd-macro)))))
677 map)))))
680 ;;; Combined function key bindings:
682 ;;;###autoload
683 (defun kmacro-start-macro-or-insert-counter (arg)
684 "Record subsequent keyboard input, defining a keyboard macro.
685 The commands are recorded even as they are executed.
687 Initializes the macro's `kmacro-counter' to ARG (or 0 if no prefix arg)
688 before defining the macro.
690 With \\[universal-argument], appends to current keyboard macro (keeping
691 the current value of `kmacro-counter').
693 When used during defining/executing a macro, inserts the current value
694 of `kmacro-counter' and increments the counter value by ARG (or by 1 if no
695 prefix argument). With just \\[universal-argument], inserts the current value
696 of `kmacro-counter', but does not modify the counter; this is the
697 same as incrementing the counter by zero.
699 The macro counter can be set directly via \\[kmacro-set-counter] and \\[kmacro-add-counter].
700 The format of the inserted value of the counter can be controlled
701 via \\[kmacro-set-format]."
702 (interactive "P")
703 (if (or defining-kbd-macro executing-kbd-macro)
704 (kmacro-insert-counter arg)
705 (kmacro-start-macro arg)))
708 ;;;###autoload
709 (defun kmacro-end-or-call-macro (arg &optional no-repeat)
710 "End kbd macro if currently being defined; else call last kbd macro.
711 With numeric prefix ARG, repeat macro that many times.
712 With \\[universal-argument], call second macro in macro ring."
713 (interactive "P")
714 (cond
715 (defining-kbd-macro
716 (if kmacro-call-repeat-key
717 (kmacro-call-macro arg no-repeat t)
718 (kmacro-end-macro arg)))
719 ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
720 kmacro-view-last-item)
721 (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
722 ((and arg (listp arg))
723 (kmacro-call-ring-2nd 1))
725 (kmacro-call-macro arg no-repeat))))
728 (defun kmacro-end-or-call-macro-repeat (arg)
729 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
730 (interactive "P")
731 (let ((keys (kmacro-get-repeat-prefix)))
732 (kmacro-end-or-call-macro arg t)
733 (if keys
734 (kmacro-repeat-on-last-key keys))))
736 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
739 ;;;###autoload
740 (defun kmacro-end-and-call-macro (arg &optional no-repeat)
741 "Call last keyboard macro, ending it first if currently being defined.
742 With numeric prefix ARG, repeat macro that many times.
743 Zero argument means repeat until there is an error.
745 To give a macro a name, so you can call it even after defining other
746 macros, use \\[kmacro-name-last-macro]."
747 (interactive "P")
748 (if defining-kbd-macro
749 (kmacro-end-macro nil))
750 (kmacro-call-macro arg no-repeat))
753 ;;;###autoload
754 (defun kmacro-end-call-mouse (event)
755 "Move point to the position clicked with the mouse and call last kbd macro.
756 If kbd macro currently being defined end it before activating it."
757 (interactive "e")
758 (when defining-kbd-macro
759 (end-kbd-macro)
760 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
761 (setq last-kbd-macro nil)
762 (message "Ignore empty macro")
763 ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
764 (while (and (null last-kbd-macro) kmacro-ring)
765 (kmacro-pop-ring1))))
766 (mouse-set-point event)
767 (kmacro-call-macro nil t))
770 ;;; Misc. commands
772 ;; An idea for macro bindings:
773 ;; Create a separate keymap installed as a minor-mode keymap (e.g. in
774 ;; the emulation-mode-map-alists) in which macro bindings are made
775 ;; independent of any other bindings. When first binding is made,
776 ;; the keymap is created, installed, and enabled. Key seq. C-x C-k +
777 ;; can then be used to toggle the use of this keymap on and off.
778 ;; This means that it would be safe(r) to bind ordinary keys like
779 ;; letters and digits, provided that we inhibit the keymap while
780 ;; executing the macro later on (but that's controversial...)
782 (defun kmacro-lambda-form (mac &optional counter format)
783 "Create lambda form for macro bound to symbol or key."
784 (if counter
785 (setq mac (list mac counter format)))
786 `(lambda (&optional arg)
787 "Keyboard macro."
788 (interactive "p")
789 (kmacro-exec-ring-item ',mac arg)))
791 (defun kmacro-extract-lambda (mac)
792 "Extract kmacro from a kmacro lambda form."
793 (and (consp mac)
794 (eq (car mac) 'lambda)
795 (setq mac (assoc 'kmacro-exec-ring-item mac))
796 (consp (cdr mac))
797 (consp (car (cdr mac)))
798 (consp (cdr (car (cdr mac))))
799 (setq mac (car (cdr (car (cdr mac)))))
800 (listp mac)
801 (= (length mac) 3)
802 (arrayp (car mac))
803 mac))
806 (defun kmacro-bind-to-key (_arg)
807 "When not defining or executing a macro, offer to bind last macro to a key.
808 The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
809 through [C-x C-k Z] are reserved for user bindings, and to bind to
810 one of these sequences, just enter the digit or letter, rather than
811 the whole sequence.
813 You can bind to any valid key sequence, but if you try to bind to
814 a key with an existing command binding, you will be asked for
815 confirmation whether to replace that binding. Note that the
816 binding is made in the `global-map' keymap, so the macro binding
817 may be shaded by a local key binding.
818 The ARG parameter is unused."
819 (interactive "p")
820 (if (or defining-kbd-macro executing-kbd-macro)
821 (if defining-kbd-macro
822 (message "Cannot save macro while defining it."))
823 (unless last-kbd-macro
824 (error "No keyboard macro defined"))
825 (let ((key-seq (read-key-sequence "Bind last macro to key: "))
826 ok cmd)
827 (when (= (length key-seq) 1)
828 (let ((ch (aref key-seq 0)))
829 (if (and (integerp ch)
830 (or (and (>= ch ?0) (<= ch ?9))
831 (and (>= ch ?A) (<= ch ?Z))))
832 (setq key-seq (concat "\C-x\C-k" key-seq)
833 ok t))))
834 (when (and (not (equal key-seq "\a"))
835 (or ok
836 (not (setq cmd (key-binding key-seq)))
837 (stringp cmd)
838 (vectorp cmd)
839 (yes-or-no-p (format "%s runs command %S. Bind anyway? "
840 (format-kbd-macro key-seq)
841 cmd))))
842 (define-key global-map key-seq
843 (kmacro-lambda-form (kmacro-ring-head)))
844 (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
847 (defun kmacro-name-last-macro (symbol)
848 "Assign a name to the last keyboard macro defined.
849 Argument SYMBOL is the name to define.
850 The symbol's function definition becomes the keyboard macro string.
851 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
852 (interactive "SName for last kbd macro: ")
853 (or last-kbd-macro
854 (error "No keyboard macro defined"))
855 (and (fboundp symbol)
856 (not (get symbol 'kmacro))
857 (not (stringp (symbol-function symbol)))
858 (not (vectorp (symbol-function symbol)))
859 (error "Function %s is already defined and not a keyboard macro"
860 symbol))
861 (if (string-equal symbol "")
862 (error "No command name given"))
863 (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
864 (put symbol 'kmacro t))
867 (defun kmacro-execute-from-register (k)
868 (kmacro-call-macro current-prefix-arg nil nil k))
870 (defun kmacro-to-register (r)
871 "Store the last keyboard macro in register R.
873 Interactively, reads the register using `register-read-with-preview'."
874 (interactive
875 (progn
876 (or last-kbd-macro (error "No keyboard macro defined"))
877 (list (register-read-with-preview "Save to register: "))))
878 (set-register r (registerv-make
879 last-kbd-macro
880 :jump-func 'kmacro-execute-from-register
881 :print-func (lambda (k)
882 (princ (format "a keyboard macro:\n %s"
883 (format-kbd-macro k))))
884 :insert-func (lambda (k)
885 (insert (format-kbd-macro k))))))
888 (defun kmacro-view-macro (&optional _arg)
889 "Display the last keyboard macro.
890 If repeated, it shows previous elements in the macro ring.
891 The ARG parameter is unused."
892 (interactive)
893 (cond
894 ((or (kmacro-ring-empty-p)
895 (not (eq last-command 'kmacro-view-macro)))
896 (setq kmacro-view-last-item nil))
897 ((null kmacro-view-last-item)
898 (setq kmacro-view-last-item kmacro-ring
899 kmacro-view-item-no 2))
900 ((consp kmacro-view-last-item)
901 (setq kmacro-view-last-item (cdr kmacro-view-last-item)
902 kmacro-view-item-no (1+ kmacro-view-item-no)))
904 (setq kmacro-view-last-item nil)))
905 (setq this-command 'kmacro-view-macro
906 last-command this-command) ;; in case we repeat
907 (kmacro-display (if kmacro-view-last-item
908 (car (car kmacro-view-last-item))
909 last-kbd-macro)
911 (if kmacro-view-last-item
912 (concat (cond ((= kmacro-view-item-no 2) "2nd")
913 ((= kmacro-view-item-no 3) "3nd")
914 (t (format "%dth" kmacro-view-item-no)))
915 " previous macro")
916 "Last macro")))
918 (defun kmacro-view-macro-repeat (&optional arg)
919 "Display the last keyboard macro.
920 If repeated, it shows previous elements in the macro ring.
921 To execute the displayed macro ring item without changing the macro ring,
922 just enter C-k.
923 This is like `kmacro-view-macro', but allows repeating macro commands
924 without repeating the prefix."
925 (interactive)
926 (let ((keys (kmacro-get-repeat-prefix)))
927 (kmacro-view-macro arg)
928 (if (and last-kbd-macro keys)
929 (kmacro-repeat-on-last-key keys))))
931 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
934 (defun kmacro-edit-macro-repeat (&optional arg)
935 "Edit last keyboard macro."
936 (interactive "P")
937 (edit-kbd-macro "\r" arg))
939 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
942 (defun kmacro-edit-macro (&optional arg)
943 "As edit last keyboard macro, but without kmacro-repeat property."
944 (interactive "P")
945 (edit-kbd-macro "\r" arg))
948 (defun kmacro-edit-lossage ()
949 "Edit most recent 300 keystrokes as a keyboard macro."
950 (interactive)
951 (kmacro-push-ring)
952 (edit-kbd-macro "\C-hl"))
955 ;;; Single-step editing of keyboard macros
957 (defvar kmacro-step-edit-active nil) ;; step-editing active
958 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
959 (defvar kmacro-step-edit-inserting) ;; inserting into macro
960 (defvar kmacro-step-edit-appending) ;; append to end of macro
961 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
962 (defvar kmacro-step-edit-key-index) ;; index of current key
963 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
964 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
965 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
967 (defvar kmacro-step-edit-map
968 (let ((map (make-sparse-keymap)))
969 ;; query-replace-map answers include: `act', `skip', `act-and-show',
970 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
971 ;; `automatic', `backup', `exit-prefix', and `help'.")
972 ;; Also: `quit', `edit-replacement'
974 (set-keymap-parent map query-replace-map)
976 (define-key map "\t" 'act-repeat)
977 (define-key map [tab] 'act-repeat)
978 (define-key map "\C-k" 'skip-rest)
979 (define-key map "c" 'automatic)
980 (define-key map "f" 'skip-keep)
981 (define-key map "q" 'quit)
982 (define-key map "d" 'skip)
983 (define-key map "\C-d" 'skip)
984 (define-key map "i" 'insert)
985 (define-key map "I" 'insert-1)
986 (define-key map "r" 'replace)
987 (define-key map "R" 'replace-1)
988 (define-key map "a" 'append)
989 (define-key map "A" 'append-end)
990 map)
991 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
992 This keymap is an extension to the `query-replace-map', allowing the
993 following additional answers: `insert', `insert-1', `replace', `replace-1',
994 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
996 (defun kmacro-step-edit-prompt (macro index)
997 ;; Show step-edit prompt
998 (let ((keys (and (not kmacro-step-edit-appending)
999 index (substring macro index executing-kbd-macro-index)))
1000 (future (and (not kmacro-step-edit-appending)
1001 (substring macro executing-kbd-macro-index)))
1002 (message-log-max nil)
1003 (curmsg (current-message)))
1005 ;; TODO: Scroll macro if max-mini-window-height is too small.
1006 (message "%s"
1007 (concat
1008 (format "Macro: %s%s%s%s%s\n"
1009 (format-kbd-macro kmacro-step-edit-new-macro 1)
1010 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
1011 (propertize (if keys (format-kbd-macro keys)
1012 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
1013 (if future " " "")
1014 (if future (format-kbd-macro future) ""))
1015 (cond
1016 ((minibufferp)
1017 (format "%s\n%s\n"
1018 (propertize "\
1019 minibuffer " 'face 'header-line)
1020 (buffer-substring (point-min) (point-max))))
1021 (curmsg
1022 (format "%s\n%s\n"
1023 (propertize "\
1024 echo area " 'face 'header-line)
1025 curmsg))
1026 (t ""))
1027 (if keys
1028 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
1029 (propertize "\
1030 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
1031 (if kmacro-step-edit-help "\
1032 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
1033 TAB: execute while same, ?: toggle help
1034 Edit: i: insert, r: replace, a: append, A: append at end,
1035 I/R: insert/replace with one sequence,
1036 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
1037 ----------------------------------------------------------------
1038 " "")
1039 (propertize "Next command:" 'face 'bold)
1040 this-command)
1041 (propertize
1042 (format "Type key sequence%s to insert and execute%s: "
1043 (if (numberp kmacro-step-edit-inserting) "" "s")
1044 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
1045 'face 'bold))))))
1047 (defun kmacro-step-edit-query ()
1048 ;; Pre-command hook function for step-edit in "command" mode
1049 (let ((resize-mini-windows t)
1050 (max-mini-window-height kmacro-step-edit-mini-window-height)
1051 act restore-index next-index)
1053 ;; Handle commands which reads additional input using read-char.
1054 (cond
1055 ((and (eq this-command 'quoted-insert)
1056 (not (eq kmacro-step-edit-action t)))
1057 ;; Find the actual end of this key sequence.
1058 ;; Must be able to backtrack in case we actually execute it.
1059 (setq restore-index executing-kbd-macro-index)
1060 (let (unread-command-events)
1061 (quoted-insert 0)
1062 (when unread-command-events
1063 (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1064 next-index executing-kbd-macro-index)))))
1066 ;; Query the user; stop macro execution temporarily.
1067 (let ((macro executing-kbd-macro)
1068 (executing-kbd-macro nil)
1069 (defining-kbd-macro nil))
1071 ;; Any action requested by previous command
1072 (cond
1073 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
1074 (cond
1075 ((eq this-command 'quoted-insert)
1076 (clear-this-command-keys) ;; recent-keys actually
1077 (let (unread-command-events)
1078 (quoted-insert (prefix-numeric-value current-prefix-arg))
1079 (setq kmacro-step-edit-new-macro
1080 (vconcat kmacro-step-edit-new-macro (recent-keys)))
1081 (when unread-command-events
1082 (setq kmacro-step-edit-new-macro
1083 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
1084 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
1085 (setq current-prefix-arg nil
1086 prefix-arg nil)
1087 (setq act 'ignore))
1089 (setq act 'act)))
1090 (setq kmacro-step-edit-action nil))
1091 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
1092 (setq act 'act))
1094 (setq kmacro-step-edit-action nil)))
1096 ;; Handle prefix arg, or query user
1097 (cond
1098 (act act) ;; set above
1100 (kmacro-step-edit-prompt macro kmacro-step-edit-key-index)
1101 (setq act (lookup-key kmacro-step-edit-map
1102 (vector (with-current-buffer (current-buffer) (read-event))))))))
1104 ;; Resume macro execution and perform the action
1105 (cond
1106 ((cond
1107 ((eq act 'act)
1109 ((eq act 'act-repeat)
1110 (setq kmacro-step-edit-action this-command)
1112 ((eq act 'quit)
1113 (setq kmacro-step-edit-replace nil)
1114 (setq kmacro-step-edit-active 'ignore)
1115 nil)
1116 ((eq act 'skip)
1117 nil)
1118 ((eq act 'skip-keep)
1119 (setq this-command 'ignore)
1121 ((eq act 'skip-rest)
1122 (setq kmacro-step-edit-active 'ignore)
1123 nil)
1124 ((memq act '(automatic exit))
1125 (setq kmacro-step-edit-active nil)
1126 (setq act t)
1128 ((member act '(insert-1 insert))
1129 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1130 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
1131 nil)
1132 ((member act '(replace-1 replace))
1133 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
1134 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1135 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1136 kmacro-step-edit-appending t))
1137 nil)
1138 ((eq act 'append)
1139 (setq kmacro-step-edit-inserting t)
1140 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1141 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1142 kmacro-step-edit-appending t))
1144 ((eq act 'append-end)
1145 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1146 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1147 kmacro-step-edit-inserting t
1148 kmacro-step-edit-appending t)
1149 (setq kmacro-step-edit-active 'append-end))
1150 (setq act t)
1152 ((eq act 'help)
1153 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1154 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
1155 nil)
1156 (t ;; Ignore unknown responses
1157 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1158 nil))
1159 (if (> executing-kbd-macro-index kmacro-step-edit-key-index)
1160 (setq kmacro-step-edit-new-macro
1161 (vconcat kmacro-step-edit-new-macro
1162 (substring executing-kbd-macro
1163 kmacro-step-edit-key-index
1164 (if (eq act t) nil
1165 executing-kbd-macro-index)))))
1166 (if restore-index
1167 (setq executing-kbd-macro-index restore-index)))
1169 (setq this-command 'ignore)))
1170 (setq kmacro-step-edit-key-index next-index)))
1172 (defun kmacro-step-edit-insert ()
1173 ;; Pre-command hook function for step-edit in "insert" mode
1174 (let ((resize-mini-windows t)
1175 (max-mini-window-height kmacro-step-edit-mini-window-height)
1176 (macro executing-kbd-macro)
1177 (executing-kbd-macro nil)
1178 (defining-kbd-macro nil)
1179 cmd keys next-index)
1180 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1181 (kmacro-step-edit-prompt macro nil)
1182 ;; Now, we have read a key sequence from the macro, but we don't want
1183 ;; to execute it yet. So push it back and read another sequence.
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 ((numberp kmacro-step-edit-inserting)
1205 (setq kmacro-step-edit-inserting nil)
1206 nil)
1207 ((equal keys "\C-j")
1208 (setq kmacro-step-edit-inserting nil)
1209 (setq kmacro-step-edit-action nil)
1210 (setq next-index kmacro-step-edit-key-index)
1212 (t nil))
1213 (setq this-command 'ignore)
1214 (setq this-command cmd)
1215 (if (memq this-command '(self-insert-command digit-argument))
1216 (setq last-command-event (aref keys (1- (length keys)))))
1217 (if keys
1218 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1219 (setq kmacro-step-edit-key-index next-index)))
1221 (defun kmacro-step-edit-pre-command ()
1222 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1223 (when kmacro-step-edit-active
1224 (cond
1225 ((eq kmacro-step-edit-active 'ignore)
1226 (setq this-command 'ignore))
1227 ((eq kmacro-step-edit-active 'append-end)
1228 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1229 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1230 kmacro-step-edit-inserting t
1231 kmacro-step-edit-appending t
1232 kmacro-step-edit-active t)))
1233 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1234 (if kmacro-step-edit-inserting
1235 (kmacro-step-edit-insert)
1236 (kmacro-step-edit-query))
1237 (setq kmacro-step-edit-num-input-keys num-input-keys)
1238 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1239 (setq kmacro-step-edit-appending nil
1240 kmacro-step-edit-active 'ignore)))))
1241 (when (eq kmacro-step-edit-active t)
1242 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1244 (defun kmacro-step-edit-minibuf-setup ()
1245 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1246 (when kmacro-step-edit-active
1247 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1249 (defun kmacro-step-edit-post-command ()
1250 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1251 (when kmacro-step-edit-active
1252 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1253 (if kmacro-step-edit-key-index
1254 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1255 (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
1258 (defun kmacro-step-edit-macro ()
1259 "Step edit and execute last keyboard macro.
1261 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1262 (interactive)
1263 (let ((kmacro-step-edit-active t)
1264 (kmacro-step-edit-new-macro "")
1265 (kmacro-step-edit-inserting nil)
1266 (kmacro-step-edit-appending nil)
1267 (kmacro-step-edit-replace t)
1268 (kmacro-step-edit-key-index 0)
1269 (kmacro-step-edit-action nil)
1270 (kmacro-step-edit-help nil)
1271 (kmacro-step-edit-num-input-keys num-input-keys)
1272 (pre-command-hook pre-command-hook)
1273 (post-command-hook post-command-hook)
1274 (minibuffer-setup-hook minibuffer-setup-hook))
1275 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1276 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1277 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1278 (call-last-kbd-macro nil nil)
1279 (when (and kmacro-step-edit-replace
1280 kmacro-step-edit-new-macro
1281 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1282 (kmacro-push-ring)
1283 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1285 (provide 'kmacro)
1287 ;;; kmacro.el ends here