Next: , Previous: Basic Preferences, Up: Basics


2.6 Basic Macros

Very often, the programmer or the text writer has to repeat some complex editing action over a series of similar blocks of text. This is where macros come in.

A macro is a stored sequence of commands. Any sequence of commands you find yourself repeating is an excellent candidate for being made into a macro. You could create a macro by editing a document that only contains valid ne commands and saving it, but by far the easiest way to create a macro is to have ne record your actions. ne allows you to record macros and then play them (execute the commands they contain) many times. You can save them on disk for future use, edit them, or bind them to any key. You could even reconfigure each key of your keyboard to play a complex macro if you wanted to.

ne can have any number of named macros loaded at the same time. It can also have one unnamed macro in its current macro buffer. The named macros are typically loaded from disk files, while the current macro buffer is where your recorded macro is held before you save it or record over it.

Recording a macro is very simple. The keystroke <Control>-T starts and stops recording a macro. When you start recording a macro, ne clears the current macro buffer and starts recording all your actions (with a few exceptions). You can see that you are recording a macro if an ‘R’ appears on the status bar. When you stop the recording process (again using <Control>-T), you can play the macro with the ‘Play Once’ item of the ‘Macros’ menu or with the <f9> key. If you want to repeat the action many times, the Play command allows you to specify a number of times to repeat the macro. You can always interrupt the macro's execution with <Control>-\.

A recorded macro has no name. It's just an anonymous sequence of commands in the current macro buffer, and it will go away when you exit ne or record another macro. If you want to save your recorded macro for future use, you can give it a name and save it with the ‘Save Macro...’ menu item or the SaveMacro command. The macro is saved as a file in your current directory by default or whatever directory you specify when prompted for the macro's name. If you save it in your ~/.ne directory then it will be easy to access it later from any other directory. The ‘Open Macro...’ menu item and the OpenMacro command load a macro from a file into the current macro buffer just as if you just Recorded it.

Any macro can be loaded from a file and played with the ‘Play Macro...’ menu item or the Macro command. (This won't modify any recorded anonymous macro that may be in the current macro buffer; OpenMacro does that.) Useful macros can be permanently bound to a keystroke as explained in Key Bindings. Moreover, whenever a command line does not specify one of ne's built in commands, it is assumed to specify the name of a macro to execute. Thus, you can execute macros just by typing their file names. Include a path if the macro file's directory is different from your current directory or your ~/.ne directory.

If the first attempt to open a macro fails, ne checks for a macro with the given name in your ~/.ne directory. This allows you to program simple extensions to ne's language. For instance, all automatic preferences macros—which are just specially named macros that contain only commands to set preferences flags—can be executed just by typing their names. For example, if you have an automatic preference for the ‘doc’ extension for example, you can set ne's flags exactly as if you loaded a file ending with ‘.doc’ by typing the command doc#ap.

In general, it is a good idea to save frequently used macros in ~/.ne so that you can invoke them by name without specifying a path regardless of your current directory. On the other hand, if you have a macro that is customized for one document or a set of documents that you store in one directory, then you might want to save the macro in that directory as well. If you do, then you would want to cd to that directory before you start ne so that you can access that macro without specifying a path.

If your macro has the same name as one of ne's built-in commands, you can only access it with the Macro name command. Built-in command names are always found first before ne command interpreter looks for macros.

The system administrator may make some macros available from ne's global directory. See Arguments.

Since loading a macro each time it is invoked would be a rather slow and expensive process, once a macro has been executed it is cached internally. Subsequent invocations of the macro will used the cached version.

Warning: macro names are not case sensitive or path sensitive. ne only caches the file name of a macro, not the path name, and uses a case insensitive comparison. That is, if you invoke ~/foobar/macro, a subsequent call for /usr/MACRO will use the cached version of ~/foobar/macro. You can clear the cache by using the UnloadMacros command. See UnloadMacros.

The behaviour of macros may vary with different preferences. If the user changes the AutoIndent and WordWrap flags, for example, new lines and new text may not appear in the same way they would have when a macro was recorded. Good general purpose macros avoid such problems by using the PushPrefs command first. This preserves the user's preferences. Then they set any preferences that could affect their behaviour. Once that is taken care of they get on with the actual work for which they were intended. Finally, they use the PopPrefs command to restore the user's preferences. Note that if a macro is stopped before it restores the preferences (either by the user pressing <Control>-\ or by a command failing) then that responsibility falls on the user.

Finally, any line in a macro that starts with a non-alphabetical character is considered a comment, so you can add comments to a macro by starting a line with ‘#’.