Support Notes
Adding Macros to Templates
Clay Martin
Templates allow you to save a lot of keystrokes. In a previous article we talked about getting more out of templates using some of the metacommands. In this article we will look at
another of the metacommands that allow you to execute a CMAC macro.
Once you have invoked the macro with the template it could present a dialog so that you can select options for what will be expanded. Or it could also look at the state of
the editor, such as, is a block of text selected? ; is the cursor at the end of a line? ; is the file empty? Based on the state of the editor or the user's choices the
macro can provide different output.
In this example we will create a template that executes a macro that will do different things based on the editor's state. What we want to happen is this. If a block is
selected we will place comment start and end around the block and mark the beginning and end with "debug start" and "debug end". For this example we will overlook the
possibility that there may be comments enclosed in the block that would mess with the block being commented out. Ideally we would want to change any open/close comments enclosed in the
block into some other character(s), and also have another macro that would remove the comments from the block and restore the interior comments.
If we were being really slick, we would not hardcode the comment characters, but pull them from the language definition. Now the other functionality for this template-macro is that if the
user did not select a block of text, we will just add an end of line comment to the end of the current line like: " //DEBUG". So our finished macro will allow us to comment out a
block of code, while marking it so that we could locate these blocks later by searching for "debug", and just mark a line of code as debug, again so we can find and remove this
temporary debug stuff later.
First the macro, we will place it in our tools.s file (you know the CMAC file where you keep all the tools you write):
void debugit( ) trans
{
/* save the current state */
int oldrefresh = REFRESH;
/* turn off for speed */
REFRESH = 0;
/**Set undo so user can undo the change**/
PUSH_UNDO;
if ( BLOCK_STAT != 0) { //have a block selected
/**Get the starting and ending lines of the text to box**/
int startline = BLOCK_LINE1;
int endline = BLOCK_LINE2;
/* add the end comment */
GOTO_LINE(endline);
EOL;
CR;
TEXT("DEBUG END */") ;(
/* add the starting comment */
GOTO_LINE(startline)(;
UP;
EOL;
CR;
TEXT("/* DEBUG START") ;
/* put the cursor at the beginning of the block */
DOWN;
GOTO_COL(1);
 }
else { // no block selected
/* go to the end of the current line */
EOL;
TEXT(" //DEBUG ") ;
/* reposition the cursor at the begining of the line */
GOTO_COL(1);
}
/* restore state and redraw the screen */
REFRESH = oldrefresh;
REDRAW;
}//debugit
Compile the CMAC file, no errors right? Now we need to create the template. Go to Tools | Edit Templates. Press the insert button. Lets name the template debugit and press
OK.
Then in the result box add this:
<!tools^debugit>
The "tools" tell ME where to find the macro, "debugit" is the name of the macro and we use the "^" character to separate the two just like you would using Macro | Run. The
<! > is the format of the run macro metacommand. Close the edit template dialog, and we are ready to go. So select some code and press ALT+F9, then start
typing "debugit" depending on your templates' names you should see the template debugit highlighted before you type the b in debugit. Then hit enter. You
should see:
/* DEBUG START
selected code;
selected code;
selected code;
selected code;
DEBUG END */
Now try putting your cursor on a line of code and press ALT+F9, then start typing "debugit", then Enter as before. You should see:
Some debug code; //DEBUG
So now you can supercharge your templates by executing macros from within them. Of course if your macro does not require a block to be selected, you can have your template space expand. You
can find out more about writing your own macros by looking over the fine set of tutorials written by David Deley located in the forums:
http://forums.multiedit.com/viewtopic.php?f=9&t=359
Support Staff
Multi Edit Software, Inc.
support@multieditsoftware.com
Send any support questions or Bug Reports to
support@multieditsoftware.com
or
by using Help | Create Bug Report from within Multi-Edit.
You may also
post your questions on our support forums at: http://www.multiedit.com/forums