22.4 Executing Macros with Variations
Using C-x q (kbd-macro-query
), you can get an effect
similar to that of query-replace
, where the macro asks you each
time around whether to make a change. While defining the macro,
type C-x q at the point where you want the query to occur. During
macro definition, the C-x q does nothing, but when you run the
macro later, C-x q asks you interactively whether to continue.
The valid responses when C-x q asks are <SPC> (or y),
<DEL> (or n), <RET> (or q), C-l and C-r.
The answers are the same as in query-replace
, though not all of
the query-replace
options are meaningful.
These responses include <SPC> to continue, and <DEL> to skip
the remainder of this repetition of the macro and start right away with
the next repetition. <RET> means to skip the remainder of this
repetition and cancel further repetitions. C-l redraws the screen
and asks you again for a character to say what to do.
C-r enters a recursive editing level, in which you can perform
editing which is not part of the macro. When you exit the recursive
edit using C-M-c, you are asked again how to continue with the
keyboard macro. If you type a <SPC> at this time, the rest of the
macro definition is executed. It is up to you to leave point and the
text in a state such that the rest of the macro will do what you
want.
C-u C-x q, which is C-x q with a numeric argument,
performs a completely different function. It enters a recursive edit
reading input from the keyboard, both when you type it during the
definition of the macro, and when it is executed from the macro. During
definition, the editing you do inside the recursive edit does not become
part of the macro. During macro execution, the recursive edit gives you
a chance to do some particularized editing on each repetition.
See Recursive Edit.
Another way to vary the behavior of a keyboard macro is to use a
register as a counter, incrementing it on each repetition of the macro.
See RegNumbers.