Python wx.stc custom highlighting

1.8k views Asked by At

By lack of answers on many occasions, and the good answers I have received here, here's my question.

I'm writing a program for the ABC music notation language (sample given below). This is not a supported language in wx.stc, and I have not yet found a good explanation or example on how to:

  1. add my own lexer/highlighter
  2. create my own highlighter from scratch and use a RichTextCtrl or the likes.

Any help in any direction is welcomed.

ABC-notation example:

%% this is a midi or postscript argument, and should be colored 
X: 1   % this is a comment and should be colored differently
T: Speed The Plough   % everything followed by a colon, except for |: should be colored
M: 4/4   % a variety of others should be colored
L: 1/8
R: reel
K: Dmaj
d3A BAFB|AF~F2 EFDE|F2AF ~G3B|   % the non-alphanumerics should be colored
ABdg fgfe|d3A BAFB|AF~F2 EFDE|
F2AF GABG|1ABdg fedc:|2ABde fdde||
|:f2ab afdf|g2fg ed (3Bcd|a2ab afdB|   % | and |: should be colored differently
ABde fe~e2|f2ab afdf|g2fg edBA|
[1F2AF GABG|ABdg fddg:|2F2AF ~G3B|ABdg fgfe||
2

There are 2 answers

0
Mike Sandford On

Based on the "scintilla" tag you've got there I presume you're aware that Scintilla is what underlies wx.stc.StyledTextCtrl.

I was able to find a brief how-to to add some new styling to Scintilla: http://sphere.sourceforge.net/flik/docs/scintilla-container_lexer.html

It's pretty low-level and it's not Python. I wish I could make this a comment instead of an 'answer' but I don't have enough reputation for that yet.

0
ekhumoro On

To do this in Python, you will need to set the lexer to "container" and then handle the "style-needed" event:

self.SetLexer(wx.stc.STC_LEX_CONTAINER)
self.SetStyleBits(5)
self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.handleStyleNeeded)

Exactly what you do inside the handler will obviously depend on the details of the syntax you are trying to highlight.

For inspiration, I would suggest you familiarize yourself with the scintilla documentation on styling, and have a look at the source code of some of the simpler built-in lexers.

A little googling also found this wx.stc example that could be helpful.