35

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?

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Rojo
  • 42,601
  • 7
  • 96
  • 188

1 Answers1

40

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: enter image description here enter image description here

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
  • 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 04 '12 at 21:51
  • @Rojo The prompt you see most of the time (it is even shown in the second screen-shot, near the closing curly brace highlighted in orange). The selections you usually don't see (this is a limitation, which I hope to overcome in the future) - to see it, first type something (a space, move the cursor, whatever) in the cell. You can make a cell evaluatable and attach a custom evaluator to it. I was hoping to have enough time to develop say automatic compilation of MathLink programs as an application (evauator), but did not get to that yet. Soon... – Leonid Shifrin Feb 04 '12 at 21:56
  • 2
    @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 04 '12 at 22:01
  • Real-time highlighting? I would think this a joke if it didn't come from you. What hooks are you using? (time to look at that download) – Mr.Wizard Feb 04 '12 at 23:17
  • I don't claim to understand what you're doing, but +1 just for boldness to do it. – Mr.Wizard Feb 04 '12 at 23:27
  • 2
    @Spartacus Yep, real-time highlighting. I use a combination of lazy evaluation, CellEventActions, and a very nice thing called Developer`CellInformation`, 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 04 '12 at 23:34
  • @Spartacus The main highlighting "engine" is the keyAction function, generated by keyActionCode. It produces a code for a single event action (key pressed). It is probably much easier to read the code of the generated highlighter (for C say), than to read the original code of the generator, where the highlighting proper is mixed with code generation etc. I intend to rewrite this, to make it a lot cleaner, and better separate the concerns. This is really a "quick and dirty" version. – Leonid Shifrin Feb 04 '12 at 23:46
  • @LeonidShifrin: Do you plan to include (customizable) syntax auto-completion functionality in your syntax highlighter package? One of the frustrating limitations in current syntax auto-completion is a lack of proper support for string-based function options, although there are plenty of other areas where it could be fine-tuned as well. – StackExchanger May 19 '12 at 07:27
  • 1
    @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
  • @Rojo and Leonid: maybe you'll be interested in this. The examples have syntax coloured Java cells. – Szabolcs Jun 01 '12 at 12:34
  • 1
    @Szabolcs Thanks! This is my code highlighter at work :). I made a Java version for Andreas. – Leonid Shifrin Jun 01 '12 at 12:37