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