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