Now, assume that you've already constructed the macro outlined in Table 6-2 and 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 F4 or 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.
6.4 Editing a Macro
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 e to 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.
Note that Emacs inserts comments all through the macro. It's attempting to map keystrokes to commands. You do not need to update these comments or add comments if you add commands to your macro; Emacs does that itself.
To tweak our macro, we change the search string on the second line from emacs to mouse. Note that we can just press C-k to wipe out the line and type mouse. Now change M-a to M-{ and M-e to M-}. We change the buffer name from emacsrefs to mouseinfo.
We've made the edits from the previous paragraph. The screen looks like this:
A modified macro that captures information about using a mouse in Emacs.
To exit the macro editing buffer, we have to type C-c C-c and go back to our NEWS buffer. Let's do that and then execute the macro again to see what happens.
Type: C-c C-c C-x b Enter M-< M-5 F4 C-x b Enter M-<
The mouseinfo buffer shows paragraphs from our copied NEWS file that mention the mouse.
6.5 The Macro Ring
Although our latest macro is interesting, it's not really a general purpose macro. It is a temporary solution to a one-time problem. It saves you some work, but it isn't general enough to save and use again. On the other hand, our macro to transpose names is generally useful. We'd like to use it again. We'd like to bind it to a key. But it is no longer the "latest" keyboard macro.
As we mentioned earlier, Emacs has a macro ring much like the infamous kill ring. It's useful in the case we've just described, but it's also useful because of the fragility of the macro definition process. You create a macro and make a wrong move that rings the bell, and your macro is canceled. It's fairly easy to create a macro that does nothing. Perhaps the macro that you just created was wonderful, and this new nonfunctional nothing macro has supplanted it. Again, the macro ring is the solution. To delete a macro from the ring, type C-x C-k C-d (for kmacro-delete-ring-head). This deletes the most recently defined keyboard macro.
What if you want to swap the positions of two macros? Instead, type C-x C-k C-t (for kmacro-swap-ring). This transposes macros 1 and 2.