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