The Highlight Definition XML Format

Overview

This section is an overview of the Highlight Definition XML format. Based on a small example it will describe the main components and their meaning and usage. The next section will go into detail with the highlight detection rules.

The formal definition, aka the DTD is stored in the file language.dtd which should be installed on your system in the folder $KDEDIR/share/apps/katepart/syntax.

Main sections of Kate Highlight Definition files

A highlighting file contains a header that sets the XML version and the doctype:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
The root of the definition file is the element language. Available attributes are:

Required attributes:

name sets the name of the language. It appears in the menus and dialogs afterwards.

section specifies the category.

extensions defines file extensions, like "*.cpp;*.h"

Optional attributes:

mimetype associates files MIME Type based.

version specifies the current version of the definition file.

kateversion specifies the latest supported Kate version.

casesensitive defines, whether the keywords are case sensitive or not.

priority is necessary if another highlight definition file uses the same extensions. The higher priority will win.

author contains the name of the author and his email-address.

license contains the license, usually LGPL, Artistic, GPL and others.

hidden defines, whether the name should appear in Kate's menus.

So the next line may look like this:

<language name="C++" version="1.00" kateversion="2.4" section="Sources" extensions="*.cpp;*.h" />
Next comes the highlighting element, which contains the optional element list and the required elements contexts and itemDatas.

list elements contain a list of keywords. In this case the keywords are class and const. You can add as many lists as you need.

The contexts element contains all contexts. The first context is by default the start of the highlighting. There are two rules in the context Normal Text, which match the list of keywords with the name somename and a rule that detects a quote and switches the context to string. To learn more about rules read the next chapter.

The third part is the itemDatas element. It contains all color and font styles needed by the contexts and rules. In this example, the itemData Normal Text, String and Keyword are used.

  <highlighting>
    <list name="somename">
      <item> class </item>
      <item> const </item>
    </list>
    <contexts>
      <context attribute="Normal Text" lineEndContext="#pop" name="Normal Text" >
        <keyword attribute="Keyword" context="#stay" String="somename" />
        <DetectChar attribute="String" context="string" char="&quot;" />
      </context>
      <context attribute="String" lineEndContext="#stay" name="string" >
        <DetectChar attribute="String" context="#pop" char="&quot;" />
      </context>
    </contexts>
    <itemDatas>
      <itemData name="Normal Text" defStyleNum="dsNormal" />
      <itemData name="Keyword" defStyleNum="dsKeyword" />
      <itemData name="String" defStyleNum="dsString" />
    </itemDatas>
  </highlighting>
The last part of a highlight definition is the optional general section. It may contain information about keywords, code folding, comments and indentation.

The comment section defines with what string a single line comment is introduced. You also can define a multiline comments using multiLine with the additional attribute end. This is used if the user presses the corresponding shortcut for comment/uncomment.

The keywords section defines whether keyword lists are casesensitive or not. Other attributes will be explained later.

  <general>
    <comments>
      <comment name="singleLine" start="#"/>
    </comments>
    <keywords casesensitive="1"/>
  </general>
</language>

The Sections in Detail

This part will describe all available attributes for contexts, itemDatas, keywords, comments, code folding and indentation.

The element context belongs into the group contexts. A context itself defines context specific rules like what should happen if the highlight system reaches the end of a line. Available attributes are:

name the context name. Rules will use this name to specify the context to switch to if the rule matches.

lineEndContext defines the context the highlight system switches to if it reaches the end of a line. This may either be a name of another context, #stay to not switch the context (e.g.. do nothing) or #pop which will cause to leave this context. It is possible to use for example #pop#pop#pop to pop three times.

lineEmptyContext defines the context if an empty line is encountered. Default: #stay.

fallthrough defines if the highlight system switches to the context specified in fallthroughContext if no rule matches. Default: false.

fallthroughContext specifies the next context if no rule matches.

dynamic if true, the context remembers strings/placeholders saved by dynamic rules. This is needed for HERE documents for example. Default: false.

The element itemData is in the group itemDatas. It defines the font style and colors. So it is possible to define your own styles and colors, however we recommend to stick to the default styles if possible so that the user will always see the same colors used in different languages. Though, sometimes there is no other way and it is necessary to change color and font attributes. The attributes name and defStyleNum are required, the other optional. Available attributes are:

name sets the name of the itemData. Contexts and rules will use this name in their attribute attribute to reference an itemData.

defStyleNum defines which default style to use. Available default styles are explained in detail later.

color defines a color. Valid formats are '#rrggbb' or '#rgb'.

selColor defines the selection color.

italic if true, the text will be italic.

bold if true, the text will be bold.

underline if true, the text will be underlined.

strikeout if true, the text will be stroked out.

spellChecking if true, the text will be spell checked, otherwise it will be ignored during spell check.

The element keywords in the group general defines keyword properties. Available attributes are:

casesensitive may be true or false. If true, all keywords are matched casesensitive

weakDeliminator is a list of characters that do not act as word delimiters. For example the dot '.' is a word delimiter. Assume a keyword in a list contains a dot, it will only match if you specify the dot as a weak delimiter.

additionalDeliminator defines additional delimiters.

wordWrapDeliminator defines characters after which a line wrap may occur.

Default delimiters and word wrap delimiters are the characters .():!+,-<=>%&*/;?[]^{|}~\, space (' ') and tabulator ('\t').

The element comment in the group comments defines comment properties which are used for ToolsComment and ToolsUncomment. Available attributes are:

name is either singleLine or multiLine. If you choose multiLine the attributes end and region are required.

start defines the string used to start a comment. In C++ this would be "/*".

end defines the string used to close a comment. In C++ this would be "*/".

region should be the name of the foldable multiline comment. Assume you have beginRegion="Comment" ... endRegion="Comment" in your rules, you should use region="Comment". This way uncomment works even if you do not select all the text of the multiline comment. The cursor only must be in the multiline comment.

The element folding in the group general defines code folding properties. Available attributes are:

indentationsensitive if true, the code folding markers will be added indentation based, like in the scripting language Python. Usually you do not need to set it, as it defaults to false.

The element indentation in the group general defines which indenter will be used, however we strongly recommend to omit this element, as the indenter usually will be set by either defining a File Type or by adding a mode line to the text file. If you specify an indenter though, you will force a specific indentation on the user, which he might not like at all. Available attributes are:

mode is the name of the indenter. Available indenters right now are: normal, cstyle, haskell, lilypond, lisp, python, ruby and xml.

Available Default Styles

Default Styles were already explained, as a short summary: Default styles are predefined font and color styles.

So here only the list of available default styles:

dsNormal, used for normal text.

dsKeyword, used for keywords.

dsDataType, used for data types.

dsDecVal, used for decimal values.

dsBaseN, used for values with a base other than 10.

dsFloat, used for float values.

dsChar, used for a character.

dsString, used for strings.

dsComment, used for comments.

dsOthers, used for 'other' things.

dsAlert, used for warning messages.

dsFunction, used for function calls.

dsRegionMarker, used for region markers.

dsError, used for error highlighting and wrong syntax.