aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim F. Storm2003-08-17 22:10:38 +0000
committerKim F. Storm2003-08-17 22:10:38 +0000
commiteef59362ac0323dc9c8474dae6890ea5782d289f (patch)
treea6a5cee117798f207b89d617a55c61a33d38da8b
parenta1839f06220e09b957ff6fcbc5d5930f0e4807a5 (diff)
downloademacs-eef59362ac0323dc9c8474dae6890ea5782d289f.tar.gz
emacs-eef59362ac0323dc9c8474dae6890ea5782d289f.zip
New file describing enhanced keyboard macro functionality.
-rw-r--r--man/kmacro.texi525
1 files changed, 525 insertions, 0 deletions
diff --git a/man/kmacro.texi b/man/kmacro.texi
new file mode 100644
index 00000000000..9d7810ad3f4
--- /dev/null
+++ b/man/kmacro.texi
@@ -0,0 +1,525 @@
1@c This is part of the Emacs manual.
2@c Copyright (C) 1985,86,87,93,94,95,97,2000,2001,2002,2003
3@c Free Software Foundation, Inc.
4@c See file emacs.texi for copying conditions.
5@node Keyboard Macros, Files, Fixit, Top
6@chapter Keyboard Macros
7@cindex defining keyboard macros
8@cindex keyboard macro
9
10
11@node Keyboard Macros
12@section Keyboard Macros
13
14@cindex defining keyboard macros
15@cindex keyboard macro
16
17 In this chapter we describe how a sequence of editing commands can
18be recorded and repeated multiple times.
19
20 A @dfn{keyboard macro} is a command defined by the user to stand for
21another sequence of keys. For example, if you discover that you are
22about to type @kbd{C-n C-d} forty times, you can speed your work by
23defining a keyboard macro to do @kbd{C-n C-d} and calling it with a
24repeat count of forty.
25
26 You define a keyboard macro while executing the commands which are the
27definition. Put differently, as you define a keyboard macro, the
28definition is being executed for the first time. This way, you can see
29what the effects of your commands are, so that you don't have to figure
30them out in your head. When you are finished, the keyboard macro is
31defined and also has been, in effect, executed once. You can then do the
32whole thing over again by invoking the macro.
33
34 Keyboard macros differ from ordinary Emacs commands in that they are
35written in the Emacs command language rather than in Lisp. This makes it
36easier for the novice to write them, and makes them more convenient as
37temporary hacks. However, the Emacs command language is not powerful
38enough as a programming language to be useful for writing anything
39intelligent or general. For such things, Lisp must be used.
40
41@menu
42* Basic Keyboard Macro:: Defining and running keyboard macros.
43* Keyboard Macro Ring:: Where previous keyboard macros are saved.
44* Keyboard Macro Counter:: Inserting incrementing numbers in macros.
45* Keyboard Macro Query:: Making keyboard macros do different things each time.
46* Save Keyboard Macro:: Giving keyboard macros names; saving them in files.
47* Edit Keyboard Macro:: Editing keyboard macros.
48* Keyboard Macro Step-Edit:: Interactively executing and editing a keyboard macro.
49@end menu
50
51@node Basic Keyboard Macro
52@section Basic Use
53
54@table @kbd
55@item C-x (
56Start defining a keyboard macro (@code{kmacro-start-macro}).
57@item C-x )
58End the definition of a keyboard macro (@code{kmacro-end-macro}).
59@item C-x e
60Execute the most recent keyboard macro (@code{kmacro-end-and-call-macro}).
61First end the definition of the keyboard macro, if currently defining it.
62To immediately execute the keyboard macro again, just repeat the @kbd{e}.
63@item C-u C-x (
64Re-execute last keyboard macro, then add more keys to its definition.
65@item C-u C-u C-x (
66Add more keys to the last keyboard macro without re-executing it.
67@item C-x q
68When this point is reached during macro execution, ask for confirmation
69(@code{kbd-macro-query}).
70@item C-x C-k n
71Give a command name (for the duration of the session) to the most
72recently defined keyboard macro (@code{name-last-kbd-macro}).
73@item C-x C-k b
74Bind the most recently defined keyboard macro to a key sequence (for
75the duration of the session) (@code{kmacro-bind-to-key}).
76@item M-x insert-kbd-macro
77Insert in the buffer a keyboard macro's definition, as Lisp code.
78@item C-x C-k e
79Edit a previously defined keyboard macro (@code{edit-kbd-macro}).
80@item C-x C-k r
81Run the last keyboard macro on each complete line in the region
82(@code{apply-macro-to-region-lines}).
83@end table
84
85@kindex C-x (
86@kindex C-x )
87@kindex C-x e
88@findex kmacro-start-macro
89@findex kmacro-end-macro
90@findex kmacro-end-and-call-macro
91 To start defining a keyboard macro, type the @kbd{C-x (} command
92(@code{kmacro-start-macro}). From then on, your keys continue to be
93executed, but also become part of the definition of the macro. @samp{Def}
94appears in the mode line to remind you of what is going on. When you are
95finished, the @kbd{C-x )} command (@code{kmacro-end-macro}) terminates the
96definition (without becoming part of it!). For example,
97
98@example
99C-x ( M-f foo C-x )
100@end example
101
102@noindent
103defines a macro to move forward a word and then insert @samp{foo}.
104
105 The macro thus defined can be invoked again with the @kbd{C-x e}
106command (@code{kmacro-end-and-call-macro}), which may be given a
107repeat count as a numeric argument to execute the macro many times.
108If you enter @kbd{C-x e} while defining a macro, the macro is
109terminated and executed immediately.
110
111 After executing the macro with @kbd{C-x e}, you can use @kbd{e}
112repeatedly to immediately repeat the macro one or more times. For example,
113
114@example
115C-x ( xyz C-x e e e
116@end example
117
118@noindent
119inserts @samp{xyzxyzxyzxyz} in the current buffer.
120
121 @kbd{C-x )} can also be given a repeat count as an argument, in
122which case it repeats the macro that many times right after defining
123it, but defining the macro counts as the first repetition (since it is
124executed as you define it). Therefore, giving @kbd{C-x )} an argument
125of 4 executes the macro immediately 3 additional times. An argument
126of zero to @kbd{C-x e} or @kbd{C-x )} means repeat the macro
127indefinitely (until it gets an error or you type @kbd{C-g} or, on
128MS-DOS, @kbd{C-@key{BREAK}}).
129
130@kindex C-x C-k C-s
131@kindex C-x C-k C-k
132Alternatively, you can use @kbd{C-x C-k C-s} to start a keyboard macro,
133and @kbd{C-x C-k C-k...} to end and execute it.
134
135 If you wish to repeat an operation at regularly spaced places in the
136text, define a macro and include as part of the macro the commands to move
137to the next place you want to use it. For example, if you want to change
138each line, you should position point at the start of a line, and define a
139macro to change that line and leave point at the start of the next line.
140Then repeating the macro will operate on successive lines.
141
142 When a command reads an argument with the minibuffer, your
143minibuffer input becomes part of the macro along with the command. So
144when you replay the macro, the command gets the same argument as
145when you entered the macro. For example,
146
147@example
148C-x ( C-a C-@key{SPC} C-n M-w C-x b f o o @key{RET} C-y C-x b @key{RET} C-x )
149@end example
150
151@noindent
152defines a macro that copies the current line into the buffer
153@samp{foo}, then returns to the original buffer.
154
155 You can use function keys in a keyboard macro, just like keyboard
156keys. You can even use mouse events, but be careful about that: when
157the macro replays the mouse event, it uses the original mouse position
158of that event, the position that the mouse had while you were defining
159the macro. The effect of this may be hard to predict. (Using the
160current mouse position would be even less predictable.)
161
162 One thing that doesn't always work well in a keyboard macro is the
163command @kbd{C-M-c} (@code{exit-recursive-edit}). When this command
164exits a recursive edit that started within the macro, it works as you'd
165expect. But if it exits a recursive edit that started before you
166invoked the keyboard macro, it also necessarily exits the keyboard macro
167as part of the process.
168
169 After you have terminated the definition of a keyboard macro, you can add
170to the end of its definition by typing @kbd{C-u C-x (}. This is equivalent
171to plain @kbd{C-x (} followed by retyping the whole definition so far. As
172a consequence it re-executes the macro as previously defined.
173
174 You can also add to the end of the definition of the last keyboard
175macro without re-execuing it by typing @kbd{C-u C-u C-x (}.
176
177 The variable @code{kmacro-execute-before-append} specifies whether
178a single @kbd{C-u} prefix causes the existing macro to be re-executed
179before appending to it.
180
181@findex apply-macro-to-region-lines
182@kindex C-x C-k r
183 The command @kbd{C-x C-k r} (@code{apply-macro-to-region-lines})
184repeats the last defined keyboard macro on each complete line within
185the current region. It does this line by line, by moving point to the
186beginning of the line and then executing the macro.
187
188@node Keyboard Macro Ring
189@section Where previous keyboard macros are saved
190
191 All defined keyboard macros are recorded in the ``keyboard macro ring'',
192a list of sequences of keys. There is only one keyboard macro ring,
193shared by all buffers.
194
195 All commands which operates on the keyboard macro ring use the
196same @kbd{C-x C-k} prefix. Most of these commands can be executed and
197repeated immediately after each other without repeating the @kbd{C-x
198C-k} prefix. For example,
199
200@example
201C-x C-k C-p C-p C-k C-k C-k C-n C-n C-k C-p C-k C-d
202@end example
203
204@noindent
205will rotate the keyboard macro ring to the ``second previous'' macro,
206execute the resulting head macro three times, rotate back to the
207original head macro, execute that once, rotate to the ``previous''
208macro, execute that, and finally delete it from the macro ring.
209
210@findex kmacro-end-or-call-macro-repeat
211@kindex C-x C-k C-k
212 The command @kbd{C-x C-k C-k} (@code{kmacro-end-or-call-macro-repeat})
213executes the keyboard macro at the head of the macro ring. You can
214repeat the macro immediately by typing another @kbd{C-k}, or you can
215rotate the macro ring immediately by typing @kbd{C-n} or @kbd{C-p}.
216
217@findex kmacro-cycle-ring-next
218@kindex C-x C-k C-n
219@findex kmacro-cycle-ring-previous
220@kindex C-x C-k C-p
221 The commands @kbd{C-x C-k C-n} (@code{kmacro-cycle-ring-next}) and
222@kbd{C-x C-k C-p} (@code{kmacro-cycle-ring-previous}) rotates the
223macro ring, bringing the next or previous keyboard macro to the head
224of the macro ring. The definition of the new head macro is displayed
225in the echo area. You can continue to rotate the macro ring
226immediately by repeating just @kbd{C-n} and @kbd{C-p} until the
227desired macro is at the head of the ring. To execute the new macro
228ring head immediately, just type @kbd{C-k}.
229
230@findex kmacro-view-macro-repeat
231@kindex C-x C-k C-v
232
233 The commands @kbd{C-x C-k C-v} (@code{kmacro-view-macro-repeat})
234displays the last keyboard macro, or when repeated (with @kbd{C-v}),
235it displays the previous macro on the macro ring, just like @kbd{C-x
236C-k C-p}, but without actually rotating the macro ring. If you enter
237@kbd{C-k} immediately after displaying a macro from the ring, that
238macro is executed, but still without altering the macro ring.
239
240 So while e.g. @kbd{C-x C-k C-p C-p C-k C-k} makes the 3rd previous
241macro the current macro and executes it twice, @kbd{C-x C-k C-v C-v
242C-v C-k C-k} will display and execute the 3rd previous macro once and
243then the current macro once.
244
245@findex kmacro-delete-ring-head
246@kindex C-x C-k C-d
247
248 The commands @kbd{C-x C-k C-d} (@code{kmacro-delete-ring-head})
249removes and deletes the macro currently at the head of the macro
250ring. You can use this to delete a macro that didn't work as
251expected, or which you don't need anymore.
252
253@findex kmacro-swap-ring
254@kindex C-x C-k C-t
255
256 The commands @kbd{C-x C-k C-t} (@code{kmacro-swap-ring})
257interchanges the head of the macro ring with the previous element on
258the macro ring.
259
260@findex kmacro-call-ring-2nd-repeat
261@kindex C-x C-k C-l
262
263 The commands @kbd{C-x C-k C-l} (@code{kmacro-call-ring-2nd-repeat})
264executes the previous (rather than the head) element on the macro ring.
265
266@node Keyboard Macro Counter
267@section Inserting incrementing numbers in macros
268
269 Each keyboard macro has an associated counter which is automatically
270incremented on every repetition of the keyboard macro. Normally, the
271macro counter is initialized to 0 when you start defining the macro,
272and incremented by 1 after each insertion of the counter value;
273that is, if you insert the macro counter twice while defining the
274macro, it will be incremented by 2 time for each repetition of the
275macro.
276
277@findex kmacro-insert-counter
278@kindex C-x C-k C-i
279 The command @kbd{C-x C-k C-i} (@code{kmacro-insert-counter}) inserts
280the current value of the keyboard macro counter and increments the
281counter by 1. You can use a numeric prefix argument to specify a
282different increment. If you specify a @kbd{C-u} prefix, the last
283inserted counter value is repeated and the counter is not incremented.
284For example, if you enter the following sequence while defining a macro
285
286@example
287C-x C-k C-i C-x C-k C-i C-u C-x C-k C-i C-x C-k C-i
288@end example
289
290@noindent
291the text @samp{0112} is inserted in the buffer, and for the first and
292second execution of the macro @samp{3445} and @samp{6778} are
293inserted.
294
295@findex kmacro-set-counter
296@kindex C-x C-k C-c
297 The command @kbd{C-x C-k C-c} (@code{kmacro-set-counter}) prompts
298for the initial value of the keyboard macro counter if you use it
299before you define a keyboard macro. If you use it while defining a
300keyboard macro, you set the macro counter to the same (initial) value
301on each repetition of the macro. If you specify a @kbd{C-u} prefix,
302the counter is reset to the value it had prior to the current
303repetition of the macro (undoing any increments so far in this
304repetition).
305
306@findex kmacro-add-counter
307@kindex C-x C-k C-a
308 The command @kbd{C-x C-k C-a} (@code{kmacro-add-counter}) prompts
309for a value to add to the macro counter.
310
311@findex kmacro-set-format
312@kindex C-x C-k C-f
313 The command @kbd{C-x C-k C-f} (@code{kmacro-set-format}) prompts
314for the format to use when inserting the macro counter. The default
315format is @samp{%d}. If you set the counter format before you define a
316macro, that format is restored before each repetition of the macro.
317Consequently, any changes you make to the macro counter format while
318defining a macro are only active for the rest of the macro.
319
320@node Keyboard Macro Query
321@section Executing Macros with Variations
322
323@kindex C-x q
324@findex kbd-macro-query
325 Using @kbd{C-x q} (@code{kbd-macro-query}), you can get an effect
326similar to that of @code{query-replace}, where the macro asks you each
327time around whether to make a change. While defining the macro,
328type @kbd{C-x q} at the point where you want the query to occur. During
329macro definition, the @kbd{C-x q} does nothing, but when you run the
330macro later, @kbd{C-x q} asks you interactively whether to continue.
331
332 The valid responses when @kbd{C-x q} asks are @key{SPC} (or @kbd{y}),
333@key{DEL} (or @kbd{n}), @key{RET} (or @kbd{q}), @kbd{C-l} and @kbd{C-r}.
334The answers are the same as in @code{query-replace}, though not all of
335the @code{query-replace} options are meaningful.
336
337 These responses include @key{SPC} to continue, and @key{DEL} to skip
338the remainder of this repetition of the macro and start right away with
339the next repetition. @key{RET} means to skip the remainder of this
340repetition and cancel further repetitions. @kbd{C-l} redraws the screen
341and asks you again for a character to say what to do.
342
343 @kbd{C-r} enters a recursive editing level, in which you can perform
344editing which is not part of the macro. When you exit the recursive
345edit using @kbd{C-M-c}, you are asked again how to continue with the
346keyboard macro. If you type a @key{SPC} at this time, the rest of the
347macro definition is executed. It is up to you to leave point and the
348text in a state such that the rest of the macro will do what you
349want.@refill
350
351 @kbd{C-u C-x q}, which is @kbd{C-x q} with a numeric argument,
352performs a completely different function. It enters a recursive edit
353reading input from the keyboard, both when you type it during the
354definition of the macro, and when it is executed from the macro. During
355definition, the editing you do inside the recursive edit does not become
356part of the macro. During macro execution, the recursive edit gives you
357a chance to do some particularized editing on each repetition.
358@xref{Recursive Edit}.
359
360 Another way to vary the behavior of a keyboard macro is to use a
361register as a counter, incrementing it on each repetition of the macro.
362@xref{RegNumbers}.
363
364@node Save Keyboard Macro
365@section Naming and Saving Keyboard Macros
366
367@cindex saving keyboard macros
368@findex name-last-kbd-macro
369@kindex C-x C-k n
370 If you wish to save a keyboard macro for later use, you can give it
371a name using @kbd{C-x C-k n} (@code{name-last-kbd-macro}).
372This reads a name as an argument using the minibuffer and defines that name
373to execute the macro. The macro name is a Lisp symbol, and defining it in
374this way makes it a valid command name for calling with @kbd{M-x} or for
375binding a key to with @code{global-set-key} (@pxref{Keymaps}). If you
376specify a name that has a prior definition other than another keyboard
377macro, an error message is shown and nothing is changed.
378
379@cindex binding keyboard macros
380@findex kmacro-bind-to-key
381@kindex C-x C-k b
382 Rather than giving a keyboard macro a name, you can bind it to a
383key using @kbd{C-x C-k b} (@code{kmacro-bind-to-key}) followed by the
384key sequence you want the keyboard macro to be bound to. You can
385bind to any key sequence in the global keymap, but since most key
386sequences already have other bindings, you should select the key
387sequence carefylly. If you try to bind to a key sequence with an
388existing binding (in any keymap), you will be asked if you really
389want to replace the existing binding of that key.
390
391To avoid problems caused by overriding existing bindings, the key
392sequences @kbd{C-x C-k 0} through @kbd{C-x C-k 9} and @kbd{C-x C-k A}
393through @kbd{C-x C-k Z} are reserved for your own keyboard macro
394bindings. In fact, to bind to one of these key sequences, you only
395need to type the digit or letter rather than the whole key sequences.
396For example,
397
398@example
399C-x C-k b 4
400@end example
401
402@noindent
403will bind the last keyboard macro to the key sequence @kbd{C-x C-k 4}.
404
405@findex insert-kbd-macro
406 Once a macro has a command name, you can save its definition in a file.
407Then it can be used in another editing session. First, visit the file
408you want to save the definition in. Then use this command:
409
410@example
411M-x insert-kbd-macro @key{RET} @var{macroname} @key{RET}
412@end example
413
414@noindent
415This inserts some Lisp code that, when executed later, will define the
416same macro with the same definition it has now. (You need not
417understand Lisp code to do this, because @code{insert-kbd-macro} writes
418the Lisp code for you.) Then save the file. You can load the file
419later with @code{load-file} (@pxref{Lisp Libraries}). If the file you
420save in is your init file @file{~/.emacs} (@pxref{Init File}) then the
421macro will be defined each time you run Emacs.
422
423 If you give @code{insert-kbd-macro} a numeric argument, it makes
424additional Lisp code to record the keys (if any) that you have bound to the
425keyboard macro, so that the macro will be reassigned the same keys when you
426load the file.
427
428@node Edit Keyboard Macro
429@section Interactively executing and editing a keyboard macro
430
431@findex kmacro-edit-macro
432@kindex C-x C-k C-e
433@kindex C-x C-k RET
434 You can edit the last keyboard macro by typing @kbd{C-x C-k C-e} or
435@kbd{C-x C-k RET} (@code{kmacro-edit-macro}). This formats the macro
436definition in a buffer and enters a specialized major mode for editing
437it. Type @kbd{C-h m} once in that buffer to display details of how to
438edit the macro. When you are finished editing, type @kbd{C-c C-c}.
439
440@findex edit-kbd-macro
441@kindex C-x C-k e
442 You can edit a named keyboard macro or a macro bound to a key by typing
443@kbd{C-x C-k e} (@code{edit-kbd-macro}). Follow that with the
444keyboard input that you would use to invoke the macro---@kbd{C-x e} or
445@kbd{M-x @var{name}} or some other key sequence.
446
447@findex kmacro-edit-lossage
448@kindex C-x C-k l
449 You can edit the last 100 keystrokes as a macro by typing
450@kbd{C-x C-k l} (@code{kmacro-edit-lossage}).
451
452@node Keyboard Macro Step-Edit
453@section Interactively executing and editing a keyboard macro
454
455@findex kmacro-step-edit-macro
456@kindex C-x C-k SPC
457 You can interactively and stepwise replay and edit the last keyboard
458macro one command at a time by typing @kbd{C-x C-k SPC}
459(@code{kmacro-step-edit-macro}). Unless you quit the macro using
460@kbd{q} or @kbd{C-g}, the edited macro replaces the last macro on the
461macro ring.
462
463This shows the last macro in the minibuffer together with the first
464(or next) command to be executed, and prompts you for an action.
465You can enter @kbd{?} to get a command summary.
466
467The following commands are available in the step-edit mode and relate
468to the first (or current) command in the keyboard macro:
469
470@itemize @bullet{}
471@item
472@kbd{SPC} and @kbd{y} execute the current command, and advance to the
473next command in the keyboard macro.
474@item
475@kbd{n}, @kbd{d}, and @kbd{DEL} skip and delete the current command.
476@item
477@kbd{f} skips the current command in this execution of the keyboard
478macro, but doesn't delete it from the macro.
479@item
480@kbd{TAB} executes the current command, as well as all similar
481commands immediately following the current command; for example, TAB
482may be used to insert a sequence of characters (corresponding to a
483sequence of @code{self-insert-command} commands).
484@item
485@kbd{c} continues execution (without further editing) until the end of
486the keyboard macro. If execution terminates normally, the edited
487macro replaces the original keyboard macro.
488@item
489@kbd{C-k} skips and deletes the rest of the keyboard macro,
490terminates step-editing, and replaces the original keyboard macro
491with the edited macro.
492@item
493@kbd{q} and @kbd{C-g} cancels the step-editing of the keyboard macro;
494discarding any changes made to the keyboard macro.
495@item
496@kbd{i KEY... C-j} reads and executes a series of key sequences (not
497including the final @kbd{C-j}), and inserts them before the current
498command in the keyboard macro, without advancing over the current
499command.
500@item
501@kbd{I KEY...} reads one key sequence, executes it, and inserts it
502before the current command in the keyboard macro, without advancing
503over the current command.
504@item
505@kbd{r KEY... C-j} reads and executes a series of key sequences (not
506including the final @kbd{C-j}), and replaces the current command in
507the keyboard macro with them, advancing over the inserted key
508sequences.
509@item
510@kbd{R KEY...} reads one key sequence, executes it, and replaces the
511current command in the keyboard macro with that key sequence,
512advancing over the inserted key sequence.
513@item
514@kbd{a KEY... C-j} executes the current command, then reads and
515executes a series of key sequences (not including the final
516@kbd{C-j}), and inserts them after the current command in the keyboard
517macro; it then advances over the current command and the inserted key
518sequences.
519@item
520@kbd{A KEY... C-j} executes the rest of the commands in the keyboard
521macro, then reads and executes a series of key sequences (not
522including the final @kbd{C-j}), and appends them at the end of the
523keyboard macro; it then terminates the step-editing and replaces the
524original keyboard macro with the edited macro.
525@end itemize