Get rid of all the manual purecopy calls in menu-bar definitions.
[bpt/emacs.git] / lisp / kmacro.el
CommitLineData
c30c4abe
KS
1;;; kmacro.el --- enhanced keyboard macros
2
acaf905b 3;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
c30c4abe
KS
4
5;; Author: Kim F. Storm <storm@cua.dk>
306d2bb3 6;; Keywords: keyboard convenience
c30c4abe
KS
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
c30c4abe 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
c30c4abe
KS
14
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.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c30c4abe
KS
22
23;;; Commentary:
24
64d728d6
KS
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.
28
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.
c30c4abe 32
c8bf445e
KS
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
c30c4abe
KS
35;; easy to repeat a macro immediately after defining it.
36;;
c8bf445e 37;; You can call the macro repeatedly by pressing F4 multiple times, or
c30c4abe
KS
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
04bf5b65 41;; is signaled by ringing the bell.
c30c4abe 42
c8bf445e
KS
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
1fa13bd4
KS
45;; first element of the macro ring.
46;;
47;; Note: an empty macro is never added to the macro ring.
48;;
c8bf445e 49;; You can execute the second element on the macro ring with C-u F4 or
1fa13bd4
KS
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.
54;;
1fa13bd4
KS
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.
58;;
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.
64
c8bf445e 65;; If you enter F3 while defining the macro, the numeric value of
c30c4abe
KS
66;; `kmacro-counter' is inserted using the `kmacro-counter-format', and
67;; `kmacro-counter' is incremented by 1 (or the numeric prefix value
c8bf445e 68;; of F3).
c30c4abe
KS
69;;
70;; The initial value of `kmacro-counter' is 0, or the numeric prefix
c8bf445e 71;; value given to F3 when starting the macro.
c30c4abe 72;;
c8bf445e 73;; Now, each time you call the macro using F4, the current
c30c4abe
KS
74;; value of `kmacro-counter' is inserted and incremented, making it
75;; easy to insert incremental numbers in the buffer.
76;;
77;; Example:
78;;
c8bf445e 79;; The following sequence: M-5 F3 x M-2 F3 y F4 F4 F4 F4
c30c4abe
KS
80;; inserts the following string: x5yx7yx9yx11y
81
7e30f58e 82;; A macro can also be called using a mouse click, default S-mouse-3.
c30c4abe
KS
83;; This calls the macro at the point where you click the mouse.
84
1fa13bd4 85;; You can edit the last macro using C-x C-k C-e.
c30c4abe 86
c8bf445e 87;; You can append to the last macro using C-u F3.
c30c4abe 88
1fa13bd4
KS
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.
c30c4abe
KS
91
92;; The following key bindings are performed:
f1180544 93;;
c30c4abe
KS
94;; Normal While defining macro
95;; --------------------------- ------------------------------
c8bf445e 96;; f3 Define macro Insert current counter value
c30c4abe
KS
97;; Prefix arg specifies initial and increase counter by prefix
98;; counter value (default 0) (default increment: 1)
99;;
c8bf445e 100;; C-u f3 APPENDs to last macro
f1180544
JB
101;;
102;; f4 Call last macro End macro
c30c4abe
KS
103;; Prefix arg specifies number
104;; of times to execute macro.
105;;
c8bf445e 106;; C-u f4 Swap last and head of macro ring.
f1180544 107;;
c30c4abe
KS
108;; S-mouse-3 Set point at click and End macro and execute macro at
109;; execute last macro. click.
110
111;;; Code:
112
7e30f58e 113;; Customization:
c30c4abe
KS
114
115(defgroup kmacro nil
116 "Simplified keyboard macro user interface."
117 :group 'keyboard
118 :group 'convenience
bf247b6e 119 :version "22.1"
c30c4abe
KS
120 :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
121 :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
122
1fa13bd4
KS
123(defcustom kmacro-call-mouse-event 'S-mouse-3
124 "The mouse event used by kmacro to call a macro.
125Set to nil if no mouse binding is desired."
126 :type 'symbol
c30c4abe
KS
127 :group 'kmacro)
128
1fa13bd4
KS
129(defcustom kmacro-ring-max 8
130 "Maximum number of keyboard macros to save in macro ring."
131 :type 'integer
c30c4abe
KS
132 :group 'kmacro)
133
1fa13bd4
KS
134
135(defcustom kmacro-execute-before-append t
136 "Controls whether appending to a macro starts by executing the macro.
137If non-nil, using a single \\[universal-argument] prefix executes the macro
138before appending, while more than one \\[universal-argument] prefix does not
139execute the macro.
140Otherwise, a single \\[universal-argument] prefix does not execute the
141macro, while more than one \\[universal-argument] prefix causes the
142macro to be executed before appending to it."
143 :type 'boolean
c30c4abe
KS
144 :group 'kmacro)
145
1fa13bd4
KS
146
147(defcustom kmacro-repeat-no-prefix t
148 "Allow repeating certain macro commands without entering the C-x C-k prefix."
149 :type 'boolean
c30c4abe
KS
150 :group 'kmacro)
151
c8bf445e
KS
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)
159
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)
164
723d6a64
KS
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)
1fa13bd4
KS
169
170;; Keymap
171
172(defvar kmacro-keymap
173 (let ((map (make-sparse-keymap)))
a1839f06 174 ;; Start, end, execute macros
28241c47 175 (define-key map "s" 'kmacro-start-macro)
a1839f06 176 (define-key map "\C-s" 'kmacro-start-macro)
c8bf445e 177 (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
a1839f06
KS
178 (define-key map "r" 'apply-macro-to-region-lines)
179 (define-key map "q" 'kbd-macro-query) ;; Like C-x q
180
181 ;; macro ring
1fa13bd4
KS
182 (define-key map "\C-n" 'kmacro-cycle-ring-next)
183 (define-key map "\C-p" 'kmacro-cycle-ring-previous)
a1839f06
KS
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)
188
189 ;; macro counter
1fa13bd4
KS
190 (define-key map "\C-f" 'kmacro-set-format)
191 (define-key map "\C-c" 'kmacro-set-counter)
a1839f06
KS
192 (define-key map "\C-i" 'kmacro-insert-counter)
193 (define-key map "\C-a" 'kmacro-add-counter)
194
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)
201
202 ;; naming and binding
28241c47 203 (define-key map "b" 'kmacro-bind-to-key)
18d1e6c9 204 (define-key map "n" 'kmacro-name-last-macro)
1fa13bd4
KS
205 map)
206 "Keymap for keyboard macro commands.")
207(defalias 'kmacro-keymap kmacro-keymap)
208
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)
723d6a64 212;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
c8bf445e
KS
213;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
214;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
1fa13bd4
KS
215;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
216;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
217
218(if kmacro-call-mouse-event
219 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
220
221
1b25dccd
KS
222;;; Called from keyboard-quit
223
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)))
229
1fa13bd4
KS
230
231;;; Keyboard macro counter
c30c4abe
KS
232
233(defvar kmacro-counter 0
fb7ada5f 234 "Current keyboard macro counter.")
c30c4abe 235
7dcb2dfd
LT
236(defvar kmacro-default-counter-format "%d")
237
c30c4abe 238(defvar kmacro-counter-format "%d"
fb7ada5f 239 "Current keyboard macro counter format.")
c30c4abe
KS
240
241(defvar kmacro-counter-format-start kmacro-counter-format
242 "Macro format at start of macro execution.")
243
1fa13bd4
KS
244(defvar kmacro-counter-value-start kmacro-counter
245 "Macro counter at start of macro execution.")
246
a1839f06
KS
247(defvar kmacro-last-counter 0
248 "Last counter inserted by key macro.")
249
250(defvar kmacro-initial-counter-value nil
251 "Initial counter value for the next keyboard macro to be defined.")
c30c4abe 252
c30c4abe 253
1fa13bd4 254(defun kmacro-insert-counter (arg)
57a1cd7b
GM
255 "Insert macro counter, then increment it by ARG.
256Interactively, ARG defaults to 1. With \\[universal-argument], insert
257previous `kmacro-counter', and do not modify counter."
1fa13bd4 258 (interactive "P")
52996e8c
KS
259 (if kmacro-initial-counter-value
260 (setq kmacro-counter kmacro-initial-counter-value
261 kmacro-initial-counter-value nil))
1fa13bd4
KS
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))))
266
267
268(defun kmacro-set-format (format)
269 "Set macro counter FORMAT."
7dcb2dfd 270 (interactive "sMacro Counter Format: ")
1fa13bd4
KS
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))
7dcb2dfd 275 (setq kmacro-default-counter-format kmacro-counter-format)))
1fa13bd4
KS
276
277
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))
282
283
284(defun kmacro-set-counter (arg)
c796a0b6 285 "Set `kmacro-counter' to ARG or prompt if missing.
a1839f06 286With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
1fa13bd4 287 (interactive "NMacro counter value: ")
52996e8c
KS
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))))
1fa13bd4
KS
296
297
298(defun kmacro-add-counter (arg)
299 "Add numeric prefix arg (prompt if missing) to macro counter.
300With \\[universal-argument], restore previous counter value."
301 (interactive "NAdd to macro counter: ")
52996e8c
KS
302 (if kmacro-initial-counter-value
303 (setq kmacro-counter kmacro-initial-counter-value
304 kmacro-initial-counter-value nil))
1fa13bd4
KS
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)))
312
313
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.
322 t)
323
324
325;;; Keyboard macro ring
326
327(defvar kmacro-ring nil
328 "The keyboard macro ring.
329Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
330the macro ring (when defining or executing) is not stored in the ring;
331instead it is available in the variables `last-kbd-macro', `kmacro-counter',
332and `kmacro-counter-format'.")
333
a1839f06
KS
334;; Remember what we are currently looking at with kmacro-view-macro.
335
336(defvar kmacro-view-last-item nil)
337(defvar kmacro-view-item-no 0)
338
1fa13bd4
KS
339
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)))
344
345
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)))
549b93dd
KS
349 (let ((history-delete-duplicates nil))
350 (add-to-history 'kmacro-ring elt kmacro-ring-max))))
1fa13bd4
KS
351
352
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)))
357
358
359(defun kmacro-pop-ring1 (&optional raw)
360 "Pop head element off macro ring (no check).
361Non-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))))
366
367
368(defun kmacro-pop-ring (&optional raw)
369 "Pop head element off macro ring.
370Non-nil arg RAW means just return raw first element."
371 (unless (kmacro-ring-empty-p)
372 (kmacro-pop-ring1 raw)))
f1180544 373
1fa13bd4 374
1fa13bd4
KS
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.
377Check 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.")
383 t)
384 ((and (null none) (null kmacro-ring))
385 (message "Only one keyboard macro defined.")
386 t)
387 (t nil)))
388
389
a1839f06
KS
390(defun kmacro-display (macro &optional trunc descr empty)
391 "Display a keyboard MACRO.
392Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
393Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
394Optional arg EMPTY is message to print if no macros are defined."
1fa13bd4
KS
395 (if macro
396 (let* ((x 60)
397 (m (format-kbd-macro macro))
398 (l (length m))
7dcb2dfd
LT
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 "..." "")))
274f1353 405 (message "%s" (or empty "No keyboard macros defined"))))
1fa13bd4
KS
406
407
c8bf445e 408(defun kmacro-repeat-on-last-key (keys)
5586939c 409 "Process kmacro commands keys immediately after cycling the ring."
c8bf445e
KS
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))))
435
436
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)))
443
444
81e28b24 445;;;###autoload
a1839f06
KS
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)))
453
454
1fa13bd4 455(defun kmacro-call-ring-2nd (arg)
c796a0b6 456 "Execute second keyboard macro in macro ring."
1fa13bd4
KS
457 (interactive "P")
458 (unless (kmacro-ring-empty-p)
a1839f06 459 (kmacro-exec-ring-item (car kmacro-ring) arg)))
1fa13bd4
KS
460
461
c8bf445e 462(defun kmacro-call-ring-2nd-repeat (arg)
c796a0b6 463 "Execute second keyboard macro in macro ring.
a1839f06
KS
464This is like `kmacro-call-ring-2nd', but allows repeating macro commands
465without repeating the prefix."
1fa13bd4 466 (interactive "P")
c8bf445e
KS
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))))
1fa13bd4 471
c8bf445e 472(put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
1fa13bd4 473
c30c4abe 474
1fa13bd4
KS
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")))
480
481
06b60517 482(defun kmacro-cycle-ring-next (&optional _arg)
1fa13bd4
KS
483 "Move to next keyboard macro in keyboard macro ring.
484Displays the selected macro in the echo area."
485 (interactive)
486 (unless (kmacro-ring-empty-p)
487 (kmacro-push-ring)
c8bf445e
KS
488 (let* ((keys (kmacro-get-repeat-prefix))
489 (len (length kmacro-ring))
1fa13bd4
KS
490 (tail (nthcdr (- len 2) kmacro-ring))
491 (elt (car (cdr tail))))
492 (setcdr tail nil)
c8bf445e
KS
493 (kmacro-split-ring-element elt)
494 (kmacro-display last-kbd-macro t)
495 (if keys
496 (kmacro-repeat-on-last-key keys)))))
1fa13bd4
KS
497
498(put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
499
500
06b60517 501(defun kmacro-cycle-ring-previous (&optional _arg)
1fa13bd4
KS
502 "Move to previous keyboard macro in keyboard macro ring.
503Displays the selected macro in the echo area."
504 (interactive)
505 (unless (kmacro-ring-empty-p)
c8bf445e
KS
506 (let ((keys (kmacro-get-repeat-prefix))
507 (cur (kmacro-ring-head)))
1fa13bd4
KS
508 (kmacro-pop-ring1)
509 (if kmacro-ring
510 (nconc kmacro-ring (list cur))
c8bf445e
KS
511 (setq kmacro-ring (list cur)))
512 (kmacro-display last-kbd-macro t)
513 (if keys
514 (kmacro-repeat-on-last-key keys)))))
1fa13bd4
KS
515
516(put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
c30c4abe 517
1fa13bd4
KS
518
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)))
527
528
06b60517 529(defun kmacro-delete-ring-head (&optional _arg)
1fa13bd4
KS
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.")))
537
538(put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
539
540;;; Traditional bindings:
541
f1180544 542
1fa13bd4 543;;;###autoload
c30c4abe 544(defun kmacro-start-macro (arg)
1fa13bd4
KS
545 "Record subsequent keyboard input, defining a keyboard macro.
546The commands are recorded even as they are executed.
63b833de 547Use \\[kmacro-end-macro] to finish recording and make the macro available.
7fdc0c13 548Use \\[kmacro-end-and-call-macro] to execute the macro.
18d1e6c9
KS
549
550Non-nil arg (prefix arg) means append to last macro defined.
1fa13bd4
KS
551
552With \\[universal-argument] prefix, append to last keyboard macro
553defined. Depending on `kmacro-execute-before-append', this may begin
554by re-executing the last macro as if you typed it again.
555
556Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
557defining the macro.
558
559Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
560The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
18d1e6c9
KS
561The format of the counter can be modified via \\[kmacro-set-format].
562
563Use \\[kmacro-name-last-macro] to give it a permanent name.
564Use \\[kmacro-bind-to-key] to bind it to a key sequence."
1fa13bd4
KS
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
4224caf8
KS
571 (kmacro-push-ring
572 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
a1839f06
KS
573 (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
574 kmacro-initial-counter-value
575 0)
576 kmacro-initial-counter-value nil
1fa13bd4
KS
577 kmacro-counter-value-start kmacro-counter
578 kmacro-last-counter kmacro-counter
7dcb2dfd
LT
579 kmacro-counter-format kmacro-default-counter-format
580 kmacro-counter-format-start kmacro-default-counter-format))
1fa13bd4 581
f1180544 582 (start-kbd-macro append
1fa13bd4
KS
583 (and append
584 (if kmacro-execute-before-append
585 (> (car arg) 4)
1b25dccd
KS
586 (= (car arg) 4))))
587 (if (and defining-kbd-macro append)
588 (setq defining-kbd-macro 'append)))))
1fa13bd4
KS
589
590
591;;;###autoload
592(defun kmacro-end-macro (arg)
593 "Finish defining a keyboard macro.
594The definition was started by \\[kmacro-start-macro].
595The macro is now available for use via \\[kmacro-call-macro],
18d1e6c9 596or it can be given a name with \\[kmacro-name-last-macro] and then invoked
1fa13bd4
KS
597under that name.
598
599With numeric arg, repeat macro now that many times,
600counting the definition just completed as the first repetition.
601An argument of zero means repeat until error."
602 (interactive "P")
e9691cca
KS
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))
59f63e9a 608 (setq last-kbd-macro nil)
e9691cca 609 (message "Ignore empty macro")
59f63e9a
MR
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)))))
1fa13bd4
KS
613
614
615;;;###autoload
c8bf445e 616(defun kmacro-call-macro (arg &optional no-repeat end-macro)
1fa13bd4 617 "Call the last keyboard macro that you defined with \\[kmacro-start-macro].
1fa13bd4
KS
618A prefix argument serves as a repeat count. Zero means repeat until error.
619
c8bf445e
KS
620When you call the macro, you can call the macro again by repeating
621just the last key in the key sequence that you used to call this
622command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
480ef9bb 623for details on how to adjust or disable this behavior.
1fa13bd4 624
c8bf445e 625To make a macro permanent so you can call it even after defining
18d1e6c9 626others, use \\[kmacro-name-last-macro]."
c8bf445e 627 (interactive "p")
df96ab1e
SM
628 (let ((repeat-key (and (or (and (null no-repeat)
629 (> (length (this-single-command-keys)) 1))
630 ;; Used when we're in the process of repeating.
631 (eq no-repeat 'repeating))
c8bf445e
KS
632 last-input-event))
633 repeat-key-str)
634 (if end-macro
635 (kmacro-end-macro arg)
636 (call-last-kbd-macro arg #'kmacro-loop-setup-function))
1698f788
MB
637 (when (consp arg)
638 (setq arg (car arg)))
c8bf445e
KS
639 (when (and (or (null arg) (> arg 0))
640 (setq repeat-key
1698f788
MB
641 (if (eq kmacro-call-repeat-key t)
642 repeat-key
643 kmacro-call-repeat-key)))
723d6a64 644 (setq repeat-key-str (format-kbd-macro (vector repeat-key) nil))
df96ab1e
SM
645 ;; Can't use the `keep-pred' arg because this overlay keymap needs to be
646 ;; removed during the next run of the kmacro (i.e. we need to add&remove
647 ;; this overlay-map at each repetition).
648 (set-temporary-overlay-map
649 (let ((map (make-sparse-keymap)))
650 (define-key map (vector repeat-key)
651 `(lambda () (interactive)
652 (kmacro-call-macro ,(and kmacro-call-repeat-with-arg arg)
653 'repeating)))
654 map)))))
1fa13bd4
KS
655
656
657;;; Combined function key bindings:
658
659;;;###autoload
660(defun kmacro-start-macro-or-insert-counter (arg)
c8bf445e
KS
661 "Record subsequent keyboard input, defining a keyboard macro.
662The commands are recorded even as they are executed.
c30c4abe 663
c8bf445e
KS
664Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
665macro.
666
667With \\[universal-argument], appends to current keyboard macro (keeping
668the current value of `kmacro-counter').
669
670When defining/executing macro, inserts macro counter and increments
671the counter with ARG or 1 if missing. With \\[universal-argument],
c796a0b6 672inserts previous `kmacro-counter' (but do not modify counter).
c30c4abe 673
1fa13bd4 674The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
c30c4abe 675The format of the counter can be modified via \\[kmacro-set-format]."
1fa13bd4 676 (interactive "P")
c30c4abe 677 (if (or defining-kbd-macro executing-kbd-macro)
1fa13bd4
KS
678 (kmacro-insert-counter arg)
679 (kmacro-start-macro arg)))
c30c4abe 680
1fa13bd4
KS
681
682;;;###autoload
c8bf445e 683(defun kmacro-end-or-call-macro (arg &optional no-repeat)
c30c4abe 684 "End kbd macro if currently being defined; else call last kbd macro.
7e30f58e 685With numeric prefix ARG, repeat macro that many times.
1fa13bd4
KS
686With \\[universal-argument], call second macro in macro ring."
687 (interactive "P")
f1180544 688 (cond
c30c4abe 689 (defining-kbd-macro
c8bf445e
KS
690 (if kmacro-call-repeat-key
691 (kmacro-call-macro arg no-repeat t)
692 (kmacro-end-macro arg)))
a1839f06
KS
693 ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
694 kmacro-view-last-item)
695 (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
1fa13bd4
KS
696 ((and arg (listp arg))
697 (kmacro-call-ring-2nd 1))
c30c4abe 698 (t
c8bf445e 699 (kmacro-call-macro arg no-repeat))))
1fa13bd4
KS
700
701
c8bf445e
KS
702(defun kmacro-end-or-call-macro-repeat (arg)
703 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
1fa13bd4 704 (interactive "P")
c8bf445e
KS
705 (let ((keys (kmacro-get-repeat-prefix)))
706 (kmacro-end-or-call-macro arg t)
707 (if keys
708 (kmacro-repeat-on-last-key keys))))
1fa13bd4 709
c8bf445e 710(put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
c30c4abe 711
c30c4abe 712
723d6a64
KS
713;;;###autoload
714(defun kmacro-end-and-call-macro (arg &optional no-repeat)
715 "Call last keyboard macro, ending it first if currently being defined.
4231e11f
RS
716With numeric prefix ARG, repeat macro that many times.
717Zero argument means repeat until there is an error.
718
719To give a macro a permanent name, so you can call it
18d1e6c9 720even after defining other macros, use \\[kmacro-name-last-macro]."
723d6a64
KS
721 (interactive "P")
722 (if defining-kbd-macro
723 (kmacro-end-macro nil))
724 (kmacro-call-macro arg no-repeat))
725
726
1fa13bd4 727;;;###autoload
c30c4abe
KS
728(defun kmacro-end-call-mouse (event)
729 "Move point to the position clicked with the mouse and call last kbd macro.
730If kbd macro currently being defined end it before activating it."
731 (interactive "e")
732 (when defining-kbd-macro
1fa13bd4 733 (end-kbd-macro))
c30c4abe 734 (mouse-set-point event)
c8bf445e 735 (kmacro-call-macro nil t))
c30c4abe 736
1fa13bd4
KS
737
738;;; Misc. commands
739
a1839f06
KS
740;; An idea for macro bindings:
741;; Create a separate keymap installed as a minor-mode keymap (e.g. in
742;; the emulation-mode-map-alists) in which macro bindings are made
743;; independent of any other bindings. When first binding is made,
c80e3b4a 744;; the keymap is created, installed, and enabled. Key seq. C-x C-k +
a1839f06
KS
745;; can then be used to toggle the use of this keymap on and off.
746;; This means that it would be safe(r) to bind ordinary keys like
747;; letters and digits, provided that we inhibit the keymap while
748;; executing the macro later on (but that's controversial...)
749
424e6532
KS
750(defun kmacro-lambda-form (mac &optional counter format)
751 "Create lambda form for macro bound to symbol or key."
752 (if counter
753 (setq mac (list mac counter format)))
754 `(lambda (&optional arg)
755 "Keyboard macro."
756 (interactive "p")
757 (kmacro-exec-ring-item ',mac arg)))
758
759(defun kmacro-extract-lambda (mac)
760 "Extract kmacro from a kmacro lambda form."
761 (and (consp mac)
762 (eq (car mac) 'lambda)
763 (setq mac (assoc 'kmacro-exec-ring-item mac))
764 (consp (cdr mac))
765 (consp (car (cdr mac)))
766 (consp (cdr (car (cdr mac))))
767 (setq mac (car (cdr (car (cdr mac)))))
768 (listp mac)
769 (= (length mac) 3)
770 (arrayp (car mac))
771 mac))
772
773
06b60517 774(defun kmacro-bind-to-key (_arg)
a1839f06
KS
775 "When not defining or executing a macro, offer to bind last macro to a key.
776The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
777through [C-x C-k Z] are reserved for user bindings, and to bind to
778one of these sequences, just enter the digit or letter, rather than
779the whole sequence.
780
781You can bind to any valid key sequence, but if you try to bind to
782a key with an existing command binding, you will be asked for
783confirmation whether to replace that binding. Note that the
784binding is made in the `global-map' keymap, so the macro binding
785may be shaded by a local key binding."
c30c4abe
KS
786 (interactive "p")
787 (if (or defining-kbd-macro executing-kbd-macro)
1fa13bd4
KS
788 (if defining-kbd-macro
789 (message "Cannot save macro while defining it."))
790 (unless last-kbd-macro
791 (error "No keyboard macro defined"))
a1839f06
KS
792 (let ((key-seq (read-key-sequence "Bind last macro to key: "))
793 ok cmd)
794 (when (= (length key-seq) 1)
795 (let ((ch (aref key-seq 0)))
be3eb6a6
RS
796 (if (and (integerp ch)
797 (or (and (>= ch ?0) (<= ch ?9))
798 (and (>= ch ?A) (<= ch ?Z))))
a1839f06
KS
799 (setq key-seq (concat "\C-x\C-k" key-seq)
800 ok t))))
801 (when (and (not (equal key-seq "\a"))
802 (or ok
803 (not (setq cmd (key-binding key-seq)))
804 (stringp cmd)
805 (vectorp cmd)
806 (yes-or-no-p (format "%s runs command %S. Bind anyway? "
807 (format-kbd-macro key-seq)
808 cmd))))
18d1e6c9 809 (define-key global-map key-seq
424e6532 810 (kmacro-lambda-form (kmacro-ring-head)))
a1839f06 811 (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
c30c4abe 812
c30c4abe 813
18d1e6c9
KS
814(defun kmacro-name-last-macro (symbol)
815 "Assign a name to the last keyboard macro defined.
816Argument SYMBOL is the name to define.
817The symbol's function definition becomes the keyboard macro string.
818Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
819 (interactive "SName for last kbd macro: ")
820 (or last-kbd-macro
821 (error "No keyboard macro defined"))
822 (and (fboundp symbol)
823 (not (get symbol 'kmacro))
824 (not (stringp (symbol-function symbol)))
825 (not (vectorp (symbol-function symbol)))
826 (error "Function %s is already defined and not a keyboard macro"
827 symbol))
828 (if (string-equal symbol "")
829 (error "No command name given"))
424e6532 830 (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
18d1e6c9
KS
831 (put symbol 'kmacro t))
832
833
06b60517 834(defun kmacro-view-macro (&optional _arg)
a1839f06
KS
835 "Display the last keyboard macro.
836If repeated, it shows previous elements in the macro ring."
1fa13bd4 837 (interactive)
a1839f06
KS
838 (cond
839 ((or (kmacro-ring-empty-p)
840 (not (eq last-command 'kmacro-view-macro)))
841 (setq kmacro-view-last-item nil))
842 ((null kmacro-view-last-item)
843 (setq kmacro-view-last-item kmacro-ring
844 kmacro-view-item-no 2))
845 ((consp kmacro-view-last-item)
846 (setq kmacro-view-last-item (cdr kmacro-view-last-item)
847 kmacro-view-item-no (1+ kmacro-view-item-no)))
848 (t
849 (setq kmacro-view-last-item nil)))
850 (setq this-command 'kmacro-view-macro
851 last-command this-command) ;; in case we repeat
852 (kmacro-display (if kmacro-view-last-item
853 (car (car kmacro-view-last-item))
854 last-kbd-macro)
855 nil
856 (if kmacro-view-last-item
857 (concat (cond ((= kmacro-view-item-no 2) "2nd")
858 ((= kmacro-view-item-no 3) "3nd")
859 (t (format "%dth" kmacro-view-item-no)))
860 " previous macro")
861 "Last macro")))
c30c4abe 862
c8bf445e 863(defun kmacro-view-macro-repeat (&optional arg)
a1839f06
KS
864 "Display the last keyboard macro.
865If repeated, it shows previous elements in the macro ring.
866To execute the displayed macro ring item without changing the macro ring,
867just enter C-k.
868This is like `kmacro-view-macro', but allows repeating macro commands
869without repeating the prefix."
c30c4abe 870 (interactive)
c8bf445e
KS
871 (let ((keys (kmacro-get-repeat-prefix)))
872 (kmacro-view-macro arg)
873 (if (and last-kbd-macro keys)
874 (kmacro-repeat-on-last-key keys))))
1fa13bd4 875
a1839f06 876(put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
1fa13bd4 877
c8bf445e
KS
878
879(defun kmacro-edit-macro-repeat (&optional arg)
1fa13bd4
KS
880 "Edit last keyboard macro."
881 (interactive "P")
882 (edit-kbd-macro "\r" arg))
883
c8bf445e 884(put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
1fa13bd4
KS
885
886
c8bf445e 887(defun kmacro-edit-macro (&optional arg)
1fa13bd4
KS
888 "As edit last keyboard macro, but without kmacro-repeat property."
889 (interactive "P")
c8bf445e 890 (edit-kbd-macro "\r" arg))
1fa13bd4
KS
891
892
893(defun kmacro-edit-lossage ()
6b8d1c72 894 "Edit most recent 300 keystrokes as a keyboard macro."
1fa13bd4
KS
895 (interactive)
896 (kmacro-push-ring)
897 (edit-kbd-macro "\C-hl"))
c30c4abe 898
c30c4abe 899
723d6a64
KS
900;;; Single-step editing of keyboard macros
901
902(defvar kmacro-step-edit-active) ;; step-editing active
903(defvar kmacro-step-edit-new-macro) ;; storage for new macro
904(defvar kmacro-step-edit-inserting) ;; inserting into macro
905(defvar kmacro-step-edit-appending) ;; append to end of macro
906(defvar kmacro-step-edit-replace) ;; replace orig macro when done
907(defvar kmacro-step-edit-prefix-index) ;; index of first prefix arg key
908(defvar kmacro-step-edit-key-index) ;; index of current key
909(defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
910(defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
911(defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
912
b016851c
SM
913(defvar kmacro-step-edit-map
914 (let ((map (make-sparse-keymap)))
915 ;; query-replace-map answers include: `act', `skip', `act-and-show',
916 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
917 ;; `automatic', `backup', `exit-prefix', and `help'.")
918 ;; Also: `quit', `edit-replacement'
919
920 (set-keymap-parent map query-replace-map)
921
922 (define-key map "\t" 'act-repeat)
923 (define-key map [tab] 'act-repeat)
924 (define-key map "\C-k" 'skip-rest)
925 (define-key map "c" 'automatic)
926 (define-key map "f" 'skip-keep)
927 (define-key map "q" 'quit)
928 (define-key map "d" 'skip)
929 (define-key map "\C-d" 'skip)
930 (define-key map "i" 'insert)
931 (define-key map "I" 'insert-1)
932 (define-key map "r" 'replace)
933 (define-key map "R" 'replace-1)
934 (define-key map "a" 'append)
935 (define-key map "A" 'append-end)
936 map)
723d6a64
KS
937 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
938This keymap is an extension to the `query-replace-map', allowing the
1c59ca78
KS
939following additional answers: `insert', `insert-1', `replace', `replace-1',
940`append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
723d6a64 941
723d6a64
KS
942(defvar kmacro-step-edit-prefix-commands
943 '(universal-argument universal-argument-more universal-argument-minus
944 digit-argument negative-argument)
c796a0b6 945 "Commands which build up a prefix arg for the current command.")
723d6a64
KS
946
947(defun kmacro-step-edit-prompt (macro index)
948 ;; Show step-edit prompt
949 (let ((keys (and (not kmacro-step-edit-appending)
1bf2f306 950 index (substring macro index executing-kbd-macro-index)))
723d6a64 951 (future (and (not kmacro-step-edit-appending)
1bf2f306 952 (substring macro executing-kbd-macro-index)))
723d6a64
KS
953 (message-log-max nil)
954 (curmsg (current-message)))
955
956 ;; TODO: Scroll macro if max-mini-window-height is too small.
2b4b4feb
KS
957 (message "%s"
958 (concat
723d6a64
KS
959 (format "Macro: %s%s%s%s%s\n"
960 (format-kbd-macro kmacro-step-edit-new-macro 1)
961 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
f1180544 962 (propertize (if keys (format-kbd-macro keys)
723d6a64
KS
963 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
964 (if future " " "")
965 (if future (format-kbd-macro future) ""))
966 (cond
967 ((minibufferp)
968 (format "%s\n%s\n"
969 (propertize "\
970 minibuffer " 'face 'header-line)
971 (buffer-substring (point-min) (point-max))))
972 (curmsg
973 (format "%s\n%s\n"
974 (propertize "\
975 echo area " 'face 'header-line)
976 curmsg))
977 (t ""))
978 (if keys
f1180544 979 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
723d6a64
KS
980 (propertize "\
981--------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
982 (if kmacro-step-edit-help "\
983 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
984 TAB: execute while same, ?: toggle help
482b44d8
KS
985 Edit: i: insert, r: replace, a: append, A: append at end,
986 I/R: insert/replace with one sequence,
723d6a64
KS
987 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
988----------------------------------------------------------------
989" "")
990 (propertize "Next command:" 'face 'bold)
991 this-command)
f1180544 992 (propertize
723d6a64
KS
993 (format "Type key sequence%s to insert and execute%s: "
994 (if (numberp kmacro-step-edit-inserting) "" "s")
1c59ca78 995 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
723d6a64
KS
996 'face 'bold))))))
997
998(defun kmacro-step-edit-query ()
999 ;; Pre-command hook function for step-edit in "command" mode
1000 (let ((resize-mini-windows t)
1001 (max-mini-window-height kmacro-step-edit-mini-window-height)
1002 act restore-index next-index)
1003
1004 ;; Handle commands which reads additional input using read-char.
1005 (cond
1006 ((and (eq this-command 'quoted-insert)
1007 (not (eq kmacro-step-edit-action t)))
1008 ;; Find the actual end of this key sequence.
1009 ;; Must be able to backtrack in case we actually execute it.
1bf2f306 1010 (setq restore-index executing-kbd-macro-index)
723d6a64
KS
1011 (let (unread-command-events)
1012 (quoted-insert 0)
1013 (when unread-command-events
1bf2f306
KS
1014 (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1015 next-index executing-kbd-macro-index)))))
723d6a64 1016
4c36be58 1017 ;; Query the user; stop macro execution temporarily.
723d6a64
KS
1018 (let ((macro executing-kbd-macro)
1019 (executing-kbd-macro nil)
1020 (defining-kbd-macro nil))
1021
1022 ;; Any action requested by previous command
1023 (cond
1024 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
1025 (cond
1026 ((eq this-command 'quoted-insert)
1027 (clear-this-command-keys) ;; recent-keys actually
1028 (let (unread-command-events)
1029 (quoted-insert (prefix-numeric-value current-prefix-arg))
f1180544 1030 (setq kmacro-step-edit-new-macro
723d6a64
KS
1031 (vconcat kmacro-step-edit-new-macro (recent-keys)))
1032 (when unread-command-events
1033 (setq kmacro-step-edit-new-macro
1034 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
1bf2f306 1035 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
723d6a64
KS
1036 (setq current-prefix-arg nil
1037 prefix-arg nil)
1038 (setq act 'ignore))
1039 (t
1040 (setq act 'act)))
1041 (setq kmacro-step-edit-action nil))
1042 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
1043 (setq act 'act))
1044 (t
1045 (setq kmacro-step-edit-action nil)))
1046
1047 ;; Handle prefix arg, or query user
1048 (cond
1049 (act act) ;; set above
1050 ((memq this-command kmacro-step-edit-prefix-commands)
1051 (unless kmacro-step-edit-prefix-index
1052 (setq kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1053 (setq act 'universal-argument))
1054 ((eq this-command 'universal-argument-other-key)
1055 (setq act 'universal-argument))
1056 (t
1057 (kmacro-step-edit-prompt macro (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
1058 (setq act (lookup-key kmacro-step-edit-map
1059 (vector (with-current-buffer (current-buffer) (read-event))))))))
1060
1061 ;; Resume macro execution and perform the action
1062 (cond
1063 ((eq act 'universal-argument)
1064 nil)
1065 ((cond
1066 ((eq act 'act)
1067 t)
1068 ((eq act 'act-repeat)
1069 (setq kmacro-step-edit-action this-command)
1070 t)
1071 ((eq act 'quit)
1072 (setq kmacro-step-edit-replace nil)
1073 (setq kmacro-step-edit-active 'ignore)
1074 nil)
1075 ((eq act 'skip)
1076 (setq kmacro-step-edit-prefix-index nil)
1077 nil)
1078 ((eq act 'skip-keep)
1079 (setq this-command 'ignore)
1080 t)
1081 ((eq act 'skip-rest)
1082 (setq kmacro-step-edit-active 'ignore)
1083 nil)
5190c95e 1084 ((memq act '(automatic exit))
723d6a64
KS
1085 (setq kmacro-step-edit-active nil)
1086 (setq act t)
1087 t)
1088 ((member act '(insert-1 insert))
1bf2f306 1089 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
723d6a64
KS
1090 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
1091 nil)
1092 ((member act '(replace-1 replace))
1093 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
1094 (setq kmacro-step-edit-prefix-index nil)
1bf2f306 1095 (if (= executing-kbd-macro-index (length executing-kbd-macro))
723d6a64
KS
1096 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1097 kmacro-step-edit-appending t))
1098 nil)
1c59ca78
KS
1099 ((eq act 'append)
1100 (setq kmacro-step-edit-inserting t)
1bf2f306 1101 (if (= executing-kbd-macro-index (length executing-kbd-macro))
723d6a64
KS
1102 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1103 kmacro-step-edit-appending t))
1104 t)
1c59ca78 1105 ((eq act 'append-end)
1bf2f306 1106 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1c59ca78
KS
1107 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1108 kmacro-step-edit-inserting t
1109 kmacro-step-edit-appending t)
1110 (setq kmacro-step-edit-active 'append-end))
1111 (setq act t)
1112 t)
723d6a64 1113 ((eq act 'help)
1bf2f306 1114 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
723d6a64
KS
1115 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
1116 nil)
1117 (t ;; Ignore unknown responses
1bf2f306 1118 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
723d6a64 1119 nil))
1bf2f306 1120 (if (> executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
723d6a64
KS
1121 (setq kmacro-step-edit-new-macro
1122 (vconcat kmacro-step-edit-new-macro
f1180544 1123 (substring executing-kbd-macro
723d6a64 1124 (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
1bf2f306 1125 (if (eq act t) nil executing-kbd-macro-index)))
723d6a64
KS
1126 kmacro-step-edit-prefix-index nil))
1127 (if restore-index
1bf2f306 1128 (setq executing-kbd-macro-index restore-index)))
723d6a64
KS
1129 (t
1130 (setq this-command 'ignore)))
1131 (setq kmacro-step-edit-key-index next-index)))
1132
1133(defun kmacro-step-edit-insert ()
1134 ;; Pre-command hook function for step-edit in "insert" mode
1135 (let ((resize-mini-windows t)
1136 (max-mini-window-height kmacro-step-edit-mini-window-height)
1137 (macro executing-kbd-macro)
1138 (executing-kbd-macro nil)
1139 (defining-kbd-macro nil)
1140 cmd keys next-index)
1bf2f306 1141 (setq executing-kbd-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
723d6a64
KS
1142 kmacro-step-edit-prefix-index nil)
1143 (kmacro-step-edit-prompt macro nil)
1144 ;; Now, we have read a key sequence from the macro, but we don't want
1145 ;; to execute it yet. So push it back and read another sequence.
1146 (reset-this-command-lengths)
1147 (setq keys (read-key-sequence nil nil nil nil t))
1148 (setq cmd (key-binding keys t nil))
1149 (if (cond
1150 ((null cmd)
1151 t)
1152 ((eq cmd 'quoted-insert)
1153 (clear-this-command-keys) ;; recent-keys actually
1154 (quoted-insert (prefix-numeric-value current-prefix-arg))
1155 (setq current-prefix-arg nil
1156 prefix-arg nil)
1157 (setq keys (vconcat keys (recent-keys)))
1158 (when (numberp kmacro-step-edit-inserting)
1159 (setq kmacro-step-edit-inserting nil)
1160 (when unread-command-events
1161 (setq keys (substring keys 0 (- (length unread-command-events)))
1bf2f306
KS
1162 executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
1163 next-index executing-kbd-macro-index
723d6a64
KS
1164 unread-command-events nil)))
1165 (setq cmd 'ignore)
1166 nil)
1167 ((memq cmd kmacro-step-edit-prefix-commands)
1168 (setq universal-argument-num-events 0)
1169 (reset-this-command-lengths)
1170 nil)
1171 ((eq cmd 'universal-argument-other-key)
1172 (setq kmacro-step-edit-action t)
1173 (setq universal-argument-num-events 0)
1174 (reset-this-command-lengths)
1175 (if (numberp kmacro-step-edit-inserting)
1176 (setq kmacro-step-edit-inserting nil))
1177 nil)
1178 ((numberp kmacro-step-edit-inserting)
1179 (setq kmacro-step-edit-inserting nil)
1180 nil)
1181 ((equal keys "\C-j")
1182 (setq kmacro-step-edit-inserting nil)
1183 (setq kmacro-step-edit-action nil)
1184 ;; Forget any (partial) prefix arg from next command
1185 (setq kmacro-step-edit-prefix-index nil)
1186 (reset-this-command-lengths)
1187 (setq overriding-terminal-local-map nil)
1188 (setq universal-argument-num-events nil)
1189 (setq next-index kmacro-step-edit-key-index)
1190 t)
1191 (t nil))
1192 (setq this-command 'ignore)
1193 (setq this-command cmd)
1194 (if (memq this-command '(self-insert-command digit-argument))
8989a920 1195 (setq last-command-event (aref keys (1- (length keys)))))
723d6a64
KS
1196 (if keys
1197 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1198 (setq kmacro-step-edit-key-index next-index)))
1199
1200(defun kmacro-step-edit-pre-command ()
1201 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1202 (when kmacro-step-edit-active
1203 (cond
1204 ((eq kmacro-step-edit-active 'ignore)
1205 (setq this-command 'ignore))
1c59ca78 1206 ((eq kmacro-step-edit-active 'append-end)
1bf2f306 1207 (if (= executing-kbd-macro-index (length executing-kbd-macro))
1c59ca78
KS
1208 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1209 kmacro-step-edit-inserting t
1210 kmacro-step-edit-appending t
1211 kmacro-step-edit-active t)))
723d6a64
KS
1212 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1213 (if kmacro-step-edit-inserting
1214 (kmacro-step-edit-insert)
1215 (kmacro-step-edit-query))
1216 (setq kmacro-step-edit-num-input-keys num-input-keys)
1217 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1218 (setq kmacro-step-edit-appending nil
1219 kmacro-step-edit-active 'ignore)))))
1220 (when (eq kmacro-step-edit-active t)
1221 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1222
1223(defun kmacro-step-edit-minibuf-setup ()
1224 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1225 (when kmacro-step-edit-active
1226 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1227
1228(defun kmacro-step-edit-post-command ()
1229 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1230 (when kmacro-step-edit-active
1231 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1232 (if kmacro-step-edit-key-index
1bf2f306
KS
1233 (setq executing-kbd-macro-index kmacro-step-edit-key-index)
1234 (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
723d6a64
KS
1235
1236
1237(defun kmacro-step-edit-macro ()
1238 "Step edit and execute last keyboard macro.
1239
1240To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1241 (interactive)
1242 (let ((kmacro-step-edit-active t)
1243 (kmacro-step-edit-new-macro "")
1244 (kmacro-step-edit-inserting nil)
1245 (kmacro-step-edit-appending nil)
1246 (kmacro-step-edit-replace t)
1247 (kmacro-step-edit-prefix-index nil)
1248 (kmacro-step-edit-key-index 0)
1249 (kmacro-step-edit-action nil)
1250 (kmacro-step-edit-help nil)
1251 (kmacro-step-edit-num-input-keys num-input-keys)
1252 (pre-command-hook pre-command-hook)
1253 (post-command-hook post-command-hook)
1254 (minibuffer-setup-hook minibuffer-setup-hook))
1255 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1256 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1257 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1258 (call-last-kbd-macro nil nil)
5190c95e
KS
1259 (when (and kmacro-step-edit-replace
1260 kmacro-step-edit-new-macro
1261 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1262 (kmacro-push-ring)
1263 (setq last-kbd-macro kmacro-step-edit-new-macro))))
723d6a64 1264
7e30f58e 1265(provide 'kmacro)
ab5796a9 1266
7e30f58e 1267;;; kmacro.el ends here