
Syntax highlighting - the task is simple and has been solved many times. But it has one unpleasant feature - if we want to highlight the syntax of a new language (for example, the bb-tags language of a habra editor, or which program's log), then most of the solutions include creating a grammar, a parser, and then embedding it all somewhere. And what to do if you get a highlight for the logs, the desire to eat, but to waste three hours of desire is not?
Suitable text editor
Before coloring the text you need to select a text editor in which we will do it. Text editors we have the sea, for every taste and color. What do we need? Something simple, of course free, so that it works under any operating system so that the text can be conveniently edited. And of course, so that embedding your syntax highlighting doesn’t turn into recompiling the hardcore C code with PEG grammar embedded there :). It is not strange, even with such a requirement of text editors a lot. Choose scite, because I know him best :). Under windows, download
from here , under MacOS and * NIX, you can directly from the repository. Install, run, we get about the following:

')
Connect to the internal editor
As we agreed at the beginning, we will not compile anything - this is a long, dreary business, and the “paint in 15 minutes” paradigm does not fit. How to get to the guts of a text editor without recompiling it? To do this, the editor must either support the scripts, or give out the interface of interprocess communication like COM. Scite scripts. They are written in the
lua language, which is simple as three rubles in rubles and is mastered in ten minutes. Almost basic :). And connect it to scite too:
- Create where it is convenient for us text file scite.lua (the name can be any).
- In scite, select options -> 'open global options file'.
- In the settings file that opens, write the full path to script.lua as follows:

Note that there are no spaces before and after the '=', this is important. - Reboot scite.
Hello world
Now every time you run scite, it will automatically load and execute the specified script. Let's check that everything works in the classic way - we will add a command to scite.lua, which displays the well-known text in scite:

If we now run scite, we will see the result:

In the special window that appears, which is proudly referred to as the 'output window', the text that we specified in the script will be displayed. Using the print () commands in the script and the output window, you can quite easily debug the scripts and output the information we need - for example, the number of emoticons in our article :).
Preparing for painting
Making sure to successfully connect to the inside of a text editor, you can proceed to painting. For a successful painting, we will need to use the
ioc principle and the script to specify scite so that it consults with us every time the text is updated. To do this, simply set a function with a special name:
function OnUpdateUI()
print( "text update" )
end
This function will be called whenever the text in the text editor window changes. The following picture shows what happens if you run the test editor and enter 'a' - we are provided with mass calls to the handler function:

We paint
In general, everything is ready: there is a function that will be automatically called every time the text changes. You can paint. The most valuable thing in scite is that it is an editor demonstrating the work of scintilla, a library for creating text editing windows. And the library, by the way, is popular - Komodo Edit, wxWidgets, GTK use just her for text editing. And this library has a regular mechanism for interacting with everything and everyone, SCT_ messages.
Detailed documentation on the website will lucidly explain exactly which messages need to be sent and in what sequence, so that the text editor can
get the text out of it, colorize the text, emphasize the words we need and place beautiful marks on the margins. In particular, in order to color the text, it is enough to send two messages:
- SCI_STARTSTYLING with indication of the symbol number, starting from which paint. the first character is number 1
- SCI_SETSTYLING indicating the number of characters to paint and color numbers
The following code will color every second character in some color:
function OnUpdateUI()
text = editor:textrange( 0, editor.Length )
for i = 1, string.len( text ), 2 do
scite.SendEditor( SCI_STARTSTYLING, i, 31 )
scite.SendEditor( SCI_SETSTYLING, 1, 5 )
end
end
Pay attention to a couple of nuances: '31' is a mask for a style number, just some kind of magic number that you should always pass to this function. '5' is the color number. By default, scite matches each number to some color. For our example, the result will look ... strange:

Nuances
As you can see, nothing complicated. Spending 10 minutes on a brief introduction to lua and armed with a list of messages to send scite you can do a lot of unnecessary and useless things with the text :). What else can you pay attention to:
- At the beginning of the OnUpdateUI () function, it makes sense to send the message SCI_CLEARDOCUMENTSTYLE to reset the coloring set last time.
- In order to tell the editor which color corresponds to which number you need to select options -> 'open global options file' and add lines like this:
file.patterns.habr=*.habr
lexer.$(file.patterns.habr)=habr
style.habr.5=fore:#FF0000,back:#0000FF
Where 'habr' is the file extension that we want to color.
All the holiday!