From the Support Desk
Template Tricks
It is fairly easy to create plain old vanilla text templates, but what about ones that do a little activity of their own? There are a number of meta-tags for templates
that can add a lot of functionality if you know how to use them. Here are some of the things you might want to do:
- Place the cursor within the text of the template.
- Highlight for overtyping, a phrase within the template.
- Prompt for some text to be added to the template results and offer a default.
- Place the current files' path within the text of the expanded template.
- Jump past the end of the template's text from within.
Let's walk through an example. Let's say we have a function called BOB, and we use it throughout our project. Thus, we want to create a template to make it
easier to place in the code. First we go to Tools-Edit Templates, make sure we are using the correct template set for the language, and press the button to
insert a template. We are prompted for the template name, BOB, of course, and we set up the following values for the template:
Keyword - bob; Min length -3; Space expand - checked, anywhere.
Ok, so now what do we want the result to look like?. We need the BOB of course. BOB has three parameters, p1, title, and lastparm. We
want to use the parm= format for the parameters. Oh yea, we want the second and third parameters on their own lines. Therefore here is what we would enter in the
results box:
BOB(p1=,
title=,
lastparm=);
So far so good, but it would be nice to do that Ctrl-I thing to fill in the parms. To do this we can add the meta command Cursor(<-CUR>)
into the middle of the result, so the cursor will be placed there after the template expansion. We can put in a value and surround it with one of those back leaning
quote marks like this:
BOB(p1=<-CUR>`value`,
title=,
lastparm=);
Note, undocumented feature here. If you place the Cursor meta command immediately next to text within those back leaning quotes, the
text will be highlighted automatically when the template expands. Next we could do the same for title, but lastparm has a default value(0). If we place the
default value in those back quotes then when we press Ctrl-I to hop to it, we will have to overtype the default, or manually remove those ticks from around it.
That's no good! So for a default value we can use the PROMPT meta command. This provides us a pop-up window where we can either hit enter to keep the default, or
overtype the default and hit enter. It looks like this:
BOB(p1=<-CUR>`value`,
title=`my title`,
lastparm=);
<-?>;
If you try this, you can see the prompt text will be displayed next to an edit box that already has the default value entered and
highlighted. And we can hit the enter key to accept or just start typing to replace that value.
Ok, now we can expand the function BOB template by typing bob followed by a space, enter a value for lastparm, or just press enter to
accept 0, overtype the first parm, hit Ctrl-I to get to the second parm, and overtype it. But that leaves our cursor just before the comma on the second
line. No doubt we have more code to type in. So we would like to hop to the line after the call to BOB and keep typing, without using the mouse or arrow keys. To
do that we add another meta command, the Field Marker (-<-?>):
BOB(p1=<-CUR>´value´,
title=´my title´,
lastparm=<PROMPT Enter the last parm´0>);
<-?>;
Now when the template expands after the last line you will see a /*?*/. To make this work, after overtyping `my title`, you press
Ctrl-I again and the cursor will move to the place where /*?*/ starts, and /*?*/ will disappear. Now we are all set to go sticking calls to
BOB all over our code! But wait! Shouldn't we document this somehow? It would be nice to have a comment above the call to - to say
where it is stored. We could just hardcode this. Nah, that would be too simple.
If we always keep functions for a project in a subdirectory (called functions of course!) under the project directory with the code, we can
automate this a bit. We can use the PATH meta command to enter the path to the current file, and just tack functions on to the end. Note, if you have long
directory names involved in the path, they will have that squiggly format of being squeezed into DOS format names. Undocumented feature here, we can use the LFN meta
command to render the path in a readable, long file name format. So our new template looks like this:
/**BOB is stored in functions **/
BOB(p1=<-CUR>´value´,
title=´my title´,
lastparm=<PROMPT Enter the last parm`0>);
<-?>;
So we should get an expansion of the template that looks like the following with our cursor sitting on the line after "lastparm=" in
the same column as BOB starts in. That is after we have stepped through filling out the parms and Ctrl-I'd (three times total) down to that line.
/**BOB is stored in c:\sas\p1\functions **/
BOB(p1=55,
title="Some cool title",
lastparm=0);
_
You don't have to actually type all the meta commands in the edit window of the template edit dialog. Instead, you can press the
metacommand button and pick them off a pop-up list. All except LFN of course!
Input Your Suggestions in the Forum:
http://www.multiedit.com/forums/viewforum.php?f=7
**Please note that you can review/search
through all forum topics,
but to submit a post you must register on the forums.