Scintilla icon Add a lexer to Scintilla and SciTE

Lexer addition.

The process of adding a new lexer to both Scintilla and SciTE is fairly long. Here is my response when asked how to add a lexer for Apache CONF files to SciTE. There is more information on writing the lexer code (steps 4 and 5, here) in the documentation for Scintilla.

Don't bother about steps which are for configurations you don't use all 6 makefiles - I'll patch them up later if you want to contribute the lexer.

  1. In scintilla/include/Scintilla.iface, add a lexer ID value:
    val SCLEX_CONF=17
  2. And any lexical class IDs:
    val SCE_CONF_DEFAULT=0
    val SCE_CONF_COMMENT=1
  3. In the scintilla/include directory run HFacer.py to regenerate the SciLexer.h file. Alternatively (if you don't want to run a Python script) just add these values to SciLexer.h as #defines and I'll put them in Scintilla.iface.
  4. In the scintilla/src/LexOthers.cxx write a ColouriseConfDoc function similar to one of the other functions such as ColouriseLatexDoc.
    static void ColouriseConfDoc (unsigned int startPos, int length, int initStyle, WordList *[], Accessor &styler) {
  5. At the end of the file associate the lexer ID and name with the function:
    LexerModule lmConf(SCLEX_CONF, ColouriseConfDoc, "conf");
  6. 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 - scintilla/win32/makefile, scintilla/win32/scintilla.mak, scintilla/gtk/makefile, scite/win32/makefile, and scite/win32/scite.mak.
  7. To the scite/src/others.properties add an entry to associate the file extension with the lexer:
    lexer.*.conf=conf
    If a new lexer file was created instead of adding to LexOthers, then a new properties file should be created by cloning scite/src/others.properties and modifying that file in the following steps.
  8. Set up the styles:
    # Default
    style.conf.0=fore:#FF0000,bold
    # Comment
    style.conf.1=fore:#007F7F,$(font.comment)
  9. If on Windows (someday this may work on GTK+ too), a filter should be added for conf files in scite/src/others.properties: filter.conf=Configuration (.conf)|*.conf|
  10. In scite/src/SciTEGlobal.properties add $(filter.conf) to the definition of open.filter.
  11. To add this language to the Language menu of SciTE, add an entry to the menu.language property including the name of the language and the file extension used most commonly for it.
  12. Build both Scintilla and SciTE.
  13. Share and enjoy