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