Stefans Tools

Useful open source tools that make your life easier

User configurable color styles

While BowPad as a huge list of ready available lexers and coloring styles which you can use to get the proper coloring for your files, it might be necessary to create a new one that suits your own files.

For example, you might have your own XML based file format, which has custom keywords and attributes which you want to have colored separately. The default XML style of course doesn't know about your own keywords and attributes, so you need to configure your own styling setup.

We're going through creating a custom style setup for an XML based language as an example. You should be able to use this as a base in case you need another style as the base for your customization.

Once you have your custom lexer styling file done, you can drag and drop that file over the BowPad window: BowPad will ask whether you want to edit the file or import it. For this to work, the file needs to have the extension set to *.bplex.

Start

First, get the template for a custom lexer styling from here. Open the file in BowPad for editing.

You can see several sections in that file. First replace all YourLanguage strings with the name of your own file language, e.g. myXML. Then set the file extensions of your own files, so BowPad knows to apply your custom styling for these files.

Next you can define your own keywords the lexer should use. As a starting point, have a look at the styling file BowPad uses for all the available lexers here.

As you can see from the Properties.ini file, different lexers have different keywords set. Under the section [lang_Vxml] you can see that BowPad has two keyword sets set up. So how do you know how many keyword sets a lexer provides? Unfortunately, that's not quite easy: you have to actually check the sourcecode of the lexer. In our case, the lexer that provides XML styling is the html-lexer, which you can find here. The other lexers you might want to use are listed here.

The html lexer uses 7 keyword sets. But for styling xml files, only the first two are used, and even the second one is only used in special situations. You can get some information by looking at the htmlWordListDesc string in the file LexHTML.cxx. So you have to add your own XML elements and attribute names in the first keyword set.

The other options under the [lang_myXML] section are:

CommentLine
Configures which chars are used to mark a line comment. in many programming languages that would be //.
CommentLineAtStart
0 if the comment line can start anywhere on a line, 1 if such comments must start at the beginning of a line.
CommentStreamStart/CommentStreamEnd
Marks the inline comment chars. For example /* and */
FunctionRegex
A regular expression which captures function names
UserFunctions
if not zero, the function regex is used to add the found function names to the keywords for styling

So for an XML based language, you would specify

CommentStreamStart=<!--
CommentStreamEnd=-->

And at last, under the section [Lexer_myXML] we can specify the colors to use. But first, you need to specify here which lexer to use. For the XML lexer, the value to use would be 5, so you have to specify Lexer=5 there.

You can find the value for the lexer in Scintillas SciLexer.h file. The value for SCLEX_XML is defined as 5, so that's the value we have to use.

Each lexer has a specific amount of color styles. They're numbered from 0-n. Since the XML lexer is basically the HTML lexer, look for SCE_HTML_ in the SciLexer.h file.

The format for the coloring style is:
StyleN=Name;Foregroundcolor;Backgroundcolor;fontname;fontstyle;fontsize
The colors are RGB values in HEX, and the font style is a combination of the following values:
0 = none
1 = bold
2 = italic
4 = underlined
leave the values empty if you want to use the default.

Simple lexer

BowPad also has a simple lexer, which can be used if none of the language specific lexers fit your file format.

The simple lexer doesn't do much, but it has 9 keyword sets, colors numbers, strings and comments. This means while the lexer does not know anything about your file format, you can still use the keyword sets to color statements, attributes, ...

For example, if you specify if for else while return for a keyword set, you'll get coloring for such statements even though the lexer doesn't know about which of the words are such statements.

The simple lexer also allows you to specify end-of-line annotations. These are defined with a regex that has to match the whole line, and an annotation text for that matching line.

Here's a template for a custom style using the simple lexer:

[language]
ABCDE=pkt;hdd

[lang_ABCDE]
keywords1=
keywords2=
keywords3=
keywords4=
keywords5=
keywords6=
keywords7=
keywords8=
CommentLine=#
CommentStreamStart=
CommentStreamEnd=

[lexers]
Lexer_ABCDE=ABCDE

[Lexer_ABCDE]
Lexer=1100
Style0=DEFAULT;$(DEFAULT)
Style1=COMMENT;$(COMMENT)
Style2=COMMENTLINE;$(COMMENT LINE)
Style3=NUMBER;$(NUMBER)
Style4=STRING;$(STRING)
Style5=OPERATOR;$(OPERATOR)
Style6=IDENTIFIER;$(IDENTIFIER)
Style7=WORD1;$(KEYWORDS)
Style8=WORD2;$(KEYWORD2)
Style9=WORD3;$(KEYWORD3)
Style10=WORD4;$(KEYWORD4)
Style11=WORD5;$(KEYWORD5)
Style12=WORD6;$(KEYWORD6)
Style13=WORD7;$(KEYWORD7)
Style14=WORD8;$(KEYWORD8)
Style15=WORD9;$(KEYWORD9)
Style16=MARKEDWORDS1;0000A0;FFFFFF;;1;
Style17=MARKEDWORDS2;0000A0;FFFFFF;;0;

Prop_fold=1
Prop_foldComments=1
Prop_ws1CaseSensitive=1
Prop_ws2CaseSensitive=1
Prop_ws3CaseSensitive=1
Prop_ws4CaseSensitive=1
Prop_foldAtWS1=0
Prop_foldAtWS2=0
Prop_foldAtWS3=0
Prop_foldAtWS4=1
Prop_foldAtWS5=0
Prop_foldAtWS6=0
Prop_stringchars="'
Prop_stylenumbers=1
Prop_operators=+ - / * < > <= >= = == === ! != ~
Prop_linecomment=#
Prop_inlineCommentStart=/*
Prop_inlineCommentEnd=*/
Prop_eol=\
Prop_markedWords1=$

ann000regex=^ci\d+(,\d+)*
ann000text=draw a circle
ann001regex=^mv\d+(,\d+)*
ann001text=move to

the options:

Prop_foldComments
1 to fold comments
Prop_wsNCaseSensitive
1 to have the keyword set behave case-sensitive. Note: if not set or set to 0 (case-insensitive), the keywords must be set in lowercase.
Prop_foldAtWS1
if 1, every keyword is a folding point
Prop_stringchars
the chars that mark a string, usually " and '
Prop_stylenumbers
1 to style numbers, otherwise numbers are not colored
Prop_operators
the operators
Prop_markedWords1
a char or string that marks a special word which is colored separately. For example you can mark all words that start with a $ separately, or if you specify e.g. Gtk_ every word that starts with that will be colored as well.