previous next top contents index framed top this page unframed
A macro command can be invoked with any control sequence that is not intercepted by the operating system/terminal combination. A control sequence is usually generated by a single terminal key (also called a macro ID key), although some terminals permit multicharacter control sequences. Many terminals have keypad keys that function as MAINED macro ID keys and can, therefore, be used to invoke macro commands. Refer to Appendix B of the MAINEDIT User's Guide for a list of the macro ID keys and multicharacter control sequences on your terminal.
A named macro is invoked by typing an identifier in response to the “Macro name:” prompt of the , command; see Section 14.2.
14.1. Defining a Macro
The / command is used to define a macro:
A macro definition is undefined (i.e., forgotten) if a null command sequence is given in its definition. For example, a macro CTRL-A is undefined by typing the characters /, CTRL-A, then /. A macro that has been undefined reacquires its default value; keys that are predefined by MAINED (e.g., arrow keys) perform their predefined functions after they have been undefined.
Macro definitions are remembered in the eparms file; they are therefore available across edit sessions. The terminal type on which a macro is defined is remembered when the macro is saved in the eparms file. A macro defined on one terminal cannot be invoked from another terminal; they must be redefined for each terminal type. This avoids confusion, since two different terminals may generate the same control sequence for entirely different keys.
The keys <bs>, <tab>, <lf>, and <eol> are available as
macro ID keys.
They may be redefined like any other macro command.
However, if these keys are redefined,
commands containing them no longer function
as described in this manual.
It is not possible (or desirable) to redefine <ecm>.
14.2. Invoking a Macro
Macro commands are invoked by typing the macro ID key or
control sequence.
Macros defined by control characters or terminal-dependent special
keys may be invoked by typing the control character or
special key. Such invocations may occur in command, overstrike,
or insert mode.
Named macros may be invoked only from command mode by means of
the , command. The , command either prompts with Macro
name: or beeps and informs you that there are no defined
named macros.
To the Macro name: prompt, you may type either the name of
the macro to execute, or a ? to see a list of defined
named macro names.
14.3. Recursive Macros
MAINED allows a macro to invoke itself.
Macros that invoke themselves are called recursive macros.
A recursive macro executes until some command in its definition
command sequence fails.
For example, the search and skip commands fail when they cannot
find their targets.
On some operating systems, it is also possible to terminate
recursive macro execution with the <abort> key.
To define a recursive macro command, define the macro as described in Section 14.1, except that instead of typing a second / to terminate the macro definition, invoke the macro being defined (e.g., if defining CTRL-A, type CTRL-A; if defining a recursive named macro FRED, type ,FRED<eol>). MAINED displays:
Macro definition terminated with recursive invocation
to indicate that the macro is recursively defined. MAINED does not execute the terminating macro command during definition of the macro. You must invoke the macro command after it is defined for it to be recursively executed.
As an example, consider a macro command, CTRL-A, that searches through a buffer and replaces each occurrence of 2 with 3. The macro definition is shown in Example 14–1.
Example 14–1. Example of a Recursive Macro Definition
| You Type | Description |
|---|---|
| / | start macro definition |
| CTRL-A | macro command name |
| T | text search |
| 2<eol> | search target (prompted for on the message line) |
| O3 | overstrike with 3 |
| <ecm> | back to command mode |
| < | cursor left (in case next character is 2) |
| CTRL-A | repeat the above until search fails |
Recursive macros are appropriate for command sequences that involve searches, skips, or other commands that eventually fail. Beware of defining a recursive macro containing no commands that can fail; when executed, MAINED repeats the command sequence “forever”, until either you type the <abort> key (not available on all operating system/terminal combinations), or you use some other operating-system-dependent means to terminate the MAINED session abnormally. In the latter case, you lose any changes made to buffers since the last time you saved them.
When repeating a recursive macro, the screen is not updated
until the macro terminates or is aborted.
14.4. Macros and Editor Modes
When a macro is invoked, command mode is entered before the
macro is executed so that the macro can have
the same effect in all modes.
The mode after macro execution depends on how the macro was defined.
If the last command in a macro's command sequence is <ecm>, the mode after macro invocation is the same as the mode immediately before the final <ecm> was typed. If the last command in a macro's command sequence is not <ecm>, the mode after macro invocation is the same as the mode immediately before macro termination (usually command mode, although use of the mode manipulation commands in Table 14–2 can result in a different mode). These rules imply that, in order to define a macro to enter command mode, the macro definition must consist of two <ecm>s instead of one; the second <ecm> is discarded.
Whenever a macro is invoked, the current mode is saved in the editor variable savedMode, and the current mode is set to command mode. The commands shown in Table 14–2 allow you to manipulate the mode. The mode stack mentioned is a circular stack 100 deep.
Table 14–2. Commands Used to Manipulate the Mode
| Command | Effect on Mode |
|---|---|
| CM | push savedMode onto the mode stack |
| RM | pop the mode stack into the current mode |
| .RM | set the current mode to the top of the mode stack (the mode stack is not popped) |
| QRM | set the current mode to savedMode |
Using the mode manipulation commands, you can define a macro that saves the mode upon entry (CM) and restores it upon exit (RM), thus leaving the mode unchanged after the macro has been invoked and executed. QRM is a simple form for macros that do not themselves invoke macros. Such macros need not push the mode on the stack, since it is available in savedMode; they can restore the mode without doing any stack manipulation.
Table B–2 of the MAINEDIT User's Guide shows a use of mode manipulation commands.
14.5. Special Macro INITIALIZE
If your eparms file contains a
named macro called INITIALIZE,
MAINED executes it as soon as you enter the first buffer in a
MAINED session.
The INITIALIZE macro allows you to set up your
MAINED environment automatically.
For example, if you would like tabs always to be every fourth
column (instead of every eight, as by default), do as in
Example 14–3.
The next time eparms is updated, it will contain this
macro definition. Subsequent invocations of MAINED will
start by executing this macro, thereby setting your tab
stops.
Example 14–3. Setting Tabs to Be Every Fourth Column
| You Type | Description |
|---|---|
| / | start macro definition |
| initialize<eol> | for macro named INITIALIZE |
| Q<tab>5<eol> | tab every fourth column |
| / | terminate macro definition |
14.5.1. Buffer-Specific INITIALIZE Macros
If a named macro is given a name of the form INITIALIZE:...,
where ... represents a pattern that may contain the wildcard
character *, then the macro is executed whenever a buffer is
created with a name that matches the pattern.
The * wildcard character matches any sequence of zero or more
characters; any other character in the pattern represents itself.
For example, a macro named INITIALIZE:*.MSL would be executed
upon starting to edit any file with a name ending in .msl.