Statements
Tip:
A shortcut for editing a user level macro command is to hold down the Ctrl key while selecting the command.
The macro function source code will appear for that command.
If you create a new macro command function in a macro file, you must save the macro file and allow Source
Insight to synchronize it with the project database files before the macro command will appear in menu and key assignments command lists.
You can also store macros in the Base project, or any other project on the project symbol path. Source Insight will search those projects when resolving macro names.
Note:
Source Insight will not add macro functions to the user-level command list unless they are saved in Source
Insight macro files using a .em extension.
Running Inline Macro Statements
The Run Macro command starts executing macro statements starting at the line the cursor is on. This allows you to run open coded macro statements in any type of file. This is very useful for testing and debugging macro code.
Running inline macro statements is also useful for running short utility macro scripts stored inside of a program comment. Remember to use the stop
statement at the end, or the macro interpreter will attempt to run past the end of the comment.
For example, this inline macro inside a C file comment searches for references to the identifier ucmMax
and causes recompilation of all the files that refer to it. To use it, the user places the cursor on the first line of the macro and invokes the Run Macro command.
#define ucmMax 120
/* Macro: touch all ucmMax references:
// to run, place cursor on next line and invoke "Run Macro" hbuf = NewBuf("TouchRefs") // create output buffer if (hbuf == 0)
stop
SearchForRefs(hbuf, "ucmMax", TRUE)
SetCurrentBuf(hbuf) // put search results in a window
SetBufDirty(hbuf, FALSE); // don't bother asking to save
Stop
*/
Statements
Macros are composed of individual statements. Compound statements consisting of two or more statements are enclosed with curly braces
{ }
.
368 Source Insight 4 User Guide
Statements
A statement can be one of the following:
•
A macro language statement element, such as an if
or while
statement. These statements are described later.
•
A call to a user-defined macro function. You define and save macro functions in a macro source file.
•
A call to an internal macro function, such as
GetCurrentBuf
. Source Insight provides many built-in functions. They are described in a later section.
•
An invocation of a Source Insight user command, such as
Line_Up
or
Copy
. All user-level commands in
Source Insight are available to the macro language. To call a user command, use the name of the command by itself, without parameters. Parentheses are not used. If the command contains embedded spaces in the command name, you must replace the spaces with underscore (_) characters. For example, to call the "Select All" command, the statement should look like
Select_All
.
You can stop execution using the stop
statement.
If you run a Source Insight command with a dialog box from a macro, the dialog box appears.
Function and variable names are not case sensitive.
Statement syntax is generally the same as in C or Java, except that semi-colons are not required, and are ignored.
Statement Examples
Here are a few example statements: hbuf = OpenBuf("file.c") // call internal function OpenBuf
SaveBufAs(hbuf, "filenew.c") // call internal function SaveBufAs
Select_All // call user-level command "Select All"
Copy // call user-level command "Copy"
Line_Up // call user-level command "Line Up" x = add2(n) // call user-defined macro function add2.
Statement Elements
The following table summarizes the macro language statements.
Statement
break if (cond)… else continue global return n return;
<or>
var while (cond)
Table 5.1: Macro Statements
stop strictvars true/false
Description
Exits a while loop.
Tests a condition.
Continues a while loop at the top.
Declares a global variable.
Returns n from a function. If a semicolon follows return, then the nil string is returned.
Stops executing the macro.
Turns on or off strict variable declarations mode.
Declares a function-local variable.
Loops while cond is true.
Source Insight 4 User Guide 369