3.6 Syntax Highlighting

Syntax highlighting is particularly useful for programming language text or other types of documents which have a strictly defined syntax. Colors indicate different syntactic categories of text according to the syntax definition in use.

Syntax definitions are stored in separate files. ne comes with a suite of syntax definitions for many popular programming languages and other common text file types. When you load a file, ne selects the appropriate syntax definition as determined by the filename extension in much the same way autoprefs are loaded. (See Virtual Extensions for ways to override a file’s extension based on file contents.) It also contains a built-in table of common filename extensions that share the same syntax definitions. For example, both ‘cbl’, and ‘cob’ files use the ‘cobol’ definition. See the Syntax command for the complete list of built-in extension mappings.

If there is no matching syntax definition for the filename extension, or if the document you are editing has no filename yet, or you just want to try a different syntax definition, you can load and use the syntax definition of your choice with the Syntax command. It takes the syntax name as a parameter. For example, the name “c” works for C syntax files with extensions ‘.c’, ‘.h’, ‘c++’, etc. ne searches for the specified syntax definition file in the ‘syntax’ subdirectory of your ~/.ne directory first. If not found there, ne then looks in the ‘syntax’ subdirectory of ne’s global directory for the syntax definition file. See Arguments.

With no parameter, the Syntax command prompts you for a syntax to load, the offered default being the currently loaded syntax if there is one. Use the Tab key at that prompt to get a list of available syntax recognizers.

One syntax definition you may find useful for any type of text file is called simply ‘tabs’. It highlights the TABs in your text so you can distinguish them from regular spaces.

You can create your own syntax definitions and store them in your ~/.ne/syntax directory (actually, modifying the colors of an existing definition is much easier; see Hints and Tricks). A complete explanation of syntax specifications is beyond the scope of this document, but the existing definition files should prove to be useful examples. In particular, the syntax/c.jsf file contains some particularly helpful comments. Syntax definition files have a ‘.jsf’ extension. Do not include that extension when using the Syntax command.

Your own syntax recognizers will be preferred over the global recognizers. If you use the Tab key at the syntax prompt to display the requester of extant recognizers, yours will be marked with an asterisk and bold if your terminal supports that.

Syntax highlighting does incur a slight penalty in memory used per line of text, and it also consumes some CPU resources. For small to medium sized files you’ll probably never notice. But for extremely large files—on the order of the size of your system’s RAM—the difference could be significant. If you invoke ne with the --no-syntax parameter, ne will disable the syntax highlighting mechanism entirely, freeing up the memory and CPU otherwise consumed. (Note that if you are that tight on memory, you may need to disable the undo buffer as well. See DoUndo.) On the other hand, ne will silently disable syntax highlighting on files longer than ten million bytes, but you can force it using the Syntax command.

Note that there is a basic difference between these two cases: when you use the --no-syntax parameter, the additional memory is not allocated at all, and syntax highlighting cannot be enabled without restarting ne. On the contrary, the automatic disabling for long files keeps only ne from computing the actual highlighting, and it can be overridden as explained above.

ne uses code from another editor—the GPL-licensed joe—for its syntax highlighting capabilities. Because of this fact, the syntax definition files are identical, even to the ‘.jsf’ extension, which is an acronym for “Joe’s Syntax File”. It’s possible that if both joe and ne are installed on your system that they share the same syntax file directory.