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