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