Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||||||||||
Hour 13: Macros |
||||||||||||||||||||||
|
Sometimes when you create a macro to do the same thing over and over again hundreds of times, you might be afraid that your macro will accidentally change text that should not have been changed. As an example, think of the macro that replaces printf(...) with write {...}. This macro can search forward for the next occurrences of printf, replace it with write, and do some more stuff to replace the parentheses with curly braces. This macro might fail to do what you want it to do because you also have occurrences of sprintf that you don't want to be replaced. The solution to your problem is to pause the macro just before it does the replacement. To do this, press C-x q (kbd-macro-query).
This task creates a macro that replaces printf(...) with write {...}. The macro pauses just before the replacement takes place, so the user can validate whether the replacement should take place.
Note - It wouldn't be necessary to use a macro, were it not for the ending parenthesis that must be replaced with a brace. Regular expression replacement wouldn't do it either, because braces might exist within the parentheses that should be replaced. |
1. Press C-x ( (start-kbd-macro) to record the macro. Press C-s (isearch-forward) and type printf(. This searches forward for the next occurrence of printf(.
2. Press C-x q (kbd-macro-query) to make the macro stop for validation at this point.
3. Press the left arrow key to place the point before the parenthesis.
4. Press C-@ (set-mark-command) to set the mark (this makes it possible to get back to this point where you replace the ending parenthesis).
5. Press M-x (forward-sexp). This brings you to the ending parenthesis, replacing the parenthesis with a brace.
6. Press C-x C-x (exchange-point-and-mark), which sets the point at the location where the mark is (that is, before the opening parenthesis), replacing it with a brace.
In Figure 13.1, you can see how Emacs asks you for permission to continue the macro. You have the following choices:
Y or Spacebar--This tells Emacs that the macro can continue.
N or Delete--This tells Emacs that you do not want the macro to continue for this iteration, but if you have invoked, for example, sams-apply-macro-on-region, it can continue to execute the macro.
Q or Enter (or Return)--This is like N, but the difference is that sams-apply-macro-on-region will not continue either. In other words, if you want the macro to repeatedly execute throughout a region (or a number of times, given with prefix argument), this will be terminated and the macro will not be executed at all anymore.
C-l (that is, Control and a lowercase L)--Pressing C-l recenters the page, so you can see the context of the location.
C-r--This enters recursive editing. This is just like recursive editing in search-and-replace (see Hour 7, "Searching for Text in a Buffer").
Tip - Another (or an extra) check is to create a copy of the file before you let your macro do its work. Afterwards you can then use ediff to validate that only the intended changes were made. |
Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||||||||||
Hour 13: Macros |
||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.