Trailing whitespace deleted.
[bpt/emacs.git] / lisp / kmacro.el
1 ;;; kmacro.el --- enhanced keyboard macros
2
3 ;; Copyright (C) 2002 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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; The kmacro package is an alternative user interface to emacs'
28 ;; keyboard macro functionality. This functionality is normally bound
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 :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
124 :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
125
126 (defcustom kmacro-call-mouse-event 'S-mouse-3
127 "The mouse event used by kmacro to call a macro.
128 Set to nil if no mouse binding is desired."
129 :type 'symbol
130 :group 'kmacro)
131
132 (defcustom kmacro-ring-max 8
133 "Maximum number of keyboard macros to save in macro ring."
134 :type 'integer
135 :group 'kmacro)
136
137
138 (defcustom kmacro-execute-before-append t
139 "Controls whether appending to a macro starts by executing the macro.
140 If non-nil, using a single \\[universal-argument] prefix executes the macro
141 before appending, while more than one \\[universal-argument] prefix does not
142 execute the macro.
143 Otherwise, a single \\[universal-argument] prefix does not execute the
144 macro, while more than one \\[universal-argument] prefix causes the
145 macro to be executed before appending to it."
146 :type 'boolean
147 :group 'kmacro)
148
149
150 (defcustom kmacro-repeat-no-prefix t
151 "Allow repeating certain macro commands without entering the C-x C-k prefix."
152 :type 'boolean
153 :group 'kmacro)
154
155 (defcustom kmacro-call-repeat-key t
156 "Allow repeating macro call using last key or a specific key."
157 :type '(choice (const :tag "Disabled" nil)
158 (const :tag "Last key" t)
159 (character :tag "Character" :value ?e)
160 (symbol :tag "Key symbol" :value RET))
161 :group 'kmacro)
162
163 (defcustom kmacro-call-repeat-with-arg nil
164 "Repeat macro call with original arg when non-nil; repeat once if nil."
165 :type 'boolean
166 :group 'kmacro)
167
168 (defcustom kmacro-step-edit-mini-window-height 0.75
169 "Override `max-mini-window-height' when step edit keyboard macro."
170 :type 'number
171 :group 'kmacro)
172
173 ;; Keymap
174
175 (defvar kmacro-keymap
176 (let ((map (make-sparse-keymap)))
177 (define-key map "s" 'kmacro-start-macro)
178 (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
179 (define-key map "\C-e" 'kmacro-edit-macro-repeat)
180 (define-key map "\r" 'kmacro-edit-macro)
181 (define-key map " " 'kmacro-step-edit-macro)
182 (define-key map "l" 'kmacro-edit-lossage)
183 (define-key map "\C-i" 'kmacro-insert-counter)
184 (define-key map "\C-a" 'kmacro-add-counter)
185 (define-key map "\C-v" 'kmacro-view-macro-repeat)
186 (define-key map "\C-l" 'kmacro-call-ring-2nd-repeat)
187 (define-key map "\C-r" 'kmacro-view-ring-2nd)
188 (define-key map "\C-n" 'kmacro-cycle-ring-next)
189 (define-key map "\C-p" 'kmacro-cycle-ring-previous)
190 (define-key map "\C-f" 'kmacro-set-format)
191 (define-key map "\C-c" 'kmacro-set-counter)
192 (define-key map "\C-t" 'kmacro-swap-ring)
193 (define-key map "b" 'kmacro-bind-to-key)
194 (define-key map "\C-d" 'kmacro-delete-ring-head)
195 ;; Compatibility bindings
196 (define-key map "q" 'kbd-macro-query)
197 (define-key map "n" 'name-last-kbd-macro)
198 (define-key map "e" 'edit-kbd-macro)
199 (define-key map "r" 'apply-macro-to-region-lines)
200 map)
201 "Keymap for keyboard macro commands.")
202 (defalias 'kmacro-keymap kmacro-keymap)
203
204 ;;; Provide some binding for startup:
205 ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
206 ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
207 ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
208 ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
209 ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
210 ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
211 ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
212
213 (if kmacro-call-mouse-event
214 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
215
216
217
218 ;;; Keyboard macro counter
219
220 (defvar kmacro-counter 0
221 "*Current keyboard macro counter.")
222
223 (defvar kmacro-counter-format "%d"
224 "*Current keyboard macro counter format.")
225
226 (defvar kmacro-counter-format-start kmacro-counter-format
227 "Macro format at start of macro execution.")
228
229 (defvar kmacro-counter-value-start kmacro-counter
230 "Macro counter at start of macro execution.")
231
232 (defvar kmacro-last-counter 0 "Last counter inserted by key macro.")
233
234
235 (defun kmacro-insert-counter (arg)
236 "Insert macro counter and increment with ARG or 1 if missing.
237 With \\[universal-argument], insert previous kmacro-counter (but do not modify counter)."
238 (interactive "P")
239 (if (and arg (listp arg))
240 (insert (format kmacro-counter-format kmacro-last-counter))
241 (insert (format kmacro-counter-format kmacro-counter))
242 (kmacro-add-counter (prefix-numeric-value arg))))
243
244
245 (defun kmacro-set-format (format)
246 "Set macro counter FORMAT."
247 (interactive "sMacro Counter Format (printf format): ")
248 (setq kmacro-counter-format
249 (if (equal format "") "%d" format))
250 ;; redefine initial macro counter if we are not executing a macro.
251 (if (not (or defining-kbd-macro executing-kbd-macro))
252 (setq kmacro-counter-format-start kmacro-counter-format)))
253
254
255 (defun kmacro-display-counter (&optional value)
256 "Display current counter value."
257 (unless value (setq value kmacro-counter))
258 (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value))
259
260
261 (defun kmacro-set-counter (arg)
262 "Set kmacro-counter to ARG or prompt if missing.
263 With \\[universal-argument], reset counter to its value prior to this iteration of the macro."
264 (interactive "NMacro counter value: ")
265 (setq kmacro-last-counter kmacro-counter
266 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
267 kmacro-counter-value-start
268 arg))
269 (unless executing-kbd-macro
270 (kmacro-display-counter)))
271
272
273 (defun kmacro-add-counter (arg)
274 "Add numeric prefix arg (prompt if missing) to macro counter.
275 With \\[universal-argument], restore previous counter value."
276 (interactive "NAdd to macro counter: ")
277 (let ((last kmacro-last-counter))
278 (setq kmacro-last-counter kmacro-counter
279 kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
280 last
281 kmacro-counter (+ kmacro-counter arg))))
282 (unless executing-kbd-macro
283 (kmacro-display-counter)))
284
285
286 (defun kmacro-loop-setup-function ()
287 "Function called prior to each iteration of macro."
288 ;; Restore macro counter format to initial format, so it is ok to change
289 ;; counter format in the macro without restoring it.
290 (setq kmacro-counter-format kmacro-counter-format-start)
291 ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
292 (setq kmacro-counter-value-start kmacro-counter)
293 ;; Return non-nil to continue execution.
294 t)
295
296
297 ;;; Keyboard macro ring
298
299 (defvar kmacro-ring nil
300 "The keyboard macro ring.
301 Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
302 the macro ring (when defining or executing) is not stored in the ring;
303 instead it is available in the variables `last-kbd-macro', `kmacro-counter',
304 and `kmacro-counter-format'.")
305
306
307 (defun kmacro-ring-head ()
308 "Return pseudo head element in macro ring."
309 (and last-kbd-macro
310 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
311
312
313 (defun kmacro-push-ring (&optional elt)
314 "Push ELT or current macro onto `kmacro-ring'."
315 (when (setq elt (or elt (kmacro-ring-head)))
316 (let ((len (length kmacro-ring)))
317 (setq kmacro-ring (cons elt kmacro-ring))
318 (if (>= len kmacro-ring-max)
319 (setcdr (nthcdr len kmacro-ring) nil)))))
320
321
322 (defun kmacro-split-ring-element (elt)
323 (setq last-kbd-macro (car elt)
324 kmacro-counter (nth 1 elt)
325 kmacro-counter-format-start (nth 2 elt)))
326
327
328 (defun kmacro-pop-ring1 (&optional raw)
329 "Pop head element off macro ring (no check).
330 Non-nil arg RAW means just return raw first element."
331 (prog1 (car kmacro-ring)
332 (unless raw
333 (kmacro-split-ring-element (car kmacro-ring)))
334 (setq kmacro-ring (cdr kmacro-ring))))
335
336
337 (defun kmacro-pop-ring (&optional raw)
338 "Pop head element off macro ring.
339 Non-nil arg RAW means just return raw first element."
340 (unless (kmacro-ring-empty-p)
341 (kmacro-pop-ring1 raw)))
342
343
344 (defun kmacro-ring-length ()
345 "Return length of macro ring, including pseudo head."
346 (+ (if last-kbd-macro 1 0) (length kmacro-ring)))
347
348
349 (defun kmacro-ring-empty-p (&optional none)
350 "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
351 Check only `last-kbd-macro' if optional arg NONE is non-nil."
352 (while (and (null last-kbd-macro) kmacro-ring)
353 (kmacro-pop-ring1))
354 (cond
355 ((null last-kbd-macro)
356 (message "No keyboard macro defined.")
357 t)
358 ((and (null none) (null kmacro-ring))
359 (message "Only one keyboard macro defined.")
360 t)
361 (t nil)))
362
363
364 (defun kmacro-display (macro &optional trunc descr empty )
365 "Display a keyboard MACRO."
366 (if macro
367 (let* ((x 60)
368 (m (format-kbd-macro macro))
369 (l (length m))
370 (z (and nil trunc (> l x))))
371 (message (format "%s: %s%s" (or descr "Macro")
372 (if z (substring m 0 (1- x)) m) (if z "..." ""))))
373 (message (or empty "No keyboard macros defined"))))
374
375
376 (defun kmacro-repeat-on-last-key (keys)
377 "Process kmacro commands keys immidiately after cycling the ring."
378 (setq keys (vconcat keys))
379 (let ((n (1- (length keys)))
380 cmd done repeat)
381 (while (and last-kbd-macro
382 (not done)
383 (aset keys n (read-event))
384 (setq cmd (key-binding keys t))
385 (setq repeat (get cmd 'kmacro-repeat)))
386 (clear-this-command-keys t)
387 (cond
388 ((eq repeat 'ring)
389 (if kmacro-ring
390 (let ((kmacro-repeat-no-prefix nil))
391 (funcall cmd nil))
392 (kmacro-display last-kbd-macro t)))
393 ((eq repeat 'head)
394 (let ((kmacro-repeat-no-prefix nil))
395 (funcall cmd nil)))
396 ((eq repeat 'stop)
397 (funcall cmd nil)
398 (setq done t)))
399 (setq last-input-event nil)))
400 (when last-input-event
401 (clear-this-command-keys t)
402 (setq unread-command-events (list last-input-event))))
403
404
405 (defun kmacro-get-repeat-prefix ()
406 (let (keys)
407 (and kmacro-repeat-no-prefix
408 (setq keys (this-single-command-keys))
409 (> (length keys) 1)
410 keys)))
411
412
413 (defun kmacro-call-ring-2nd (arg)
414 "Execute second keyboard macro at in macro ring."
415 (interactive "P")
416 (unless (kmacro-ring-empty-p)
417 ;; should use counter format specific to the macro on the ring!
418 (let ((kmacro-counter (nth 1 (car kmacro-ring)))
419 (kmacro-counter-format-start (nth 2 (car kmacro-ring))))
420 (execute-kbd-macro (car (car kmacro-ring)) arg #'kmacro-loop-setup-function)
421 (setcar (cdr (car kmacro-ring)) kmacro-counter))))
422
423
424 (defun kmacro-call-ring-2nd-repeat (arg)
425 "Like `kmacro-call-ring-2nd', but allow repeat without repeating prefix."
426 (interactive "P")
427 (let ((keys (kmacro-get-repeat-prefix)))
428 (kmacro-call-ring-2nd arg)
429 (if (and kmacro-ring keys)
430 (kmacro-repeat-on-last-key keys))))
431
432 (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
433
434
435 (defun kmacro-view-ring-2nd ()
436 "Display the current head of the keyboard macro ring."
437 (interactive)
438 (unless (kmacro-ring-empty-p)
439 (kmacro-display (car (car kmacro-ring)) "2nd macro")))
440
441
442
443 (defun kmacro-cycle-ring-next (&optional arg)
444 "Move to next keyboard macro in keyboard macro ring.
445 Displays the selected macro in the echo area."
446 (interactive)
447 (unless (kmacro-ring-empty-p)
448 (kmacro-push-ring)
449 (let* ((keys (kmacro-get-repeat-prefix))
450 (len (length kmacro-ring))
451 (tail (nthcdr (- len 2) kmacro-ring))
452 (elt (car (cdr tail))))
453 (setcdr tail nil)
454 (kmacro-split-ring-element elt)
455 (kmacro-display last-kbd-macro t)
456 (if keys
457 (kmacro-repeat-on-last-key keys)))))
458
459 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
460
461
462 (defun kmacro-cycle-ring-previous (&optional arg)
463 "Move to previous keyboard macro in keyboard macro ring.
464 Displays the selected macro in the echo area."
465 (interactive)
466 (unless (kmacro-ring-empty-p)
467 (let ((keys (kmacro-get-repeat-prefix))
468 (cur (kmacro-ring-head)))
469 (kmacro-pop-ring1)
470 (if kmacro-ring
471 (nconc kmacro-ring (list cur))
472 (setq kmacro-ring (list cur)))
473 (kmacro-display last-kbd-macro t)
474 (if keys
475 (kmacro-repeat-on-last-key keys)))))
476
477 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
478
479
480 (defun kmacro-swap-ring ()
481 "Swap first two elements on keyboard macro ring."
482 (interactive)
483 (unless (kmacro-ring-empty-p)
484 (let ((cur (kmacro-ring-head)))
485 (kmacro-pop-ring1)
486 (kmacro-push-ring cur))
487 (kmacro-display last-kbd-macro t)))
488
489
490 (defun kmacro-delete-ring-head (&optional arg)
491 "Delete current macro from keyboard macro ring."
492 (interactive)
493 (unless (kmacro-ring-empty-p t)
494 (if (null kmacro-ring)
495 (setq last-kbd-macro nil)
496 (kmacro-pop-ring))
497 (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
498
499 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
500
501 ;;; Traditional bindings:
502
503
504 ;;;###autoload
505 (defun kmacro-start-macro (arg)
506 "Record subsequent keyboard input, defining a keyboard macro.
507 The commands are recorded even as they are executed.
508 Use \\[kmacro-end-macro] to finish recording and make the macro available.
509 Use \\[kmacro-end-and-call-macro] to execute the macro.
510 Use \\[name-last-kbd-macro] to give it a permanent name.
511 Non-nil arg (prefix arg) means append to last macro defined;
512
513 With \\[universal-argument] prefix, append to last keyboard macro
514 defined. Depending on `kmacro-execute-before-append', this may begin
515 by re-executing the last macro as if you typed it again.
516
517 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
518 defining the macro.
519
520 Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
521 The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
522 The format of the counter can be modified via \\[kmacro-set-format]."
523 (interactive "P")
524 (if (or defining-kbd-macro executing-kbd-macro)
525 (message "Already defining keyboard macro.")
526 (let ((append (and arg (listp arg))))
527 (unless append
528 (if last-kbd-macro
529 (let ((len (length kmacro-ring)))
530 (setq kmacro-ring
531 (cons
532 (list last-kbd-macro kmacro-counter kmacro-counter-format-start)
533 kmacro-ring))
534 (if (>= len kmacro-ring-max)
535 (setcdr (nthcdr len kmacro-ring) nil))))
536 (setq kmacro-counter (if arg (prefix-numeric-value arg) 0)
537 kmacro-counter-value-start kmacro-counter
538 kmacro-last-counter kmacro-counter
539 kmacro-counter-format-start kmacro-counter-format))
540
541 (start-kbd-macro append
542 (and append
543 (if kmacro-execute-before-append
544 (> (car arg) 4)
545 (= (car arg) 4)))))))
546
547
548 ;;;###autoload
549 (defun kmacro-end-macro (arg)
550 "Finish defining a keyboard macro.
551 The definition was started by \\[kmacro-start-macro].
552 The macro is now available for use via \\[kmacro-call-macro],
553 or it can be given a name with \\[name-last-kbd-macro] and then invoked
554 under that name.
555
556 With numeric arg, repeat macro now that many times,
557 counting the definition just completed as the first repetition.
558 An argument of zero means repeat until error."
559 (interactive "P")
560 (end-kbd-macro arg #'kmacro-loop-setup-function)
561 (when (and last-kbd-macro (= (length last-kbd-macro) 0))
562 (message "Ignore empty macro")
563 (kmacro-pop-ring)))
564
565
566 ;;;###autoload
567 (defun kmacro-call-macro (arg &optional no-repeat end-macro)
568 "Call the last keyboard macro that you defined with \\[kmacro-start-macro].
569 A prefix argument serves as a repeat count. Zero means repeat until error.
570
571 When you call the macro, you can call the macro again by repeating
572 just the last key in the key sequence that you used to call this
573 command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
574 for details on how to adjust or disable this behaviour.
575
576 To make a macro permanent so you can call it even after defining
577 others, use M-x name-last-kbd-macro."
578 (interactive "p")
579 (let ((repeat-key (and (null no-repeat)
580 (> (length (this-single-command-keys)) 1)
581 last-input-event))
582 repeat-key-str)
583 (if end-macro
584 (kmacro-end-macro arg)
585 (call-last-kbd-macro arg #'kmacro-loop-setup-function))
586 (when (consp arg)
587 (setq arg (car arg)))
588 (when (and (or (null arg) (> arg 0))
589 (setq repeat-key
590 (if (eq kmacro-call-repeat-key t)
591 repeat-key
592 kmacro-call-repeat-key)))
593 (setq repeat-key-str (format-kbd-macro (vector repeat-key) nil))
594 (while repeat-key
595 (message "(Type %s to repeat macro%s)"
596 repeat-key-str
597 (if (and kmacro-call-repeat-with-arg
598 arg (> arg 1))
599 (format " %d times" arg) ""))
600 (if (equal repeat-key (read-event))
601 (progn
602 (clear-this-command-keys t)
603 (call-last-kbd-macro (and kmacro-call-repeat-with-arg arg)
604 #'kmacro-loop-setup-function)
605 (setq last-input-event nil))
606 (setq repeat-key nil)))
607 (when last-input-event
608 (clear-this-command-keys t)
609 (setq unread-command-events (list last-input-event))))))
610
611
612 ;;; Combined function key bindings:
613
614 ;;;###autoload
615 (defun kmacro-start-macro-or-insert-counter (arg)
616 "Record subsequent keyboard input, defining a keyboard macro.
617 The commands are recorded even as they are executed.
618
619 Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
620 macro.
621
622 With \\[universal-argument], appends to current keyboard macro (keeping
623 the current value of `kmacro-counter').
624
625 When defining/executing macro, inserts macro counter and increments
626 the counter with ARG or 1 if missing. With \\[universal-argument],
627 inserts previous kmacro-counter (but do not modify counter).
628
629 The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
630 The format of the counter can be modified via \\[kmacro-set-format]."
631 (interactive "P")
632 (if (or defining-kbd-macro executing-kbd-macro)
633 (kmacro-insert-counter arg)
634 (kmacro-start-macro arg)))
635
636
637 ;;;###autoload
638 (defun kmacro-end-or-call-macro (arg &optional no-repeat)
639 "End kbd macro if currently being defined; else call last kbd macro.
640 With numeric prefix ARG, repeat macro that many times.
641 With \\[universal-argument], call second macro in macro ring."
642 (interactive "P")
643 (cond
644 (defining-kbd-macro
645 (if kmacro-call-repeat-key
646 (kmacro-call-macro arg no-repeat t)
647 (kmacro-end-macro arg)))
648 ((and arg (listp arg))
649 (kmacro-call-ring-2nd 1))
650 (t
651 (kmacro-call-macro arg no-repeat))))
652
653
654 (defun kmacro-end-or-call-macro-repeat (arg)
655 "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
656 (interactive "P")
657 (let ((keys (kmacro-get-repeat-prefix)))
658 (kmacro-end-or-call-macro arg t)
659 (if keys
660 (kmacro-repeat-on-last-key keys))))
661
662 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
663
664
665 ;;;###autoload
666 (defun kmacro-end-and-call-macro (arg &optional no-repeat)
667 "Call last keyboard macro, ending it first if currently being defined.
668 With numeric prefix ARG, repeat macro that many times."
669 (interactive "P")
670 (if defining-kbd-macro
671 (kmacro-end-macro nil))
672 (kmacro-call-macro arg no-repeat))
673
674
675 ;;;###autoload
676 (defun kmacro-end-call-mouse (event)
677 "Move point to the position clicked with the mouse and call last kbd macro.
678 If kbd macro currently being defined end it before activating it."
679 (interactive "e")
680 (when defining-kbd-macro
681 (end-kbd-macro))
682 (mouse-set-point event)
683 (kmacro-call-macro nil t))
684
685
686 ;;; Misc. commands
687
688 (defun kmacro-bind-to-key (arg)
689 "When not defining or executing a macro, offer to bind last macro to a key."
690 (interactive "p")
691 (if (or defining-kbd-macro executing-kbd-macro)
692 (if defining-kbd-macro
693 (message "Cannot save macro while defining it."))
694 (unless last-kbd-macro
695 (error "No keyboard macro defined"))
696 (let ((key-seq (read-key-sequence "Bind last macro to key: ")))
697 (unless (equal key-seq "\a")
698 (define-key global-map key-seq last-kbd-macro)))))
699
700
701 (defun kmacro-view-macro (&optional arg)
702 "Display the last keyboard macro."
703 (interactive)
704 (kmacro-display last-kbd-macro))
705
706
707 (defun kmacro-view-macro-repeat (&optional arg)
708 "Like `kmacro-view-macro', but allow repeat without repeating prefix."
709 (interactive)
710 (let ((keys (kmacro-get-repeat-prefix)))
711 (kmacro-view-macro arg)
712 (if (and last-kbd-macro keys)
713 (kmacro-repeat-on-last-key keys))))
714
715 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'head)
716
717
718 (defun kmacro-edit-macro-repeat (&optional arg)
719 "Edit last keyboard macro."
720 (interactive "P")
721 (edit-kbd-macro "\r" arg))
722
723 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
724
725
726 (defun kmacro-edit-macro (&optional arg)
727 "As edit last keyboard macro, but without kmacro-repeat property."
728 (interactive "P")
729 (edit-kbd-macro "\r" arg))
730
731
732 (defun kmacro-edit-lossage ()
733 "Edit most recent 100 keystrokes as a keyboard macro."
734 (interactive)
735 (kmacro-push-ring)
736 (edit-kbd-macro "\C-hl"))
737
738
739 ;;; Single-step editing of keyboard macros
740
741 (defvar kmacro-step-edit-active) ;; step-editing active
742 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
743 (defvar kmacro-step-edit-inserting) ;; inserting into macro
744 (defvar kmacro-step-edit-appending) ;; append to end of macro
745 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
746 (defvar kmacro-step-edit-prefix-index) ;; index of first prefix arg key
747 (defvar kmacro-step-edit-key-index) ;; index of current key
748 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
749 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
750 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
751
752 (defvar kmacro-step-edit-map (make-sparse-keymap)
753 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
754 This keymap is an extension to the `query-replace-map', allowing the
755 following additional answers: `insert', `insert-1', `replace', `replace-1',
756 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
757
758 ;; query-replace-map answers include: `act', `skip', `act-and-show',
759 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
760 ;; `automatic', `backup', `exit-prefix', and `help'.")
761 ;; Also: `quit', `edit-replacement'
762
763 (set-keymap-parent kmacro-step-edit-map query-replace-map)
764
765 (define-key kmacro-step-edit-map "\t" 'act-repeat)
766 (define-key kmacro-step-edit-map [tab] 'act-repeat)
767 (define-key kmacro-step-edit-map "\C-k" 'skip-rest)
768 (define-key kmacro-step-edit-map "c" 'automatic)
769 (define-key kmacro-step-edit-map "f" 'skip-keep)
770 (define-key kmacro-step-edit-map "q" 'quit)
771 (define-key kmacro-step-edit-map "d" 'skip)
772 (define-key kmacro-step-edit-map "\C-d" 'skip)
773 (define-key kmacro-step-edit-map "i" 'insert)
774 (define-key kmacro-step-edit-map "I" 'insert-1)
775 (define-key kmacro-step-edit-map "r" 'replace)
776 (define-key kmacro-step-edit-map "R" 'replace-1)
777 (define-key kmacro-step-edit-map "a" 'append)
778 (define-key kmacro-step-edit-map "A" 'append-end)
779
780 (defvar kmacro-step-edit-prefix-commands
781 '(universal-argument universal-argument-more universal-argument-minus
782 digit-argument negative-argument)
783 "Commands which builds up a prefix arg for the current command")
784
785 (defun kmacro-step-edit-prompt (macro index)
786 ;; Show step-edit prompt
787 (let ((keys (and (not kmacro-step-edit-appending)
788 index (substring macro index executing-macro-index)))
789 (future (and (not kmacro-step-edit-appending)
790 (substring macro executing-macro-index)))
791 (message-log-max nil)
792 (curmsg (current-message)))
793
794 ;; TODO: Scroll macro if max-mini-window-height is too small.
795 (message (concat
796 (format "Macro: %s%s%s%s%s\n"
797 (format-kbd-macro kmacro-step-edit-new-macro 1)
798 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
799 (propertize (if keys (format-kbd-macro keys)
800 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
801 (if future " " "")
802 (if future (format-kbd-macro future) ""))
803 (cond
804 ((minibufferp)
805 (format "%s\n%s\n"
806 (propertize "\
807 minibuffer " 'face 'header-line)
808 (buffer-substring (point-min) (point-max))))
809 (curmsg
810 (format "%s\n%s\n"
811 (propertize "\
812 echo area " 'face 'header-line)
813 curmsg))
814 (t ""))
815 (if keys
816 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
817 (propertize "\
818 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
819 (if kmacro-step-edit-help "\
820 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
821 TAB: execute while same, ?: toggle help
822 Edit: i: insert, r: replace, a: append, A: append at end,
823 I/R: insert/replace with one sequence,
824 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
825 ----------------------------------------------------------------
826 " "")
827 (propertize "Next command:" 'face 'bold)
828 this-command)
829 (propertize
830 (format "Type key sequence%s to insert and execute%s: "
831 (if (numberp kmacro-step-edit-inserting) "" "s")
832 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
833 'face 'bold))))))
834
835 (defun kmacro-step-edit-query ()
836 ;; Pre-command hook function for step-edit in "command" mode
837 (let ((resize-mini-windows t)
838 (max-mini-window-height kmacro-step-edit-mini-window-height)
839 act restore-index next-index)
840
841 ;; Handle commands which reads additional input using read-char.
842 (cond
843 ((and (eq this-command 'quoted-insert)
844 (not (eq kmacro-step-edit-action t)))
845 ;; Find the actual end of this key sequence.
846 ;; Must be able to backtrack in case we actually execute it.
847 (setq restore-index executing-macro-index)
848 (let (unread-command-events)
849 (quoted-insert 0)
850 (when unread-command-events
851 (setq executing-macro-index (- executing-macro-index (length unread-command-events))
852 next-index executing-macro-index)))))
853
854 ;; Query the user; stop macro exection temporarily
855 (let ((macro executing-kbd-macro)
856 (executing-kbd-macro nil)
857 (defining-kbd-macro nil))
858
859 ;; Any action requested by previous command
860 (cond
861 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
862 (cond
863 ((eq this-command 'quoted-insert)
864 (clear-this-command-keys) ;; recent-keys actually
865 (let (unread-command-events)
866 (quoted-insert (prefix-numeric-value current-prefix-arg))
867 (setq kmacro-step-edit-new-macro
868 (vconcat kmacro-step-edit-new-macro (recent-keys)))
869 (when unread-command-events
870 (setq kmacro-step-edit-new-macro
871 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
872 executing-macro-index (- executing-macro-index (length unread-command-events)))))
873 (setq current-prefix-arg nil
874 prefix-arg nil)
875 (setq act 'ignore))
876 (t
877 (setq act 'act)))
878 (setq kmacro-step-edit-action nil))
879 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
880 (setq act 'act))
881 (t
882 (setq kmacro-step-edit-action nil)))
883
884 ;; Handle prefix arg, or query user
885 (cond
886 (act act) ;; set above
887 ((memq this-command kmacro-step-edit-prefix-commands)
888 (unless kmacro-step-edit-prefix-index
889 (setq kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
890 (setq act 'universal-argument))
891 ((eq this-command 'universal-argument-other-key)
892 (setq act 'universal-argument))
893 (t
894 (kmacro-step-edit-prompt macro (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
895 (setq act (lookup-key kmacro-step-edit-map
896 (vector (with-current-buffer (current-buffer) (read-event))))))))
897
898 ;; Resume macro execution and perform the action
899 (cond
900 ((eq act 'universal-argument)
901 nil)
902 ((cond
903 ((eq act 'act)
904 t)
905 ((eq act 'act-repeat)
906 (setq kmacro-step-edit-action this-command)
907 t)
908 ((eq act 'quit)
909 (setq kmacro-step-edit-replace nil)
910 (setq kmacro-step-edit-active 'ignore)
911 nil)
912 ((eq act 'skip)
913 (setq kmacro-step-edit-prefix-index nil)
914 nil)
915 ((eq act 'skip-keep)
916 (setq this-command 'ignore)
917 t)
918 ((eq act 'skip-rest)
919 (setq kmacro-step-edit-active 'ignore)
920 nil)
921 ((memq act '(automatic exit))
922 (setq kmacro-step-edit-active nil)
923 (setq act t)
924 t)
925 ((member act '(insert-1 insert))
926 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
927 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
928 nil)
929 ((member act '(replace-1 replace))
930 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
931 (setq kmacro-step-edit-prefix-index nil)
932 (if (= executing-macro-index (length executing-kbd-macro))
933 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
934 kmacro-step-edit-appending t))
935 nil)
936 ((eq act 'append)
937 (setq kmacro-step-edit-inserting t)
938 (if (= executing-macro-index (length executing-kbd-macro))
939 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
940 kmacro-step-edit-appending t))
941 t)
942 ((eq act 'append-end)
943 (if (= executing-macro-index (length executing-kbd-macro))
944 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
945 kmacro-step-edit-inserting t
946 kmacro-step-edit-appending t)
947 (setq kmacro-step-edit-active 'append-end))
948 (setq act t)
949 t)
950 ((eq act 'help)
951 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
952 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
953 nil)
954 (t ;; Ignore unknown responses
955 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
956 nil))
957 (if (> executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
958 (setq kmacro-step-edit-new-macro
959 (vconcat kmacro-step-edit-new-macro
960 (substring executing-kbd-macro
961 (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
962 (if (eq act t) nil executing-macro-index)))
963 kmacro-step-edit-prefix-index nil))
964 (if restore-index
965 (setq executing-macro-index restore-index)))
966 (t
967 (setq this-command 'ignore)))
968 (setq kmacro-step-edit-key-index next-index)))
969
970 (defun kmacro-step-edit-insert ()
971 ;; Pre-command hook function for step-edit in "insert" mode
972 (let ((resize-mini-windows t)
973 (max-mini-window-height kmacro-step-edit-mini-window-height)
974 (macro executing-kbd-macro)
975 (executing-kbd-macro nil)
976 (defining-kbd-macro nil)
977 cmd keys next-index)
978 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
979 kmacro-step-edit-prefix-index nil)
980 (kmacro-step-edit-prompt macro nil)
981 ;; Now, we have read a key sequence from the macro, but we don't want
982 ;; to execute it yet. So push it back and read another sequence.
983 (reset-this-command-lengths)
984 (setq keys (read-key-sequence nil nil nil nil t))
985 (setq cmd (key-binding keys t nil))
986 (if (cond
987 ((null cmd)
988 t)
989 ((eq cmd 'quoted-insert)
990 (clear-this-command-keys) ;; recent-keys actually
991 (quoted-insert (prefix-numeric-value current-prefix-arg))
992 (setq current-prefix-arg nil
993 prefix-arg nil)
994 (setq keys (vconcat keys (recent-keys)))
995 (when (numberp kmacro-step-edit-inserting)
996 (setq kmacro-step-edit-inserting nil)
997 (when unread-command-events
998 (setq keys (substring keys 0 (- (length unread-command-events)))
999 executing-macro-index (- executing-macro-index (length unread-command-events))
1000 next-index executing-macro-index
1001 unread-command-events nil)))
1002 (setq cmd 'ignore)
1003 nil)
1004 ((memq cmd kmacro-step-edit-prefix-commands)
1005 (setq universal-argument-num-events 0)
1006 (reset-this-command-lengths)
1007 nil)
1008 ((eq cmd 'universal-argument-other-key)
1009 (setq kmacro-step-edit-action t)
1010 (setq universal-argument-num-events 0)
1011 (reset-this-command-lengths)
1012 (if (numberp kmacro-step-edit-inserting)
1013 (setq kmacro-step-edit-inserting nil))
1014 nil)
1015 ((numberp kmacro-step-edit-inserting)
1016 (setq kmacro-step-edit-inserting nil)
1017 nil)
1018 ((equal keys "\C-j")
1019 (setq kmacro-step-edit-inserting nil)
1020 (setq kmacro-step-edit-action nil)
1021 ;; Forget any (partial) prefix arg from next command
1022 (setq kmacro-step-edit-prefix-index nil)
1023 (reset-this-command-lengths)
1024 (setq overriding-terminal-local-map nil)
1025 (setq universal-argument-num-events nil)
1026 (setq next-index kmacro-step-edit-key-index)
1027 t)
1028 (t nil))
1029 (setq this-command 'ignore)
1030 (setq this-command cmd)
1031 (if (memq this-command '(self-insert-command digit-argument))
1032 (setq last-command-char (aref keys (1- (length keys)))))
1033 (if keys
1034 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1035 (setq kmacro-step-edit-key-index next-index)))
1036
1037 (defun kmacro-step-edit-pre-command ()
1038 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1039 (when kmacro-step-edit-active
1040 (cond
1041 ((eq kmacro-step-edit-active 'ignore)
1042 (setq this-command 'ignore))
1043 ((eq kmacro-step-edit-active 'append-end)
1044 (if (= executing-macro-index (length executing-kbd-macro))
1045 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1046 kmacro-step-edit-inserting t
1047 kmacro-step-edit-appending t
1048 kmacro-step-edit-active t)))
1049 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1050 (if kmacro-step-edit-inserting
1051 (kmacro-step-edit-insert)
1052 (kmacro-step-edit-query))
1053 (setq kmacro-step-edit-num-input-keys num-input-keys)
1054 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1055 (setq kmacro-step-edit-appending nil
1056 kmacro-step-edit-active 'ignore)))))
1057 (when (eq kmacro-step-edit-active t)
1058 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1059
1060 (defun kmacro-step-edit-minibuf-setup ()
1061 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1062 (when kmacro-step-edit-active
1063 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1064
1065 (defun kmacro-step-edit-post-command ()
1066 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1067 (when kmacro-step-edit-active
1068 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1069 (if kmacro-step-edit-key-index
1070 (setq executing-macro-index kmacro-step-edit-key-index)
1071 (setq kmacro-step-edit-key-index executing-macro-index))))
1072
1073
1074 (defun kmacro-step-edit-macro ()
1075 "Step edit and execute last keyboard macro.
1076
1077 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1078 (interactive)
1079 (let ((kmacro-step-edit-active t)
1080 (kmacro-step-edit-new-macro "")
1081 (kmacro-step-edit-inserting nil)
1082 (kmacro-step-edit-appending nil)
1083 (kmacro-step-edit-replace t)
1084 (kmacro-step-edit-prefix-index nil)
1085 (kmacro-step-edit-key-index 0)
1086 (kmacro-step-edit-action nil)
1087 (kmacro-step-edit-help nil)
1088 (kmacro-step-edit-num-input-keys num-input-keys)
1089 (pre-command-hook pre-command-hook)
1090 (post-command-hook post-command-hook)
1091 (minibuffer-setup-hook minibuffer-setup-hook))
1092 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1093 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1094 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1095 (call-last-kbd-macro nil nil)
1096 (when (and kmacro-step-edit-replace
1097 kmacro-step-edit-new-macro
1098 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1099 (kmacro-push-ring)
1100 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1101
1102 (provide 'kmacro)
1103 ;;; kmacro.el ends here