Выбрать главу

6.9 Executing Macros on a Region

A special command lets you execute a macro on each line in a region. How frequently do you encounter an email with text that you want to yank, but that is quoted several indentation levels? Of course, we can think of several ways to delete the indentation quickly, but a line-oriented macro is a quick approach too. You define the macro and execute it on a region by typing C-x C-k r (for apply-macro-to-region-lines). Remember that earlier we said that macros should set themselves up to repeat? This command is different because it expects to work on one line at a time. You don't want to set it up to repeat by moving to the next line; it does that automatically.

Table 6-5 shows a quick line-oriented macro that deletes indentation marks from text quoted in an email or newsgroup message.

Table 6-5. Macro for deleting indentation marks

Keystrokes Action
F3 Start the macro definition.
C-a Move to the beginning of the line.
M-f Move forward one word.
M-b Move to the beginning of this word.
C-Space Set the mark.
C-a Move to the beginning of the line.
C-w Delete the extraneous indentation characters.
F4 End the macro definition.

Initial state:

Text indented at various levels (Mac OS X).

Mark the text as a region, move to the beginning of the region, then type: C-x C-k r

Indentation is deleted (Mac OS X).

6.10 Beyond Macros

Macros are an important tool for streamlining repetitive editing. They let you write your own commands for performing complex tasks without needing to know anything more than you already know: the basic Emacs commands for moving around and manipulating text. Even if you're an Emacs novice, you should be able to use macros with little difficulty.

However, Emacs is almost infinitely flexible, and macros cannot do everything. In many situations, there's no substitute for writing a Lisp function that does exactly what you want. If you know Lisp or would like to learn some, you can write your own Lisp functions to do more complex tasks than keyboard macros can handle. Chapter 11 covers the basics of writing Lisp functions.

Table 6-6 summarizes macro commands.

Table 6-6. Macro commands

Keystrokes Command name Action
C-x ( kmacro-start-macro Start macro definition.
F3 kmacro-start-macro-or-insert-counter Start macro definition. If pressed while defining a macro, insert a counter.
C-x ) kmacro-end-macro End macro definition.
F4 kmacro-end-or-call-macro End macro definition (if definition is in progress) or invoke last keyboard macro.
C-x e kmacro-end-and-call-macro Execute last keyboard macro defined. Can type e to repeat macro.
C-x C-k n name-last-kbd-macro Name the last macro you created (before saving it).
(none) insert-kbd-macro Insert the macro you named into a file.
(none) macroname Execute a named keyboard macro.
C-x q kbd-macro-query Insert a query in a macro definition.
C-u C-x q (none) Insert a recursive edit in a macro definition.
C-M-c exit-recursive-edit Exit a recursive edit.
C-x C-k b kmacro-bind-to-key Bind a macro to a key (C-x C-k 0-9 and A-Z are reserved for macro bindings). Lasts for current session only.
C-x C-k Space kmacro-step-edit-macro Edit a macro while stepping through it (in our opinion, the interface is overly complex).
C-x C-k l kmacro-edit-lossage Turn the last 100 keystrokes into a keyboard macro. If any mouse clicks are among the last 100 keystrokes, does not work.
C-x C-k e edit-kbd-macro Edit a keyboard macro by typing C-x e for the last keyboard macro defined, M-x for a named macro, C-h l for lossage, or keystrokes for a macro bound to a key.
C-x C-k Enter kmacro-edit-macro Edit the last keyboard macro.
C-x C-k C-e kmacro-edit-macro-repeat Edit the last keyboard macro again.
C-x C-k C-t kmacro-swap-ring Transpose last keyboard macro with previous keyboard macro.
C-x C-k C-d kmacro-delete-ring-head Delete last keyboard macro from the macro ring.
C-x C-k C-p kmacro-cycle-ring-previous Move to the previous macro in the macro ring.
C-x C-k C-n kmacro-cycle-ring-next Move to the next macro in the macro ring.
C-x C-k r apply-macro-to-region-lines Apply this macro to each line in a region.