Coyote's Guide to IDL Programming

Inserting a Program Template

QUESTION: I've read that you can use programming templates in the IDL Workbench to insert programming constructs like For Loops and Case Statements into your code, just like the EMACS guys have been doing for eons. But, uh, I was hoping I didn't have to learn Lisp to do it. Isn't it easier in the Workbench? How do you do it?

ANSWER: Yes, it is much easier in the Workbench, and you can let your Lisp skills languish for a few more years. Thanks to Doug Dirks, who helped illuminate this subject in an IDL newsgroup article.

Templates are a subset of what the IDL Workbench calls Content Assist. This includes such things as command completion, etc. To insert a content assist item either into your code (or on the IDL command line, if the item you are inserting goes on a single line), you press Ctrl-Space. One of two things will happen.

  1. If you have already typed all of the name of the item you want to insert, or enough to identify the name of the item uniquely, the entire content assist item will be inserted at that location.
  2. If you have typed less that enough to identify the item uniquely, then a list will pop up and you can choose the item you want to insert from the list.

For example, suppose you are tired of typing this at the IDL command line:

   IDL> file = Dialog_Pickfile()

You can type this:

   IDL> file = Di Ctrl-Space

and you will be presented with a list of IDL commands that start with "di" and you can choose the Dialog_Pickfile command from among them.

Command completion
Content Assist aids in command completion.
There are several templates built into the IDL Workbench, and you can use or modify these or even add your own. To see which templates you have defined, choose the Window -> Preferences -> IDL -> Templates menu selection.

Defined templates
The default templates in the IDL Workbench.

Templates are selected by typing their names (shown in the Name field of the figure above), not by typing the actual code that will be inserted into the file. So, for example, to insert a For loop into your code, you might type this in a editor window:

   fo CNTL-space

The For loop template appears first in the pop-up list, as shown in the figure below.

Inserting For loop template.
Inserting a For loop template into a file.

You can create your own templates. Here is how you can create a Dialog_Pickfile template, named pfile. Choose the Window -> Preferences -> IDL -> Templates menu selection and then click the New button. In the New Template dialog that appears, give the template the name pfile and write a short description of what the template does. I wrote Select a file with Dialog_Pickfile(). In the pattern section, type the code you wish to insert. I type this:

   file = Dialog_Pickfile(
Then, I chose the Insert Variable button, and inserted the Cursor variable. This will locate the cursor after the template has been inserted into your code. In other words, I can start typing at this location in the code. After inserting the Cursor variable, I added the final closing parenthesis. My pattern section looked like this:

   file = Dialog_Pickfile(${cursor})
Creating your own template.
Creating your own template.

To add the template, I clicked the OK button. Then the Apply and OK buttons in the Template Preferences dialog.

To use this template at the IDL command line, for example, I just type:

   IDL> pf CNTL-space

And the entire dialog is immediately inserted into the IDL command line, with my cursor positioned between the parentheses.

If you try to create your own IDL template, or even edit one of the ones supplied with IDL, you will notice there is a button named Automatically Insert that can be checked. You might wonder what this button does. It does nothing. It is a bug, apparently, in IDL 7.0 and 7.0.1. It is scheduled to be fixed in a future update.

Web Coyote's Guide to IDL Programming