(struct interval): Move from lisp.h.
[emacs.git] / lisp / kmacro.el
bloba4cb27f5280088b9e06a54e26a5c0c22ad9963c1
1 ;;; kmacro.el --- enhanced keyboard macros
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard convenience
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
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.
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.
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.
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.
50 ;; Note: an empty macro is never added to the macro ring.
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.
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.
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.
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).
74 ;; The initial value of `kmacro-counter' is 0, or the numeric prefix
75 ;; value given to F3 when starting the macro.
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.
81 ;; Example:
83 ;; The following sequence: M-5 F3 x M-2 F3 y F4 F4 F4 F4
84 ;; inserts the following string: x5yx7yx9yx11y
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.
89 ;; You can edit the last macro using C-x C-k C-e.
91 ;; You can append to the last macro using C-u F3.
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.
96 ;; The following key bindings are performed:
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)
104 ;; C-u f3 APPENDs to last macro
106 ;; f4 Call last macro End macro
107 ;; Prefix arg specifies number
108 ;; of times to execute macro.
110 ;; C-u f4 Swap last and head of macro ring.
112 ;; S-mouse-3 Set point at click and End macro and execute macro at
113 ;; execute last macro. click.
115 ;;; Code:
117 ;; Customization:
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"))
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)
132 (defcustom kmacro-ring-max 8
133 "Maximum number of keyboard macros to save in macro ring."
134 :type 'integer
135 :group 'kmacro)
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)
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)
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)
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)
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)
173 ;; Keymap
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)
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)
213 (if kmacro-call-mouse-event
214 (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
218 ;;; Keyboard macro counter
220 (defvar kmacro-counter 0
221 "*Current keyboard macro counter.")
223 (defvar kmacro-counter-format "%d"
224 "*Current keyboard macro counter format.")
226 (defvar kmacro-counter-format-start kmacro-counter-format
227 "Macro format at start of macro execution.")
229 (defvar kmacro-counter-value-start kmacro-counter
230 "Macro counter at start of macro execution.")
232 (defvar kmacro-last-counter 0 "Last counter inserted by key macro.")
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))))
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)))
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))
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)))
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)))
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.
297 ;;; Keyboard macro ring
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'.")
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)))
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)))))
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)))
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))))
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)))
344 (defun kmacro-ring-length ()
345 "Return length of macro ring, including pseudo head."
346 (+ (if last-kbd-macro 1 0) (length kmacro-ring)))
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.")
358 ((and (null none) (null kmacro-ring))
359 (message "Only one keyboard macro defined.")
361 (t nil)))
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"))))
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))))
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)))
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))))
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))))
432 (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
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")))
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)))))
459 (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
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)))))
477 (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
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)))
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.")))
499 (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
501 ;;; Traditional bindings:
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;
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.
517 Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
518 defining the macro.
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))
541 (start-kbd-macro append
542 (and append
543 (if kmacro-execute-before-append
544 (> (car arg) 4)
545 (= (car arg) 4)))))))
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.
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)))
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.
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.
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))))))
612 ;;; Combined function key bindings:
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.
619 Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
620 macro.
622 With \\[universal-argument], appends to current keyboard macro (keeping
623 the current value of `kmacro-counter').
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).
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)))
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))
651 (kmacro-call-macro arg no-repeat))))
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))))
662 (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
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 Zero argument means repeat until there is an error.
671 To give a macro a permanent name, so you can call it
672 even after defining other macros, use \\[name-last-kbd-macro]."
673 (interactive "P")
674 (if defining-kbd-macro
675 (kmacro-end-macro nil))
676 (kmacro-call-macro arg no-repeat))
679 ;;;###autoload
680 (defun kmacro-end-call-mouse (event)
681 "Move point to the position clicked with the mouse and call last kbd macro.
682 If kbd macro currently being defined end it before activating it."
683 (interactive "e")
684 (when defining-kbd-macro
685 (end-kbd-macro))
686 (mouse-set-point event)
687 (kmacro-call-macro nil t))
690 ;;; Misc. commands
692 (defun kmacro-bind-to-key (arg)
693 "When not defining or executing a macro, offer to bind last macro to a key."
694 (interactive "p")
695 (if (or defining-kbd-macro executing-kbd-macro)
696 (if defining-kbd-macro
697 (message "Cannot save macro while defining it."))
698 (unless last-kbd-macro
699 (error "No keyboard macro defined"))
700 (let ((key-seq (read-key-sequence "Bind last macro to key: ")))
701 (unless (equal key-seq "\a")
702 (define-key global-map key-seq last-kbd-macro)))))
705 (defun kmacro-view-macro (&optional arg)
706 "Display the last keyboard macro."
707 (interactive)
708 (kmacro-display last-kbd-macro))
711 (defun kmacro-view-macro-repeat (&optional arg)
712 "Like `kmacro-view-macro', but allow repeat without repeating prefix."
713 (interactive)
714 (let ((keys (kmacro-get-repeat-prefix)))
715 (kmacro-view-macro arg)
716 (if (and last-kbd-macro keys)
717 (kmacro-repeat-on-last-key keys))))
719 (put 'kmacro-view-macro-repeat 'kmacro-repeat 'head)
722 (defun kmacro-edit-macro-repeat (&optional arg)
723 "Edit last keyboard macro."
724 (interactive "P")
725 (edit-kbd-macro "\r" arg))
727 (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
730 (defun kmacro-edit-macro (&optional arg)
731 "As edit last keyboard macro, but without kmacro-repeat property."
732 (interactive "P")
733 (edit-kbd-macro "\r" arg))
736 (defun kmacro-edit-lossage ()
737 "Edit most recent 100 keystrokes as a keyboard macro."
738 (interactive)
739 (kmacro-push-ring)
740 (edit-kbd-macro "\C-hl"))
743 ;;; Single-step editing of keyboard macros
745 (defvar kmacro-step-edit-active) ;; step-editing active
746 (defvar kmacro-step-edit-new-macro) ;; storage for new macro
747 (defvar kmacro-step-edit-inserting) ;; inserting into macro
748 (defvar kmacro-step-edit-appending) ;; append to end of macro
749 (defvar kmacro-step-edit-replace) ;; replace orig macro when done
750 (defvar kmacro-step-edit-prefix-index) ;; index of first prefix arg key
751 (defvar kmacro-step-edit-key-index) ;; index of current key
752 (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
753 (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
754 (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
756 (defvar kmacro-step-edit-map (make-sparse-keymap)
757 "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
758 This keymap is an extension to the `query-replace-map', allowing the
759 following additional answers: `insert', `insert-1', `replace', `replace-1',
760 `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
762 ;; query-replace-map answers include: `act', `skip', `act-and-show',
763 ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
764 ;; `automatic', `backup', `exit-prefix', and `help'.")
765 ;; Also: `quit', `edit-replacement'
767 (set-keymap-parent kmacro-step-edit-map query-replace-map)
769 (define-key kmacro-step-edit-map "\t" 'act-repeat)
770 (define-key kmacro-step-edit-map [tab] 'act-repeat)
771 (define-key kmacro-step-edit-map "\C-k" 'skip-rest)
772 (define-key kmacro-step-edit-map "c" 'automatic)
773 (define-key kmacro-step-edit-map "f" 'skip-keep)
774 (define-key kmacro-step-edit-map "q" 'quit)
775 (define-key kmacro-step-edit-map "d" 'skip)
776 (define-key kmacro-step-edit-map "\C-d" 'skip)
777 (define-key kmacro-step-edit-map "i" 'insert)
778 (define-key kmacro-step-edit-map "I" 'insert-1)
779 (define-key kmacro-step-edit-map "r" 'replace)
780 (define-key kmacro-step-edit-map "R" 'replace-1)
781 (define-key kmacro-step-edit-map "a" 'append)
782 (define-key kmacro-step-edit-map "A" 'append-end)
784 (defvar kmacro-step-edit-prefix-commands
785 '(universal-argument universal-argument-more universal-argument-minus
786 digit-argument negative-argument)
787 "Commands which builds up a prefix arg for the current command")
789 (defun kmacro-step-edit-prompt (macro index)
790 ;; Show step-edit prompt
791 (let ((keys (and (not kmacro-step-edit-appending)
792 index (substring macro index executing-macro-index)))
793 (future (and (not kmacro-step-edit-appending)
794 (substring macro executing-macro-index)))
795 (message-log-max nil)
796 (curmsg (current-message)))
798 ;; TODO: Scroll macro if max-mini-window-height is too small.
799 (message (concat
800 (format "Macro: %s%s%s%s%s\n"
801 (format-kbd-macro kmacro-step-edit-new-macro 1)
802 (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
803 (propertize (if keys (format-kbd-macro keys)
804 (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
805 (if future " " "")
806 (if future (format-kbd-macro future) ""))
807 (cond
808 ((minibufferp)
809 (format "%s\n%s\n"
810 (propertize "\
811 minibuffer " 'face 'header-line)
812 (buffer-substring (point-min) (point-max))))
813 (curmsg
814 (format "%s\n%s\n"
815 (propertize "\
816 echo area " 'face 'header-line)
817 curmsg))
818 (t ""))
819 (if keys
820 (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
821 (propertize "\
822 --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
823 (if kmacro-step-edit-help "\
824 Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
825 TAB: execute while same, ?: toggle help
826 Edit: i: insert, r: replace, a: append, A: append at end,
827 I/R: insert/replace with one sequence,
828 End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
829 ----------------------------------------------------------------
830 " "")
831 (propertize "Next command:" 'face 'bold)
832 this-command)
833 (propertize
834 (format "Type key sequence%s to insert and execute%s: "
835 (if (numberp kmacro-step-edit-inserting) "" "s")
836 (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
837 'face 'bold))))))
839 (defun kmacro-step-edit-query ()
840 ;; Pre-command hook function for step-edit in "command" mode
841 (let ((resize-mini-windows t)
842 (max-mini-window-height kmacro-step-edit-mini-window-height)
843 act restore-index next-index)
845 ;; Handle commands which reads additional input using read-char.
846 (cond
847 ((and (eq this-command 'quoted-insert)
848 (not (eq kmacro-step-edit-action t)))
849 ;; Find the actual end of this key sequence.
850 ;; Must be able to backtrack in case we actually execute it.
851 (setq restore-index executing-macro-index)
852 (let (unread-command-events)
853 (quoted-insert 0)
854 (when unread-command-events
855 (setq executing-macro-index (- executing-macro-index (length unread-command-events))
856 next-index executing-macro-index)))))
858 ;; Query the user; stop macro exection temporarily
859 (let ((macro executing-kbd-macro)
860 (executing-kbd-macro nil)
861 (defining-kbd-macro nil))
863 ;; Any action requested by previous command
864 (cond
865 ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
866 (cond
867 ((eq this-command 'quoted-insert)
868 (clear-this-command-keys) ;; recent-keys actually
869 (let (unread-command-events)
870 (quoted-insert (prefix-numeric-value current-prefix-arg))
871 (setq kmacro-step-edit-new-macro
872 (vconcat kmacro-step-edit-new-macro (recent-keys)))
873 (when unread-command-events
874 (setq kmacro-step-edit-new-macro
875 (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
876 executing-macro-index (- executing-macro-index (length unread-command-events)))))
877 (setq current-prefix-arg nil
878 prefix-arg nil)
879 (setq act 'ignore))
881 (setq act 'act)))
882 (setq kmacro-step-edit-action nil))
883 ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
884 (setq act 'act))
886 (setq kmacro-step-edit-action nil)))
888 ;; Handle prefix arg, or query user
889 (cond
890 (act act) ;; set above
891 ((memq this-command kmacro-step-edit-prefix-commands)
892 (unless kmacro-step-edit-prefix-index
893 (setq kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
894 (setq act 'universal-argument))
895 ((eq this-command 'universal-argument-other-key)
896 (setq act 'universal-argument))
898 (kmacro-step-edit-prompt macro (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
899 (setq act (lookup-key kmacro-step-edit-map
900 (vector (with-current-buffer (current-buffer) (read-event))))))))
902 ;; Resume macro execution and perform the action
903 (cond
904 ((eq act 'universal-argument)
905 nil)
906 ((cond
907 ((eq act 'act)
909 ((eq act 'act-repeat)
910 (setq kmacro-step-edit-action this-command)
912 ((eq act 'quit)
913 (setq kmacro-step-edit-replace nil)
914 (setq kmacro-step-edit-active 'ignore)
915 nil)
916 ((eq act 'skip)
917 (setq kmacro-step-edit-prefix-index nil)
918 nil)
919 ((eq act 'skip-keep)
920 (setq this-command 'ignore)
922 ((eq act 'skip-rest)
923 (setq kmacro-step-edit-active 'ignore)
924 nil)
925 ((memq act '(automatic exit))
926 (setq kmacro-step-edit-active nil)
927 (setq act t)
929 ((member act '(insert-1 insert))
930 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
931 (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
932 nil)
933 ((member act '(replace-1 replace))
934 (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
935 (setq kmacro-step-edit-prefix-index nil)
936 (if (= executing-macro-index (length executing-kbd-macro))
937 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
938 kmacro-step-edit-appending t))
939 nil)
940 ((eq act 'append)
941 (setq kmacro-step-edit-inserting t)
942 (if (= executing-macro-index (length executing-kbd-macro))
943 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
944 kmacro-step-edit-appending t))
946 ((eq act 'append-end)
947 (if (= executing-macro-index (length executing-kbd-macro))
948 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
949 kmacro-step-edit-inserting t
950 kmacro-step-edit-appending t)
951 (setq kmacro-step-edit-active 'append-end))
952 (setq act t)
954 ((eq act 'help)
955 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
956 (setq kmacro-step-edit-help (not kmacro-step-edit-help))
957 nil)
958 (t ;; Ignore unknown responses
959 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
960 nil))
961 (if (> executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
962 (setq kmacro-step-edit-new-macro
963 (vconcat kmacro-step-edit-new-macro
964 (substring executing-kbd-macro
965 (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
966 (if (eq act t) nil executing-macro-index)))
967 kmacro-step-edit-prefix-index nil))
968 (if restore-index
969 (setq executing-macro-index restore-index)))
971 (setq this-command 'ignore)))
972 (setq kmacro-step-edit-key-index next-index)))
974 (defun kmacro-step-edit-insert ()
975 ;; Pre-command hook function for step-edit in "insert" mode
976 (let ((resize-mini-windows t)
977 (max-mini-window-height kmacro-step-edit-mini-window-height)
978 (macro executing-kbd-macro)
979 (executing-kbd-macro nil)
980 (defining-kbd-macro nil)
981 cmd keys next-index)
982 (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
983 kmacro-step-edit-prefix-index nil)
984 (kmacro-step-edit-prompt macro nil)
985 ;; Now, we have read a key sequence from the macro, but we don't want
986 ;; to execute it yet. So push it back and read another sequence.
987 (reset-this-command-lengths)
988 (setq keys (read-key-sequence nil nil nil nil t))
989 (setq cmd (key-binding keys t nil))
990 (if (cond
991 ((null cmd)
993 ((eq cmd 'quoted-insert)
994 (clear-this-command-keys) ;; recent-keys actually
995 (quoted-insert (prefix-numeric-value current-prefix-arg))
996 (setq current-prefix-arg nil
997 prefix-arg nil)
998 (setq keys (vconcat keys (recent-keys)))
999 (when (numberp kmacro-step-edit-inserting)
1000 (setq kmacro-step-edit-inserting nil)
1001 (when unread-command-events
1002 (setq keys (substring keys 0 (- (length unread-command-events)))
1003 executing-macro-index (- executing-macro-index (length unread-command-events))
1004 next-index executing-macro-index
1005 unread-command-events nil)))
1006 (setq cmd 'ignore)
1007 nil)
1008 ((memq cmd kmacro-step-edit-prefix-commands)
1009 (setq universal-argument-num-events 0)
1010 (reset-this-command-lengths)
1011 nil)
1012 ((eq cmd 'universal-argument-other-key)
1013 (setq kmacro-step-edit-action t)
1014 (setq universal-argument-num-events 0)
1015 (reset-this-command-lengths)
1016 (if (numberp kmacro-step-edit-inserting)
1017 (setq kmacro-step-edit-inserting nil))
1018 nil)
1019 ((numberp kmacro-step-edit-inserting)
1020 (setq kmacro-step-edit-inserting nil)
1021 nil)
1022 ((equal keys "\C-j")
1023 (setq kmacro-step-edit-inserting nil)
1024 (setq kmacro-step-edit-action nil)
1025 ;; Forget any (partial) prefix arg from next command
1026 (setq kmacro-step-edit-prefix-index nil)
1027 (reset-this-command-lengths)
1028 (setq overriding-terminal-local-map nil)
1029 (setq universal-argument-num-events nil)
1030 (setq next-index kmacro-step-edit-key-index)
1032 (t nil))
1033 (setq this-command 'ignore)
1034 (setq this-command cmd)
1035 (if (memq this-command '(self-insert-command digit-argument))
1036 (setq last-command-char (aref keys (1- (length keys)))))
1037 (if keys
1038 (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1039 (setq kmacro-step-edit-key-index next-index)))
1041 (defun kmacro-step-edit-pre-command ()
1042 (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1043 (when kmacro-step-edit-active
1044 (cond
1045 ((eq kmacro-step-edit-active 'ignore)
1046 (setq this-command 'ignore))
1047 ((eq kmacro-step-edit-active 'append-end)
1048 (if (= executing-macro-index (length executing-kbd-macro))
1049 (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
1050 kmacro-step-edit-inserting t
1051 kmacro-step-edit-appending t
1052 kmacro-step-edit-active t)))
1053 ((/= kmacro-step-edit-num-input-keys num-input-keys)
1054 (if kmacro-step-edit-inserting
1055 (kmacro-step-edit-insert)
1056 (kmacro-step-edit-query))
1057 (setq kmacro-step-edit-num-input-keys num-input-keys)
1058 (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1059 (setq kmacro-step-edit-appending nil
1060 kmacro-step-edit-active 'ignore)))))
1061 (when (eq kmacro-step-edit-active t)
1062 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1064 (defun kmacro-step-edit-minibuf-setup ()
1065 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1066 (when kmacro-step-edit-active
1067 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1069 (defun kmacro-step-edit-post-command ()
1070 (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1071 (when kmacro-step-edit-active
1072 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1073 (if kmacro-step-edit-key-index
1074 (setq executing-macro-index kmacro-step-edit-key-index)
1075 (setq kmacro-step-edit-key-index executing-macro-index))))
1078 (defun kmacro-step-edit-macro ()
1079 "Step edit and execute last keyboard macro.
1081 To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1082 (interactive)
1083 (let ((kmacro-step-edit-active t)
1084 (kmacro-step-edit-new-macro "")
1085 (kmacro-step-edit-inserting nil)
1086 (kmacro-step-edit-appending nil)
1087 (kmacro-step-edit-replace t)
1088 (kmacro-step-edit-prefix-index nil)
1089 (kmacro-step-edit-key-index 0)
1090 (kmacro-step-edit-action nil)
1091 (kmacro-step-edit-help nil)
1092 (kmacro-step-edit-num-input-keys num-input-keys)
1093 (pre-command-hook pre-command-hook)
1094 (post-command-hook post-command-hook)
1095 (minibuffer-setup-hook minibuffer-setup-hook))
1096 (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1097 (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1098 (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1099 (call-last-kbd-macro nil nil)
1100 (when (and kmacro-step-edit-replace
1101 kmacro-step-edit-new-macro
1102 (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
1103 (kmacro-push-ring)
1104 (setq last-kbd-macro kmacro-step-edit-new-macro))))
1106 (provide 'kmacro)
1107 ;;; kmacro.el ends here