The possibility to execute little `editor programs' can make an editor into a really powerful tool. In stedi there are already many built in commands that the user would have to define in terms of macro's if he would be working with another editor. Yet the implementation of more and more commands can make an editor so large that there is no memory left to use it. Therefore also stedi is equipped with a complete macro processor that can execute user defined procedures that may use parameters, variables and control flow. Those macro's may either be loaded in memory or reside on disk. There are several possibilities that are attempted in locating a macro when the user decides to invoke one.
The macro's that are located fastest are the ones that are stored inside the default file. These macro's are loaded at the startup of stedi only. If another default file is read later neither the key redefinitions nor the macro's are replaced by those of the new default file. Macro's can also be loaded during a session. The command
mc name
creates a macro with the given name. The contents of the macro are a
copy of the current buffer. When the default file is written also
these loaded macro's are put into it. To make this scheme complete there
is also the
md name
command that deletes the given macro from the buffer with the loaded
macro's. It is not allowed to make more than one macro with the same
name. The old macro has to be deleted first. In addition to the above
two commands there is a command that allows the user to see which
macro's are currently loaded. The command
mv
makes a list of all available macro's. This list is put in the current
text buffer (it has to go somewhere). If this would upset the current
buffer too much one could first go to one of the yank buffers (buffer 9
or buffer 0, see p.
mv name
will list the contents of the macro with the given name into the current
buffer, provided the macro can be located.
If the user asks for a macro to be executed and the macro cannot be
located inside the above buffers stedi will look inside the current
directory for a file with the given name and an extension `.mac'. If
such a file is located it will be loaded and executed. After its
execution it is removed from memory again. A file system which possesses
a good caching mechanism will still give a rather fast performance, even
when one macro is calling another macro from disk many times. Finally,
if such a file cannot be located in the current directory it is also
looked for in the macro directory. This directory is indicated either by
the path name after the -d option at startup (p.
) or by the contents of the `STEDIMAC' environment variable
(p.
).
In principle only alphanumeric characters should be used for the names of the macro's although this may differ from one system to the other.
The statement with which to execute macro's is the X statement in the command line :
X name arguments separated by blanks
will execute the macro indicated by `name' with four arguments. The
first argument is the string "arguments", the second one is the string
"separated" etc. These arguments can be used as
variables inside the macro. These variables are referred to by a dollar
sign followed by a single digit. The digit may be enclosed inside curly
brackets. The arguments are referred to by their order of occurrence.
The first argument is $1 etc. There can be no more than nine
arguments this way. Arguments that are used but that were not provided
in the call to the macro are taken as empty strings. The special object
$0 is the name of the macro itself.
If, during the execution of a macro, an error condition is encountered, its execution is halted and the appropriate error message is printed. In addition the number of the line in which this error occurred is appended to the message. This number is between parentheses. If it happens that the offending line is in a macro that was called from another macro the parent macro is also stopped, so also its line number will be appended. This gives a full tracing of how the execution got to the point that gave the offending condition. It could be that the user would like to intercept an error condition, so that he may clean up a partially finished operation. If a macro contains the label statement (see below)
label onerror
the editor will go to the first statement after this statement, rather
than returning immediately. Such a transfer of control cannot take place
when the error is against the syntax on one of the flow control
statements given later in this chapter. The label will be used only once
in a given macro. If a second error occurs the macro will be exited.
The command to enter text from a macro into the current buffer is the
`double quote' command (p.
). A command that
consists of a string enclosed by double quotes, will put the string in
the text at the position of the cursor.
Any line that starts with either the character # or the character * is considered to contain commentary and is skipped. When a macro is loaded and it contains a closed fold the fold is loaded in an opened form. This means that the statements inside a closed fold are also executed.