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