Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / kmacro.el
blob76d2125c9d85af27b02928506a997364ad87e71f
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)
129 (defcustom kmacro-ring-max 8
130 "Maximum number of keyboard macros to save in macro ring."
131 :type 'integer)
134 (defcustom kmacro-execute-before-append t
135 "Controls whether appending to a macro starts by executing the macro.
136 If non-nil, using a single \\[universal-argument] prefix executes the macro
137 before appending, while more than one \\[universal-argument] prefix does not
138 execute the macro.
139 Otherwise, a single \\[universal-argument] prefix does not execute the
140 macro, while more than one \\[universal-argument] prefix causes the
141 macro to be executed before appending to it."
142 :type 'boolean)
145 (defcustom kmacro-repeat-no-prefix t
146 "Allow repeating certain macro commands without entering the C-x C-k prefix."
147 :type 'boolean)
149 (defcustom kmacro-call-repeat-key t
150 "Allow repeating macro call using last key or a specific key."
151 :type '(choice (const :tag "Disabled" nil)
152 (const :tag "Last key" t)
153 (character :tag "Character" :value ?e)
154 (symbol :tag "Key symbol" :value RET)))
156 (defcustom kmacro-call-repeat-with-arg nil
157 "Repeat macro call with original arg when non-nil; repeat once if nil."
158 :type 'boolean)
160 (defcustom kmacro-step-edit-mini-window-height 0.75
161 "Override `max-mini-window-height' when step edit keyboard macro."
162 :type 'number)
164 ;; Keymap
166 (defvar kmacro-keymap
167 (let ((map (make-sparse-keymap)))
168 ;; Start, end, execute macros
169 (define-key map "s" 'kmacro-start-macro)
170 (define-key map "\C-s" 'kmacro-start-macro)
171 (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
172 (define-key map "r" 'apply-macro-to-region-lines)
173 (define-key map "q" 'kbd-macro-query) ;; Like C-x q
175 ;; macro ring
176 (define-key map "\C-n" 'kmacro-cycle-ring-next)
177 (define-key map "\C-p" 'kmacro-cycle-ring-previous)
178 (define-key map "\C-v" 'kmacro-view-macro-repeat)
179 (define-key map "\C-d" 'kmacro-delete-ring-head)
180 (define-key map "\C-t" 'kmacro-swap-ring)
181 (define-key map "\C-l" 'kmacro-call-ring-2nd-repeat)
183 ;; macro counter
184 (define-key map "\C-f" 'kmacro-set-format)
185 (define-key map "\C-c" 'kmacro-set-counter)
186 (define-key map "\C-i" 'kmacro-insert-counter)
187 (define-key map "\C-a" 'kmacro-add-counter)
189 ;; macro editing
190 (define-key map "\C-e" 'kmacro-edit-macro-repeat)
191 (define-key map "\r" 'kmacro-edit-macro)
192 (define-key map "e" 'edit-kbd-macro)
193 (define-key map "l" 'kmacro-edit-lossage)
194 (define-key map " " 'kmacro-step-edit-macro)
196 ;; naming and binding
197 (define-key map "b" 'kmacro-bind-to-key)
198 (define-key map "n" 'kmacro-name-last-macro)
199 (define-key map "x" 'kmacro-to-register)
200 map)
201 "Keymap for keyboard macro commands.")
202 (defalias 'kmacro-keymap kmacro-keymap)
204 ;;; Provide some binding for startup:
205 ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
206 ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
207 ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
208 ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
209 ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
210 ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
211 ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
213 (if kmacro-call-mouse-event
214 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
217 ;;; Called from keyboard-quit
219 (defun kmacro-keyboard-quit ()
220 (or (not defining-kbd-macro)
221 (eq defining-kbd-macro 'append)
222 (kmacro-ring-empty-p)
223 (kmacro-pop-ring)))
226 ;;; Keyboard macro counter
228 (defvar kmacro-counter 0
229 "Current keyboard macro counter.")
231 (defvar kmacro-default-counter-format "%d")
233 (defvar kmacro-counter-format "%d"
234 "Current keyboard macro counter format.")
236 (defvar kmacro-counter-format-start kmacro-counter-format
237 "Macro format at start of macro execution.")
239 (defvar kmacro-counter-value-start kmacro-counter
240 "Macro counter at start of macro execution.")
242 (defvar kmacro-last-counter 0
243 "Last counter inserted by key macro.")
245 (defvar kmacro-initial-counter-value nil
246 "Initial counter value for the next keyboard macro to be defined.")
249 (defun kmacro-insert-counter (arg)
250 "Insert macro counter, then increment it by ARG.
251 Interactively, ARG defaults to 1. With \\[universal-argument], insert
252 previous `kmacro-counter', and do not modify counter."
253 (interactive "P")
254 (if kmacro-initial-counter-value
255 (setq kmacro-counter kmacro-initial-counter-value
256 kmacro-initial-counter-value nil))
257 (if (consp arg)
258 (insert (format kmacro-counter-format kmacro-last-counter))
259 (insert (format kmacro-counter-format kmacro-counter))
260 (kmacro-add-counter (prefix-numeric-value arg))))
263 (defun kmacro-set-format (format)
264 "Set macro counter FORMAT."
265 (interactive "sMacro Counter Format: ")
266 (setq kmacro-counter-format
267 (if (equal format "") "%d" format))
268 ;; redefine initial macro counter if we are not executing a macro.
269 (if (not (or defining-kbd-macro executing-kbd-macro))
270 (setq kmacro-default-counter-format kmacro-counter-format)))
273 (defun kmacro-display-counter (&optional value)
274 "Display current counter value."
275 (unless value (setq value kmacro-counter))
276 (message "New macro counter value: %s (%d)"
277 (format kmacro-counter-format value) value))
279 (defun kmacro-set-counter (arg)
280 "Set `kmacro-counter' to ARG or prompt if missing.
281 With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
282 (interactive "NMacro counter value: ")
283 (if (not (or defining-kbd-macro executing-kbd-macro))
284 (kmacro-display-counter (setq kmacro-initial-counter-value arg))
285 (setq kmacro-last-counter kmacro-counter
286 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
287 kmacro-counter-value-start
288 arg))
289 (unless executing-kbd-macro
290 (kmacro-display-counter))))
293 (defun kmacro-add-counter (arg)
294 "Add numeric prefix arg (prompt if missing) to macro counter.
295 With \\[universal-argument], restore previous counter value."
296 (interactive "NAdd to macro counter: ")
297 (if kmacro-initial-counter-value
298 (setq kmacro-counter kmacro-initial-counter-value
299 kmacro-initial-counter-value nil))
300 (let ((last kmacro-last-counter))
301 (setq kmacro-last-counter kmacro-counter
302 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
303 last
304 kmacro-counter (+ kmacro-counter arg))))
305 (unless executing-kbd-macro
306 (kmacro-display-counter)))
309 (defun kmacro-loop-setup-function ()
310 "Function called prior to each iteration of macro."
311 ;; Restore macro counter format to initial format, so it is ok to change
312 ;; counter format in the macro without restoring it.
313 (setq kmacro-counter-format kmacro-counter-format-start)
314 ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
315 (setq kmacro-counter-value-start kmacro-counter)
316 ;; Return non-nil to continue execution.
320 ;;; Keyboard macro ring
322 (defvar kmacro-ring nil
323 "The keyboard macro ring.
324 Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
325 the macro ring (when defining or executing) is not stored in the ring;
326 instead it is available in the variables `last-kbd-macro', `kmacro-counter',
327 and `kmacro-counter-format'.")
329 ;; Remember what we are currently looking at with kmacro-view-macro.
331 (defvar kmacro-view-last-item nil)
332 (defvar kmacro-view-item-no 0)
335 (defun kmacro-ring-head ()
336 "Return pseudo head element in macro ring."
337 (and last-kbd-macro
338 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
341 (defun kmacro-push-ring (&optional elt)
342 "Push ELT or current macro onto `kmacro-ring'."
343 (when (setq elt (or elt (kmacro-ring-head)))
344 (let ((history-delete-duplicates nil))
345 (add-to-history 'kmacro-ring elt kmacro-ring-max))))
348 (defun kmacro-split-ring-element (elt)
349 (setq last-kbd-macro (car elt)
350 kmacro-counter (nth 1 elt)
351 kmacro-counter-format-start (nth 2 elt)))
354 (defun kmacro-pop-ring1 (&optional raw)
355 "Pop head element off macro ring (no check).
356 Non-nil arg RAW means just return raw first element."
357 (prog1 (car kmacro-ring)
358 (unless raw
359 (kmacro-split-ring-element (car kmacro-ring)))
360 (setq kmacro-ring (cdr kmacro-ring))))
363 (defun kmacro-pop-ring (&optional raw)
364 "Pop head element off macro ring.
365 Non-nil arg RAW means just return raw first element."
366 (unless (kmacro-ring-empty-p)
367 (kmacro-pop-ring1 raw)))
370 (defun kmacro-ring-empty-p (&optional none)
371 "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
372 Check only `last-kbd-macro' if optional arg NONE is non-nil."
373 (while (and (null last-kbd-macro) kmacro-ring)
374 (kmacro-pop-ring1))
375 (cond
376 ((null last-kbd-macro)
377 (message "No keyboard macro defined.")
379 ((and (null none) (null kmacro-ring))
380 (message "Only one keyboard macro defined.")
382 (t nil)))
385 (defun kmacro-display (macro &optional trunc descr empty)
386 "Display a keyboard MACRO.
387 Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
388 Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
389 Optional arg EMPTY is message to print if no macros are defined."
390 (if macro
391 (let* ((x 60)
392 (m (format-kbd-macro macro))
393 (l (length m))
394 (z (and trunc (> l x))))
395 (message "%s%s: %s%s" (or descr "Macro")
396 (if (= kmacro-counter 0) ""
397 (format " [%s]"
398 (format kmacro-counter-format-start kmacro-counter)))
399 (if z (substring m 0 (1- x)) m) (if z "..." "")))
400 (message "%s" (or empty "No keyboard macros defined"))))
403 (defun kmacro-repeat-on-last-key (keys)
404 "Process kmacro commands keys immediately after cycling the ring."
405 (setq keys (vconcat keys))
406 (let ((n (1- (length keys)))
407 cmd done repeat)
408 (while (and last-kbd-macro
409 (not done)
410 (aset keys n (read-event))
411 (setq cmd (key-binding keys t))
412 (setq repeat (get cmd 'kmacro-repeat)))
413 (clear-this-command-keys t)
414 (cond
415 ((eq repeat 'ring)
416 (if kmacro-ring
417 (let ((kmacro-repeat-no-prefix nil))
418 (funcall cmd nil))
419 (kmacro-display last-kbd-macro t)))
420 ((eq repeat 'head)
421 (let ((kmacro-repeat-no-prefix nil))
422 (funcall cmd nil)))
423 ((eq repeat 'stop)
424 (funcall cmd nil)
425 (setq done t)))
426 (setq last-input-event nil)))
427 (when last-input-event
428 (clear-this-command-keys t)
429 (push last-input-event unread-command-events)))
432 (defun kmacro-get-repeat-prefix ()
433 (let (keys)
434 (and kmacro-repeat-no-prefix
435 (setq keys (this-single-command-keys))
436 (> (length keys) 1)
437 keys)))
440 ;;;###autoload
441 (defun kmacro-exec-ring-item (item arg)
442 "Execute item ITEM from the macro ring.
443 ARG is the number of times to execute the item."
444 ;; Use counter and format specific to the macro on the ring!
445 (let ((kmacro-counter (nth 1 item))
446 (kmacro-counter-format-start (nth 2 item)))
447 (execute-kbd-macro (car item) arg #'kmacro-loop-setup-function)
448 (setcar (cdr item) kmacro-counter)))
451 (defun kmacro-call-ring-2nd (arg)
452 "Execute second keyboard macro in macro ring."
453 (interactive "P")
454 (unless (kmacro-ring-empty-p)
455 (kmacro-exec-ring-item (car kmacro-ring) arg)))
458 (defun kmacro-call-ring-2nd-repeat (arg)
459 "Execute second keyboard macro in macro ring.
460 This is like `kmacro-call-ring-2nd', but allows repeating macro commands
461 without repeating the prefix."
462 (interactive "P")
463 (let ((keys (kmacro-get-repeat-prefix)))
464 (kmacro-call-ring-2nd arg)
465 (if (and kmacro-ring keys)
466 (kmacro-repeat-on-last-key keys))))
468 (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
471 (defun kmacro-view-ring-2nd ()
472 "Display the current head of the keyboard macro ring."
473 (interactive)
474 (unless (kmacro-ring-empty-p)
475 (kmacro-display (car (car kmacro-ring)) nil "2nd macro")))
478 (defun kmacro-cycle-ring-next (&optional _arg)
479 "Move to next keyboard macro in keyboard macro ring.
480 Displays the selected macro in the echo area.
481 The ARG parameter is unused."
482 (interactive)
483 (unless (kmacro-ring-empty-p)
484 (kmacro-push-ring)
485 (let* ((keys (kmacro-get-repeat-prefix))
486 (len (length kmacro-ring))
487 (tail (nthcdr (- len 2) kmacro-ring))
488 (elt (car (cdr tail))))
489 (setcdr tail nil)
490 (kmacro-split-ring-element elt)
491 (kmacro-display last-kbd-macro t)
492 (if keys
493 (kmacro-repeat-on-last-key keys)))))
495 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
498 (defun kmacro-cycle-ring-previous (&optional _arg)
499 "Move to previous keyboard macro in keyboard macro ring.
500 Displays the selected macro in the echo area.
501 The ARG parameter is unused."
502 (interactive)
503 (unless (kmacro-ring-empty-p)
504 (let ((keys (kmacro-get-repeat-prefix))
505 (cur (kmacro-ring-head)))
506 (kmacro-pop-ring1)
507 (if kmacro-ring
508 (nconc kmacro-ring (list cur))
509 (setq kmacro-ring (list cur)))
510 (kmacro-display last-kbd-macro t)
511 (if keys
512 (kmacro-repeat-on-last-key keys)))))
514 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
517 (defun kmacro-swap-ring ()
518 "Swap first two elements on keyboard macro ring."
519 (interactive)
520 (unless (kmacro-ring-empty-p)
521 (let ((cur (kmacro-ring-head)))
522 (kmacro-pop-ring1)
523 (kmacro-push-ring cur))
524 (kmacro-display last-kbd-macro t)))
527 (defun kmacro-delete-ring-head (&optional _arg)
528 "Delete current macro from keyboard macro ring.
529 The ARG parameter is unused."
530 (interactive)
531 (unless (kmacro-ring-empty-p t)
532 (if (null kmacro-ring)
533 (setq last-kbd-macro nil)
534 (kmacro-pop-ring))
535 (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
537 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
539 ;;; Traditional bindings:
542 ;;;###autoload
543 (defun kmacro-start-macro (arg)
544 "Record subsequent keyboard input, defining a keyboard macro.
545 The commands are recorded even as they are executed.
546 Use \\[kmacro-end-macro] to finish recording and make the macro available.
547 Use \\[kmacro-end-and-call-macro] to execute the macro.
549 Non-nil arg (prefix arg) means append to last macro defined.
551 With \\[universal-argument] prefix, append to last keyboard macro
552 defined. Depending on `kmacro-execute-before-append', this may begin
553 by re-executing the last macro as if you typed it again.
555 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
556 defining the macro.
558 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
559 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
560 The format of the counter can be modified via \\[kmacro-set-format].
562 Use \\[kmacro-name-last-macro] to give it a name that will remain valid even
563 after another macro is defined.
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 macro)
617 "Call the keyboard MACRO that you defined with \\[kmacro-start-macro].
618 A prefix argument serves as a repeat count. Zero means repeat until error.
619 MACRO defaults to `last-kbd-macro'.
621 When you call the macro, you can call the macro again by repeating
622 just the last key in the key sequence that you used to call this
623 command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
624 for details on how to adjust or disable this behavior.
626 To give a macro a name so you can call it even after defining others,
627 use \\[kmacro-name-last-macro]."
628 (interactive "p")
629 (let ((repeat-key (and (or (and (null no-repeat)
630 (> (length (this-single-command-keys)) 1))
631 ;; Used when we're in the process of repeating.
632 (eq no-repeat 'repeating))
633 last-input-event)))
634 (if end-macro
635 (kmacro-end-macro arg) ; modifies last-kbd-macro
636 (let ((last-kbd-macro (or macro last-kbd-macro)))
637 (call-last-kbd-macro arg #'kmacro-loop-setup-function)))
638 (when (consp arg)
639 (setq arg (car arg)))
640 (when (and (or (null arg) (> arg 0))
641 (setq repeat-key
642 (if (eq kmacro-call-repeat-key t)
643 repeat-key
644 kmacro-call-repeat-key)))
645 ;; Issue a hint to the user, if the echo area isn't in use.
646 (unless (current-message)
647 (message "(Type %s to repeat macro%s)"
648 (format-kbd-macro (vector repeat-key) nil)
649 (if (and kmacro-call-repeat-with-arg
650 arg (> arg 1))
651 (format " %d times" arg) "")))
652 ;; Can't use the `keep-pred' arg because this overlay keymap
653 ;; needs to be removed during the next run of the kmacro
654 ;; (i.e. we must add and remove this map at each repetition).
655 (set-transient-map
656 (let ((map (make-sparse-keymap)))
657 (define-key map (vector repeat-key)
658 `(lambda () (interactive)
659 (kmacro-call-macro ,(and kmacro-call-repeat-with-arg arg)
660 'repeating nil ,(if end-macro
661 last-kbd-macro
662 (or macro last-kbd-macro)))))
663 map)))))
666 ;;; Combined function key bindings:
668 ;;;###autoload
669 (defun kmacro-start-macro-or-insert-counter (arg)
670 "Record subsequent keyboard input, defining a keyboard macro.
671 The commands are recorded even as they are executed.
673 Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
674 macro.
676 With \\[universal-argument], appends to current keyboard macro (keeping
677 the current value of `kmacro-counter').
679 When defining/executing macro, inserts macro counter and increments
680 the counter with ARG or 1 if missing. With \\[universal-argument],
681 inserts previous `kmacro-counter' (but do not modify counter).
683 The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
684 The format of the counter can be modified via \\[kmacro-set-format]."
685 (interactive "P")
686 (if (or defining-kbd-macro executing-kbd-macro)
687 (kmacro-insert-counter arg)
688 (kmacro-start-macro arg)))
691 ;;;###autoload
692 (defun kmacro-end-or-call-macro (arg &optional no-repeat)
693 "End kbd macro if currently being defined; else call last kbd macro.
694 With numeric prefix ARG, repeat macro that many times.
695 With \\[universal-argument], call second macro in macro ring."
696 (interactive "P")
697 (cond
698 (defining-kbd-macro
699 (if kmacro-call-repeat-key
700 (kmacro-call-macro arg no-repeat t)
701 (kmacro-end-macro arg)))
702 ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
703 kmacro-view-last-item)
704 (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
705 ((and arg (listp arg))
706 (kmacro-call-ring-2nd 1))
708 (kmacro-call-macro arg no-repeat))))
711 (defun kmacro-end-or-call-macro-repeat (arg)
712 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
713 (interactive "P")
714 (let ((keys (kmacro-get-repeat-prefix)))
715 (kmacro-end-or-call-macro arg t)
716 (if keys
717 (kmacro-repeat-on-last-key keys))))
719 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
722 ;;;###autoload
723 (defun kmacro-end-and-call-macro (arg &optional no-repeat)
724 "Call last keyboard macro, ending it first if currently being defined.
725 With numeric prefix ARG, repeat macro that many times.
726 Zero argument means repeat until there is an error.
728 To give a macro a name, so you can call it even after defining other
729 macros, use \\[kmacro-name-last-macro]."
730 (interactive "P")
731 (if defining-kbd-macro
732 (kmacro-end-macro nil))
733 (kmacro-call-macro arg no-repeat))
736 ;;;###autoload
737 (defun kmacro-end-call-mouse (event)
738 "Move point to the position clicked with the mouse and call last kbd macro.
739 If kbd macro currently being defined end it before activating it."
740 (interactive "e")
741 (when defining-kbd-macro
742 (end-kbd-macro)
743 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
744 (setq last-kbd-macro nil)
745 (message "Ignore empty macro")
746 ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
747 (while (and (null last-kbd-macro) kmacro-ring)
748 (kmacro-pop-ring1))))
749 (mouse-set-point event)
750 (kmacro-call-macro nil t))
753 ;;; Misc. commands
755 ;; An idea for macro bindings:
756 ;; Create a separate keymap installed as a minor-mode keymap (e.g. in
757 ;; the emulation-mode-map-alists) in which macro bindings are made
758 ;; independent of any other bindings. When first binding is made,
759 ;; the keymap is created, installed, and enabled. Key seq. C-x C-k +
760 ;; can then be used to toggle the use of this keymap on and off.
761 ;; This means that it would be safe(r) to bind ordinary keys like
762 ;; letters and digits, provided that we inhibit the keymap while
763 ;; executing the macro later on (but that's controversial...)
765 (defun kmacro-lambda-form (mac &optional counter format)
766 "Create lambda form for macro bound to symbol or key."
767 (if counter
768 (setq mac (list mac counter format)))
769 `(lambda (&optional arg)
770 "Keyboard macro."
771 (interactive "p")
772 (kmacro-exec-ring-item ',mac arg)))
774 (defun kmacro-extract-lambda (mac)
775 "Extract kmacro from a kmacro lambda form."
776 (and (eq (car-safe mac) 'lambda)
777 (setq mac (assoc 'kmacro-exec-ring-item mac))
778 (setq mac (car-safe (cdr-safe (car-safe (cdr-safe mac)))))
779 (listp mac)
780 (= (length mac) 3)
781 (arrayp (car mac))
782 mac))
785 (defalias 'kmacro-p #'kmacro-extract-lambda
786 "Return non-nil if MAC is a kmacro keyboard macro.")
788 (defun kmacro-bind-to-key (_arg)
789 "When not defining or executing a macro, offer to bind last macro to a key.
790 The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
791 through [C-x C-k Z] are reserved for user bindings, and to bind to
792 one of these sequences, just enter the digit or letter, rather than
793 the whole sequence.
795 You can bind to any valid key sequence, but if you try to bind to
796 a key with an existing command binding, you will be asked for
797 confirmation whether to replace that binding. Note that the
798 binding is made in the `global-map' keymap, so the macro binding
799 may be shaded by a local key binding.
800 The ARG parameter is unused."
801 (interactive "p")
802 (if (or defining-kbd-macro executing-kbd-macro)
803 (if defining-kbd-macro
804 (message "Cannot save macro while defining it."))
805 (unless last-kbd-macro
806 (error "No keyboard macro defined"))
807 (let ((key-seq (read-key-sequence "Bind last macro to key: "))
808 ok cmd)
809 (when (= (length key-seq) 1)
810 (let ((ch (aref key-seq 0)))
811 (if (and (integerp ch)
812 (or (and (>= ch ?0) (<= ch ?9))
813 (and (>= ch ?A) (<= ch ?Z))))
814 (setq key-seq (concat "\C-x\C-k" key-seq)
815 ok t))))
816 (when (and (not (equal key-seq "\a"))
817 (or ok
818 (not (setq cmd (key-binding key-seq)))
819 (stringp cmd)
820 (vectorp cmd)
821 (yes-or-no-p (format "%s runs command %S. Bind anyway? "
822 (format-kbd-macro key-seq)
823 cmd))))
824 (define-key global-map key-seq
825 (kmacro-lambda-form (kmacro-ring-head)))
826 (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
828 (defun kmacro-keyboard-macro-p (symbol)
829 "Return non-nil if SYMBOL is the name of some sort of keyboard macro."
830 (let ((f (symbol-function symbol)))
831 (when f
832 (or (stringp f)
833 (vectorp f)
834 (kmacro-p f)))))
836 (defun kmacro-name-last-macro (symbol)
837 "Assign a name to the last keyboard macro defined.
838 Argument SYMBOL is the name to define.
839 The symbol's function definition becomes the keyboard macro string.
840 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
841 (interactive "SName for last kbd macro: ")
842 (or last-kbd-macro
843 (error "No keyboard macro defined"))
844 (and (fboundp symbol)
845 (not (kmacro-keyboard-macro-p symbol))
846 (error "Function %s is already defined and not a keyboard macro"
847 symbol))
848 (if (string-equal symbol "")
849 (error "No command name given"))
850 ;; FIXME: Use plain old `last-kbd-macro' for kmacros where it doesn't
851 ;; make a difference?
852 (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
853 ;; This used to be used to detect when a symbol corresponds to a kmacro.
854 ;; Nowadays it's unused because we used `kmacro-p' instead to see if the
855 ;; symbol's function definition matches that of a kmacro, which is more
856 ;; reliable.
857 (put symbol 'kmacro t))
860 (defun kmacro-execute-from-register (k)
861 (kmacro-call-macro current-prefix-arg nil nil k))
863 (defun kmacro-to-register (r)
864 "Store the last keyboard macro in register R.
866 Interactively, reads the register using `register-read-with-preview'."
867 (interactive
868 (progn
869 (or last-kbd-macro (error "No keyboard macro defined"))
870 (list (register-read-with-preview "Save to register: "))))
871 (set-register r (registerv-make
872 last-kbd-macro
873 :jump-func 'kmacro-execute-from-register
874 :print-func (lambda (k)
875 (princ (format "a keyboard macro:\n %s"
876 (format-kbd-macro k))))
877 :insert-func (lambda (k)
878 (insert (format-kbd-macro k))))))
881 (defun kmacro-view-macro (&optional _arg)
882 "Display the last keyboard macro.
883 If repeated, it shows previous elements in the macro ring.
884 The ARG parameter is unused."
885 (interactive)
886 (cond
887 ((or (kmacro-ring-empty-p)
888 (not (eq last-command 'kmacro-view-macro)))
889 (setq kmacro-view-last-item nil))
890 ((null kmacro-view-last-item)
891 (setq kmacro-view-last-item kmacro-ring
892 kmacro-view-item-no 2))
893 ((consp kmacro-view-last-item)
894 (setq kmacro-view-last-item (cdr kmacro-view-last-item)
895 kmacro-view-item-no (1+ kmacro-view-item-no)))
897 (setq kmacro-view-last-item nil)))
898 (setq this-command 'kmacro-view-macro
899 last-command this-command) ;; in case we repeat
900 (kmacro-display (if kmacro-view-last-item
901 (car (car kmacro-view-last-item))
902 last-kbd-macro)
904 (if kmacro-view-last-item
905 (concat (cond ((= kmacro-view-item-no 2) "2nd")
906 ((= kmacro-view-item-no 3) "3nd")
907 (t (format "%dth" kmacro-view-item-no)))
908 " previous macro")
909 "Last macro")))
911 (defun kmacro-view-macro-repeat (&optional arg)
912 "Display the last keyboard macro.
913 If repeated, it shows previous elements in the macro ring.
914 To execute the displayed macro ring item without changing the macro ring,
915 just enter C-k.
916 This is like `kmacro-view-macro', but allows repeating macro commands
917 without repeating the prefix."
918 (interactive)
919 (let ((keys (kmacro-get-repeat-prefix)))
920 (kmacro-view-macro arg)
921 (if (and last-kbd-macro keys)
922 (kmacro-repeat-on-last-key keys))))
924 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
927 (defun kmacro-edit-macro-repeat (&optional arg)
928 "Edit last keyboard macro."
929 (interactive "P")
930 (edit-kbd-macro "\r" arg))
932 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
935 (defun kmacro-edit-macro (&optional arg)
936 "As edit last keyboard macro, but without kmacro-repeat property."
937 (interactive "P")
938 (edit-kbd-macro "\r" arg))
941 (defun kmacro-edit-lossage ()
942 "Edit most recent 300 keystrokes as a keyboard macro."
943 (interactive)
944 (kmacro-push-ring)
945 (edit-kbd-macro "\C-hl"))
948 ;;; Single-step editing of keyboard macros
950 (defvar kmacro-step-edit-active nil) ;; step-editing active
951 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
952 (defvar kmacro-step-edit-inserting) ;; inserting into macro
953 (defvar kmacro-step-edit-appending) ;; append to end of macro
954 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
955 (defvar kmacro-step-edit-key-index) ;; index of current key
956 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
957 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
958 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
960 (defvar kmacro-step-edit-map
961 (let ((map (make-sparse-keymap)))
962 ;; query-replace-map answers include: `act', `skip', `act-and-show',
963 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
964 ;; `automatic', `backup', `exit-prefix', and `help'.")
965 ;; Also: `quit', `edit-replacement'
967 (set-keymap-parent map query-replace-map)
969 (define-key map "\t" 'act-repeat)
970 (define-key map [tab] 'act-repeat)
971 (define-key map "\C-k" 'skip-rest)
972 (define-key map "c" 'automatic)
973 (define-key map "f" 'skip-keep)
974 (define-key map "q" 'quit)
975 (define-key map "d" 'skip)
976 (define-key map "\C-d" 'skip)
977 (define-key map "i" 'insert)
978 (define-key map "I" 'insert-1)
979 (define-key map "r" 'replace)
980 (define-key map "R" 'replace-1)
981 (define-key map "a" 'append)
982 (define-key map "A" 'append-end)
983 map)
984 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
985 This keymap is an extension to the `query-replace-map', allowing the
986 following additional answers: `insert', `insert-1', `replace', `replace-1',
987 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
989 (defun kmacro-step-edit-prompt (macro index)
990 ;; Show step-edit prompt
991 (let ((keys (and (not kmacro-step-edit-appending)
992 index (substring macro index executing-kbd-macro-index)))
993 (future (and (not kmacro-step-edit-appending)
994 (substring macro executing-kbd-macro-index)))
995 (message-log-max nil)
996 (curmsg (current-message)))
998 ;; TODO: Scroll macro if max-mini-window-height is too small.
999 (message "%s"
1000 (concat
1001 (format "Macro: %s%s%s%s%s\n"
1002 (format-kbd-macro kmacro-step-edit-new-macro 1)
1003 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
1004 (propertize (if keys (format-kbd-macro keys)
1005 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
1006 (if future " " "")
1007 (if future (format-kbd-macro future) ""))
1008 (cond
1009 ((minibufferp)
1010 (format "%s\n%s\n"
1011 (propertize "\
1012 minibuffer " 'face 'header-line)
1013 (buffer-substring (point-min) (point-max))))
1014 (curmsg
1015 (format "%s\n%s\n"
1016 (propertize "\
1017 echo area " 'face 'header-line)
1018 curmsg))
1019 (t ""))
1020 (if keys
1021 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
1022 (propertize "\
1023 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
1024 (if kmacro-step-edit-help "\
1025 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
1026 TAB: execute while same, ?: toggle help
1027 Edit: i: insert, r: replace, a: append, A: append at end,
1028 I/R: insert/replace with one sequence,
1029 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
1030 ----------------------------------------------------------------
1031 " "")
1032 (propertize "Next command:" 'face 'bold)
1033 this-command)
1034 (propertize
1035 (format "Type key sequence%s to insert and execute%s: "
1036 (if (numberp kmacro-step-edit-inserting) "" "s")
1037 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
1038 'face 'bold))))))
1040 (defun kmacro-step-edit-query ()
1041 ;; Pre-command hook function for step-edit in "command" mode
1042 (let ((resize-mini-windows t)
1043 (max-mini-window-height kmacro-step-edit-mini-window-height)
1044 act restore-index next-index)
1046 ;; Handle commands which reads additional input using read-char.
1047 (cond
1048 ((and (eq this-command 'quoted-insert)
1049 (not (eq kmacro-step-edit-action t)))
1050 ;; Find the actual end of this key sequence.
1051 ;; Must be able to backtrack in case we actually execute it.
1052 (setq restore-index executing-kbd-macro-index)
1053 (let (unread-command-events)
1054 (quoted-insert 0)
1055 (when unread-command-events
1056 (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1057 next-index executing-kbd-macro-index)))))
1059 ;; Query the user; stop macro execution temporarily.
1060 (let ((macro executing-kbd-macro)
1061 (executing-kbd-macro nil)
1062 (defining-kbd-macro nil))
1064 ;; Any action requested by previous command
1065 (cond
1066 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
1067 (cond
1068 ((eq this-command 'quoted-insert)
1069 (clear-this-command-keys) ;; recent-keys actually
1070 (let (unread-command-events)
1071 (quoted-insert (prefix-numeric-value current-prefix-arg))
1072 (setq kmacro-step-edit-new-macro
1073 (vconcat kmacro-step-edit-new-macro (recent-keys)))
1074 (when unread-command-events
1075 (setq kmacro-step-edit-new-macro
1076 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
1077 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
1078 (setq current-prefix-arg nil
1079 prefix-arg nil)
1080 (setq act 'ignore))
1082 (setq act 'act)))
1083 (setq kmacro-step-edit-action nil))
1084 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
1085 (setq act 'act))
1087 (setq kmacro-step-edit-action nil)))
1089 ;; Handle prefix arg, or query user
1090 (cond
1091 (act act) ;; set above
1093 (kmacro-step-edit-prompt macro kmacro-step-edit-key-index)
1094 (setq act (lookup-key kmacro-step-edit-map
1095 (vector (with-current-buffer (current-buffer) (read-event))))))))
1097 ;; Resume macro execution and perform the action
1098 (cond
1099 ((cond
1100 ((eq act 'act)
1102 ((eq act 'act-repeat)
1103 (setq kmacro-step-edit-action this-command)
1105 ((eq act 'quit)
1106 (setq kmacro-step-edit-replace nil)
1107 (setq kmacro-step-edit-active 'ignore)
1108 nil)
1109 ((eq act 'skip)
1110 nil)
1111 ((eq act 'skip-keep)
1112 (setq this-command 'ignore)
1114 ((eq act 'skip-rest)
1115 (setq kmacro-step-edit-active 'ignore)
1116 nil)
1117 ((memq act '(automatic exit))
1118 (setq kmacro-step-edit-active nil)
1119 (setq act t)
1121 ((member act '(insert-1 insert))
1122 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1123 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
1124 nil)
1125 ((member act '(replace-1 replace))
1126 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
1127 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1128 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1129 kmacro-step-edit-appending t))
1130 nil)
1131 ((eq act 'append)
1132 (setq kmacro-step-edit-inserting t)
1133 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1134 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1135 kmacro-step-edit-appending t))
1137 ((eq act 'append-end)
1138 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1139 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1140 kmacro-step-edit-inserting t
1141 kmacro-step-edit-appending t)
1142 (setq kmacro-step-edit-active 'append-end))
1143 (setq act t)
1145 ((eq act 'help)
1146 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1147 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
1148 nil)
1149 (t ;; Ignore unknown responses
1150 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1151 nil))
1152 (if (> executing-kbd-macro-index kmacro-step-edit-key-index)
1153 (setq kmacro-step-edit-new-macro
1154 (vconcat kmacro-step-edit-new-macro
1155 (substring executing-kbd-macro
1156 kmacro-step-edit-key-index
1157 (if (eq act t) nil
1158 executing-kbd-macro-index)))))
1159 (if restore-index
1160 (setq executing-kbd-macro-index restore-index)))
1162 (setq this-command 'ignore)))
1163 (setq kmacro-step-edit-key-index next-index)))
1165 (defun kmacro-step-edit-insert ()
1166 ;; Pre-command hook function for step-edit in "insert" mode
1167 (let ((resize-mini-windows t)
1168 (max-mini-window-height kmacro-step-edit-mini-window-height)
1169 (macro executing-kbd-macro)
1170 (executing-kbd-macro nil)
1171 (defining-kbd-macro nil)
1172 cmd keys next-index)
1173 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1174 (kmacro-step-edit-prompt macro nil)
1175 ;; Now, we have read a key sequence from the macro, but we don't want
1176 ;; to execute it yet. So push it back and read another sequence.
1177 (setq keys (read-key-sequence nil nil nil nil t))
1178 (setq cmd (key-binding keys t nil))
1179 (if (cond
1180 ((null cmd)
1182 ((eq cmd 'quoted-insert)
1183 (clear-this-command-keys) ;; recent-keys actually
1184 (quoted-insert (prefix-numeric-value current-prefix-arg))
1185 (setq current-prefix-arg nil
1186 prefix-arg nil)
1187 (setq keys (vconcat keys (recent-keys)))
1188 (when (numberp kmacro-step-edit-inserting)
1189 (setq kmacro-step-edit-inserting nil)
1190 (when unread-command-events
1191 (setq keys (substring keys 0 (- (length unread-command-events)))
1192 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1193 next-index executing-kbd-macro-index
1194 unread-command-events nil)))
1195 (setq cmd 'ignore)
1196 nil)
1197 ((numberp kmacro-step-edit-inserting)
1198 (setq kmacro-step-edit-inserting nil)
1199 nil)
1200 ((equal keys "\C-j")
1201 (setq kmacro-step-edit-inserting nil)
1202 (setq kmacro-step-edit-action nil)
1203 (setq next-index kmacro-step-edit-key-index)
1205 (t nil))
1206 (setq this-command 'ignore)
1207 (setq this-command cmd)
1208 (if (memq this-command '(self-insert-command digit-argument))
1209 (setq last-command-event (aref keys (1- (length keys)))))
1210 (if keys
1211 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1212 (setq kmacro-step-edit-key-index next-index)))
1214 (defun kmacro-step-edit-pre-command ()
1215 (remove-hook 'post-command-hook #'kmacro-step-edit-post-command)
1216 (when kmacro-step-edit-active
1217 (cond
1218 ((eq kmacro-step-edit-active 'ignore)
1219 (setq this-command 'ignore))
1220 ((eq kmacro-step-edit-active 'append-end)
1221 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1222 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1223 kmacro-step-edit-inserting t
1224 kmacro-step-edit-appending t
1225 kmacro-step-edit-active t)))
1226 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1227 (if kmacro-step-edit-inserting
1228 (kmacro-step-edit-insert)
1229 (kmacro-step-edit-query))
1230 (setq kmacro-step-edit-num-input-keys num-input-keys)
1231 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1232 (setq kmacro-step-edit-appending nil
1233 kmacro-step-edit-active 'ignore)))))
1234 (when (eq kmacro-step-edit-active t)
1235 (add-hook 'post-command-hook #'kmacro-step-edit-post-command t)))
1237 (defun kmacro-step-edit-minibuf-setup ()
1238 (remove-hook 'pre-command-hook #'kmacro-step-edit-pre-command t)
1239 (when kmacro-step-edit-active
1240 (add-hook 'pre-command-hook #'kmacro-step-edit-pre-command nil t)))
1242 (defun kmacro-step-edit-post-command ()
1243 (remove-hook 'pre-command-hook #'kmacro-step-edit-pre-command)
1244 (when kmacro-step-edit-active
1245 (add-hook 'pre-command-hook #'kmacro-step-edit-pre-command nil nil)
1246 (if kmacro-step-edit-key-index
1247 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1248 (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
1251 (defun kmacro-step-edit-macro ()
1252 "Step edit and execute last keyboard macro.
1254 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1255 (interactive)
1256 (let ((kmacro-step-edit-active t)
1257 (kmacro-step-edit-new-macro "")
1258 (kmacro-step-edit-inserting nil)
1259 (kmacro-step-edit-appending nil)
1260 (kmacro-step-edit-replace t)
1261 (kmacro-step-edit-key-index 0)
1262 (kmacro-step-edit-action nil)
1263 (kmacro-step-edit-help nil)
1264 (kmacro-step-edit-num-input-keys num-input-keys)
1265 (pre-command-hook pre-command-hook)
1266 (post-command-hook post-command-hook)
1267 (minibuffer-setup-hook minibuffer-setup-hook))
1268 (add-hook 'pre-command-hook #'kmacro-step-edit-pre-command nil)
1269 (add-hook 'post-command-hook #'kmacro-step-edit-post-command t)
1270 (add-hook 'minibuffer-setup-hook #'kmacro-step-edit-minibuf-setup t)
1271 (call-last-kbd-macro nil nil)
1272 (when (and kmacro-step-edit-replace
1273 kmacro-step-edit-new-macro
1274 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1275 (kmacro-push-ring)
1276 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1278 (provide 'kmacro)
1280 ;;; kmacro.el ends here