ZEMAX Users' Knowledge Base - http://www.zemax.com/kb
How to Use Programmer's Editors With ZPL
http://www.zemax.com/kb/articles/202/1/How-to-Use-Programmers-Editors-With-ZPL/Page1.html
By Neil Barrett
Published on 1 August 2007
 
One of the many benefits of ZPL macros is that they can be written in any text editor, such as Windows' own Notepad. However, macros are often a lot easier to read, understand and debug when the macro commands are highlighted, and programmer's text editors provide more powerful text editing features.

This article describes how to use two such programmer's editors with ZPL.

Programmer's Editors
One of the great benefits of ZPL macros is that they can be written in any text editor, such as Notepad.

However, dedicated Programmer's Editors provide more powerful editing and searching functions, and macros are often a lot easier to read, understand and debug when the macro commands are highlighted. Thankfully there are a number of low cost editors available that support syntax highlighting.

One such editor is called ConTEXT. It is available for free (which is as low cost as it gets!) and enables us to view our macros like this:




The Syntax File
The key to any such editor is the Syntax file. This is essentially a text file that groups commands together by category, and then assigns a colour to each category.

In ConTEXT the syntax file is referred to as the highlighter file, and is a .chl file (ConTEXT HighLighter). Other packages will generally have different names for this file, e.g. EditPlus (not free, but low cost and very good), uses a .stx file. But no matter the file extension, the operation of each is broadly similar. Syntax files for both these editors can be downloaded from the links at the end of the last page of this article.

In most editors you can also change the colour scheme. ConTEXT supports this easily from Options… Environment Options:



This picture shows that the group “Keywords 1”, which contains the numeric functions, is coloured blue. You can also change this in the ZPL.chl file itself, which in turn can be opened from within any text editor!

In ConTEXT, the highlighter file also dictates what the file is saved as, so your macro will automatically be saved as filename.zpl

The ZPL.chl file used here is included at the end of this article, as is the link to the ConTEXT website. If you choose to use ConTEXT, then be sure to put the ZPL.chl file in the highlighters folder.

Limitations of Programmer's Editors
There are however some limitations inherent to Programmer's editors:

1. While they are helpful in eliminating spelling errors of keywords, they do not check syntax! We have to check this by running the macro, and looking at any errors generated.

2. They need updating. In the case of ZEMAX and ZPL the addition of new features and their associated functions is a regular occurrence. These need to be added into the .chl file under the relevant keyword type.

In the particular case of ZPL another issue that can arise for our editor is that the same descriptor can be used for different operations, and where this takes place the editor will (in general) default to the colour of the first keyword group defined. An example of this is ATYP (Aperture TYPe).

In ZEMAX “ATYP” can be used as a keyword to set the aperture type, or a numeric function to get (i.e. read back) the aperture type. These two operations may be written together in a small block such as:

ATYP = 1
x = ATYP()
PRINT x


Which simply prints the value 1 when run. The first line in the above is the keyword, as denoted by the fact it is on the left of the equals sign. The second line is the numeric function, as denoted by the fact it is on the right of the equals sign and has parenthesis. However, if we put this into ConTEXT we see:



ConTEXT in this case cannot tell the difference between the two operations, and so it defaults to the list in which ATYP is defined first, which in our case is the numeric function list, and colours both instances blue.

Generally, this is not a problem if we understand the differences between keywords and numeric functions in ZPL, and is even less of a problem if we read the ZEMAX manual (always a good thing to do) which tells us that ATYP as a keyword is obsolete and that we should be using SETSYSTEMPROPERTY or SYSP! So we can re-write as:



Problem solved!

Some Default ConTEXT Options
If you want your default file to be a .zpl, and like your lines to be numbered, etc, then here are some changes to the default settings that I find useful. These options are accessed from the Editor tab:



Summary and References
Editors that support syntax highlighting can really help you when writing macros. Many Editors are available for low cost, and are highly configurable to display your code how you want to read it.