Adding a Lexer to wxStyledTextCtrl

January 26, 2007 at 3:23 am 1 comment

This is my 2 days work to create a new lexer for wxStyledTextCtrl (wxSTC). The first think you should now is that wxSTC is a wrapper around a widely know text control named scintilla. So if you want to add a lexer you really are adding it to the scintilla distribution that wxSTC comes with.

There is this web page http://scintilla.sourceforge.net/SciTELexer.html where you can found how to add a lexer to scintilla and his Text Editing application called SciTE. The first step are basically the same.

You should have a $(wxDIR)/contrib/src/stc dir where you have all the thinks that goes with the control, I not a expert in this just I saw what I needed to add the lexer, in the documentation you will find more information. for the moment you shold go to that directory and follow the step. We supposed we’re adding some language call it foo.

1. So go to the scintilla/include dir and open the SciLexer.h file, ther you will add a ID value:
#define SCLEX_FOO=79, (supposed the last ID was 78)
And any lexical class IDs:
#define SCE_FOO_DEFAULT=0
#define SCE_FOO_COMMENT=1

2. Now you should add the ID value to stc.h locate in $(wxDIR)/contrib/include/wx/stc.
#define wxSTC_LEX_FOO 79 and
#define wxSTC_FOO_DEFAULT 0
#define wxSTC_FOO_COMMENT 1

I think you should be able to recognize a order in the file so you can do it that way.

3. In the scintilla/src/LexOthers.cxx write a ColouriseFooDoc function similar to one of the other functions such as ColouriseLatexDoc. For a more detailed guide open the file LexCpp.cxx and see the ColouriseCppDoc. What I did just to copy and paste that function and fit it to my needs.

LexerModule lmfoo(SCLEX_FOO, ColouriseFooDoc, “conf”);

4. If this is a complex lexer then it may be better off in its own file, in which case clone one of the current files and then add the file to all of the make files where LexOthers is currently referenced

5. You should be able to compile wxSTC and does all.

That was the steps I followed to get the lexer working, you should see this page to understand what the Colourise function does: [http://www.scintilla.org/Lexer.txt]

To get the lexer working we would modify the sample that comes with wxSTC, you should find it in $(wxDIR)/contrib/samples/stc, here what we need to do is to modify the prefs.cpp file, this contains all the preferences for the lexers:

1. If you have some Keywords you should added just follow the way C++ and Python does.

2. To the g_LanguagePrefs const add _T(“FOO”) with your definitions.

That’s all you should be able to compile the sample and select the Foo language from the View->Highlight Language->Foo

I hope that serves you.

Advertisements

Entry filed under: wxWidgets.

wxOGL QCad Fluid Power Library

1 Comment Add your own

  • 1. llcqbdcn  |  August 23, 2007 at 3:15 pm

    Ive heard about a “poor man’s copyright.” What is it?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts


%d bloggers like this: