&TC.Hollingsworth; &TC.Hollingsworth.mail;

# Extending &katepart;

## Introduction

Like any advanced text editor component, &katepart; offers a variety of ways to extend its functionality. You can write simple scripts to add functionality with &javascript;. Finally, once you have extended &katepart;, you are welcome to join us and share your enhancements with the world!

## Working with Syntax Highlighting

### Overview

Syntax Highlighting is what makes the editor automatically display text in different styles/colors, depending on the function of the string in relation to the purpose of the file. In program source code for example, control statements may be rendered bold, while data types and comments get different colors from the rest of the text. This greatly enhances the readability of the text, and thus helps the author to be more efficient and productive.

A C++ function, rendered with syntax highlighting.

A C++ function, rendered with syntax highlighting.

The same C++ function, without highlighting.

The same C++ function, without highlighting.

Of the two examples, which is easiest to read?

&kappname; comes with a flexible, configurable and capable system for doing syntax highlighting, and the standard distribution provides definitions for a wide range of programming, scripting and markup languages and other text file formats. In addition you can provide your own definitions in simple &XML; files.

&kappname; will automatically detect the right syntax rules when you open a file, based on the &MIME; Type of the file, determined by its extension, or, if it has none, the contents. Should you experience a bad choice, you can manually set the syntax to use from the Tools Highlighting menu.

The styles and colors used by each syntax highlight definition can be configured using the Highlighting Text Styles tab of the Config Dialog, while the &MIME; Types and file extensions it should be used for are handled by the Modes & Filetypes tab.

Syntax highlighting is there to enhance the readability of correct text, but you cannot trust it to validate your text. Marking text for syntax is difficult depending on the format you are using, and in some cases the authors of the syntax rules will be proud if 98% of text gets correctly rendered, though most often you need a rare style to see the incorrect 2%.

### The &kappname; Syntax Highlight System

This section will discuss the &kappname; syntax highlighting mechanism in more detail. It is for you if you want to know about it, or if you want to change or create syntax definitions.

#### How it Works

Whenever you open a file, one of the first things the &kappname; editor does is detect which syntax definition to use for the file. While reading the text of the file, and while you type away in it, the syntax highlighting system will analyze the text using the rules defined by the syntax definition and mark in it where different contexts and styles begin and end.

When you type in the document, the new text is analyzed and marked on the fly, so that if you delete a character that is marked as the beginning or end of a context, the style of surrounding text changes accordingly.

The syntax definitions used by the &kappname; Syntax Highlighting System are &XML; files, containing

- Rules for detecting the role of text, organized into context blocks

- Keyword lists

- Style Item definitions

When analyzing the text, the detection rules are evaluated in the order in which they are defined, and if the beginning of the current string matches a rule, the related context is used. The start point in the text is moved to the final point at which that rule matched and a new loop of the rules begins, starting in the context set by the matched rule.

#### Rules

The detection rules are the heart of the highlighting detection system. A rule is a string, character or regular expression against which to match the text being analyzed. It contains information about which style to use for the matching part of the text. It may switch the working context of the system either to an explicitly mentioned context or to the previous context used by the text.

Rules are organized in context groups. A context group is used for main text concepts within the format, for example quoted text strings or comment blocks in program source code. This ensures that the highlighting system does not need to loop through all rules when it is not necessary, and that some character sequences in the text can be treated differently depending on the current context.

Contexts may be generated dynamically to allow the usage of instance specific data in rules.

#### Context Styles and Keywords

In some programming languages, integer numbers are treated differently from floating point ones by the compiler (the program that converts the source code to a binary executable), and there may be characters having a special meaning within a quoted string. In such cases, it makes sense to render them differently from the surroundings so that they are easy to identify while reading the text. So even if they do not represent special contexts, they may be seen as such by the syntax highlighting system, so that they can be marked for different rendering.

A syntax definition may contain as many styles as required to cover the concepts of the format it is used for.

In many formats, there are lists of words that represent a specific concept. For example, in programming languages, control statements are one concept, data type names another, and built in functions of the language a third. The &kappname; Syntax Highlighting System can use such lists to detect and mark words in the text to emphasize concepts of the text formats.

#### Default Styles

If you open a C++ source file, a &Java; source file and an &HTML; document in &kappname;, you will see that even though the formats are different, and thus different words are chosen for special treatment, the colors used are the same. This is because &kappname; has a predefined list of Default Styles which are employed by the individual syntax definitions.

This makes it easy to recognize similar concepts in different text formats. For example, comments are present in almost any programming, scripting or markup language, and when they are rendered using the same style in all languages, you do not have to stop and think to identify them within the text.

All styles in a syntax definition use one of the default styles. A few syntax definitions use more styles than there are defaults, so if you use a format often, it may be worth launching the configuration dialog to see if some concepts use the same style. For example, there is only one default style for strings, but as the Perl programming language operates with two types of strings, you can enhance the highlighting by configuring those to be slightly different. All available default styles will be explained later.

### The Highlight Definition &XML; Format

#### Overview

&kappname; uses the Syntax-Highlighting framework from &kde-frameworks;. The default highlighting &XML; files shipped with &kappname; are compiled into the Syntax-Highlighting library by default.

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, also known as the XSD you find in Syntax Highlighting repository in the file `language.xsd`

Custom `.xml` highlight definition files are located in `org.kde.syntax-highlighting/syntax/` in your user folder found with ``qtpaths``--paths GenericDataLocation`` which usually are ``$HOME`/.local/share/` and `/usr/share/`.

In Flatpak and Snap packages, the above directory will not work as the data location is different for each application. In a Flatpak application, the location of custom &XML; files is usually ``$HOME`/.var/app/flatpak-package-name/data/org.kde.syntax-highlighting/syntax/` and in a Snap application that location is ``$HOME`/snap/snap-package-name/current/.local/share/org.kde.syntax-highlighting/syntax/`.

On &Windows; these files are located `%USERPROFILE%\AppData\Local\org.kde.syntax-highlighting\syntax`. %USERPROFILE% usually expands to `C:\Users\user`.

In summary, for most configurations the directory of custom &XML; files is as follows:

For local user | ``$HOME`/.local/share/org.kde.syntax-highlighting/syntax/`
For all users | `/usr/share/org.kde.syntax-highlighting/syntax/`
For Flatpak packages | ``$HOME`/.var/app/flatpak-package-name/data/org.kde.syntax-highlighting/syntax/`
For Snap packages | ``$HOME`/snap/snap-package-name/current/.local/share/org.kde.syntax-highlighting/syntax/`
On &Windows; | `%USERPROFILE%\AppData\Local\org.kde.syntax-highlighting\syntax`
On &macOS; | ``$HOME`/Library/Application Support/org.kde.syntax-highlighting/syntax/`

If multiple files exist for the same language, the file with the highest `version` attribute in the `language` element will be loaded.

##### Main sections of &kappname; Highlight Definition files

A highlighting file contains a header that sets the &XML; version:

      <?xml version="1.0" encoding="UTF-8"?>

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, such as "*.cpp;*.h"

  `version` specifies the current revision of the definition file in terms of an integer number. Whenever you change a highlighting definition file, make sure to increase this number.

  `kateversion` specifies the latest supported &kappname; version.

  Optional attributes:

  `mimetype` associates files &MIME; type.

  `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 the MIT license for new syntax-highlighting files.

  `style` contains the provided language and is used by the indenters for the attribute `required-syntax-style`.

  `indenter` defines which indenter will be used by default. Available indenters are: ada, normal, cstyle, cmake, haskell, latex, lilypond, lisp, lua, pascal, python, replicode, ruby and xml.

  `hidden` defines whether the name should appear in &kappname;'s menus.

  So the next line may look like this:

      <language name="C++" version="1" 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.

  Since &kde-frameworks; 5.53, a list can include keywords from another list or language/file, using the `include` element. `##` is used to separate the list name and the language definition name, in the same way as in the `IncludeRules` rule. This is useful to avoid duplicating keyword lists, if you need to include the keywords of another language/file. For example, the othername list contains the str keyword and all the keywords of the types list, which belongs to the ISO C++ language.

  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>
          <list name="othername">
            <item>str</item>
            <include>types##ISO C++</include>
          </list>
          <contexts>
            <context attribute="Normal Text" lineEndContext="#pop" name="Normal Text" >
              <keyword attribute="Keyword" context="#stay" String="somename" />
              <keyword attribute="Keyword" context="#stay" String="othername" />
              <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, indentation, empty lines and spell checking.

  The `comment` section defines with what string a single line comment is introduced. You also can define a multiline comment 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 case sensitive or not. Other attributes will be explained later.

  The other sections, `folding`, `emptyLines` and `spellchecking`, are usually not necessary and are explained later.

        <general>
          <comments>
            <comment name="singleLine" start="#"/>
            <comment name="multiLine" start="###" end="###" region="CommentFolding"/>
          </comments>
          <keywords casesensitive="1"/>
          <folding indentationsensitive="0"/>
          <emptyLines>
            <emptyLine regexpr="\s+"/>
            <emptyLine regexpr="\s*#.*"/>
          </emptyLines>
          <spellchecking>
            <encoding char="á" string="\'a"/>
            <encoding char="à" string="\`a"/>
          </spellchecking>
        </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 in the group `contexts`. A context itself defines context specific rules such as what should happen if the highlight system reaches the end of a line. Available attributes are:

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

  `attribute` identifies the style to use for a character when no rule matches or when a rule does not specify attribute. In the latter case, attribute of the context specified in the rule's context will be used.

  `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 (&eg;. do nothing) or `#pop` which will cause it to leave this context. It is possible to use for example `#pop#pop#pop` to pop three times, or even `#pop#pop!OtherContext` to pop two times and switch to the context named `OtherContext`. It is also possible to switch to a context that belongs to another language definition, in the same way as in the `IncludeRules` rules, &eg;, `SomeContext##JavaScript`. Context switches are also described in . Default: #stay.

  `lineEmptyContext` defines the context if an empty line is encountered. The nomenclature of context switches is the same as previously described in lineEndContext. Default: #stay.

  `fallthroughContext` specifies the next context to switch to if no rule matches. The nomenclature of context switches is the same as previously described in lineEndContext. Default: #stay.

  `fallthrough` defines if the highlight system switches to the context specified in `fallthroughContext` if no rule matches. Note that since &kde; &frameworks; 5.62 this attribute is deprecated in favor of `fallthroughContext`, since if the `fallthroughContext` attribute is present it is implicitly understood that the value of `fallthrough` is true. Default: false.

  `noIndentationBasedFolding` disables indentation-based folding in the context. If indentation-based folding is not activated, this attribute is useless. This is defined in the element folding of the group general. 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 you 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 others are 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 struck out.

  `spellChecking` if true, the text will be spellchecked.

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 case sensitively.

  `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 Tools Comment, Tools Uncomment and Tools Toggle Comment. Available attributes are:

  `name` is either singleLine or multiLine. If you choose multiLine the attributes end and region are required. If you choose singleLine you can add the optional attribute position.

  `start` defines the string used to start a comment. In C++ this would be "/*" in multiline comments. This attribute is required for types multiLine and singleLine.

  `end` defines the string used to close a comment. In C++ this would be "*/". This attribute is only available and is required for comments of type multiLine.

  `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. This attribute is only available for type multiLine.

  `position` defines where the single line comment is inserted. By default, the single line comment is placed at the beginning of the line at column 0, but if you use position="afterwhitespace" the comment is inserted after leading whitespaces right, before the first non-whitespace character. This is useful for putting comments correctly in languages where indentation is important, such as Python or YAML. This attribute is optional and the only possible value is afterwhitespace. This is only available for type singleLine.

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, as in the scripting language Python. Usually you do not need to set it, as it defaults to false.

The element `emptyLine` in the group `emptyLines` defines which lines should be treated as empty lines. This allows modifying the behavior of the lineEmptyContext attribute in the elements `context`. Available attributes are:

  `regexpr` defines a regular expression that will be treated as an empty line. By default, empty lines do not contain any characters, therefore, this adds additional empty lines, for example, if you want lines with spaces to also be considered empty lines. However, in most syntax definitions you do not need to set this attribute.

The element `encoding` in the group `spellchecking` defines a character encoding for spell checking. Available attributes:

  `char` is a encoded character.

  `string` is a sequence of characters that will be encoded as the character char in the spell checking. For example, in the language LaTeX, the string `\"{A}` represents the character `Ä`.

#### Available Default Styles

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

General default styles:

  `dsNormal`, when no special highlighting is required.

  `dsKeyword`, built-in language keywords.

  `dsFunction`, function calls and definitions.

  `dsVariable`, if applicable: variable names (&eg; $someVar in PHP/Perl).

  `dsControlFlow`, control flow keywords like if, else, switch, break, return, yield, ...

  `dsOperator`, operators like + - * / :: < >

  `dsBuiltIn`, built-in functions, classes, and objects.

  `dsExtension`, common extensions, such as &Qt; classes and functions/macros in C++ and Python.

  `dsPreprocessor`, preprocessor statements or macro definitions.

  `dsAttribute`, annotations such as @override and __declspec(...).

String-related default styles:

  `dsChar`, single characters, such as 'x'.

  `dsSpecialChar`, chars with special meaning in strings such as escapes, substitutions, or regex operators.

  `dsString`, strings like "hello world".

  `dsVerbatimString`, verbatim or raw strings like 'raw \backlash' in Perl, CoffeeScript, and shells, as well as r'\raw' in Python.

  `dsSpecialString`, SQL, regexes, HERE docs, &latex; math mode, ...

  `dsImport`, import, include, require of modules.

Number-related default styles:

  `dsDataType`, built-in data types like int, void, u64.

  `dsDecVal`, decimal values.

  `dsBaseN`, values with a base other than 10.

  `dsFloat`, floating point values.

  `dsConstant`, built-in and user defined constants like PI.

Comment and documentation-related default styles:

  `dsComment`, comments.

  `dsDocumentation`, /** Documentation comments */ or """docstrings""".

  `dsAnnotation`, documentation commands like @param, @brief.

  `dsCommentVar`, the variable names used in above commands, like "foobar" in @param foobar.

  `dsRegionMarker`, region markers like //BEGIN, //END in comments.

Other default styles:

  `dsInformation`, notes and tips like @note in doxygen.

  `dsWarning`, warnings like @warning in doxygen.

  `dsAlert`, special words like TODO, FIXME, XXXX.

  `dsError`, error highlighting and wrong syntax.

  `dsOthers`, when nothing else fits.

### Highlight Detection Rules

This section describes the syntax detection rules.

Each rule can match zero or more characters at the beginning of the string they are tested against. If the rule matches, the matching characters are assigned the style or attribute defined by the rule, and a rule may ask that the current context is switched.

A rule looks like this:

    <RuleName attribute="(identifier)" context="(identifier)" [rule specific attributes] />

The attribute identifies the style to use for matched characters by name, and the context identifies the context to use from here.

The context can be identified by:

- An identifier, which is the name of the other context.

- An order telling the engine to stay in the current context (`#stay`), or to pop back to a previous context used in the string (`#pop`). An empty or absent context is equivalent to `#stay`.

  To go back more steps, the #pop keyword can be repeated: `#pop#pop#pop`

- An order followed by an exclamation mark (!) and an identifier, which will make the engine first follow the order and then switch to the other context, &eg; `#pop#pop!OtherContext`.

- An identifier, which is a context name, followed by two hashes (`##`) and another identifier, which is the name of a language definition. This naming is similar to that used in `IncludeRules` rules and allows you to switch to a context belonging to another syntax highlighting definition, e.g. `SomeContext##JavaScript`.

Rule specific attributes varies and are described in the following sections.

#### Common attributes

All rules have the following attributes in common and are available whenever `(common attributes)` appears. All attributes are optional.

- attribute: An attribute maps to a defined itemData. Default: attribute from the context specified in context attribute.

- context: Specify the context to which the highlighting system switches if the rule matches. Default: #stay.

- beginRegion: Start a code folding block. Default: unset.

- endRegion: Close a code folding block. Default: unset.

- lookAhead: If true, the highlighting system will not process the matches length. Default: false.

- firstNonSpace: Match only, if the string is the first non-whitespace in the line. Default: false.

- column: Match only, if the column matches. Default: unset.

#### Dynamic rules

Some rules allow the optional attribute `dynamic` of type boolean that defaults to false. If dynamic is true, a rule can use placeholders representing the text matched by a regular expression rule that switched to the current context in its `string` or `char` attributes. In a `string`, the placeholder %N (where N is a number) will be replaced with the corresponding capture N from the calling regular expression, starting from 1. In a `char` the placeholder must be a number N and it will be replaced with the first character of the corresponding capture N from the calling regular expression. Whenever a rule allows this attribute it will contain a (dynamic).

- dynamic: may be (true|false).

`How does it work:`

In the regular expressions of the `RegExpr` rules, all text within simple curved brackets `(PATTERN)` is captured and remembered. These captures can be used in the context to which it is switched, in the rules with the attribute `dynamic` true, by %N (in String) or N (in char).

It is important to mention that a text captured in a `RegExpr` rule is only stored for the switched context, specified in its `context` attribute.

- If the captures will not be used, both by dynamic rules and in the same regular expression, `non-capturing groups` should be used: `(?:PATTERN)`

  The lookahead or lookbehind groups such as `(?=PATTERN)`, `(?!PATTERN)` or `(?<=PATTERN)` are not captured. See Regular Expressions for more information.

- The capture groups can be used within the same regular expression, using \N instead of %N respectively. For more information, see Capturing matching text (back references) in Regular Expressions.

Example 1:

In this simple example, the text matched by the regular expression `=*` is captured and inserted into %1 in the dynamic rule. This allows the comment to end with the same amount of `=` as at the beginning. This matches text like: `[[ comment ]]`, `[=[ comment ]=]` or `[=====[ comment ]=====]`.

In addition, the captures are available only in the switched context Multi-line Comment.

    <context name="Normal" attribute="Normal Text" lineEndContext="#stay">
      <RegExpr context="Multi-line Comment" attribute="Comment" String="\[(=*)\[" beginRegion="RegionComment"/>
    </context>
    <context name="Multi-line Comment" attribute="Comment" lineEndContext="#stay">
      <StringDetect context="#pop" attribute="Comment" String="]%1]" dynamic="true" endRegion="RegionComment"/>
    </context>

Example 2:

In the dynamic rule, %1 corresponds to the capture that matches `#+`, and %2 to `&quot;+`. This matches text as: `#label""""inside the context""""#`.

These captures will not be available in other contexts, such as OtherContext, FindEscapes or SomeContext.

    <context name="SomeContext" attribute="Normal Text" lineEndContext="#stay">
      <RegExpr context="#pop!NamedString" attribute="String" String="(#+)(?:[\w-]|[^[:ascii:]])(&quot;+)"/>
    </context>
    <context name="NamedString" attribute="String" lineEndContext="#stay">
      <RegExpr context="#pop!OtherContext" attribute="String" String="%2(?:%1)?" dynamic="true"/>
      <DetectChar context="FindEscapes" attribute="Escape" char="\"/>
    </context>

Example 3:

This matches text like: `Class::function<T>( ... )`.

    <context name="Normal" attribute="Normal Text" lineEndContext="#stay">
      <RegExpr context="FunctionName" lookAhead="true"
                  String="\b([a-zA-Z_][\w-]*)(::)([a-zA-Z_][\w-]*)(?:&lt;[\w\-\s]*&gt;)?(\()"/>
    </context>
    <context name="FunctionName" attribute="Normal Text" lineEndContext="#pop">
      <StringDetect context="#stay" attribute="Class" String="%1" dynamic="true"/>
      <StringDetect context="#stay" attribute="Operator" String="%2" dynamic="true"/>
      <StringDetect context="#stay" attribute="Function" String="%3" dynamic="true"/>
      <DetectChar context="#pop" attribute="Normal Text" char="4" dynamic="true"/>
    </context>

#### Local deliminators

Some rules allow the optional attributes `weakDeliminator` and `additionalDeliminator` which are combined with attributes of the same name of `keywords` tag. For example, when `'%'` is a weak delimiter of `keywords`, it can become a word delimiter only for a rule by putting it in its `additionalDeliminator` attribute. Whenever a rule allows these attributes it will contain a (local deliminators).

- weakDeliminator: list of characters that do not act as word delimiters.

- additionalDeliminator: defines additional delimiters.

#### The Rules in Detail

DetectChar

  Detect a single specific character. Commonly used for example to find the ends of quoted strings.

      <DetectChar char="(character)" (common attributes) (dynamic) />

  The `char` attribute defines the character to match.

Detect2Chars

  Detect two specific characters in a defined order.

      <Detect2Chars char="(character)" char1="(character)" (common attributes) />

  The `char` attribute defines the first character to match, `char1` the second.

  This rule is present for historical reasons and for readability it's preferable to use `StringDetect`.

AnyChar

  Detect one character of a set of specified characters.

      <AnyChar String="(string)" (common attributes) />

  The `String` attribute defines the set of characters.

StringDetect

  Detect an exact string.

      <StringDetect String="(string)" [insensitive="true|false"] (common attributes) (dynamic) />

  The `String` attribute defines the string to match. The `insensitive` attribute defaults to false and is passed to the string comparison function. If the value is true insensitive comparing is used.

WordDetect

  Detect an exact string but additionally require word boundaries such as a dot `'.'` or a whitespace on the beginning and the end of the word. Think of `\b<string>\b` in terms of a regular expression, but it is faster than the rule `RegExpr`.

      <WordDetect String="(string)" [insensitive="true|false"] (common attributes) (local deliminators) />

  The `String` attribute defines the string to match. The `insensitive` attribute defaults to false and is passed to the string comparison function. If the value is true insensitive comparing is used.

  Since: &kate; 3.5 (&kde; 4.5)

RegExpr

  Matches against a regular expression.

      <RegExpr String="(string)" [insensitive="true|false"] [minimal="true|false"] (common attributes) (dynamic) />

  The `String` attribute defines the regular expression.

  `insensitive` defaults to false and is passed to the regular expression engine.

  `minimal` defaults to false and is passed to the regular expression engine.

  Because the rules are always matched against the beginning of the current string, a regular expression starting with a caret (`^`) indicates that the rule should only be matched against the start of a line.

  See Regular Expressions for more information on those.

keyword

  Detect a keyword from a specified list.

      <keyword String="(list name)" (common attributes) (local deliminators) />

  The `String` attribute identifies the keyword list by name. A list with that name must exist.

  The highlighting system processes keyword rules in a very optimized way. This makes it an absolute necessity that any keywords to be matched need to be surrounded by defined delimiters, either implied (the default delimiters), or explicitly specified within the additionalDeliminator property of the keywords tag.

  If a keyword to be matched shall contain a delimiter character, this respective character must be added to the weakDeliminator property of the keywords tag. This character will then loose its delimiter property in all keyword rules. It is also possible to use the weakDeliminator attribute of keyword so that this modification only applies to this rule.

Int

  Detect an integer number (as the regular expression: `\b[0-9]+`).

      <Int (common attributes) (local deliminators) />

  This rule has no specific attributes.

Float

  Detect a floating point number (as the regular expression: `(\b[0-9]+\.[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?`).

      <Float (common attributes) (local deliminators) />

  This rule has no specific attributes.

HlCOct

  Detect an octal point number representation (as the regular expression: `\b0[0-7]+`).

      <HlCOct (common attributes) (local deliminators) />

  This rule has no specific attributes.

HlCHex

  Detect a hexadecimal number representation (as a regular expression: `\b0[xX][0-9a-fA-F]+`).

      <HlCHex (common attributes) (local deliminators) />

  This rule has no specific attributes.

HlCStringChar

  Detect an escaped character.

      <HlCStringChar (common attributes) />

  This rule has no specific attributes.

  It matches literal representations of characters commonly used in program code, for example `\n` (newline) or `\t` (TAB).

  The following characters will match if they follow a backslash (`\`): `abefnrtv"'?\`. Additionally, escaped hexadecimal numbers such as for example `\xff` and escaped octal numbers, for example `\033` will match.

HlCChar

  Detect an C character.

      <HlCChar (common attributes) />

  This rule has no specific attributes.

  It matches C characters enclosed in a tick (Example: `'c'`). The ticks may be a simple character or an escaped character. See HlCStringChar for matched escaped character sequences.

RangeDetect

  Detect a string with defined start and end characters.

      <RangeDetect char="(character)"  char1="(character)" (common attributes) />

  `char` defines the character starting the range, `char1` the character ending the range.

  Useful to detect for example small quoted strings and the like, but note that since the highlighting engine works on one line at a time, this will not find strings spanning over a line break.

LineContinue

  Matches a specified char at the end of a line.

      <LineContinue (common attributes) [char="\"] />

  `char` optional character to match, default is backslash (`'\'`). New since &kde; 4.13.

  This rule is useful for switching context at end of line. This is needed for example in C/C++ to continue macros or strings.

IncludeRules

  Include rules from another context or language/file.

      <IncludeRules context="contextlink" [includeAttrib="true|false"] />

  The `context` attribute defines which context to include.

  If it is a simple string it includes all defined rules into the current context, example:

      <IncludeRules context="anotherContext" />

  If the string contains a `##` the highlight system will look for a context from another language definition with the given name, for example

      <IncludeRules context="String##C++" />

  would include the context String from the C++ highlighting definition.

  If `includeAttrib` attribute is true, change the destination attribute to the one of the source. This is required to make, for example, commenting work, if text matched by the included context is a different highlight from the host context.

DetectSpaces

  Detect whitespaces.

      <DetectSpaces (common attributes) />

  This rule has no specific attributes.

  Use this rule if you know that there can be several whitespaces ahead, for example in the beginning of indented lines. This rule will skip all whitespace at once, instead of testing multiple rules and skipping one at a time due to no match.

DetectIdentifier

  Detect identifier strings (as the regular expression: `[a-zA-Z_][a-zA-Z0-9_]*`).

      <DetectIdentifier (common attributes) />

  This rule has no specific attributes.

  Use this rule to skip a string of word characters at once, rather than testing with multiple rules and skipping one at a time due to no match.

#### Tips & Tricks

Once you have understood how the context switching works it will be easy to write highlight definitions. Though you should carefully check what rule you choose in what situation. Regular expressions are very mighty, but they are slow compared to the other rules. So you may consider the following tips.

- Regular expressions are easy to use but often there is another much faster way to achieve the same result. Consider you only want to match the character `'#'` if it is the first character in the line. A regular expression based solution would look like this:

      <RegExpr attribute="Macro" context="macro" String="^\s*#" />

  You can achieve the same much faster in using:

      <DetectChar attribute="Macro" context="macro" char="#" firstNonSpace="true" />

  If you want to match the regular expression `'^#'` you can still use `DetectChar` with the attribute `column="0"`. The attribute `column` counts characters, so a tabulator is only one character.

- In `RegExpr` rules, use the attribute `column="0"` if the pattern `^PATTERN` will be used to match text at the beginning of a line. This improves performance, as it will avoid looking for matches in the rest of the columns.

- In regular expressions, use non-capturing groups `(?:PATTERN)` instead of capturing groups `(PATTERN)`, if the captures will not be used in the same regular expression or in dynamic rules. This avoids storing captures unnecessarily.

- You can switch contexts without processing characters. Assume that you want to switch context when you meet the string `*/`, but need to process that string in the next context. The below rule will match, and the `lookAhead` attribute will cause the highlighter to keep the matched string for the next context.

      <StringDetect attribute="Comment" context="#pop" String="*/" lookAhead="true" />

- Use `DetectSpaces` if you know that many whitespaces occur.

- Use `DetectIdentifier` instead of the regular expression `'[a-zA-Z_]\w*'`.

- Use default styles whenever you can. This way the user will find a familiar environment.

- Look into other &XML; files to see how other people implement tricky rules.

- You can validate every &XML; file by using the command `validatehl.sh mySyntax.xml`. The file `validatehl.sh` uses `language.xsd` which are both available in Syntax Highlighting repository.

- If you repeat complex regular expression very often you can use ENTITIES. Example:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE language
      [
              <!ENTITY myref    "[A-Za-z_:][\w.:_-]*">
      ]>

  Now you can use &myref; instead of the regular expression.

- On Kate Editor, you can reload syntaxes using the built-in command line (`F7` shortcut by default) and the `reload-highlighting` command.

- You can use the command-line utility named `ksyntaxhighlighter6` (`kate-syntax-highlighter` on older versions) to test a syntax and display the style and regions associated with each part of a text.

  Result of `ksyntaxhighlighter6 --output-format=ansi --syntax-trace=format test.cpp`.

  Result of `ksyntaxhighlighter6 --output-format=ansi --syntax-trace=format test.cpp`.

  Use `ksyntaxhighlighter6 -h` for more options.

## Working with Color Themes

### Overview

Color themes define the colors of the text editing area and the syntax highlighting. A color theme encompasses the following:

- The text style, used for syntax highlighting through the default styles attributes. For example, the text color and the selected text color.

- The background of the text editing area, including the text selection and the current line.

- The icon border of the text area: their background, the separator line, the line numbers, the line word wrap markers, the modified line marks and the code folding.

- Text decorators such as the search markers, the indentation and tab/space line marks, the bracket matching and the spell checking.

- Bookmarks and snippets.

To avoid confusion, the following is out of scope:

- The font type and the font size.

- The colors of the text editing application, such as the scroll bar map, the menus, the tab bar, the window color, etc. In &kde; applications, like &kate; or &kdevelop;, these colors are defined by the `&kde; &plasma; global color scheme`, which are set in the Colors module in &systemsettings; or from the application itself in the menu Settings Color Scheme.

Breeze Light and Breeze Dark color themes with the C++ syntax highlighting.

Breeze Light and Breeze Dark color themes with the C++ syntax highlighting.

### The KSyntaxHighlighting Color Themes

The KSyntaxHighlighting framework, which is the syntax highlighting engine, is the library that `provides and manages the color themes`. This is part of &kde; &frameworks; and is used in &kde; text editors such as &kate;, &kwrite;, &kile; and &kdevelop;. This dependency looks like the following:

Dependence of &kde; &frameworks; 5 libraries on text editors.

Dependence of &kde; &frameworks; libraries on text editors.

KSyntaxHighlighting includes a variety of built-in themes which are displayed on the Color Themes page of the Kate editor website.

The KTextEditor framework, which is the text editing engine, provides a user interface for creating and editing color themes, including a tool for importing and exporting themes. This is the simplest way to create and edit them, you can access it from the Configure dialog of the text editor. More details in .

The &GUI; to manage color themes in &kate;'s settings.

The &GUI; to manage color themes in &kate;'s settings.

It is important to mention that, in the &kde; text editors like &kate; or &kdevelop;, the KSyntaxHighlighting color themes are used since &kde; &frameworks; 5.75, released on October 10, 2020. Previously, &kate;'s color schemes (KConfig based schema config) were used and are now deprecated. However, it is possible to convert the old &kate; schemes to the KSyntaxHighlighting color themes. The KSyntaxHighlighting repository includes the `utils/kateschema_to_theme_converter.py` script and the `utils/schema-converter/` utility for that purpose.

### The Color Themes &JSON; Format

#### Overview

Color themes are stored in &JSON; format files, with the `.theme` extension.

In the KSyntaxHighlighting source code, the &JSON; files of built-in themes are located in the `data/themes/` directory. Note that in text editors, the built-in themes are compiled into the KSyntaxHighlighting library, therefore, the way to access them is through the source code or by exporting them from the &GUI; to manage themes of KTextEditor.

It is also possible to easily add additional or custom themes, which are loaded from the file system. User-customized theme files are located in the `org.kde.syntax-highlighting/themes/` directory in your user folder, which you can find with the command ``qtpaths``--paths GenericDataLocation`` and are commonly ``$HOME`/.local/share/` and `/usr/share/`.

In Flatpak and Snap packages, the above directory will not work as the data location is different for each application. In a Flatpak application, the location of custom theme files is usually ``$HOME`/.var/app/flatpak-package-name/data/org.kde.syntax-highlighting/themes/` and in a Snap application that location is ``$HOME`/snap/snap-package-name/current/.local/share/org.kde.syntax-highlighting/themes/`.

On &Windows; these files are located `%USERPROFILE%\AppData\Local\org.kde.syntax-highlighting\themes`. %USERPROFILE% usually expands to `C:\Users\user-name`.

In summary, for most configurations the custom themes directory is as follows:

For local user | ``$HOME`/.local/share/org.kde.syntax-highlighting/themes/`
For all users | `/usr/share/org.kde.syntax-highlighting/themes/`
For Flatpak packages | ``$HOME`/.var/app/flatpak-package-name/data/org.kde.syntax-highlighting/themes/`
For Snap packages | ``$HOME`/snap/snap-package-name/current/.local/share/org.kde.syntax-highlighting/themes/`
On &Windows; | `%USERPROFILE%\AppData\Local\org.kde.syntax-highlighting\themes`
On &macOS; | ``$HOME`/Library/Application Support/org.kde.syntax-highlighting/themes/`

If multiple theme files exist with the same name, the file with the highest `revision` will be loaded.

#### The &JSON; Structure

The structure of a &JSON; file is explained on their website. Basically, a &JSON; format file consists of:

- Collections of key/value pairs, separated by commas and grouped in `{ }` which we will call objects.

- Ordered lists of values, separated by commas and grouped in `[ ]` which we will call array.

The nomenclature key, value, object and array will be used in this article. If this is your first time working with &JSON; files, understanding them is as simple as looking at the examples below.

#### Main Sections of the &JSON; Color Theme Files

The root object of the color theme &JSON; file contains the following schema keys:

- `metadata`: It is mandatory. The value is an object with the theme's metadata, such as name, revision and license.

  This is detailed in .

- `editor-colors`: It is mandatory. The value is an object with the colors of the text editing area, such as the background, the icon border and the text decoration.

  This is detailed in .

- `text-styles`: It is mandatory. The value is an object with the default text style attributes of the syntax highlighting. Each attribute defines its text color, its selected text color, or whether it bold or italic, for example. The text styles can be referenced from the attributes of the syntax definition &XML; files.

  This is detailed in .

- `custom-styles`: It is optional. Defines text styles for the attributes of specific syntax highlighting definitions. For example, in a highlighting definition such as `Python` or `Markdown` you can specify a different text style that overrides the default defined in `text-styles`.

  This is detailed in .

The &JSON; language does not support comments. However, you can use the optional key `_comments` in the root object to write comments, for example, if you are adapting an existing theme you can put the URL of the original repository. The most practical way is to use an array of strings.

Below is an example file for the Breeze Light theme. You can notice that, to avoid the example being too large, the `editor-colors` and `text-styles` objects do not contain all the required keys. You can see the full archive of the Breeze Light theme in the KSyntaxHighlighting repository.

    {
        "_comments": [
            "This is a comment.",
            "If this theme is an adaptation of another, put the link to the original repository."
        ],
        "metadata": {
            "name" : "Breeze Light",
            "revision" : 5,
            "copyright": [
                "SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>",
                "SPDX-FileCopyrightText: 2016 Dominik Haumann <dhaumann@kde.org>"
            ],
            "license": "SPDX-License-Identifier: MIT"
        },
        "editor-colors": {
            "BackgroundColor" : "#ffffff",
            "CodeFolding" : "#94caef",
            "BracketMatching" : "#ffff00",
            "CurrentLine" : "#f8f7f6",
            "IconBorder" : "#f0f0f0",
            "IndentationLine" : "#d2d2d2",
            "LineNumbers" : "#a0a0a0",
            "CurrentLineNumber" : "#1e1e1e",
            The other editor color keys...

        },
        "text-styles": {
            "Normal" : {
                "text-color" : "#1f1c1b",
                "selected-text-color" : "#ffffff",
                "bold" : false,
                "italic" : false,
                "underline" : false,
                "strike-through" : false
            },
            "Keyword" : {
                "text-color" : "#1f1c1b",
                "selected-text-color" : "#ffffff",
                "bold" : true
            },
            "Function" : {
                "text-color" : "#644a9b",
                "selected-text-color" : "#452886"
            },
            "Variable" : {
                "text-color" : "#0057ae",
                "selected-text-color" : "#00316e"
            },
            The other text style keys...

        },
        "custom-styles": {
            "ISO C++": {
                "Data Type": {
                    "bold": true,
                    "selected-text-color": "#009183",
                    "text-color": "#00b5cf"
                },
                "Keyword": {
                    "text-color": "#6431b3"
                }
            },
            "YAML": {
                "Attribute": {
                    "selected-text-color": "#00b5cf",
                    "text-color": "#00b5cf"
                }
            }
        }
    }

#### Metadata

The &JSON; object of the `metadata` key contains relevant information on the theme. This object has the following keys:

- `name`: It is a string sets the name of the language. It appears in the menus and dialogs afterwards. It is mandatory.

- `revision`: It is an integer number that specifies the current revision of the theme file. Whenever you update a color theme file, make sure to increase this number. It is mandatory.

- `license`: It is a string that defines the license of the theme, using the identifier `SPDX-License-Identifier` from the standard SPDX license communication format. It is optional.

  You can see the full list of SPDX license identifiers here.

- `copyright`: It is an array of strings that specifies the authors of the theme, using the identifier `SPDX-FileCopyrightText` from the standard SPDX license communication format. It is optional.

    "metadata": {
        "name" : "Breeze Light",
        "revision" : 5,
        "copyright": [
            "SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>",
            "SPDX-FileCopyrightText: 2016 Dominik Haumann <dhaumann@kde.org>"
        ],
        "license": "SPDX-License-Identifier: MIT"
    }

### Colors in Detail

This section details all the available color attributes and available color settings.

#### Editor Colors

Corresponds to the colors of the text editing area.

In the &JSON; theme file, the respective key `editor-colors` has as value an object where each key references an attribute color of the text editor. Here, `all available keys are mandatory`, their values are `strings` with hexadecimal color codes, like #00B5CF.

In the &GUI; to manage themes of KTextEditor, these attributes can be modified in the `Colors` tab.

The available keys are the following; the keys used in the &JSON; file are listed in bold, the names used in the &GUI; are shown in parentheses.

Editor Background Colors
  `BackgroundColor` (Text Area)

    This is the default background for the editor area, it will be the dominant color on the editor area.

  `TextSelection` (Selected Text)

    This is the background for selected text.

  `CurrentLine` (Current Line)

    Set the color for the current line. Setting this a bit different from the Normal text background helps to keep focus on the current line.

  `SearchHighlight` (Search Highlight)

    Set the color for the text that matches your last search.

  `ReplaceHighlight` (Replace Highlight)

    Set the color for the text that matches your last replace operation.

Icon Border
  `IconBorder` (Background Area)

    This color is used for the marks, line numbers and folding marker borders in the left side of the editor view when they are displayed.

  `LineNumbers` (Line Numbers)

    This color is used to draw the line numbers on the left side of the view when displayed.

  `CurrentLineNumber` (Current Line Number)

    This color is used to draw the line number of the current line, on the left side of the view when displayed. Setting this a bit different from LineNumbers helps to keep focus on the current line.

  `Separator` (Separator)

    This color is used to draw the vertical line that separates the icon border from the background of the text area.

  `WordWrapMarker` (Word Wrap Marker)

    This color is used to draw a pattern to the left of dynamically wrapped lines when those are aligned vertically, as well as for the static word wrap marker.

  `CodeFolding` (Code Folding)

    This color is used to highlight the section of code that would be folded when you click on the code folding arrow to the left of a document. For more information, see the code folding documentation.

  `ModifiedLines` (Modified Lines)

    This color is used to highlight to the left of a document lines that have been modified but not yet saved. For more information, see .

  `SavedLines` (Saved Lines)

    This color is used to highlight to the left of a document lines that have been modified this session and saved. For more information, see .

Text Decorations
  `SpellChecking` (Spelling Mistake Line)

    This color is used to indicate spelling mistakes.

  `TabMarker` (Tab and Space Markers)

    This color is used to draw white space indicators, when they are enabled.

  `IndentationLine` (Indentation Line)

    This color is used to draw a line to the left of indented blocks, if that feature is enabled.

  `BracketMatching` (Bracket Highlight)

    This color is used to draw the background of matching brackets.

Marker Colors
  `MarkBookmark` (Bookmark)

    This color is used to indicate bookmarks. Note that this color has an opacity of 22% (and 33% for the current line) with respect to the background. For more information, see .

  `MarkBreakpointActive` (Active Breakpoint)

    This color is used by the GDB plugin to indicate an active breakpoint. Notice that this color has an opacity to the background. For more information, see the GDB Plugin documentation.

  `MarkBreakpointReached` (Reached Breakpoint)

    This color is used by the GDB plugin to indicate a breakpoint you have reached while debugging. Notice that this color has an opacity to the background. For more information, see the GDB Plugin documentation.

  `MarkBreakpointDisabled` (Disabled Breakpoint)

    This color is used by the GDB plugin to indicate an inactive breakpoint. Notice that this color has an opacity to the background. For more information, see the GDB Plugin documentation.

  `MarkExecution` (Execution)

    This color is used by the GDB plugin the line presently being executed. Notice that this color has an opacity to the background. For more information, see the GDB Plugin documentation.

  `MarkWarning` (Warning)

    This color is used by the build plugin to indicate a line that has caused a compiler warning. Notice that this color has an opacity to the background. For more information, see the Build Plugin documentation.

  `MarkError` (Error)

    This color is used by the build plugin to indicate a line that has caused a compiler error. Notice that this color has an opacity to the background. For more information, see the Build Plugin documentation.

Text Templates & Snippets
  `TemplateBackground` (Background)

    This color is used by the &kate; Snippets plugin to mark the background of a snippet. For more information, see the &kate; Snippets documentation.

  `TemplatePlaceholder` (Editable Placeholder)

    This color is used by the &kate; Snippets plugin to mark a placeholder that you can click in to edit manually. For more information, see the &kate; Snippets documentation.

  `TemplateFocusedPlaceholder` (Focused Editable Placeholder)

    This color is used by the &kate; Snippets plugin to mark the placeholder that you are presently editing. For more information, see the &kate; Snippets documentation.

  `TemplateReadOnlyPlaceholder` (Not Editable Placeholder)

    This color is used by the &kate; Snippets plugin to mark a placeholder that cannot be edited manually, such as one that is automatically populated. For more information, see the &kate; Snippets documentation.

#### Default Text Styles

The default text styles are inherited by the highlight text styles, allowing the editor to present text in a very consistent way, for example comment text is using the same style in almost all of the text formats that KSyntaxHighlighting can highlight.

These text styles can be referenced from the `default styles` used in syntax highlighting definition &XML; files, for example, the Normal attribute is equivalent to dsNormal in the &XML; files, and DataType is equivalent to dsDataType. See in the syntax highlighting documentation.

Make sure to choose readable colors with good contrast especially in combination with the `Editor Colors`. See .

In the &JSON; file, the respective key `text-styles` has as value an object where each key corresponds to the name of a default text style, which are equivalent to those used in the syntax highlighting definitions. Here, `all available text style keys are mandatory`, these are listed below.

    "text-styles": {
        "Normal" : {
            "text-color" : "#1f1c1b",
            "selected-text-color" : "#ffffff",
            "bold" : false,
            "italic" : false,
            "underline" : false,
            "strike-through" : false
        },
        "Keyword" : {
            "text-color" : "#1f1c1b",
            "selected-text-color" : "#ffffff",
            "bold" : true
        },
        "Function" : {
            "text-color" : "#644a9b",
            "selected-text-color" : "#452886"
        },
        The other text style keys...
    }

Each key of default text style has a &JSON; object as its value, where values such as color, bold, italic, etc. are specified. These keys are as follows:

  `text-color`: It is a string with the text color in hexadecimal color code. This key/value is required.

  `selected-text-color`: The text color when it is selected is generally the same value as text-color. When the text is selected, the background is defined by the value of TextSelection in the Editor Colors, so you must ensure that the text has good contrast and is readable with this background. The value is a string with a hexadecimal color code. This key/value is required.

  `bold`: It is a boolean that determines if the text is in bold. This key is optional, the default value is `false`.

  `italic`: It is a boolean that determines if the text is curved. This key is optional, the default value is `false`.

  `underline`: It is a boolean that determines if the text is underlined. This key is optional, the default value is `false`.

  `strike-through`: It is a boolean that determines if the text is strike through. This key is optional, the default value is `false`.

  `background-color`: Determines the background of the text, used for example in alerts in comments. The value is a string with a hexadecimal color code. This key is optional, by default there is no background.

  `selected-background-color`: Determines the background of the text when it is selected. The value is a string with a hexadecimal color code. This key is optional, by default there is no background.

In the &GUI; to manage color themes of KTextEditor, these attributes can be modified in the `Default Text Styles` tab. The name in the list of styles is using the style configured for the item, providing you with an immediate preview when configuring a style. Each style lets you select common attributes as well as foreground and background colors. To unset a background color, right-click to use the context menu.

The available text style keys are the following; the keys used in the &JSON; file are listed in bold, the names used in the &GUI; are shown in parentheses if they are different.

Normal Text & Source Code

  `Normal`: Default text style for normal text and source code without special highlighting.

  `Keyword`: Text style for built-in language keywords.

  `Function`: Text style for function definitions and function calls.

  `Variable`: Text style for variables, if applicable. For instance, variables in PHP/Perl typically start with a `$`, so all identifiers following the pattern `$foo` are highlighted as variable.

  `ControlFlow` (Control Flow): Text style for control flow keywords, such as if, then, else, return, switch, break, yield, continue, etc.

  `Operator`: Text style for operators, such as `+`, `-`, `*`, `/`, `%`, etc.

  `BuiltIn` (Built-in): Text style for built-in language classes, functions and objects.

  `Extension`: Text style for well-known extensions, such as &Qt; classes, functions/macros in C++ and Python or boost.

  `Preprocessor`: Text style for preprocessor statements or macro definitions.

  `Attribute`: Text style for annotations or attributes of functions or objects, e.g. `@override` in Java, or `__declspec(...)` and `__attribute__((...))` in C++.

Numbers, Types & Constants

  `DataType` (Data Type): Text style for built-in data types such as int, char, float, void, u64, etc.

  `DecVal` (Decimal/Value): Text style for decimal values.

  `BaseN` (Base-N Integer): Text style for numbers with base other than 10.

  `Float` (Floating Point): Text style for floating point numbers.

  `Constant`: Text style for language constants and user defined constants, e.g. True, False, None in Python or nullptr in C/C++; or math constants like PI.

Strings & Characters

  `Char` (Character): Text style for single characters such as `'x'`.

  `SpecialChar` (Special Character): Text style for escaped characters in strings, e.g. `hello\n`, and other characters with special meaning in strings, such as substitutions or regex operators.

  `String`: Text style for strings like `hello world`.

  `VerbatimString` (Verbatim String): Text style for verbatim or raw strings like `'raw \backlash'` in Perl, CoffeeScript, and shells, as well as `r'\raw'` in Python, or such as HERE docs.

  `SpecialString` (Special String): Text style for special strings, such as regular expressions in ECMAScript, the &latex; math mode, SQL, etc.

  `Import` (Imports, Modules, Includes): Text style for includes, imports, modules or &latex; packages.

Comments & Documentation

  `Comment`: Text style for normal comments.

  `Documentation`: Text style for comments that reflect API documentation, such as `/** doxygen comments */` or `"""docstrings"""`.

  `Annotation`: Text style for annotations in comments or documentation commands, such as `@param` in Doxygen or JavaDoc.

  `CommentVar` (Comment Variable): Text style that refers to variables names used in above commands in a comment, such as `foobar` in `@param foobar`, in Doxygen or JavaDoc.

  `RegionMarker` (Region Marker): Text style for region markers, typically defined by `//BEGIN` and `//END` in comments.

  `Information`: Text style for information, notes and tips, such as the keyword `@note` in Doxygen.

  `Warning`: Text style for warnings, such as the keyword `@warning` in Doxygen.

  `Alert`: Text style for special words in comments, such as `TODO`, `FIXME`, `XXXX` and `WARNING`.

Miscellaneous

  `Error`: Text style indicating error highlighting and wrong syntax.

  `Others`: Text style for attributes that do not match any of the other default styles.

#### Custom Highlighting Text Styles

Here you can establish text styles for a specific syntax highlighting definition, overriding the `default text style` described in the previous section.

In the &JSON; theme file, this corresponds to the `custom-styles` key, whose value is an object where each subschema key corresponds to the `name of a syntax highlighting definition`. Its value is an object where each key refers to the `style attributes name` defined in the `itemData` elements of the syntax highlighting &XML; file, and the respective value is a sub-object with the keys text-color, selected-text-color, bold, italic, underline, strike-through, background-color and selected-background-color, defined in the previous section. Each of these values are optional, since if they are not present, the style set in `text-styles` is considered.

For example, in this piece of code, the ISO C++ syntax highlighting definition has a special text style for the Type Modifiers and Standard Classes attributes. In the corresponding &XML; file isocpp.xml, the defined attribute Standard Classes uses the default style `BuiltIn` (or dsBuiltIn). In this attribute, only the value of `text-color` is overwritten by the new color #6431b3.

    "custom-styles": {
        "ISO C++": {
            "Standard Classes": {
                "text-color": "#6431b3"
            },
            "Type Modifiers": {
                "bold": true,
                "selected-text-color": "#009183",
                "text-color": "#00b5cf"
            }
        }
    }

- You should consider that these text styles are associated with the attribute names defined in the syntax highlighting &XML; files. If an XML file is updated and some attributes are renamed or removed, the custom style defined in the theme will no longer apply.

- Syntax highlighting definitions often include other definitions. For example, the QML highlighter includes the JavaScript highlighter, since they share functionality in highlighting.

In the &GUI; to manage themes of KTextEditor, these attributes can be modified in the `Highlighting Text Styles` tab. By default, the editor preselects the highlighting of the current document. You will notice that many highlights contain other highlights represented by groups in the style list. For example most highlights import the Alert highlight, and many source code formats import the Doxygen highlight.

### The Color Themes &GUI;

The simplest way to create and edit color themes is through the &GUI; within the Configure dialog provided by KTextEditor. To access it, select Settings Configure Application... from the menubar in your text editor. This brings up the Configure dialog box, there select Color Themes in the side panel.

&kate;'s settings dialog box with the color theme management.

&kate;'s settings dialog box with the color theme management.

In this dialog you can configure all the colors in any theme you have, as well as create/copy new themes, delete them, export them to a `.theme` file with &JSON; format or import them from external `.theme` files. Each theme has settings for text colors and styles.

The built-in themes cannot be modified by default. To do this, you need to copy them and give them a new name.

To use a theme permanently in your text editor, you must select it in the combobox labeled Default theme for Application at the bottom of the dialog and press Apply or OK. By default, the `Automatic Selection` option is active, which chooses a more appropriate color theme according to the &kde; &plasma; color scheme used in the text editing application; it usually chooses between Breeze Light and Breeze Dark if the scheme is light or dark, respectively.

You can adjust the &kde; global color scheme in the Colors module in &systemsettings;. You can also change it in some applications individually such as &kate; or &kdevelop;, from the menu Settings Color Scheme.

#### Create a new theme

To create a new theme, it is first necessary to copy an existing one. Select an existing theme which you want to use as a base, such as Breeze Light or Breeze Dark, and click Copy. Then write a name for the new theme.

If you want to modify a built-in or read-only theme, you must first copy it under a different name.

#### Import or export &JSON; theme files

You can export a selected theme (including built-in ones) to a &JSON; file with `.theme` extension, with the Export button. This will open a dialog to save the file. To add a color theme from an external &JSON; file, just press the Import button and select the `.theme` file from the dialog.

- As mentioned above, user-customized theme files are stored in the `org.kde.syntax-highlighting/themes/` directory. When you copy or create a theme, it will automatically appear there. Also, importing or adding a theme is equivalent to copying an external `.theme` file into this directory. KSyntaxHighlighting automatically picks up color theme files from this directory.

- If you want to publish a theme created by you, it is essential to check the metadata object of the &JSON; file, adding the respective license and checking the revision number.

#### Editing color themes

##### Colors

Here the colors of the text editing area are adjusted. These are detailed in .

##### Default Text Styles

The default text styles are inherited by the highlight text styles, allowing the editor to present text in a very consistent way, for example comment text is using the same style in almost all of the text formats that KSyntaxHighlighting can highlight.

The name in the list of styles is using the style configured for the item, providing you with an immediate preview when configuring a style.

Each style lets you select common attributes as well as foreground and background colors. To unset a background color, right-click to use the context menu.

The attributes of this area are detailed in .

##### Highlighting Text Styles

Here you can edit the text styles used by a specific highlight definition. The editor preselects the highlight used by your current document. To work on a different highlight, select one in the Highlight combobox above the style list.

The name in the list of styles is using the style configured for the item, providing you with an immediate preview when configuring a style.

Each style lets you select common attributes as well as foreground and background colors. To unset a background color, right-click to use the context menu. In addition you can see if a style is equal to the default style used for the item, and set it to that if not.

You will notice that many highlights contain other highlights represented by groups in the style list. For example most highlights import the Alert highlight, and many source code formats imports the Doxygen highlight. Editing colors in those groups only affects the styles when used in the edited highlight format.

### Tips & Tricks

#### Contrast of Text Colors

An important aspect when working with color themes is to choose a text contrast that makes it easier to read, especially in combination with the background.

The `Kontrast` application is a color contrast checker. It tells you that the text color and background color combinations are readable and accessible, so this is an excellent tool to help you create color themes.

You can download `Kontrast` from the &kde; Applications website or from the Flatpak package on Flathub (only in GNU/Linux).

The GNOME `Contrast` application is similar. You can download the Flatpak package on Flathub (only in GNU/Linux).

A python script in Syntax Highlighting repository lets you visualize all the colors in a theme, as well as the contrast with the various backgrounds configured.

#### Suggestions of Consistency with Syntax Highlighting

KSyntaxHighlighting includes more than 300 syntax highlighting definitions, therefore it is ideal that you make sure your new theme looks good in all syntax highlighting definitions. The built-in color themes have the following similarities that it is recommended (but not required) to follow to achieve a correct display of all syntax highlighting definitions:

- Use bold for the Keyword and ControlFlow text styles.

- Do not use background color in any text style, except Alert and RegionMarker.

Most of the syntax highlighters are intended to look good on the default themes Breeze Light and Breeze Dark, therefore, another way to maintain consistency is to use similar colors in the text styles, like green for Preprocessor and Others, blue for DataType and Attribute, or purple for Function.

Note that these recommendations are not mandatory when creating and publishing a theme.

## Scripting with &javascript;

The &kappname; editor component is easily extensible by writing scripts. The scripting language is ECMAScript (widely known as &javascript;). &kappname; supports two kinds of scripts: indentation and command line scripts. A functionality similar to command line scripts is also provided in the snippets plugin.

### Indentation Scripts

Indentation scripts - also referred as indenters - automatically indent the source code while typing text. As an example, after hitting the return key the indentation level often increases.

The following sections describe step by step how to create the skeleton for a simple indenter. As a first step, create a new `*.js` file called &eg; `javascript.js` in the local home folder `$`XDG_DATA_HOME`/katepart5/script/indentation`. Therein, the environment variable `XDG_DATA_HOME` typically expands to either `~/.local` or `~/.local/share`.

On &Windows; these files are located in `%USERPROFILE%\AppData\Local\katepart5\script\indentation`. %USERPROFILE% usually expands to `C:\\Users\\user`.

#### The Indentation Script Header

The header of the file `javascript.js` is embedded as &JSON; at the beginning of the document as follows:

    var katescript = {
        "name": "JavaScript",
        "author": "Example Name <example.name@some.address.org>",
        "license": "BSD License",
        "revision": 1,
        "kate-version": "5.1",
        "required-syntax-style": "javascript",
        "indent-languages": ["javascript"],
        "priority": 0,
    }; // kate-script-header, must be at the start of the file without comments

Each entry is explained in detail now:

- `name` [required]: This is the indenter name that appears in the menu Tools Indentation and in the configuration dialog.

- `author` [optional]: The author's name and contact information.

- `license` [optional]: Short form of the license, such as BSD License or LGPLv3.

- `revision` [required]: The revision of the script. This number should be increased whenever the script is modified.

- `kate-version` [required]: Minimum required &kappname; version.

- `required-syntax-style` [optional]: The required syntax style, which matches the specified `style` in syntax highlighting files. This is important for indenters that rely on specific highlight information in the document. If a required syntax style is specified, the indenter is available only when the appropriate highlighter is active. This prevents undefined behavior caused by using the indenter without the expected highlighting schema. For instance, the Ruby indenter makes use of this in the files `ruby.js` and `ruby.xml`.

- `indent-languages` [optional]: &JSON; array of syntax styles the indenter can indent correctly, &eg;: `["c++", "java"]`.

- `priority` [optional]: If several indenters are suited for a certain highlighted file, the priority decides which indenter is chosen as default indenter.

#### The Indenter Source Code

Having specified the header this section explains how the indentation scripting itself works. The basic skeleton of the body looks like this:

    // required katepart js libraries, &eg; range.js if you use Range
    require ("range.js");

    triggerCharacters = "{}/:;";
    function indent(line, indentWidth, ch)
    {
        // called for each newline (ch == '\n') and all characters specified in
        // the global variable triggerCharacters. When calling ToolsFormat Indentation
        // the variable ch is empty, i.e. ch == ''.
        //
        // see also: Scripting API
        return -2;
    }

The function `indent()` has three parameters:

- `line`: the line that has to be indented

- `indentWidth`: the indentation width in number of spaces

- `ch`: either a newline character (`ch == '\n'`), the trigger character specified in `triggerCharacters` or empty if the user invoked the action Tools Format Indentation.

The return value of the `indent()` function specifies how the line will be indented. If the return value is a simple integer number, it is interpreted as follows:

- return value `-2`: do nothing

- return value `-1`: keep indentation (searches for previous non-blank line)

- return value `0`: numbers >= 0 specify the indentation depth in spaces

Alternatively, an array of two elements can be returned:

- `return [ indent, align ];`

In this case, the first element is the indentation depth as above with the same meaning of the special values. However, the second element is an absolute value representing a column for alignment. If this value is higher than the indent value, the difference represents a number of spaces to be added after the indentation of the first parameter. Otherwise, the second number is ignored. Using tabs and spaces for indentation is often referred to as mixed mode.

Consider the following example: Assume using tabs to indent, and tab width is set to 4. Here, <tab> represents a tab and '.' a space:

    1: <tab><tab>foobar("hello",
    2: <tab><tab>......."world");

When indenting line 2, the `indent()` function returns [8, 15]. As result, two tabs are inserted to indent to column 8, and 7 spaces are added to align the second parameter under the first, so that it stays aligned if the file is viewed with a different tab width.

A default &kde; installation ships &kappname; with several indenters. The corresponding &javascript; source code can be found in `$`XDG_DATA_DIRS`/katepart5/script/indentation`.

On &Windows; these files are located in `%USERPROFILE%\AppData\Local\katepart5\script\indentation`. %USERPROFILE% usually expands to `C:\\Users\\user`.

Developing an indenter requires reloading the scripts to see whether the changes behave appropriately. Instead of restarting the application, simply switch to the command line and invoke the command `reload-scripts`.

If you develop useful scripts please consider contributing to the &kappname; Project by providing a merge request to our GitLab project.

### Command Line Scripts

A functionality similar to command line scripts is also provided in the snippets plugin. This plugin may provide an easier starting point, especially for small custom scripts.

As it is hard to satisfy everyone's needs, &kappname; supports little helper tools for quick text manipulation through the built-in command line. For instance, the command `sort` is implemented as a script. This section explains how to create `*.js` files to extend &kappname; with arbitrary helper scripts.

Command line scripts are located in the same folder as indentation scripts. So as a first step, create a new `*.js` file called `myutils.js` in the local home folder `$`XDG_DATA_HOME`/katepart5/script/commands`. Therein, the environment variable `XDG_DATA_HOME` typically expands to either `~/.local` or `~/.local/share`.

On &Windows; these files are located in `%USERPROFILE%\AppData\Local\katepart5\script\commands`. %USERPROFILE% usually expands to `C:\\Users\\user`.

#### The Command Line Script Header

The header of each command line script is embedded in &JSON; at the beginning of the script as follows:

    var katescript = {
        "author": "Example Name <example.name@some.address.org>",
        "license": "LGPLv2+",
        "revision": 1,
        "kate-version": "5.1",
        "functions": ["sort", "moveLinesDown"],
        "actions": [
            {   "function": "sort",
                "name": "Sort Selected Text",
                "category": "Editing",
                "interactive": "false"
            },
            {   "function": "moveLinesDown",
                "name": "Move Lines Down",
                "category": "Editing",
                "shortcut": "Ctrl+Shift+Down",
                "interactive": "false"
            }
        ]
    }; // kate-script-header, must be at the start of the file without comments

Each entry is explained in detail now:

- `author` [optional]: The author's name and contact information.

- `license` [optional]: Short form of the license, such as BSD License or LGPLv2.

- `revision` [required]: The revision of the script. This number should be increased whenever the script is modified.

- `kate-version` [required]: Minimum required &kappname; version.

- `functions` [required]: &JSON; array of commands in the script.

- `actions` [optional]: &JSON; Array of &JSON; objects that defines the actions that appear in the application menu. Detailed information is provided in the section Binding Shortcuts.

Since the value of `functions` is a &JSON; array, a single script is able to contain an arbitrary number of command line commands. Each function is available through &kappname;'s built-in command line.

#### The Script Source Code

All functions specified in the header have to be implemented in the script. For instance, the script file from the example above needs to implement the two functions `sort` and `moveLinesDown`. All functions have the following syntax:

    // required katepart js libraries, &eg; range.js if you use Range
    require ("range.js");

    function <name>(arg1, arg2, ...)
    {
        // ... implementation, see also: Scripting API
    }

Arguments in the command line are passed to the function as `arg1`, `arg2`, &etc; In order to provide documentation for each command, simply implement the '`help`' function as follows:

    function help(cmd)
    {
        if (cmd == "sort") {
            return i18n("Sort the selected text.");
        } else if (cmd == "...") {
            // ...
        }
    }

Executing `help sort` in the command line then calls this help function with the argument `cmd` set to the given command, &ie; `cmd == "sort"`. &kappname; then presents the returned text as documentation to the user. Make sure to translate the strings.

Developing a command line script requires reloading the scripts to see whether the changes behave appropriately. Instead of restarting the application, simply switch to the command line and invoke the command `reload-scripts`.

##### Binding Shortcuts

In order to make the scripts accessible in the application menu and assign shortcuts, the script needs to provide an appropriate script header. In the above example, both functions `sort` and `moveLinesDown` appear in the menu due to the following part in the script header:

    var katescript = {
        ...
        "actions": [
            {   "function": "sort",
                "name": "Sort Selected Text",
                "icon": "",
                "category": "Editing",
                "interactive": "false"
            },
            {   "function": "moveLinesDown",
                "name": "Move Lines Down",
                "icon": "",
                "category": "Editing",
                "shortcut": "Ctrl+Shift+Down",
                "interactive": "false"
            }
        ]
    };

The fields for one action are as follows:

- `function` [required]: The function that should appear in the menu Tools Scripts.

- `name` [required]: The text appears in the script menu.

- `icon` [optional]: The icon appears next to the text in the menu. All &kde; icon names can be used here.

- `category` [optional]: If a category is specified, the script appears in a submenu.

- `shortcut` [optional]: The shortcut given here is the default shortcut. Example: `Ctrl+Alt+t`. See the &Qt; documentation for further details.

- `interactive` [optional]: If the script needs user input in the command line, set this to `true`.

If you develop useful scripts please consider contributing to the &kappname; Project by providing a merge request to our GitLab project.

### Scripting API

The scripting API presented here is available to all scripts, &ie; indentation scripts and command line commands. The `Cursor` and `Range` classes are provided by library files in `$`XDG_DATA_DIRS`/katepart5/libraries`. If you want to use them in your script, which needs to use some of the `Document` or `View` functions, please include the necessary library by using:

    // required katepart js libraries, &eg; range.js if you use Range
    require ("range.js");

To extend the standard scripting API with your own functions and prototypes simply create a new file in &kde;'s local configuration folder `$`XDG_DATA_HOME`/katepart5/libraries` and include it into your script using:

    require ("myscriptnamehere.js");

On &Windows; these files are located in `%USERPROFILE%\AppData\Local\katepart5\libraries`. %USERPROFILE% usually expands to `C:\\Users\\user`.

To extend existing prototypes like `Cursor` or `Range`, the recommended way is to not modify the global `*.js` files. Instead, change the `Cursor` prototype in &javascript; after the `cursor.js` is included into your script via `require`.

#### Cursors and Ranges

As &kappname; is a text editor, all the scripting API is based on cursors and ranges whenever possible. A Cursor is a simple `(line, column)` tuple representing a text position in the document. A Range spans text from a starting cursor position to an ending cursor position. The API is explained in detail in the next sections.

##### The Cursor Prototype

    Cursor();

  Constructor. Returns a Cursor at position `(0, 0)`.

  Example: `var cursor = new Cursor();`

    Cursor(int line, int column);

  Constructor. Returns a Cursor at position (line, column).

  Example: `var cursor = new Cursor(3, 42);`

    Cursor(Cursor other);

  Copy constructor. Returns a copy of the cursor other.

  Example: `var copy = new Cursor(other);`

    Cursor Cursor.clone();

  Returns a clone of the cursor.

  Example: `var clone = cursor.clone();`

    Cursor.setPosition(int line, int column);

  Sets the cursor position to line and column.

  Since: &kde; 4.11

    bool Cursor.isValid();

  Check whether the cursor is valid. The cursor is invalid, if line and/or column are set to `-1`.

  Example: `var valid = cursor.isValid();`

    Cursor Cursor.invalid();

  Returns a new invalid cursor located at `(-1, -1)`.

  Example: `var invalidCursor = cursor.invalid();`

    int Cursor.compareTo(Cursor other);

  Compares this cursor to the cursor other. Returns

  - `-1`, if this cursor is located before the cursor other,

  - `0`, if both cursors equal and

  - `+1`, if this cursor is located after the cursor other.

    bool Cursor.equals(Cursor other);

  Returns `true`, if this cursor and the cursor other are equal, otherwise `false`.

    String Cursor.toString();

  Returns the cursor as a string of the form `Cursor(line, column)`.

##### The Range Prototype

    Range();

  Constructor. Calling `new Range()` returns a Range at (0, 0) - (0, 0).

    Range(Cursor start, Cursor end);

  Constructor. Calling `new Range(start, end)` returns the Range (start, end).

    Range(int startLine, int startColumn, int endLine, int endColumn);

  Constructor. Calling `new Range(startLine, startColumn, endLine, endColumn)` returns the Range from (startLine, startColumn) to (endLine, endColumn).

    Range(Range other);

  Copy constructor. Returns a copy of Range other.

    Range Range.clone();

  Returns a clone of the range.

  Example: `var clone = range.clone();`

    bool Range.isEmpty();

  Returns `true`, if the start and end cursors are equal.

  Example: `var empty = range.isEmpty();`

  Since: &kde; 4.11

    bool Range.isValid();

  Returns `true`, if both start and end cursor are valid, otherwise `false`.

  Example: `var valid = range.isValid();`

    Range Range.invalid();

  Returns the Range from (-1, -1) to (-1, -1).

    bool Range.contains(Cursor cursor);

  Returns `true`, if this range contains the cursor position, otherwise `false`.

    bool Range.contains(Range other);

  Returns `true`, if this range contains the Range other, otherwise `false`.

    bool Range.containsColumn(int column);

  Returns `true`, if column is in the half open interval `[start.column, end.column)`, otherwise `false`.

    bool Range.containsLine(int line);

  Returns `true`, if line is in the half open interval `[start.line, end.line)`, otherwise `false`.

    bool Range.overlaps(Range other);

  Returns `true`, if this range and the range other share a common region, otherwise `false`.

    bool Range.overlapsLine(int line);

  Returns `true`, if line is in the interval `[start.line, end.line]`, otherwise `false`.

    bool Range.overlapsColumn(int column);

  Returns `true`, if column is in the interval `[start.column, end.column]`, otherwise `false`.

    bool Range.onSingleLine();

  Returns `true`, if the range starts and ends at the same line, &ie; if Range.start.line == Range.end.line.

  Since: &kde; 4.9

    bool Range.equals(Range other);

  Returns `true`, if this range and the Range other are equal, otherwise `false`.

    String Range.toString();

  Returns the range as a string of the form `Range(Cursor(line, column), Cursor(line, column))`.

#### Global Functions

This section lists all global functions.

##### Reading & Including Files

    String read(String file);

  Will search the given file relative to the `katepart5/script/files` directory and return its content as a string.

    void require(String file);

  Will search the given file relative to the `katepart5/script/libraries` directory and evaluate it. `require` is internally guarded against multiple inclusions of the same file.

  Since: &kde; 4.10

##### Debugging

    void debug(String text);

  Prints text to `stdout` in the console launching the application.

##### Translation

In order to support full localization, there are several functions to translate strings in scripts, namely `i18n`, `i18nc`, `i18np` and `i18ncp`. These functions behave exactly like &kde;'s translation functions.

The translation functions translate the wrapped strings through &kde;'s translation system to the language used in the application. Strings in scripts being developed in the official &kappname; sources are automatically extracted and translatable. In other words, as a &kappname; developer you do not have to bother with message extraction and translation. It should be noted though, that the translation only works inside the &kde; infrastructure, &ie;, new strings in 3rd-party scripts developed outside of &kde; are not translated. Therefore, please consider contributing your scripts to &kate; such that proper translation is possible.

    void i18n(String text, arg1, ...);

  Translates text into the language used by the application. The arguments arg1, ..., are optional and used to replace the placeholders `%1`, `%2`, &etc;

    void i18nc(String context, String text, arg1, ...);

  Translates text into the language used by the application. Additionally, the string context is visible to translators so they can provide a better translation. The arguments arg1, ..., are optional and used to replace the placeholders `%1`, `%2`, &etc;

    void i18np(String singular, String plural, int number, arg1, ...);

  Translates either singular or plural into the language used by the application, depending on the given number. The arguments arg1, ..., are optional and used to replace the placeholders `%1`, `%2`, &etc;

    void i18ncp(String context, String singular, String plural, int number, arg1, ...);

  Translates either singular or plural into the language used by the application, depending on the given number. Additionally, the string context is visible to translators so they can provide a better translation. The arguments arg1, ..., are optional and used to replace the placeholders `%1`, `%2`, &etc;

#### The View API

Whenever a script is being executed, there is a global variable `view` representing the current active editor view. The following is a list of all available View functions.

    void view.copy()

  Copy the selection if there is one, otherwise the current line if the option `[ ] Copy/Cut the current line if no selection` is set.

  Since: &kde-frameworks; 5.79

    void view.cut()

  Cut the selection if there is one, otherwise the current line if the option `[ ] Copy/Cut the current line if no selection` is set.

  Since: &kde-frameworks; 5.79

    void view.paste()

  Paste the clipboard content.

  Since: &kde-frameworks; 5.79

    Cursor view.cursorPosition()

  Returns the current cursor position in the view.

    void view.setCursorPosition(int line, int column);
    void view.setCursorPosition(Cursor cursor);

  Set the current cursor position to either (line, column) or to the given cursor.

    Cursor view.virtualCursorPosition();

  Returns the virtual cursor position with each tab counting the corresponding amount of spaces depending on the current tab width.

    void view.setVirtualCursorPosition(int line, int column);
    void view.setVirtualCursorPosition(Cursor cursor);

  Set the current virtual cursor position to (line, column) or to the given cursor.

    String view.selectedText();

  Returns the selected text. If no text is selected, the returned string is empty.

    bool view.hasSelection();

  Returns `true`, if the view has selected text, otherwise `false`.

    Range view.selection();

  Returns the selected text range. The returned range is invalid if there is no selected text.

    void view.setSelection(Range range);

  Set the selected text to the given range.

    void view.removeSelectedText();

  Remove the selected text. If the view does not have any selected text, this does nothing.

    void view.selectAll();

  Selects the entire text in the document.

    void view.clearSelection();

  Clears the text selection without removing the text.

    void view.setBlockSelection(bool on);

  Set block selection mode on or off.

    bool view.blockSelection();

  Returns `true`, if block selection mode is on, otherwise `false`.

    void view.align(Range range);

  Properly re-indent lines within range according to current indentation settings.

    void view.alignOn(Range range, String pattern = "");

  Aligns lines in range on the column given by the regular expression pattern. With an empty pattern it will align on the first non-blank character by default. If the pattern has a capture it will indent on the captured match.

  Examples:

  `view.alignOn(document.documentRange(), '-');` will insert spaces before the first `-` of each lines to align them all on the same column.

  `view.alignOn(document.documentRange(), ':\\s+(.)');` will insert spaces before the first non-blank character that occurs after a colon to align them all on the same column.

    object view.executeCommand(String command,
                               String args,
                               Range range);

  Executes the command line command command with the optional arguments args and the optional range. The returned object has a boolean property object.ok that indicates whether execution of the command was successful. In case of an error, the string object.status contains an error message.

  Since: &kde-frameworks; 5.50

    Range view.searchText(Range range,
                          String pattern,
                          bool backwards = false);

  Search for the first occurrence of pattern in range and returns the matched range. Search is performed backwards if the optional boolean parameter backwards is set to `true`.

  The returned range is invalid (see Range.isValid()) if pattern is not found in range.

  Since: &kde-frameworks; 5.97

#### The Document API

Whenever a script is being executed, there is a global variable `document` representing the current active document. The following is a list of all available Document functions.

    String document.fileName();

  Returns the document's filename or an empty string for unsaved text buffers.

    String document.url();

  Returns the document's full &URL; or an empty string for unsaved text buffers.

    String document.mimeType();

  Returns the document's &MIME; type or the &MIME; type `application/octet-stream` if no appropriate &MIME; type could be found.

    String document.encoding();

  Returns the currently used encoding to save the file.

    String document.highlightingMode();

  Returns the global highlighting mode used for the whole document.

    String document.highlightingModeAt(Cursor pos);

  Returns the highlighting mode used at the given position in the document.

    Array document.embeddedHighlightingModes();

  Returns an array of highlighting modes embedded in this document.

    bool document.isModified();

  Returns `true`, if the document has unsaved changes (modified), otherwise `false`.

    String document.text();

  Returns the entire content of the document in a single text string. Newlines are marked with the newline character `\n`.

    String document.text(int fromLine, int fromColumn, int toLine, int toColumn);
    String document.text(Cursor from, Cursor to);
    String document.text(Range range);

  Returns the text in the given range. It is recommended to use the cursor and range based version for better readability of the source code.

    String document.line(int line);

  Returns the given text line as string. The string is empty if the requested line is out of range.

    String document.wordAt(int line, int column);
    String document.wordAt(Cursor cursor);

  Returns the word at the given cursor position.

    Range document.wordRangeAt(int line, int column);
    Range document.wordRangeAt(Cursor cursor);

  Return the range of the word at the given cursor position. The returned range is invalid (see Range.isValid()), if the text position is after the end of a line. If there is no word at the given cursor, an empty range is returned.

  Since: &kde; 4.9

    String document.charAt(int line, int column);
    String document.charAt(Cursor cursor);

  Returns the character at the given cursor position.

    String document.firstChar(int line);

  Returns the first character in the given line that is not a whitespace. The first character is at column 0. If the line is empty or only contains whitespace characters, the returned string is empty.

    String document.lastChar(int line);

  Returns the last character in the given line that is not a whitespace. If the line is empty or only contains whitespace characters, the returned string is empty.

    bool document.isSpace(int line, int column);
    bool document.isSpace(Cursor cursor);

  Returns `true`, if the character at the given cursor position is a whitespace, otherwise `false`.

    bool document.matchesAt(int line, int column, String text);
    bool document.matchesAt(Cursor cursor, String text);

  Returns `true`, if the given text matches at the corresponding cursor position, otherwise `false`.

    bool document.startsWith(int line, String text, bool skipWhiteSpaces);

  Returns `true`, if the line starts with text, otherwise `false`. The argument skipWhiteSpaces controls whether leading whitespaces are ignored.

    bool document.endsWith(int line, String text, bool skipWhiteSpaces);

  Returns `true`, if the line ends with text, otherwise `false`. The argument skipWhiteSpaces controls whether trailing whitespaces are ignored.

    bool document.setText(String text);

  Sets the entire document text.

    bool document.clear();

  Removes the entire text in the document.

    bool document.truncate(int line, int column);
    bool document.truncate(Cursor cursor);

  Truncate the given line at the given column or cursor position. Returns `true` on success, or `false` if the given line is not part of the document range.

    bool document.insertText(int line, int column, String text);
    bool document.insertText(Cursor cursor, String text);

  Inserts the text at the given cursor position. Returns `true` on success, or `false`, if the document is in read-only mode.

    bool document.removeText(int fromLine, int fromColumn, int toLine, int toColumn);
    bool document.removeText(Cursor from, Cursor to);
    bool document.removeText(Range range);

  Removes the text in the given range. Returns `true` on success, or `false`, if the document is in read-only mode.

    bool document.insertLine(int line, String text);

  Inserts text in the given line. Returns `true` on success, or `false`, if the document is in read-only mode or the line is not in the document range.

    bool document.removeLine(int line);

  Removes the given text line. Returns `true` on success, or `false`, if the document is in read-only mode or the line is not in the document range.

    bool document.wrapLine(int line, int column);
    bool document.wrapLine(Cursor cursor);

  Wraps the line at the given cursor position. Returns `true` on success, otherwise `false`, &eg; if line < 0.

  Since: &kde; 4.9

    void document.joinLines(int startLine, int endLine);

  Joins the lines from startLine to endLine. Two succeeding text lines are always separated with a single space.

    int document.lines();

  Returns the number of lines in the document.

    bool document.isLineModified(int line);

  Returns `true`, if line currently contains unsaved data.

  Since: &kde; 5.0

    bool document.isLineSaved(int line);

  Returns `true`, if line was changed, but the document was saved. Hence, the line currently does not contain any unsaved data.

  Since: &kde; 5.0

    bool document.isLineTouched(int line);

  Returns `true`, if line currently contains unsaved data or was changed before.

  Since: &kde; 5.0

    bool document.findTouchedLine(int startLine, bool down);

  Search for the next touched line starting at line. The search is performed either upwards or downwards depending on the search direction specified in down.

  Since: &kde; 5.0

    int document.length();

  Returns the number of characters in the document.

    int document.lineLength(int line);

  Returns the line's length.

    void document.editBegin();

  Starts an edit group for undo/redo grouping. Make sure to always call `editEnd()` as often as you call `editBegin()`. Calling `editBegin()` internally uses a reference counter, &ie;, this call can be nested.

    void document.editEnd();

  Ends an edit group. The last call of `editEnd()` (&ie; the one for the first call of `editBegin()`) finishes the edit step.

    int document.firstColumn(int line);

  Returns the first non-whitespace column in the given line. If there are only whitespaces in the line, the return value is `-1`.

    int document.lastColumn(int line);

  Returns the last non-whitespace column in the given line. If there are only whitespaces in the line, the return value is `-1`.

    int document.prevNonSpaceColumn(int line, int column);
    int document.prevNonSpaceColumn(Cursor cursor);

  Returns the column with a non-whitespace character starting at the given cursor position and searching backwards.

    int document.nextNonSpaceColumn(int line, int column);
    int document.nextNonSpaceColumn(Cursor cursor);

  Returns the column with a non-whitespace character starting at the given cursor position and searching forwards.

    int document.prevNonEmptyLine(int line);

  Returns the next non-empty line containing non-whitespace characters searching backwards.

    int document.nextNonEmptyLine(int line);

  Returns the next non-empty line containing non-whitespace characters searching forwards.

    bool document.isInWord(String character, int attribute);

  Returns `true`, if the given character with the given attribute can be part of a word, otherwise `false`.

    bool document.canBreakAt(String character, int attribute);

  Returns `true`, if the given character with the given attribute is suited to wrap a line, otherwise `false`.

    bool document.canComment(int startAttribute, int endAttribute);

  Returns `true`, if a range starting and ending with the given attributes is suited to be commented out, otherwise `false`.

    String document.commentMarker(int attribute);

  Returns the comment marker for single line comments for a given attribute.

    String document.commentStart(int attribute);

  Returns the comment marker for the start of multi-line comments for a given attribute.

    String document.commentEnd(int attribute);

  Returns the comment marker for the end of multi-line comments for a given attribute.

    Range document.documentRange();

  Returns a range that encompasses the whole document.

    Cursor documentEnd();

  Returns a cursor positioned at the last column of the last line in the document.

    bool isValidTextPosition(int line, int column);
    bool isValidTextPosition(Cursor cursor);

  Returns `true`, if the given cursor position is positioned at a valid text position. A text position is valid only if it locate at the start, in the middle, or the end of a valid line. Further, a text position is invalid if it is located in a Unicode surrogate.

  Since: &kde; 5.0

    int document.attribute(int line, int column);
    int document.attribute(Cursor cursor);

  Returns the attribute at the given cursor position.

    bool document.isAttribute(int line, int column, int attribute);
    bool document.isAttribute(Cursor cursor, int attribute);

  Returns `true`, if the attribute at the given cursor position equals attribute, otherwise `false`.

    String document.attributeName(int line, int column);
    String document.attributeName(Cursor cursor);

  Returns the attribute name as human readable text. This is equal to the `itemData` name in the syntax highlighting files.

    bool document.isAttributeName(int line, int column, String name);
    bool document.isAttributeName(Cursor cursor, String name);

  Returns `true`, if the attribute name at a certain cursor position matches the given name, otherwise `false`.

    String document.variable(String key);

  Returns the value of the requested document variable key. If the document variable does not exist, the return value is an empty string.

    void document.setVariable(String key, String value);

  Set the value of the requested document variable key.

  See also: Kate document variables

  Since: &kde; 4.8

    int document.firstVirtualColumn(int line);

  Returns the virtual column of the first non-whitespace character in the given line or `-1`, if the line is empty or contains only whitespace characters.

    int document.lastVirtualColumn(int line);

  Returns the virtual column of the last non-whitespace character in the given line or `-1`, if the line is empty or contains only whitespace characters.

    int document.toVirtualColumn(int line, int column);
    int document.toVirtualColumn(Cursor cursor);
    Cursor document.toVirtualCursor(Cursor cursor);

  Converts the given real cursor position to a virtual cursor position, either returning an int or a Cursor object.

    int document.fromVirtualColumn(int line, int virtualColumn);
    int document.fromVirtualColumn(Cursor virtualCursor);
    Cursor document.fromVirtualCursor(Cursor virtualCursor);

  Converts the given virtual cursor position to a real cursor position, either returning an int or a Cursor object.

    Cursor document.anchor(int line, int column, Char character);
    Cursor document.anchor(Cursor cursor, Char character);

  Searches backward for the given character starting from the given cursor. As an example, if '(' is passed as character, this function will return the position of the opening '('. This reference counting, &ie; other '(...)' are ignored.

    Cursor document.rfind(int line, int column, String text, int attribute = -1);
    Cursor document.rfind(Cursor cursor, String text, int attribute = -1);

  Find searching backwards the given text with the appropriate attribute. The argument attribute is ignored if it is set to `-1`. The returned cursor is invalid, if the text could not be found.

    int document.defStyleNum(int line, int column);
    int document.defStyleNum(Cursor cursor);

  Returns the default style used at the given cursor position.

    bool document.isCode(int line, int column);
    bool document.isCode(Cursor cursor);

  Returns `true`, if the attribute at the given cursor position is not equal to all of the following styles: `dsComment`, `dsString`, `dsRegionMarker`, `dsChar`, `dsOthers`.

    bool document.isComment(int line, int column);
    bool document.isComment(Cursor cursor);

  Returns `true`, if the attribute of the character at the cursor position is `dsComment`, otherwise `false`.

    bool document.isString(int line, int column);
    bool document.isString(Cursor cursor);

  Returns `true`, if the attribute of the character at the cursor position is `dsString`, otherwise `false`.

    bool document.isRegionMarker(int line, int column);
    bool document.isRegionMarker(Cursor cursor);

  Returns `true`, if the attribute of the character at the cursor position is `dsRegionMarker`, otherwise `false`.

    bool document.isChar(int line, int column);
    bool document.isChar(Cursor cursor);

  Returns `true`, if the attribute of the character at the cursor position is `dsChar`, otherwise `false`.

    bool document.isOthers(int line, int column);
    bool document.isOthers(Cursor cursor);

  Returns `true`, if the attribute of the character at the cursor position is `dsOthers`, otherwise `false`.

    void document.indent(Range range, int change);

  Indents all lines in range by change tabs or change times `tabSize` spaces depending on the users preferences. The change parameter can be negative.

#### The Editor API

In addition to the document and view API, there is a general editor API that provides functions for general editor scripting functionality.

    String editor.clipboardText();

  Returns the text that currently is in the global clipboard.

  Since: &kde-frameworks; 5.50

    String editor.clipboardHistory();

  The editor holds a clipboard history that contains up to 10 clipboard entries. This function returns all entries that currently are in the clipboard history.

  Since: &kde-frameworks; 5.50

    void editor.setClipboardText(String text);

  Set the contents of the clipboard to text. The text will be added to the clipboard history.

  Since: &kde-frameworks; 5.50