It is often a good idea to add extra commands (typically C-aand C-e) that aren't strictly necessary, just to make sure that you're positioned correctly on the line. The fewer assumptions that a macro makes, the better it works. So, if a sequence of commands works correctly only if you start at the end of the line, start the macro with C-e, even if you already "know" that you want to give the command only when you're at the end of the line.
Finally, while we're reciting rules and cautions, here's one more: keep in mind that you probably want to execute macros repeatedly. With a little foresight, you'll be able to create macros that can be executed in long chains without problems.
In general, good macros have three parts:
• They find the place you want the macro to start working (often using search).
• They do the work that needs to be done on the text.
• They prepare themselves to repeat.
How can a macro prepare itself to repeat? For example, assume that you're writing a macro to delete the third column of a table. After deleting the column, the macro should position itself at the beginning of the next line (or wherever it needs to be) so you don't have to reposition the cursor before reusing it.
Here's a slightly more complex example. If you start a macro with a search, you have to make sure that the end of the macro moves the cursor past the last spot you searched for. If you don't, the macro will keep finding the same place in the file and never go on to the next occurrence of what you're searching for. As a general rule, if your macro operates on a line of text, it should end by moving to the beginning of the next line. Remember that your goal is to create a sequence of keystrokes that can be executed many times in a row, with no interruption.
6.3 A More Complicated Macro Example
Sometimes you may want to find all the references to a particular topic in a file. Table 6-2lists steps for creating a macro that takes takes every sentence in the buffer that contains the word Emacs and copies it to another buffer. If you try this macro, you'll need to type some text about Emacs into a buffer. You can also get a test file to work with by opening the Emacs NEWS file (using C-h n), then writing it to a file ( C-x C-w NEWS). This buffer is in view mode by default; change to text mode by typing M-x text-mode Enter.
Table 6-2. Steps for macro that creates a buffer of Emacs references
Keystrokes |
Action |
F3 or C-x ( |
Start macro definition; Def appears on the mode line. |
C-s emacs |
Find the word Emacs. |
Enter |
Stop the search after it is successful; if the search is unsuccessful, it rings the bell and stops the macro. |
M-a |
Move to the beginning of the sentence. [34] |
C-Space |
Set the mark. |
M-e |
Move to the end of the sentence. |
M-w |
Copy the sentence to the kill ring. |
C-x b emacsrefs Enter |
Move to a buffer called emacsrefs . |
C-y |
Insert the sentence. |
Enter |
Start the next sentence on a new line. |
C-x b Enter |
Move back to the original buffer. |
F4 or C-x ) |
End the macro definition; Def is removed from the mode line. |
Now, assume that you've already constructed the macro outlined in Table 6-2and that you can invoke it with F4. The following screen shows what happens when you run it five times and then display the emacsrefs
buffer.
Type: M-5 F4or M-5 C-x e, followed by C-x b Enter

By executing the macro repeatedly, we've created a buffer that contains references to the Emacs editor.
As in the previous example, you can jump back and forth between an unlimited number of buffers while defining a macro. Macros don't need to be confined to one buffer. Macros that work with several buffers are more difficult to debug; when several buffers are involved, it becomes harder for you to keep track of where the cursor and the mark are. It is also easy to make mistaken assumptions about what buffer you're visiting; hence, it's a good idea to specify the buffer name explicitly. However, after you get accustomed to working with macros and multiple buffers, you'll be amazed at how much work you can do with almost no effort.
Windows are sometimes useful in macros, but, again, you have to watch out. It's better to start a macro with one window on the screen, have the macro open other windows, and finally close all but one window ( C-x 1). If you write a macro with two windows on the screen and later try to execute it with four windows on the screen, the results will be unpredictable at best! In general, moving to a named buffer, C-x b buffername
, is preferable to moving to the "other" window using C-x o(too vague to be generally useful). The other window could be anything—a *Help*
buffer, *Completion*
buffer, *shell*
buffer, and so on. Moving to a named buffer always gets you to the right place, no matter how (or whether) the buffer is displayed.
You can edit a macro and make changes to it in a few different ways. For this example, we chose an all-purpose editing command, edit-kbd-macro, which is bound to C-x C-k e. Several macro editing commands are available, but this one works for all types of macros, so it's good to learn.
Our macro could use a bit of tweaking. First of all, finding references to Emacs in our copy of the Emacs NEWS file is pretty lame. Perhaps we're interested in using a mouse more frequently with Emacs and would like to know about changes to that part of the interface. We'll edit the macro to search for the word mouse . We'll also modify it so it marks a paragraph rather than a sentence since a sentence doesn't really provide enough context to be helpful.
Let's start editing the macro.
Type: C-x C-k e

Emacs prompts you for the type of macro to edit.
Emacs asks you if you want to edit the last keyboard macro ( C-x e), a named macro ( M-x), the last 100 keystrokes as a macro, termed "lossage" ( C-h l), or keys (meaning the keystrokes you bound a macro to). Yes, that's a lot of choices, and later in the chapter we describe named macros and binding macros to keys (you can experiment on your own with creating a macro from lossage). For now, just choose C-x eto edit the last keyboard macro.
Type: C-x e

Emacs opens an *Edit Macro*
buffer.
Notice two fields near the top of this buffer, Command:
and Key:
. Right now, Command:
says last-kbd-macro
. If this were a named macro, the command would be the name you gave your macro. Additionally, for frequent use, you can bind your macro to a key, at which point the Key:
field lists the keystrokes to execute this macro. Right now it says none
because we haven't defined any keystrokes yet.
Читать дальше