Highlight Patterns Modification: Difference between revisions

From Bluefish Wiki
Jump to navigation Jump to search
Fal7i (talk | contribs)
mNo edit summary
m moved Man 2 ch08s12 to Highlight Patterns Modification: Title updates (BV)
 
(No difference)

Latest revision as of 01:24, 9 July 2010


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">
<?php
// 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:

File:Man2 highlighting pattern1.png
The HTML Pattern

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:

p

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:

align="center"

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

matching:

"center"

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:

<?php
// this is a comment ?>
?>

Now a pattern named PHP Block:

File:Man2 highlighting pattern5.png
The PHP Block Pattern

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:

mode:
patternname:
case_sensitive(0-on/1-off):
start reg-ex:
end reg-ex:
start & end pattern(1), only start(2), subpattern(3):
parent-match:
foreground-color:
background-color:
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.

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.