# Customizing syntax highlighting for private cell styles

Is there any way to set up a cell style with a particular syntax highlighting? For example, I'd like to have a CProgram cell to write in blue the C special words. I have done it by setting InputAutoReplacements for each word, replacing it with a StyleBox. It more or less works, but it is by far not the same as the usual highlighting on input cells... If you paste a bunch of code at once it doesn't highlight. If you, e.g, had set "int" to be spelled in blue, if you erase the t and go on writing, it all becomes blue. Perhaps it is fine enough, but I'd like to know if there's a better way.

Furthermore, what's the option that makes Input cells automatically turn the text you enter into RawBoxes and such? Is that customizable?

-
 Perhaps there are much simpler ways to do it, but I could not help exposing one of my toys :) – Leonid Shifrin Feb 4 '12 at 20:37 No need to help yourself, it's a great toy – Rojo Feb 4 '12 at 21:08

As a part of a larger sets of development tools which I am working on currently, I have developed a general syntax highlighter generator which does just that (not yet with styles though, this is coming). I wanted to put in on GitHub and do a bit more polishing / development, but since you asked the question, here goes.

### Features

• From a simple lexical specification, a whole package is generated, which provides code highlighting capabilities for a given language. Once you generate the package, you (supposedly) start happily using it, and don't depend on the master package (the generator), unless you want to generate another highlighter package. For those who are familiar with js Google prettify, this is similar in spirit, but less developed as of now.
• The lexical analyzer is generated automatically from your specifications, but you can also override it with a custom one.
• You can customize it in many ways, including colors for keywords etc.
• There are several optimizations which can be switched on and off, to control the responsiveness of the highlighter
• keywords etc are highlighted as you type
• Bracket and paren-matching / highlighting is supported.
• Cells can be evaluatable.

For the impatient, the package and a notebook with an example for C language can be downloaded here and here. I also made a gist where one can look at them as well. The notebook can be regarded as a brief manual to the package.

### Installation steps

• Place a CodeHighlighterGenerator.m package into any directory where Mathematica can find it (e.g. FileNameJoin[{\$UserBaseDirectory,"Applications"}])

• Open the CodeHighlighterGenerator.nb notebook and follow the discussion there.

### Future plans

I plan to place the project to GitHub properly in a few days. There are several directions in which I plan to extend the package, it is a work in progress. All comments & suggestions are more than welcome!

Here are a few screen-shots:

-
great package generator! I don't see the prompt nor see what I am selecting in those cells. I'm on W7. Is that expected? – Rojo Feb 4 '12 at 21:51
@Rojo Ok, this is an important omission, so let me explain. You see the prompt when you press one of the key triggering the event actions. By default, there are just 3 such keys: left-arrow key, right-arrow key, and space bar. This was done to omptimize the responsiveness of the highighter (one can enable other keys, but this will slow it down, particularly when typing, so I disabled them by default). So, to see the prompt, press one of those. – Leonid Shifrin Feb 4 '12 at 22:01
@Spartacus Yep, real-time highlighting. I use a combination of lazy evaluation, CellEventActions, and a very nice thing called DeveloperCellInformation , which I discovered by pure chance and which allows me to track the current cursor position. Even with this, making this work was a highly non-trivial thing (for me anyway). But the package has other goodies. Check out the mergeOptions function for example - with it, I can specify partial option trees and get them merged onto full option tree of the original function. OTOH, parts of the code are quite ugly, I intend to rewrite soon. – Leonid Shifrin Feb 4 '12 at 23:34
@StackExchanger Yes, I do :). The code in its current form has certain limitations, so I intend to rewrite it, to make more modular and extensible. Once this is done, I'd like to build some more features, including this. – Leonid Shifrin May 19 '12 at 20:51
@Szabolcs Thanks! This is my code highlighter at work :). I made a Java version for Andreas. – Leonid Shifrin Jun 1 '12 at 12:37