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