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