Commit | Line | Data |
---|---|---|
c30c4abe KS |
1 | ;;; kmacro.el --- enhanced keyboard macros |
2 | ||
306d2bb3 | 3 | ;; Copyright (C) 2002 Free Software Foundation, Inc. |
c30c4abe KS |
4 | |
5 | ;; Author: Kim F. Storm <storm@cua.dk> | |
306d2bb3 | 6 | ;; Keywords: keyboard convenience |
c30c4abe KS |
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 | |
7e30f58e | 29 | ;; to C-x (, C-x ), and C-x e, but these bindings are too hard to |
c30c4abe KS |
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 F7 and F8. Personally, I prefer F1 and F2, but those | |
34 | ;; keys already have default bindings. | |
35 | ;; | |
36 | ;; To start defining a keyboard macro, use F7. To end the macro, | |
37 | ;; use F8, and to call the macro also use F8. This makes it very | |
38 | ;; easy to repeat a macro immediately after defining it. | |
39 | ;; | |
40 | ;; You can call the macro repeatedly by pressing F8 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 | ||
1fa13bd4 KS |
46 | ;; When you define a macro with F7/F8, it is automatically added to |
47 | ;; the head of the "keyboard macro ring", and F8 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 F8 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 | ||
c30c4abe KS |
69 | ;; If you enter F7 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 F7). | |
73 | ;; | |
74 | ;; The initial value of `kmacro-counter' is 0, or the numeric prefix | |
75 | ;; value given to F7 when starting the macro. | |
76 | ;; | |
77 | ;; Now, each time you call the macro using F8, 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 F7 x M-2 F7 y F8 F8 F8 F8 | |
84 | ;; inserts the following string: x5yx7yx9yx11y | |
85 | ||
7e30f58e | 86 | ;; A macro can also be called using a mouse click, default S-mouse-3. |
c30c4abe KS |
87 | ;; This calls the macro at the point where you click the mouse. |
88 | ||
1fa13bd4 | 89 | ;; You can edit the last macro using C-x C-k C-e. |
c30c4abe KS |
90 | |
91 | ;; You can append to the last macro using C-u F7. | |
92 | ||
1fa13bd4 KS |
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. | |
c30c4abe KS |
95 | |
96 | ;; The following key bindings are performed: | |
97 | ;; | |
98 | ;; Normal While defining macro | |
99 | ;; --------------------------- ------------------------------ | |
100 | ;; f7 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 f7 APPENDs to last macro | |
105 | ;; | |
106 | ;; f8 Call last macro End macro | |
107 | ;; Prefix arg specifies number | |
108 | ;; of times to execute macro. | |
109 | ;; | |
110 | ;; C-u f8 Swap last and head of macro ring. | |
111 | ;; | |
c30c4abe KS |
112 | ;; S-mouse-3 Set point at click and End macro and execute macro at |
113 | ;; execute last macro. click. | |
114 | ||
115 | ;;; Code: | |
116 | ||
7e30f58e | 117 | ;; Customization: |
c30c4abe KS |
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 | ||
1fa13bd4 KS |
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 | |
c30c4abe KS |
130 | :group 'kmacro) |
131 | ||
1fa13bd4 KS |
132 | (defcustom kmacro-ring-max 8 |
133 | "Maximum number of keyboard macros to save in macro ring." | |
134 | :type 'integer | |
c30c4abe KS |
135 | :group 'kmacro) |
136 | ||
1fa13bd4 KS |
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 | |
c30c4abe KS |
147 | :group 'kmacro) |
148 | ||
1fa13bd4 KS |
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 | |
c30c4abe KS |
153 | :group 'kmacro) |
154 | ||
1fa13bd4 KS |
155 | |
156 | ;; Keymap | |
157 | ||
158 | (defvar kmacro-keymap | |
159 | (let ((map (make-sparse-keymap))) | |
160 | (define-key map "\C-s" 'kmacro-start-macro) | |
161 | (define-key map "\C-k" 'kmacro-end-or-call-macro-rep) | |
162 | (define-key map "\C-e" 'kmacro-edit-macro) | |
163 | (define-key map "\r" 'kmacro-edit-macro-nr) | |
164 | (define-key map "l" 'kmacro-edit-lossage) | |
165 | (define-key map "\C-i" 'kmacro-insert-counter) | |
166 | (define-key map "\C-a" 'kmacro-add-counter) | |
167 | (define-key map "\C-v" 'kmacro-view-macro-rep) | |
168 | (define-key map "\C-l" 'kmacro-call-ring-2nd-rep) | |
169 | (define-key map "\C-r" 'kmacro-view-ring-2nd) | |
170 | (define-key map "\C-n" 'kmacro-cycle-ring-next) | |
171 | (define-key map "\C-p" 'kmacro-cycle-ring-previous) | |
172 | (define-key map "\C-f" 'kmacro-set-format) | |
173 | (define-key map "\C-c" 'kmacro-set-counter) | |
174 | (define-key map "\C-t" 'kmacro-swap-ring) | |
175 | (define-key map "\C-b" 'kmacro-bind-to-key) | |
176 | (define-key map "\C-d" 'kmacro-delete-ring-head) | |
177 | ;; Compatibility bindings | |
178 | (define-key map "q" 'kbd-macro-query) | |
179 | (define-key map "n" 'name-last-kbd-macro) | |
180 | (define-key map "e" 'edit-kbd-macro) | |
bcaf8c79 | 181 | (define-key map "r" 'apply-macro-to-region-lines) |
1fa13bd4 KS |
182 | map) |
183 | "Keymap for keyboard macro commands.") | |
184 | (defalias 'kmacro-keymap kmacro-keymap) | |
185 | ||
186 | ;;; Provide some binding for startup: | |
187 | ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro) | |
188 | ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro) | |
189 | ;;;###autoload (global-set-key "\C-xe" 'kmacro-call-macro) | |
190 | ;;;###autoload (global-set-key [f7] 'kmacro-start-macro-or-insert-counter) | |
191 | ;;;###autoload (global-set-key [f8] 'kmacro-end-or-call-macro) | |
192 | ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap) | |
193 | ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap) | |
194 | ||
195 | (if kmacro-call-mouse-event | |
196 | (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse)) | |
197 | ||
198 | ||
199 | ||
200 | ;;; Keyboard macro counter | |
c30c4abe KS |
201 | |
202 | (defvar kmacro-counter 0 | |
7e30f58e | 203 | "*Current keyboard macro counter.") |
c30c4abe KS |
204 | |
205 | (defvar kmacro-counter-format "%d" | |
7e30f58e | 206 | "*Current keyboard macro counter format.") |
c30c4abe KS |
207 | |
208 | (defvar kmacro-counter-format-start kmacro-counter-format | |
209 | "Macro format at start of macro execution.") | |
210 | ||
1fa13bd4 KS |
211 | (defvar kmacro-counter-value-start kmacro-counter |
212 | "Macro counter at start of macro execution.") | |
213 | ||
7e30f58e | 214 | (defvar kmacro-last-counter 0 "Last counter inserted by key macro.") |
c30c4abe | 215 | |
c30c4abe | 216 | |
1fa13bd4 KS |
217 | (defun kmacro-insert-counter (arg) |
218 | "Insert macro counter and increment with ARG or 1 if missing. | |
219 | With \\[universal-argument], insert previous kmacro-counter (but do not modify counter)." | |
220 | (interactive "P") | |
221 | (if (and arg (listp arg)) | |
222 | (insert (format kmacro-counter-format kmacro-last-counter)) | |
223 | (insert (format kmacro-counter-format kmacro-counter)) | |
224 | (kmacro-add-counter (prefix-numeric-value arg)))) | |
225 | ||
226 | ||
227 | (defun kmacro-set-format (format) | |
228 | "Set macro counter FORMAT." | |
229 | (interactive "sMacro Counter Format (printf format): ") | |
230 | (setq kmacro-counter-format | |
231 | (if (equal format "") "%d" format)) | |
232 | ;; redefine initial macro counter if we are not executing a macro. | |
233 | (if (not (or defining-kbd-macro executing-kbd-macro)) | |
234 | (setq kmacro-counter-format-start kmacro-counter-format))) | |
235 | ||
236 | ||
237 | (defun kmacro-display-counter (&optional value) | |
238 | "Display current counter value." | |
239 | (unless value (setq value kmacro-counter)) | |
240 | (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value)) | |
241 | ||
242 | ||
243 | (defun kmacro-set-counter (arg) | |
244 | "Set kmacro-counter to ARG or prompt if missing. | |
245 | With \\[universal-argument], reset counter to its value prior to this iteration of the macro." | |
246 | (interactive "NMacro counter value: ") | |
247 | (setq kmacro-last-counter kmacro-counter | |
248 | kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg)) | |
249 | kmacro-counter-value-start | |
250 | arg)) | |
251 | (unless executing-kbd-macro | |
252 | (kmacro-display-counter))) | |
253 | ||
254 | ||
255 | (defun kmacro-add-counter (arg) | |
256 | "Add numeric prefix arg (prompt if missing) to macro counter. | |
257 | With \\[universal-argument], restore previous counter value." | |
258 | (interactive "NAdd to macro counter: ") | |
259 | (let ((last kmacro-last-counter)) | |
260 | (setq kmacro-last-counter kmacro-counter | |
261 | kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg)) | |
262 | last | |
263 | kmacro-counter (+ kmacro-counter arg)))) | |
264 | (unless executing-kbd-macro | |
265 | (kmacro-display-counter))) | |
266 | ||
267 | ||
268 | (defun kmacro-loop-setup-function () | |
269 | "Function called prior to each iteration of macro." | |
270 | ;; Restore macro counter format to initial format, so it is ok to change | |
271 | ;; counter format in the macro without restoring it. | |
272 | (setq kmacro-counter-format kmacro-counter-format-start) | |
273 | ;; Save initial counter value so we can restore it with C-u kmacro-set-counter. | |
274 | (setq kmacro-counter-value-start kmacro-counter) | |
275 | ;; Return non-nil to continue execution. | |
276 | t) | |
277 | ||
278 | ||
279 | ;;; Keyboard macro ring | |
280 | ||
281 | (defvar kmacro-ring nil | |
282 | "The keyboard macro ring. | |
283 | Each element is a list (MACRO COUNTER FORMAT). Actually, the head of | |
284 | the macro ring (when defining or executing) is not stored in the ring; | |
285 | instead it is available in the variables `last-kbd-macro', `kmacro-counter', | |
286 | and `kmacro-counter-format'.") | |
287 | ||
288 | ||
289 | (defun kmacro-ring-head () | |
290 | "Return pseudo head element in macro ring." | |
291 | (and last-kbd-macro | |
292 | (list last-kbd-macro kmacro-counter kmacro-counter-format-start))) | |
293 | ||
294 | ||
295 | (defun kmacro-push-ring (&optional elt) | |
296 | "Push ELT or current macro onto `kmacro-ring'." | |
297 | (when (setq elt (or elt (kmacro-ring-head))) | |
298 | (let ((len (length kmacro-ring))) | |
299 | (setq kmacro-ring (cons elt kmacro-ring)) | |
300 | (if (>= len kmacro-ring-max) | |
301 | (setcdr (nthcdr len kmacro-ring) nil))))) | |
302 | ||
303 | ||
304 | (defun kmacro-split-ring-element (elt) | |
305 | (setq last-kbd-macro (car elt) | |
306 | kmacro-counter (nth 1 elt) | |
307 | kmacro-counter-format-start (nth 2 elt))) | |
308 | ||
309 | ||
310 | (defun kmacro-pop-ring1 (&optional raw) | |
311 | "Pop head element off macro ring (no check). | |
312 | Non-nil arg RAW means just return raw first element." | |
313 | (prog1 (car kmacro-ring) | |
314 | (unless raw | |
315 | (kmacro-split-ring-element (car kmacro-ring))) | |
316 | (setq kmacro-ring (cdr kmacro-ring)))) | |
317 | ||
318 | ||
319 | (defun kmacro-pop-ring (&optional raw) | |
320 | "Pop head element off macro ring. | |
321 | Non-nil arg RAW means just return raw first element." | |
322 | (unless (kmacro-ring-empty-p) | |
323 | (kmacro-pop-ring1 raw))) | |
324 | ||
325 | ||
326 | (defun kmacro-ring-length () | |
327 | "Return length of macro ring, including pseudo head." | |
328 | (+ (if last-kbd-macro 1 0) (length kmacro-ring))) | |
329 | ||
330 | ||
331 | (defun kmacro-ring-empty-p (&optional none) | |
332 | "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty. | |
333 | Check only `last-kbd-macro' if optional arg NONE is non-nil." | |
334 | (while (and (null last-kbd-macro) kmacro-ring) | |
335 | (kmacro-pop-ring1)) | |
336 | (cond | |
337 | ((null last-kbd-macro) | |
338 | (message "No keyboard macro defined.") | |
339 | t) | |
340 | ((and (null none) (null kmacro-ring)) | |
341 | (message "Only one keyboard macro defined.") | |
342 | t) | |
343 | (t nil))) | |
344 | ||
345 | ||
346 | (defun kmacro-display (macro &optional trunc descr empty ) | |
7e30f58e | 347 | "Display a keyboard MACRO." |
1fa13bd4 KS |
348 | (if macro |
349 | (let* ((x 60) | |
350 | (m (format-kbd-macro macro)) | |
351 | (l (length m)) | |
352 | (z (and nil trunc (> l x)))) | |
353 | (message (format "%s: %s%s" (or descr "Macro") | |
354 | (if z (substring m 0 (1- x)) m) (if z "..." "")))) | |
355 | (message (or empty "No keyboard macros defined")))) | |
356 | ||
357 | ||
358 | (defun kmacro-call-ring-2nd (arg) | |
359 | "Execute second keyboard macro at in macro ring." | |
360 | (interactive "P") | |
361 | (unless (kmacro-ring-empty-p) | |
362 | ;; should use counter format specific to the macro on the ring! | |
363 | (let ((kmacro-counter (nth 1 (car kmacro-ring))) | |
364 | (kmacro-counter-format-start (nth 2 (car kmacro-ring)))) | |
365 | (execute-kbd-macro (car (car kmacro-ring)) arg #'kmacro-loop-setup-function) | |
366 | (setcar (cdr (car kmacro-ring)) kmacro-counter)))) | |
367 | ||
368 | ||
369 | (defun kmacro-call-ring-2nd-rep (arg) | |
370 | "Like `kmacro-call-ring-2nd', but allow repeat without kmacro prefix." | |
371 | (interactive "P") | |
372 | (kmacro-call-ring-2nd arg) | |
373 | (if kmacro-ring | |
374 | (kmacro-repeat-loop))) | |
375 | ||
376 | (put 'kmacro-call-ring-2nd-rep 'kmacro-repeat 'head) | |
377 | ||
c30c4abe | 378 | |
1fa13bd4 KS |
379 | (defun kmacro-view-ring-2nd () |
380 | "Display the current head of the keyboard macro ring." | |
381 | (interactive) | |
382 | (unless (kmacro-ring-empty-p) | |
383 | (kmacro-display (car (car kmacro-ring)) "2nd macro"))) | |
384 | ||
385 | ||
386 | (defun kmacro-repeat-loop () | |
387 | "Process kmacro commands keys immidiately after cycling the ring." | |
388 | (when kmacro-repeat-no-prefix | |
389 | (let (cmd done repeat) | |
390 | (while (and last-kbd-macro | |
391 | (not done) | |
392 | (setq cmd (lookup-key kmacro-keymap (vector (read-event)))) | |
393 | (setq repeat (get cmd 'kmacro-repeat))) | |
394 | (clear-this-command-keys t) | |
395 | (cond | |
396 | ((eq repeat 'ring) | |
397 | (if kmacro-ring | |
398 | (let ((kmacro-repeat-no-prefix nil)) | |
399 | (funcall cmd nil)) | |
400 | (kmacro-display last-kbd-macro t))) | |
401 | ((eq repeat 'head) | |
402 | (funcall cmd nil)) | |
403 | ((eq repeat 'stop) | |
404 | (funcall cmd nil) | |
405 | (setq done t))) | |
406 | (setq last-input-event nil))) | |
407 | (when last-input-event | |
408 | (clear-this-command-keys t) | |
409 | (setq unread-command-events (list last-input-event))))) | |
410 | ||
411 | ||
412 | (defun kmacro-cycle-ring-next (&optional arg) | |
413 | "Move to next keyboard macro in keyboard macro ring. | |
414 | Displays the selected macro in the echo area." | |
415 | (interactive) | |
416 | (unless (kmacro-ring-empty-p) | |
417 | (kmacro-push-ring) | |
418 | (let* ((len (length kmacro-ring)) | |
419 | (tail (nthcdr (- len 2) kmacro-ring)) | |
420 | (elt (car (cdr tail)))) | |
421 | (setcdr tail nil) | |
422 | (kmacro-split-ring-element elt)) | |
423 | (kmacro-display last-kbd-macro t) | |
424 | (kmacro-repeat-loop))) | |
425 | ||
426 | (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring) | |
427 | ||
428 | ||
429 | (defun kmacro-cycle-ring-previous (&optional arg) | |
430 | "Move to previous keyboard macro in keyboard macro ring. | |
431 | Displays the selected macro in the echo area." | |
432 | (interactive) | |
433 | (unless (kmacro-ring-empty-p) | |
434 | (let ((cur (kmacro-ring-head))) | |
435 | (kmacro-pop-ring1) | |
436 | (if kmacro-ring | |
437 | (nconc kmacro-ring (list cur)) | |
438 | (setq kmacro-ring (list cur)))) | |
439 | (kmacro-display last-kbd-macro t) | |
440 | (kmacro-repeat-loop))) | |
441 | ||
442 | (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring) | |
c30c4abe | 443 | |
1fa13bd4 KS |
444 | |
445 | (defun kmacro-swap-ring () | |
446 | "Swap first two elements on keyboard macro ring." | |
447 | (interactive) | |
448 | (unless (kmacro-ring-empty-p) | |
449 | (let ((cur (kmacro-ring-head))) | |
450 | (kmacro-pop-ring1) | |
451 | (kmacro-push-ring cur)) | |
452 | (kmacro-display last-kbd-macro t))) | |
453 | ||
454 | ||
455 | (defun kmacro-delete-ring-head (&optional arg) | |
456 | "Delete current macro from keyboard macro ring." | |
457 | (interactive) | |
458 | (unless (kmacro-ring-empty-p t) | |
459 | (if (null kmacro-ring) | |
460 | (setq last-kbd-macro nil) | |
461 | (kmacro-pop-ring)) | |
462 | (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty."))) | |
463 | ||
464 | (put 'kmacro-delete-ring-head 'kmacro-repeat 'head) | |
465 | ||
466 | ;;; Traditional bindings: | |
467 | ||
468 | ||
469 | ;;;###autoload | |
c30c4abe | 470 | (defun kmacro-start-macro (arg) |
1fa13bd4 KS |
471 | "Record subsequent keyboard input, defining a keyboard macro. |
472 | The commands are recorded even as they are executed. | |
473 | Use \\[end-kbd-macro] to finish recording and make the macro available. | |
474 | Use \\[call-last-kbd-macro] to execute the macro. | |
475 | Use \\[name-last-kbd-macro] to give it a permanent name. | |
476 | Non-nil arg (prefix arg) means append to last macro defined; | |
477 | ||
478 | With \\[universal-argument] prefix, append to last keyboard macro | |
479 | defined. Depending on `kmacro-execute-before-append', this may begin | |
480 | by re-executing the last macro as if you typed it again. | |
481 | ||
482 | Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before | |
483 | defining the macro. | |
484 | ||
485 | Use \\[kmacro-insert-counter] to insert (and increment) the macro counter. | |
486 | The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter]. | |
487 | The format of the counter can be modified via \\[kmacro-set-format]." | |
488 | (interactive "P") | |
489 | (if (or defining-kbd-macro executing-kbd-macro) | |
490 | (message "Already defining keyboard macro.") | |
491 | (let ((append (and arg (listp arg)))) | |
492 | (unless append | |
493 | (if last-kbd-macro | |
494 | (let ((len (length kmacro-ring))) | |
495 | (setq kmacro-ring | |
496 | (cons | |
497 | (list last-kbd-macro kmacro-counter kmacro-counter-format-start) | |
498 | kmacro-ring)) | |
499 | (if (>= len kmacro-ring-max) | |
500 | (setcdr (nthcdr len kmacro-ring) nil)))) | |
501 | (setq kmacro-counter (if arg (prefix-numeric-value arg) 0) | |
502 | kmacro-counter-value-start kmacro-counter | |
503 | kmacro-last-counter kmacro-counter | |
504 | kmacro-counter-format-start kmacro-counter-format)) | |
505 | ||
506 | (start-kbd-macro append | |
507 | (and append | |
508 | (if kmacro-execute-before-append | |
509 | (> (car arg) 4) | |
510 | (= (car arg) 4))))))) | |
511 | ||
512 | ||
513 | ;;;###autoload | |
514 | (defun kmacro-end-macro (arg) | |
515 | "Finish defining a keyboard macro. | |
516 | The definition was started by \\[kmacro-start-macro]. | |
517 | The macro is now available for use via \\[kmacro-call-macro], | |
518 | or it can be given a name with \\[name-last-kbd-macro] and then invoked | |
519 | under that name. | |
520 | ||
521 | With numeric arg, repeat macro now that many times, | |
522 | counting the definition just completed as the first repetition. | |
523 | An argument of zero means repeat until error." | |
524 | (interactive "P") | |
525 | (end-kbd-macro arg #'kmacro-loop-setup-function) | |
526 | (when (and last-kbd-macro (= (length last-kbd-macro) 0)) | |
527 | (message "Ignore empty macro") | |
528 | (kmacro-pop-ring))) | |
529 | ||
530 | ||
531 | ;;;###autoload | |
532 | (defun kmacro-call-macro (arg) | |
533 | "Call the last keyboard macro that you defined with \\[kmacro-start-macro]. | |
534 | ||
535 | A prefix argument serves as a repeat count. Zero means repeat until error. | |
536 | ||
537 | To make a macro permanent so you can call it even after | |
538 | defining others, use M-x name-last-kbd-macro." | |
539 | (interactive "p") | |
540 | (call-last-kbd-macro arg #'kmacro-loop-setup-function)) | |
541 | ||
542 | ||
543 | ||
544 | ;;; Combined function key bindings: | |
545 | ||
546 | ;;;###autoload | |
547 | (defun kmacro-start-macro-or-insert-counter (arg) | |
7e30f58e | 548 | "Set `kmacro-counter' to ARG or 0 if missing, and `start-kbd-macro'. |
c30c4abe KS |
549 | With \\[universal-argument], append to current keyboard macro (keep kmacro-counter). |
550 | ||
551 | When defining/executing macro, insert macro counter and increment with | |
552 | ARG or 1 if missing. | |
553 | With \\[universal-argument], insert previous kmacro-counter (but do not modify counter). | |
554 | ||
1fa13bd4 | 555 | The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter]. |
c30c4abe | 556 | The format of the counter can be modified via \\[kmacro-set-format]." |
1fa13bd4 | 557 | (interactive "P") |
c30c4abe | 558 | (if (or defining-kbd-macro executing-kbd-macro) |
1fa13bd4 KS |
559 | (kmacro-insert-counter arg) |
560 | (kmacro-start-macro arg))) | |
c30c4abe | 561 | |
1fa13bd4 KS |
562 | |
563 | ;;;###autoload | |
564 | (defun kmacro-end-or-call-macro (arg) | |
c30c4abe | 565 | "End kbd macro if currently being defined; else call last kbd macro. |
7e30f58e | 566 | With numeric prefix ARG, repeat macro that many times. |
1fa13bd4 KS |
567 | With \\[universal-argument], call second macro in macro ring." |
568 | (interactive "P") | |
c30c4abe KS |
569 | (cond |
570 | (defining-kbd-macro | |
1fa13bd4 KS |
571 | (kmacro-end-macro arg)) |
572 | ((and arg (listp arg)) | |
573 | (kmacro-call-ring-2nd 1)) | |
c30c4abe | 574 | (t |
1fa13bd4 KS |
575 | (kmacro-call-macro arg)))) |
576 | ||
577 | ||
578 | (defun kmacro-end-or-call-macro-rep (arg) | |
579 | "As `kmacro-end-or-call-macro' but allows repeat without kmacro prefix." | |
580 | (interactive "P") | |
581 | (kmacro-end-or-call-macro arg) | |
582 | (kmacro-repeat-loop)) | |
583 | ||
584 | (put 'kmacro-end-or-call-macro-rep 'kmacro-repeat 'head) | |
c30c4abe | 585 | |
c30c4abe | 586 | |
1fa13bd4 | 587 | ;;;###autoload |
c30c4abe KS |
588 | (defun kmacro-end-call-mouse (event) |
589 | "Move point to the position clicked with the mouse and call last kbd macro. | |
590 | If kbd macro currently being defined end it before activating it." | |
591 | (interactive "e") | |
592 | (when defining-kbd-macro | |
1fa13bd4 | 593 | (end-kbd-macro)) |
c30c4abe | 594 | (mouse-set-point event) |
1fa13bd4 | 595 | (kmacro-call-macro nil)) |
c30c4abe | 596 | |
1fa13bd4 KS |
597 | |
598 | ;;; Misc. commands | |
599 | ||
600 | (defun kmacro-bind-to-key (arg) | |
601 | "When not defining or executing a macro, offer to bind last macro to a key." | |
c30c4abe KS |
602 | (interactive "p") |
603 | (if (or defining-kbd-macro executing-kbd-macro) | |
1fa13bd4 KS |
604 | (if defining-kbd-macro |
605 | (message "Cannot save macro while defining it.")) | |
606 | (unless last-kbd-macro | |
607 | (error "No keyboard macro defined")) | |
608 | (let ((key-seq (read-key-sequence "Bind last macro to key: "))) | |
609 | (unless (equal key-seq "\a") | |
610 | (define-key global-map key-seq last-kbd-macro))))) | |
c30c4abe | 611 | |
c30c4abe | 612 | |
1fa13bd4 KS |
613 | (defun kmacro-view-macro (&optional arg) |
614 | "Display the last keyboard macro." | |
615 | (interactive) | |
616 | (kmacro-display last-kbd-macro)) | |
c30c4abe | 617 | |
c30c4abe | 618 | |
1fa13bd4 KS |
619 | (defun kmacro-view-macro-rep (&optional arg) |
620 | "Like `kmacro-view-macro', but allow repeat without kmacro prefix." | |
c30c4abe | 621 | (interactive) |
1fa13bd4 KS |
622 | (kmacro-view-macro arg) |
623 | (if last-kbd-macro | |
624 | (kmacro-repeat-loop))) | |
625 | ||
626 | (put 'kmacro-view-macro-rep 'kmacro-repeat 'head) | |
627 | ||
628 | (defun kmacro-edit-macro (&optional arg) | |
629 | "Edit last keyboard macro." | |
630 | (interactive "P") | |
631 | (edit-kbd-macro "\r" arg)) | |
632 | ||
633 | (put 'kmacro-edit-macro 'kmacro-repeat 'stop) | |
634 | ||
635 | ||
636 | (defun kmacro-edit-macro-nr (&optional arg) | |
637 | "As edit last keyboard macro, but without kmacro-repeat property." | |
638 | (interactive "P") | |
639 | (kmacro-edit-macro arg)) | |
640 | ||
641 | ||
642 | (defun kmacro-edit-lossage () | |
643 | "Edit most recent 100 keystrokes as a keyboard macro." | |
644 | (interactive) | |
645 | (kmacro-push-ring) | |
646 | (edit-kbd-macro "\C-hl")) | |
c30c4abe | 647 | |
c30c4abe | 648 | |
7e30f58e SM |
649 | (provide 'kmacro) |
650 | ;;; kmacro.el ends here |