Document reserved keys
[emacs.git] / lisp / kmacro.el
blob8855fa5c3144c5c844e16778fce937c27154ec35
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 (defvar kmacro-default-counter-format "%d")
240 (defvar kmacro-counter-format "%d"
241 "Current keyboard macro counter format.")
243 (defvar kmacro-counter-format-start kmacro-counter-format
244 "Macro format at start of macro execution.")
246 (defvar kmacro-counter-value-start kmacro-counter
247 "Macro counter at start of macro execution.")
249 (defvar kmacro-last-counter 0
250 "Last counter inserted by key macro.")
252 (defvar kmacro-initial-counter-value nil
253 "Initial counter value for the next keyboard macro to be defined.")
256 (defun kmacro-insert-counter (arg)
257 "Insert macro counter, then increment it by ARG.
258 Interactively, ARG defaults to 1. With \\[universal-argument], insert
259 previous `kmacro-counter', and do not modify counter."
260 (interactive "P")
261 (if kmacro-initial-counter-value
262 (setq kmacro-counter kmacro-initial-counter-value
263 kmacro-initial-counter-value nil))
264 (if (and arg (listp arg))
265 (insert (format kmacro-counter-format kmacro-last-counter))
266 (insert (format kmacro-counter-format kmacro-counter))
267 (kmacro-add-counter (prefix-numeric-value arg))))
270 (defun kmacro-set-format (format)
271 "Set macro counter FORMAT."
272 (interactive "sMacro Counter Format: ")
273 (setq kmacro-counter-format
274 (if (equal format "") "%d" format))
275 ;; redefine initial macro counter if we are not executing a macro.
276 (if (not (or defining-kbd-macro executing-kbd-macro))
277 (setq kmacro-default-counter-format kmacro-counter-format)))
280 (defun kmacro-display-counter (&optional value)
281 "Display current counter value."
282 (unless value (setq value kmacro-counter))
283 (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value))
286 (defun kmacro-set-counter (arg)
287 "Set `kmacro-counter' to ARG or prompt if missing.
288 With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
289 (interactive "NMacro counter value: ")
290 (if (not (or defining-kbd-macro executing-kbd-macro))
291 (kmacro-display-counter (setq kmacro-initial-counter-value arg))
292 (setq kmacro-last-counter kmacro-counter
293 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
294 kmacro-counter-value-start
295 arg))
296 (unless executing-kbd-macro
297 (kmacro-display-counter))))
300 (defun kmacro-add-counter (arg)
301 "Add numeric prefix arg (prompt if missing) to macro counter.
302 With \\[universal-argument], restore previous counter value."
303 (interactive "NAdd to macro counter: ")
304 (if kmacro-initial-counter-value
305 (setq kmacro-counter kmacro-initial-counter-value
306 kmacro-initial-counter-value nil))
307 (let ((last kmacro-last-counter))
308 (setq kmacro-last-counter kmacro-counter
309 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
310 last
311 kmacro-counter (+ kmacro-counter arg))))
312 (unless executing-kbd-macro
313 (kmacro-display-counter)))
316 (defun kmacro-loop-setup-function ()
317 "Function called prior to each iteration of macro."
318 ;; Restore macro counter format to initial format, so it is ok to change
319 ;; counter format in the macro without restoring it.
320 (setq kmacro-counter-format kmacro-counter-format-start)
321 ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
322 (setq kmacro-counter-value-start kmacro-counter)
323 ;; Return non-nil to continue execution.
327 ;;; Keyboard macro ring
329 (defvar kmacro-ring nil
330 "The keyboard macro ring.
331 Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
332 the macro ring (when defining or executing) is not stored in the ring;
333 instead it is available in the variables `last-kbd-macro', `kmacro-counter',
334 and `kmacro-counter-format'.")
336 ;; Remember what we are currently looking at with kmacro-view-macro.
338 (defvar kmacro-view-last-item nil)
339 (defvar kmacro-view-item-no 0)
342 (defun kmacro-ring-head ()
343 "Return pseudo head element in macro ring."
344 (and last-kbd-macro
345 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
348 (defun kmacro-push-ring (&optional elt)
349 "Push ELT or current macro onto `kmacro-ring'."
350 (when (setq elt (or elt (kmacro-ring-head)))
351 (let ((history-delete-duplicates nil))
352 (add-to-history 'kmacro-ring elt kmacro-ring-max))))
355 (defun kmacro-split-ring-element (elt)
356 (setq last-kbd-macro (car elt)
357 kmacro-counter (nth 1 elt)
358 kmacro-counter-format-start (nth 2 elt)))
361 (defun kmacro-pop-ring1 (&optional raw)
362 "Pop head element off macro ring (no check).
363 Non-nil arg RAW means just return raw first element."
364 (prog1 (car kmacro-ring)
365 (unless raw
366 (kmacro-split-ring-element (car kmacro-ring)))
367 (setq kmacro-ring (cdr kmacro-ring))))
370 (defun kmacro-pop-ring (&optional raw)
371 "Pop head element off macro ring.
372 Non-nil arg RAW means just return raw first element."
373 (unless (kmacro-ring-empty-p)
374 (kmacro-pop-ring1 raw)))
377 (defun kmacro-ring-empty-p (&optional none)
378 "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
379 Check only `last-kbd-macro' if optional arg NONE is non-nil."
380 (while (and (null last-kbd-macro) kmacro-ring)
381 (kmacro-pop-ring1))
382 (cond
383 ((null last-kbd-macro)
384 (message "No keyboard macro defined.")
386 ((and (null none) (null kmacro-ring))
387 (message "Only one keyboard macro defined.")
389 (t nil)))
392 (defun kmacro-display (macro &optional trunc descr empty)
393 "Display a keyboard MACRO.
394 Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
395 Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
396 Optional arg EMPTY is message to print if no macros are defined."
397 (if macro
398 (let* ((x 60)
399 (m (format-kbd-macro macro))
400 (l (length m))
401 (z (and trunc (> l x))))
402 (message "%s%s: %s%s" (or descr "Macro")
403 (if (= kmacro-counter 0) ""
404 (format " [%s]"
405 (format kmacro-counter-format-start kmacro-counter)))
406 (if z (substring m 0 (1- x)) m) (if z "..." "")))
407 (message "%s" (or empty "No keyboard macros defined"))))
410 (defun kmacro-repeat-on-last-key (keys)
411 "Process kmacro commands keys immediately after cycling the ring."
412 (setq keys (vconcat keys))
413 (let ((n (1- (length keys)))
414 cmd done repeat)
415 (while (and last-kbd-macro
416 (not done)
417 (aset keys n (read-event))
418 (setq cmd (key-binding keys t))
419 (setq repeat (get cmd 'kmacro-repeat)))
420 (clear-this-command-keys t)
421 (cond
422 ((eq repeat 'ring)
423 (if kmacro-ring
424 (let ((kmacro-repeat-no-prefix nil))
425 (funcall cmd nil))
426 (kmacro-display last-kbd-macro t)))
427 ((eq repeat 'head)
428 (let ((kmacro-repeat-no-prefix nil))
429 (funcall cmd nil)))
430 ((eq repeat 'stop)
431 (funcall cmd nil)
432 (setq done t)))
433 (setq last-input-event nil)))
434 (when last-input-event
435 (clear-this-command-keys t)
436 (push last-input-event unread-command-events)))
439 (defun kmacro-get-repeat-prefix ()
440 (let (keys)
441 (and kmacro-repeat-no-prefix
442 (setq keys (this-single-command-keys))
443 (> (length keys) 1)
444 keys)))
447 ;;;###autoload
448 (defun kmacro-exec-ring-item (item arg)
449 "Execute item ITEM from the macro ring.
450 ARG is the number of times to execute the item."
451 ;; Use counter and format specific to the macro on the ring!
452 (let ((kmacro-counter (nth 1 item))
453 (kmacro-counter-format-start (nth 2 item)))
454 (execute-kbd-macro (car item) arg #'kmacro-loop-setup-function)
455 (setcar (cdr item) kmacro-counter)))
458 (defun kmacro-call-ring-2nd (arg)
459 "Execute second keyboard macro in macro ring."
460 (interactive "P")
461 (unless (kmacro-ring-empty-p)
462 (kmacro-exec-ring-item (car kmacro-ring) arg)))
465 (defun kmacro-call-ring-2nd-repeat (arg)
466 "Execute second keyboard macro in macro ring.
467 This is like `kmacro-call-ring-2nd', but allows repeating macro commands
468 without repeating the prefix."
469 (interactive "P")
470 (let ((keys (kmacro-get-repeat-prefix)))
471 (kmacro-call-ring-2nd arg)
472 (if (and kmacro-ring keys)
473 (kmacro-repeat-on-last-key keys))))
475 (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
478 (defun kmacro-view-ring-2nd ()
479 "Display the current head of the keyboard macro ring."
480 (interactive)
481 (unless (kmacro-ring-empty-p)
482 (kmacro-display (car (car kmacro-ring)) nil "2nd macro")))
485 (defun kmacro-cycle-ring-next (&optional _arg)
486 "Move to next keyboard macro in keyboard macro ring.
487 Displays the selected macro in the echo area.
488 The ARG parameter is unused."
489 (interactive)
490 (unless (kmacro-ring-empty-p)
491 (kmacro-push-ring)
492 (let* ((keys (kmacro-get-repeat-prefix))
493 (len (length kmacro-ring))
494 (tail (nthcdr (- len 2) kmacro-ring))
495 (elt (car (cdr tail))))
496 (setcdr tail nil)
497 (kmacro-split-ring-element elt)
498 (kmacro-display last-kbd-macro t)
499 (if keys
500 (kmacro-repeat-on-last-key keys)))))
502 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
505 (defun kmacro-cycle-ring-previous (&optional _arg)
506 "Move to previous keyboard macro in keyboard macro ring.
507 Displays the selected macro in the echo area.
508 The ARG parameter is unused."
509 (interactive)
510 (unless (kmacro-ring-empty-p)
511 (let ((keys (kmacro-get-repeat-prefix))
512 (cur (kmacro-ring-head)))
513 (kmacro-pop-ring1)
514 (if kmacro-ring
515 (nconc kmacro-ring (list cur))
516 (setq kmacro-ring (list cur)))
517 (kmacro-display last-kbd-macro t)
518 (if keys
519 (kmacro-repeat-on-last-key keys)))))
521 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
524 (defun kmacro-swap-ring ()
525 "Swap first two elements on keyboard macro ring."
526 (interactive)
527 (unless (kmacro-ring-empty-p)
528 (let ((cur (kmacro-ring-head)))
529 (kmacro-pop-ring1)
530 (kmacro-push-ring cur))
531 (kmacro-display last-kbd-macro t)))
534 (defun kmacro-delete-ring-head (&optional _arg)
535 "Delete current macro from keyboard macro ring.
536 The ARG parameter is unused."
537 (interactive)
538 (unless (kmacro-ring-empty-p t)
539 (if (null kmacro-ring)
540 (setq last-kbd-macro nil)
541 (kmacro-pop-ring))
542 (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
544 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
546 ;;; Traditional bindings:
549 ;;;###autoload
550 (defun kmacro-start-macro (arg)
551 "Record subsequent keyboard input, defining a keyboard macro.
552 The commands are recorded even as they are executed.
553 Use \\[kmacro-end-macro] to finish recording and make the macro available.
554 Use \\[kmacro-end-and-call-macro] to execute the macro.
556 Non-nil arg (prefix arg) means append to last macro defined.
558 With \\[universal-argument] prefix, append to last keyboard macro
559 defined. Depending on `kmacro-execute-before-append', this may begin
560 by re-executing the last macro as if you typed it again.
562 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
563 defining the macro.
565 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
566 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
567 The format of the counter can be modified via \\[kmacro-set-format].
569 Use \\[kmacro-name-last-macro] to give it a name that will remain valid even
570 after another macro is defined.
571 Use \\[kmacro-bind-to-key] to bind it to a key sequence."
572 (interactive "P")
573 (if (or defining-kbd-macro executing-kbd-macro)
574 (message "Already defining keyboard macro.")
575 (let ((append (and arg (listp arg))))
576 (unless append
577 (if last-kbd-macro
578 (kmacro-push-ring
579 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
580 (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
581 kmacro-initial-counter-value
583 kmacro-initial-counter-value nil
584 kmacro-counter-value-start kmacro-counter
585 kmacro-last-counter kmacro-counter
586 kmacro-counter-format kmacro-default-counter-format
587 kmacro-counter-format-start kmacro-default-counter-format))
589 (start-kbd-macro append
590 (and append
591 (if kmacro-execute-before-append
592 (> (car arg) 4)
593 (= (car arg) 4))))
594 (if (and defining-kbd-macro append)
595 (setq defining-kbd-macro 'append)))))
598 ;;;###autoload
599 (defun kmacro-end-macro (arg)
600 "Finish defining a keyboard macro.
601 The definition was started by \\[kmacro-start-macro].
602 The macro is now available for use via \\[kmacro-call-macro],
603 or it can be given a name with \\[kmacro-name-last-macro] and then invoked
604 under that name.
606 With numeric arg, repeat macro now that many times,
607 counting the definition just completed as the first repetition.
608 An argument of zero means repeat until error."
609 (interactive "P")
610 ;; Isearch may push the kmacro-end-macro key sequence onto the macro.
611 ;; Just ignore it when executing the macro.
612 (unless executing-kbd-macro
613 (end-kbd-macro arg #'kmacro-loop-setup-function)
614 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
615 (setq last-kbd-macro nil)
616 (message "Ignore empty macro")
617 ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
618 (while (and (null last-kbd-macro) kmacro-ring)
619 (kmacro-pop-ring1)))))
622 ;;;###autoload
623 (defun kmacro-call-macro (arg &optional no-repeat end-macro macro)
624 "Call the keyboard MACRO that you defined with \\[kmacro-start-macro].
625 A prefix argument serves as a repeat count. Zero means repeat until error.
626 MACRO defaults to `last-kbd-macro'.
628 When you call the macro, you can call the macro again by repeating
629 just the last key in the key sequence that you used to call this
630 command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
631 for details on how to adjust or disable this behavior.
633 To give a macro a name so you can call it even after defining others,
634 use \\[kmacro-name-last-macro]."
635 (interactive "p")
636 (let ((repeat-key (and (or (and (null no-repeat)
637 (> (length (this-single-command-keys)) 1))
638 ;; Used when we're in the process of repeating.
639 (eq no-repeat 'repeating))
640 last-input-event)))
641 (if end-macro
642 (kmacro-end-macro arg) ; modifies last-kbd-macro
643 (let ((last-kbd-macro (or macro last-kbd-macro)))
644 (call-last-kbd-macro arg #'kmacro-loop-setup-function)))
645 (when (consp arg)
646 (setq arg (car arg)))
647 (when (and (or (null arg) (> arg 0))
648 (setq repeat-key
649 (if (eq kmacro-call-repeat-key t)
650 repeat-key
651 kmacro-call-repeat-key)))
652 ;; Issue a hint to the user, if the echo area isn't in use.
653 (unless (current-message)
654 (message "(Type %s to repeat macro%s)"
655 (format-kbd-macro (vector repeat-key) nil)
656 (if (and kmacro-call-repeat-with-arg
657 arg (> arg 1))
658 (format " %d times" arg) "")))
659 ;; Can't use the `keep-pred' arg because this overlay keymap
660 ;; needs to be removed during the next run of the kmacro
661 ;; (i.e. we must add and remove this map at each repetition).
662 (set-transient-map
663 (let ((map (make-sparse-keymap)))
664 (define-key map (vector repeat-key)
665 `(lambda () (interactive)
666 (kmacro-call-macro ,(and kmacro-call-repeat-with-arg arg)
667 'repeating nil ,(if end-macro
668 last-kbd-macro
669 (or macro last-kbd-macro)))))
670 map)))))
673 ;;; Combined function key bindings:
675 ;;;###autoload
676 (defun kmacro-start-macro-or-insert-counter (arg)
677 "Record subsequent keyboard input, defining a keyboard macro.
678 The commands are recorded even as they are executed.
680 Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
681 macro.
683 With \\[universal-argument], appends to current keyboard macro (keeping
684 the current value of `kmacro-counter').
686 When defining/executing macro, inserts macro counter and increments
687 the counter with ARG or 1 if missing. With \\[universal-argument],
688 inserts previous `kmacro-counter' (but do not modify counter).
690 The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
691 The format of the counter can be modified via \\[kmacro-set-format]."
692 (interactive "P")
693 (if (or defining-kbd-macro executing-kbd-macro)
694 (kmacro-insert-counter arg)
695 (kmacro-start-macro arg)))
698 ;;;###autoload
699 (defun kmacro-end-or-call-macro (arg &optional no-repeat)
700 "End kbd macro if currently being defined; else call last kbd macro.
701 With numeric prefix ARG, repeat macro that many times.
702 With \\[universal-argument], call second macro in macro ring."
703 (interactive "P")
704 (cond
705 (defining-kbd-macro
706 (if kmacro-call-repeat-key
707 (kmacro-call-macro arg no-repeat t)
708 (kmacro-end-macro arg)))
709 ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
710 kmacro-view-last-item)
711 (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
712 ((and arg (listp arg))
713 (kmacro-call-ring-2nd 1))
715 (kmacro-call-macro arg no-repeat))))
718 (defun kmacro-end-or-call-macro-repeat (arg)
719 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
720 (interactive "P")
721 (let ((keys (kmacro-get-repeat-prefix)))
722 (kmacro-end-or-call-macro arg t)
723 (if keys
724 (kmacro-repeat-on-last-key keys))))
726 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
729 ;;;###autoload
730 (defun kmacro-end-and-call-macro (arg &optional no-repeat)
731 "Call last keyboard macro, ending it first if currently being defined.
732 With numeric prefix ARG, repeat macro that many times.
733 Zero argument means repeat until there is an error.
735 To give a macro a name, so you can call it even after defining other
736 macros, use \\[kmacro-name-last-macro]."
737 (interactive "P")
738 (if defining-kbd-macro
739 (kmacro-end-macro nil))
740 (kmacro-call-macro arg no-repeat))
743 ;;;###autoload
744 (defun kmacro-end-call-mouse (event)
745 "Move point to the position clicked with the mouse and call last kbd macro.
746 If kbd macro currently being defined end it before activating it."
747 (interactive "e")
748 (when defining-kbd-macro
749 (end-kbd-macro)
750 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
751 (setq last-kbd-macro nil)
752 (message "Ignore empty macro")
753 ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
754 (while (and (null last-kbd-macro) kmacro-ring)
755 (kmacro-pop-ring1))))
756 (mouse-set-point event)
757 (kmacro-call-macro nil t))
760 ;;; Misc. commands
762 ;; An idea for macro bindings:
763 ;; Create a separate keymap installed as a minor-mode keymap (e.g. in
764 ;; the emulation-mode-map-alists) in which macro bindings are made
765 ;; independent of any other bindings. When first binding is made,
766 ;; the keymap is created, installed, and enabled. Key seq. C-x C-k +
767 ;; can then be used to toggle the use of this keymap on and off.
768 ;; This means that it would be safe(r) to bind ordinary keys like
769 ;; letters and digits, provided that we inhibit the keymap while
770 ;; executing the macro later on (but that's controversial...)
772 (defun kmacro-lambda-form (mac &optional counter format)
773 "Create lambda form for macro bound to symbol or key."
774 (if counter
775 (setq mac (list mac counter format)))
776 `(lambda (&optional arg)
777 "Keyboard macro."
778 (interactive "p")
779 (kmacro-exec-ring-item ',mac arg)))
781 (defun kmacro-extract-lambda (mac)
782 "Extract kmacro from a kmacro lambda form."
783 (and (consp mac)
784 (eq (car mac) 'lambda)
785 (setq mac (assoc 'kmacro-exec-ring-item mac))
786 (consp (cdr mac))
787 (consp (car (cdr mac)))
788 (consp (cdr (car (cdr mac))))
789 (setq mac (car (cdr (car (cdr mac)))))
790 (listp mac)
791 (= (length mac) 3)
792 (arrayp (car mac))
793 mac))
796 (defun kmacro-bind-to-key (_arg)
797 "When not defining or executing a macro, offer to bind last macro to a key.
798 The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
799 through [C-x C-k Z] are reserved for user bindings, and to bind to
800 one of these sequences, just enter the digit or letter, rather than
801 the whole sequence.
803 You can bind to any valid key sequence, but if you try to bind to
804 a key with an existing command binding, you will be asked for
805 confirmation whether to replace that binding. Note that the
806 binding is made in the `global-map' keymap, so the macro binding
807 may be shaded by a local key binding.
808 The ARG parameter is unused."
809 (interactive "p")
810 (if (or defining-kbd-macro executing-kbd-macro)
811 (if defining-kbd-macro
812 (message "Cannot save macro while defining it."))
813 (unless last-kbd-macro
814 (error "No keyboard macro defined"))
815 (let ((key-seq (read-key-sequence "Bind last macro to key: "))
816 ok cmd)
817 (when (= (length key-seq) 1)
818 (let ((ch (aref key-seq 0)))
819 (if (and (integerp ch)
820 (or (and (>= ch ?0) (<= ch ?9))
821 (and (>= ch ?A) (<= ch ?Z))))
822 (setq key-seq (concat "\C-x\C-k" key-seq)
823 ok t))))
824 (when (and (not (equal key-seq "\a"))
825 (or ok
826 (not (setq cmd (key-binding key-seq)))
827 (stringp cmd)
828 (vectorp cmd)
829 (yes-or-no-p (format "%s runs command %S. Bind anyway? "
830 (format-kbd-macro key-seq)
831 cmd))))
832 (define-key global-map key-seq
833 (kmacro-lambda-form (kmacro-ring-head)))
834 (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
837 (defun kmacro-name-last-macro (symbol)
838 "Assign a name to the last keyboard macro defined.
839 Argument SYMBOL is the name to define.
840 The symbol's function definition becomes the keyboard macro string.
841 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
842 (interactive "SName for last kbd macro: ")
843 (or last-kbd-macro
844 (error "No keyboard macro defined"))
845 (and (fboundp symbol)
846 (not (get symbol 'kmacro))
847 (not (stringp (symbol-function symbol)))
848 (not (vectorp (symbol-function symbol)))
849 (error "Function %s is already defined and not a keyboard macro"
850 symbol))
851 (if (string-equal symbol "")
852 (error "No command name given"))
853 (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
854 (put symbol 'kmacro t))
857 (defun kmacro-execute-from-register (k)
858 (kmacro-call-macro current-prefix-arg nil nil k))
860 (defun kmacro-to-register (r)
861 "Store the last keyboard macro in register R.
863 Interactively, reads the register using `register-read-with-preview'."
864 (interactive
865 (progn
866 (or last-kbd-macro (error "No keyboard macro defined"))
867 (list (register-read-with-preview "Save to register: "))))
868 (set-register r (registerv-make
869 last-kbd-macro
870 :jump-func 'kmacro-execute-from-register
871 :print-func (lambda (k)
872 (princ (format "a keyboard macro:\n %s"
873 (format-kbd-macro k))))
874 :insert-func (lambda (k)
875 (insert (format-kbd-macro k))))))
878 (defun kmacro-view-macro (&optional _arg)
879 "Display the last keyboard macro.
880 If repeated, it shows previous elements in the macro ring.
881 The ARG parameter is unused."
882 (interactive)
883 (cond
884 ((or (kmacro-ring-empty-p)
885 (not (eq last-command 'kmacro-view-macro)))
886 (setq kmacro-view-last-item nil))
887 ((null kmacro-view-last-item)
888 (setq kmacro-view-last-item kmacro-ring
889 kmacro-view-item-no 2))
890 ((consp kmacro-view-last-item)
891 (setq kmacro-view-last-item (cdr kmacro-view-last-item)
892 kmacro-view-item-no (1+ kmacro-view-item-no)))
894 (setq kmacro-view-last-item nil)))
895 (setq this-command 'kmacro-view-macro
896 last-command this-command) ;; in case we repeat
897 (kmacro-display (if kmacro-view-last-item
898 (car (car kmacro-view-last-item))
899 last-kbd-macro)
901 (if kmacro-view-last-item
902 (concat (cond ((= kmacro-view-item-no 2) "2nd")
903 ((= kmacro-view-item-no 3) "3nd")
904 (t (format "%dth" kmacro-view-item-no)))
905 " previous macro")
906 "Last macro")))
908 (defun kmacro-view-macro-repeat (&optional arg)
909 "Display the last keyboard macro.
910 If repeated, it shows previous elements in the macro ring.
911 To execute the displayed macro ring item without changing the macro ring,
912 just enter C-k.
913 This is like `kmacro-view-macro', but allows repeating macro commands
914 without repeating the prefix."
915 (interactive)
916 (let ((keys (kmacro-get-repeat-prefix)))
917 (kmacro-view-macro arg)
918 (if (and last-kbd-macro keys)
919 (kmacro-repeat-on-last-key keys))))
921 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
924 (defun kmacro-edit-macro-repeat (&optional arg)
925 "Edit last keyboard macro."
926 (interactive "P")
927 (edit-kbd-macro "\r" arg))
929 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
932 (defun kmacro-edit-macro (&optional arg)
933 "As edit last keyboard macro, but without kmacro-repeat property."
934 (interactive "P")
935 (edit-kbd-macro "\r" arg))
938 (defun kmacro-edit-lossage ()
939 "Edit most recent 300 keystrokes as a keyboard macro."
940 (interactive)
941 (kmacro-push-ring)
942 (edit-kbd-macro "\C-hl"))
945 ;;; Single-step editing of keyboard macros
947 (defvar kmacro-step-edit-active nil) ;; step-editing active
948 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
949 (defvar kmacro-step-edit-inserting) ;; inserting into macro
950 (defvar kmacro-step-edit-appending) ;; append to end of macro
951 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
952 (defvar kmacro-step-edit-key-index) ;; index of current key
953 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
954 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
955 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
957 (defvar kmacro-step-edit-map
958 (let ((map (make-sparse-keymap)))
959 ;; query-replace-map answers include: `act', `skip', `act-and-show',
960 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
961 ;; `automatic', `backup', `exit-prefix', and `help'.")
962 ;; Also: `quit', `edit-replacement'
964 (set-keymap-parent map query-replace-map)
966 (define-key map "\t" 'act-repeat)
967 (define-key map [tab] 'act-repeat)
968 (define-key map "\C-k" 'skip-rest)
969 (define-key map "c" 'automatic)
970 (define-key map "f" 'skip-keep)
971 (define-key map "q" 'quit)
972 (define-key map "d" 'skip)
973 (define-key map "\C-d" 'skip)
974 (define-key map "i" 'insert)
975 (define-key map "I" 'insert-1)
976 (define-key map "r" 'replace)
977 (define-key map "R" 'replace-1)
978 (define-key map "a" 'append)
979 (define-key map "A" 'append-end)
980 map)
981 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
982 This keymap is an extension to the `query-replace-map', allowing the
983 following additional answers: `insert', `insert-1', `replace', `replace-1',
984 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
986 (defun kmacro-step-edit-prompt (macro index)
987 ;; Show step-edit prompt
988 (let ((keys (and (not kmacro-step-edit-appending)
989 index (substring macro index executing-kbd-macro-index)))
990 (future (and (not kmacro-step-edit-appending)
991 (substring macro executing-kbd-macro-index)))
992 (message-log-max nil)
993 (curmsg (current-message)))
995 ;; TODO: Scroll macro if max-mini-window-height is too small.
996 (message "%s"
997 (concat
998 (format "Macro: %s%s%s%s%s\n"
999 (format-kbd-macro kmacro-step-edit-new-macro 1)
1000 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
1001 (propertize (if keys (format-kbd-macro keys)
1002 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
1003 (if future " " "")
1004 (if future (format-kbd-macro future) ""))
1005 (cond
1006 ((minibufferp)
1007 (format "%s\n%s\n"
1008 (propertize "\
1009 minibuffer " 'face 'header-line)
1010 (buffer-substring (point-min) (point-max))))
1011 (curmsg
1012 (format "%s\n%s\n"
1013 (propertize "\
1014 echo area " 'face 'header-line)
1015 curmsg))
1016 (t ""))
1017 (if keys
1018 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
1019 (propertize "\
1020 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
1021 (if kmacro-step-edit-help "\
1022 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
1023 TAB: execute while same, ?: toggle help
1024 Edit: i: insert, r: replace, a: append, A: append at end,
1025 I/R: insert/replace with one sequence,
1026 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
1027 ----------------------------------------------------------------
1028 " "")
1029 (propertize "Next command:" 'face 'bold)
1030 this-command)
1031 (propertize
1032 (format "Type key sequence%s to insert and execute%s: "
1033 (if (numberp kmacro-step-edit-inserting) "" "s")
1034 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
1035 'face 'bold))))))
1037 (defun kmacro-step-edit-query ()
1038 ;; Pre-command hook function for step-edit in "command" mode
1039 (let ((resize-mini-windows t)
1040 (max-mini-window-height kmacro-step-edit-mini-window-height)
1041 act restore-index next-index)
1043 ;; Handle commands which reads additional input using read-char.
1044 (cond
1045 ((and (eq this-command 'quoted-insert)
1046 (not (eq kmacro-step-edit-action t)))
1047 ;; Find the actual end of this key sequence.
1048 ;; Must be able to backtrack in case we actually execute it.
1049 (setq restore-index executing-kbd-macro-index)
1050 (let (unread-command-events)
1051 (quoted-insert 0)
1052 (when unread-command-events
1053 (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1054 next-index executing-kbd-macro-index)))))
1056 ;; Query the user; stop macro execution temporarily.
1057 (let ((macro executing-kbd-macro)
1058 (executing-kbd-macro nil)
1059 (defining-kbd-macro nil))
1061 ;; Any action requested by previous command
1062 (cond
1063 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
1064 (cond
1065 ((eq this-command 'quoted-insert)
1066 (clear-this-command-keys) ;; recent-keys actually
1067 (let (unread-command-events)
1068 (quoted-insert (prefix-numeric-value current-prefix-arg))
1069 (setq kmacro-step-edit-new-macro
1070 (vconcat kmacro-step-edit-new-macro (recent-keys)))
1071 (when unread-command-events
1072 (setq kmacro-step-edit-new-macro
1073 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
1074 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
1075 (setq current-prefix-arg nil
1076 prefix-arg nil)
1077 (setq act 'ignore))
1079 (setq act 'act)))
1080 (setq kmacro-step-edit-action nil))
1081 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
1082 (setq act 'act))
1084 (setq kmacro-step-edit-action nil)))
1086 ;; Handle prefix arg, or query user
1087 (cond
1088 (act act) ;; set above
1090 (kmacro-step-edit-prompt macro kmacro-step-edit-key-index)
1091 (setq act (lookup-key kmacro-step-edit-map
1092 (vector (with-current-buffer (current-buffer) (read-event))))))))
1094 ;; Resume macro execution and perform the action
1095 (cond
1096 ((cond
1097 ((eq act 'act)
1099 ((eq act 'act-repeat)
1100 (setq kmacro-step-edit-action this-command)
1102 ((eq act 'quit)
1103 (setq kmacro-step-edit-replace nil)
1104 (setq kmacro-step-edit-active 'ignore)
1105 nil)
1106 ((eq act 'skip)
1107 nil)
1108 ((eq act 'skip-keep)
1109 (setq this-command 'ignore)
1111 ((eq act 'skip-rest)
1112 (setq kmacro-step-edit-active 'ignore)
1113 nil)
1114 ((memq act '(automatic exit))
1115 (setq kmacro-step-edit-active nil)
1116 (setq act t)
1118 ((member act '(insert-1 insert))
1119 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1120 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
1121 nil)
1122 ((member act '(replace-1 replace))
1123 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
1124 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1125 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1126 kmacro-step-edit-appending t))
1127 nil)
1128 ((eq act 'append)
1129 (setq kmacro-step-edit-inserting t)
1130 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1131 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1132 kmacro-step-edit-appending t))
1134 ((eq act 'append-end)
1135 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1136 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1137 kmacro-step-edit-inserting t
1138 kmacro-step-edit-appending t)
1139 (setq kmacro-step-edit-active 'append-end))
1140 (setq act t)
1142 ((eq act 'help)
1143 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1144 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
1145 nil)
1146 (t ;; Ignore unknown responses
1147 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1148 nil))
1149 (if (> executing-kbd-macro-index kmacro-step-edit-key-index)
1150 (setq kmacro-step-edit-new-macro
1151 (vconcat kmacro-step-edit-new-macro
1152 (substring executing-kbd-macro
1153 kmacro-step-edit-key-index
1154 (if (eq act t) nil
1155 executing-kbd-macro-index)))))
1156 (if restore-index
1157 (setq executing-kbd-macro-index restore-index)))
1159 (setq this-command 'ignore)))
1160 (setq kmacro-step-edit-key-index next-index)))
1162 (defun kmacro-step-edit-insert ()
1163 ;; Pre-command hook function for step-edit in "insert" mode
1164 (let ((resize-mini-windows t)
1165 (max-mini-window-height kmacro-step-edit-mini-window-height)
1166 (macro executing-kbd-macro)
1167 (executing-kbd-macro nil)
1168 (defining-kbd-macro nil)
1169 cmd keys next-index)
1170 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1171 (kmacro-step-edit-prompt macro nil)
1172 ;; Now, we have read a key sequence from the macro, but we don't want
1173 ;; to execute it yet. So push it back and read another sequence.
1174 (setq keys (read-key-sequence nil nil nil nil t))
1175 (setq cmd (key-binding keys t nil))
1176 (if (cond
1177 ((null cmd)
1179 ((eq cmd 'quoted-insert)
1180 (clear-this-command-keys) ;; recent-keys actually
1181 (quoted-insert (prefix-numeric-value current-prefix-arg))
1182 (setq current-prefix-arg nil
1183 prefix-arg nil)
1184 (setq keys (vconcat keys (recent-keys)))
1185 (when (numberp kmacro-step-edit-inserting)
1186 (setq kmacro-step-edit-inserting nil)
1187 (when unread-command-events
1188 (setq keys (substring keys 0 (- (length unread-command-events)))
1189 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1190 next-index executing-kbd-macro-index
1191 unread-command-events nil)))
1192 (setq cmd 'ignore)
1193 nil)
1194 ((numberp kmacro-step-edit-inserting)
1195 (setq kmacro-step-edit-inserting nil)
1196 nil)
1197 ((equal keys "\C-j")
1198 (setq kmacro-step-edit-inserting nil)
1199 (setq kmacro-step-edit-action nil)
1200 (setq next-index kmacro-step-edit-key-index)
1202 (t nil))
1203 (setq this-command 'ignore)
1204 (setq this-command cmd)
1205 (if (memq this-command '(self-insert-command digit-argument))
1206 (setq last-command-event (aref keys (1- (length keys)))))
1207 (if keys
1208 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1209 (setq kmacro-step-edit-key-index next-index)))
1211 (defun kmacro-step-edit-pre-command ()
1212 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1213 (when kmacro-step-edit-active
1214 (cond
1215 ((eq kmacro-step-edit-active 'ignore)
1216 (setq this-command 'ignore))
1217 ((eq kmacro-step-edit-active 'append-end)
1218 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1219 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1220 kmacro-step-edit-inserting t
1221 kmacro-step-edit-appending t
1222 kmacro-step-edit-active t)))
1223 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1224 (if kmacro-step-edit-inserting
1225 (kmacro-step-edit-insert)
1226 (kmacro-step-edit-query))
1227 (setq kmacro-step-edit-num-input-keys num-input-keys)
1228 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1229 (setq kmacro-step-edit-appending nil
1230 kmacro-step-edit-active 'ignore)))))
1231 (when (eq kmacro-step-edit-active t)
1232 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1234 (defun kmacro-step-edit-minibuf-setup ()
1235 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1236 (when kmacro-step-edit-active
1237 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1239 (defun kmacro-step-edit-post-command ()
1240 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1241 (when kmacro-step-edit-active
1242 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1243 (if kmacro-step-edit-key-index
1244 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1245 (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
1248 (defun kmacro-step-edit-macro ()
1249 "Step edit and execute last keyboard macro.
1251 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1252 (interactive)
1253 (let ((kmacro-step-edit-active t)
1254 (kmacro-step-edit-new-macro "")
1255 (kmacro-step-edit-inserting nil)
1256 (kmacro-step-edit-appending nil)
1257 (kmacro-step-edit-replace t)
1258 (kmacro-step-edit-key-index 0)
1259 (kmacro-step-edit-action nil)
1260 (kmacro-step-edit-help nil)
1261 (kmacro-step-edit-num-input-keys num-input-keys)
1262 (pre-command-hook pre-command-hook)
1263 (post-command-hook post-command-hook)
1264 (minibuffer-setup-hook minibuffer-setup-hook))
1265 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1266 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1267 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1268 (call-last-kbd-macro nil nil)
1269 (when (and kmacro-step-edit-replace
1270 kmacro-step-edit-new-macro
1271 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1272 (kmacro-push-ring)
1273 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1275 (provide 'kmacro)
1277 ;;; kmacro.el ends here