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