Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 13: Macros

Previous HourNext Hour

Sections in this Hour:

 

Making Macros that Ask for Permission to Continue


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).

Replacing printf(...) with write {...}.the Secure Way

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.


Follow these steps:

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.

7. Press C-x ) (end-kbd-macro) to stop recording the macro.

Caution - Note that this macro does not fulfill the requirement for macros that should be executed with sams-apply-macro-on-region. This is not a problem if all occurrences should be replaced. But it is a problem if only some of them should be replaced, because the macro will continue beyond the limit of the region and do one more replacement before it stops.


In Figure 13.1, you can see how Emacs asks you for permission to continue the macro. You have the following choices:

Caution - Emacs suggests the keys Y, N, and Enter, whereas XEmacs suggests the keys Space, Delete, and Q. Don't let that confuse you, because all six key bindings work in both GNU Emacs and XEmacs. Adapt to the ones that your version of Emacs suggests.


Figure 13.1
Emacs asks for permission to continue executing the macro.

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

ContentsIndex

Hour 13: Macros

Previous HourNext Hour

Sections in this Hour: