Highlight Patterns Modification

From Bluefish Wiki
Revision as of 02:24, 9 July 2010 by Bill Valaski (talk | contribs) (moved Man 2 ch08s12 to Highlight Patterns Modification: Title updates (BV))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Man2 warning.png This page is a copy from Manual 1. It needs to be modified to match Bluefish version 2.0.0!
Once you have verified this page (and corrected if necessary),

please modify the status to u (updated), i.e. st=u (see Guidelines for Writing this Manual)

Man2 icon 48.png
Chapter VIII. Customising Bluefish
12. Modifying the highlighting patterns

The highlight patterns are build from Perl compatible regular expressions. A pattern has options for coloring and styling the text it matches. Within a match other patterns can be used to color parts of that match. There are three types of patterns:

  1. Start pattern and end pattern: that is two distinct patterns, match from the start pattern to the end pattern
  2. Only start pattern: that is a unique pattern that matches from start to end
  3. Subpattern from parent: that is a subpattern from the parent pattern, specified by the range in the parent pattern.

One specific pattern can also be used within several other parent patterns. The parent-match option is a regular expression that defines all parents for a certain pattern. If empty it will default to ^top$, so basically it will be on the top level.

So how does it work? Lets take a look at a little example text, a piece of PHP code within some HTML code:

<p align="center">
// this is a comment ?>

The first thing the highlighting engine does is finding the pattern that has the lowest match. Using the default patterns for PHP, the pattern named HTML:

has a match at position 0:

<p align="center">

So now the highlighting engine searches for the lowest match in all subpatterns of HTML, in the region matched by a type 2 pattern. Again, the lowest match will count. The pattern named <html> Tags:

File:Man2 highlighting pattern2.png
The <html> Tags Pattern

has a match at position 1. This pattern is a type 3 pattern, so it matches a subpattern of the parent:


The match from subpattern <html> Tags ends at position 2 and it does not have any child patterns, so the highlighting engine continues at position 2 with all subpatterns from HTML. A type 2 pattern named HTML Attributes:

File:Man2 highlighting pattern3.png
The HTML Attributes Pattern

has the lowest match:


This pattern does have a child pattern, again a type 3 pattern called HTML Attribute Contents:

File:Man2 highlighting pattern4.png
The HTML Attribute Contents Pattern



The pattern HTML Attribute Contents does not have any child patterns, and subpatterns of HTML Attributes do not have any more matches, and also HTML subpatterns do not have any more matches. So we are back on the main level, the remaining code to highlight is:

// this is a comment ?>

Now a pattern named PHP Block:

has the lowest match. This is a type 1 pattern, so the highlight engine continues with all the remaining code, but it will not only search for the lowest match of the child patterns of PHP Block, but it will also ue it for the end pattern of PHP Block. The lowest match in this example is a pattern named Comment (C++/single line):

File:Man2 highlighting pattern6.png
The Comment (C++/single line) Pattern

As you can see the ?> within the comment does not end the php pattern, because it lies within a subpattern of PHP Block:

// this is a comment ?>

The pattern Comment (C++/single line) does not have any child patterns, so the remaining code for the PHP subpatterns is:


It is very obvious now, the lowest match will be the end pattern of the php pattern, so we're back on the main level, and we have matched all of the code!

File:Man2 syntax highlighting example.png
Syntax Highlighting Example

The config file for highlighting is a colon separated array with the following content:

start reg-ex:
end reg-ex:
start & end pattern(1), only start(2), subpattern(3):
don't change weight(0), non-bold(1), bold(2):
don't change style(0), non-italic(1), italic(2):

The same options are found in the syntax highlighting preferences.

As an exercise you may want to add the highlighting patterns for the xsl stylesheet file type created previously. They will be based on the xml patterns with just small changes.

Man2 note.png If you check the force bold weight check box, you should also check that the font you use has a bold variant in the Editor tab of the Preferences panel.

11. Modifying the files filters
Home | ToC
 Chapter IX. Shortcuts available within Bluefish