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