Kate Part
Kate Part
Thad McGinnis
Anne-Marie Mahfouf
Anders Lund
T.C. Hollingsworth
Christoph Cullmann
Lauri Watts
The KatePart Handbook
2
Contents
1 Introduction 8
2 Some Fundamentals 9
2.1 Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
6 Extending KatePart 42
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.2 Working with Syntax Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.2.2 The KatePart Syntax Highlight System . . . . . . . . . . . . . . . . . . . . . 43
6.2.2.1 How it Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.2.2.2 Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.2.2.3 Context Styles and Keywords . . . . . . . . . . . . . . . . . . . . . 44
6.2.2.4 Default Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.2.3 The Highlight Definition XML Format . . . . . . . . . . . . . . . . . . . . . . 45
6.2.3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.2.3.2 The Sections in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.2.3.3 Available Default Styles . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.2.4 Highlight Detection Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
6.2.4.1 The Rules in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.2.4.2 Tips & Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.3 Working with Color Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.3.2 The KSyntaxHighlighting Color Themes . . . . . . . . . . . . . . . . . . . . 58
6.3.3 The Color Themes JSON Format . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.3.3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.3.3.2 The JSON Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.3.3.3 Main Sections of the JSON Color Theme Files . . . . . . . . . . . . 60
6.3.3.4 Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.3.4 Colors in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.3.4.1 Editor Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.3.4.2 Default Text Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.3.4.3 Custom Highlighting Text Styles . . . . . . . . . . . . . . . . . . . 72
6.3.5 The Color Themes GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.3.5.1 Create a new theme . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.3.5.2 Import or export JSON theme files . . . . . . . . . . . . . . . . . . 74
4
The KatePart Handbook
7 Configure KatePart 92
7.1 The Editor Component Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 92
7.1.1 Appearance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
7.1.1.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
7.1.1.2 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.1.2 Color Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.1.3 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.1.3.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.1.3.2 Text Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.1.3.3 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.1.3.4 Auto Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.1.3.5 Spellcheck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.1.3.6 Vi Input Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.1.4 Open/Save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5
The KatePart Handbook
7.1.4.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
7.1.4.2 Advanced . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.1.4.3 Modes & Filetypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.2 Configuring With Document Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 102
7.2.1 How KatePart uses Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
7.2.2 Available Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
7.2.3 Extended Options in .kateconfig files . . . . . . . . . . . . . . . . . . . . . 105
B Index 122
6
Abstract
Chapter 1
Introduction
KatePart is a fully featured text editor component used by many Qt™ and KDE applications.
KatePart is more than a text editor; it is meant to be a programmer’s editor, and could be con-
sidered as at least a partial alternative to more powerful editors. One of KatePart’s main features
is the colorized syntax, customized for many different programming languages such as: C/C++,
Java™, Python, Perl, Bash, Modula 2, HTML, and Ada.
KWrite is a simple text editor application based on KatePart. It has a single document interface
(SDI) allowing you to edit one file at the time per window. Since KWrite is a very simple im-
plementation of KatePart, it does not require its own documentation. If you know how to use
KWrite, you can use KatePart anywhere!
8
The KatePart Handbook
Chapter 2
Some Fundamentals
KWrite and many other KatePart users are very simple to use. Anyone that has used a text editor
should have no problems.
2.2 Shortcuts
Many of the shortcuts are configurable by way of the Settings menu. By default KatePart honors
the following shortcuts:
9
The KatePart Handbook
10
The KatePart Handbook
Ctrl+D Comment.
Ctrl+Shift+D Uncomment.
Ctrl+G Go to line...
Ctrl+I Indent selection.
Ctrl+Shift+I Unindent selection.
Ctrl+J Join Lines.
Ctrl+P Print.
Ctrl+R Replace.
Ctrl+S Invokes the Save command.
Ctrl+Shift+S Save As.
Ctrl+U Uppercase.
Ctrl+Shift+U Lowercase.
Ctrl+Alt+U Capitalize.
Ctrl+V / Shift+Ins Paste the clipboard text into line edit.
Delete the marked text and copy it to the
Ctrl+X / Shift+Ins clipboard.
Ctrl+Z Undo.
Ctrl+Shift+Z Redo.
Ctrl+- Shrink Font.
Ctrl++Ctrl+= Enlarge Font.
Ctrl+Shift+- Fold Toplevel Nodes.
Ctrl+Shift++ Unfold Toplevel Nodes.
Ctrl+Space Invoke Code Completion.
F5 Reload.
F6 Show/Hide Icon Border.
F7 Switch to Command Line.
F9 Show/Hide Folding Markers.
F10 Dynamic Word Wrap.
F11 Show/Hide Line Numbers.
Ctrl+T Transpose Characters.
Ctrl+Shift+O Automatic Spell Checking.
Ctrl+Shift+V Switch to Next Input Mode.
Ctrl+8 Reuse Word Above.
Ctrl+9 Reuse Word Below.
Ctrl+Alt+# Expand Abbreviation.
Ctrl+Alt+Up Add a cursor above current cursor.
Ctrl+Alt+Down Add a cursor below current cursor.
Create a cursor at the end of each line in
Shift+Alt+I
selection.
Find next occurrence of the word under
Alt+J
cursor and select it.
Find all occurrences of the word under
Ctrl+Alt+Shift+J
cursor and select them.
11
The KatePart Handbook
Chapter 3
Anders Lund
Dominik Haumann
3.1 Overview
The KatePart editor is the editing area of the KatePart window. This editor is shared between
Kate and KWrite, and it can also be used in Konqueror for displaying text files from your local
computer, or from the network.
The editor is composed of the following components:
12
The KatePart Handbook
• For the Up and Down keys it means scroll rather than move the cursor.
• For the Left and Right keys it means skip words rather than characters.
• For the PgUp and PgDn keys it means move to the visible edge of the view rather than browse.
• For the Home and End keys it means move to the beginning or end of the document rather
than the beginning or end of the line.
KatePart also provides you with a way to quickly jump to a matching brace or parenthesis: place
the cursor on the inside of a parenthesis or brace character, and press Ctrl+6 to jump to the
matching parenthesis or brace.
In addition you can use bookmarks to quickly jump to positions that you define on your own.
• If nothing is already selected, from the text cursor position to the mouse cursor position.
• If there is a selection, from and including that selection to the mouse cursor position.
13
The KatePart Handbook
N OTE
When selecting text by dragging the mouse, the selected text is copied to the clipboard, and can be
pasted by clicking the middle mouse button in the editor, or in any other application to which you want
to paste the text.
To select using the keyboard, hold down the Shift key while using the navigation keys (Arrow
keys, PgUp, PgDn, Home and End, possibly in combination with Ctrl to extend the move of the
text cursor).
See also the section Navigating in the Text in this chapter.
To Copy the current selection, use the Edit → Copy menu item or the keyboard shortcut (defaults
to Ctrl+C).
To Deselect the current selection, use the Edit → Deselect menu item, or the keyboard shortcut
(default is Ctrl+Shift+A), or click with the left mouse button in the editor.
WARNING
If Persistent Selection and Overwrite Selection are both enabled, typing or pasting text when the text
cursor is inside the selection will cause it to be replaced and deselected.
14
The KatePart Handbook
T IP
If you are using the KDE desktop, you can retrieve earlier copied text from any application using the
Klipper icon in the system tray.
KatePart has an incremental search bar and a power search and replace bar, which offers the
means of entering a replacement string along with a few extra options.
The bars offer the following common options:
Find
This is where to enter the search string. The interpretation of the string depends on some
of the options described below.
Match case
If enabled, the search will be limited to entries that match the case (upper or lower) of each
of the characters in the search pattern.
The power search and replace bar offers some additional options:
Plain Text
Literally match any occurrence of the search string.
Whole Words
If selected, the search will only match if there is a word boundary at both ends of the string
matching, meaning not an alphanumeric character - either some other visible character or
a line end.
Escape Sequences
If selected, the Add menuitem at the bottom of the context menu of the text boxes will be
enabled and allows you to add escape sequences to the search pattern from a predefined
list.
Regular Expression
If selected, the search string is interpreted as a regular expression. The Add menuitem at
the bottom of the context menu of the text boxes will be enabled and allows you to add
regular expression items to the search pattern from a predefined list.
See Regular Expressions for more on these.
Search in the selection only
If checked, the search and replace will be performed within the selected text only.
Find all
Clicking this button highlights all matches in the document and shows the number of found
matches in a small popup.
15
The KatePart Handbook
To find text, launch the incremental search bar with Ctrl+F or from the Edit → Find... menu item.
This opens the incremental search bar at the bottom of the editor window. On the left side of the
bar is a button with an icon to close the bar, followed by a small text box for entering the search
pattern.
When you start entering the characters of your search pattern, the search starts immediately. If
there is a match in the text this is highlighted and the background color of the entry field changes
to light green. If the search pattern does not match any string in the text, this is indicated by a
light red background color of the entry field.
Use the or button to jump to the next or previous match in the document.
Matches in the document are highlighted even when you close the search bar. To clear this high-
lighting, press the Esc key.
You can choose whether the search should be case sensitive. Selecting will limit finds to
entries that match the case (upper or lower) of each of the characters in the search pattern.
Click on the button at the right side of the incremental search bar to switch to the power
search and replace bar.
To repeat the last find operation, if any, without calling the incremental search bar, use Edit
→ Find Next (F3) or Edit → Find Previous (Shift+F3).
To replace text, launch the power search and replace bar using the Edit → Replace command, or
the Ctrl+R shortcut.
On the upper left side of the bar is a button with an icon to close the bar, followed by a small
combo box for entering the search pattern. The box remembers recently used patterns.
You can control the search mode by selecting the options Plain Text, Whole Words, Escape Se-
quences or Regular Expression from the drop down box.
If Escape sequences or Regular expression are selected, the Add... menuitem at the bottom of
the context menu of the text boxes will be enabled and allows you to add escape sequences or
regular expression items to the search or replace pattern from predefined lists.
Use the or button to jump to the next or previous match in the document.
Enter the text to replace with in the text box labeled Replace and click the Replace button to
replace only the highlighted text or the Replace All button to replace the search text in the whole
document.
You can modify the search and replace behavior by selecting different options at the bottom of
the bar. Selecting will limit finds to entries that match the case (upper or lower) of each of
the characters in the search pattern. will search and replace within the current selection
only. The Find All button highlights all matches in the document and shows the number of found
matches in a small popup.
Click on the button at the right side of the power search and replace bar to switch to the
incremental search bar.
16
The KatePart Handbook
T IP
If you are using a regular expression to find the text to replace, you can employ backreferences to
reuse text captured in parenthesized subpatterns of the expression.
See Regular Expressions for more on those.
T IP
You can do find, replace and ifind (incremental search) from the command line.
• Move the insertion cursor to the line and activate the Bookmarks → Set Bookmark (Ctrl+B)
command.
• Click in the Icon Border next to the line.
Bookmarks are available in the Bookmarks menu. The individual bookmarks are available as
menu items, labeled with the line number of the line with the bookmark, and the first few char-
acters of the text in the line. To move the insertion cursor to the beginning of a bookmarked line,
open the menu and select the bookmark.
To quickly move between bookmarks or to the next/previous bookmark, use the Bookmarks
→ Next (Alt+PgDn) or Bookmarks → Previous (Alt+PgUp) commands.
• While typing, the editor will automatically insert a hard line break after the last whitespace
character at a position before the maximum line width is reached.
• While loading a document, the editor will wrap the text in a similar way, so that no lines are
longer than the maximum line width, if they contain any whitespace allowing that.
N OTE
There is currently no way to set word wrap for document types, or even to enable or disable the feature
on a per document level. This will be fixed in a future version of KatePart.
17
The KatePart Handbook
T IP
You can set all sorts of configuration variables, including those related to indentation using Document
Variables and File types.
None
Selecting this mode turns automatic indenting off entirely.
Normal
This indenter simply keeps the indentation similar to the previous line with any content
other than whitespace. You can combine this with using the indent and unindent actions
for indenting to your own taste.
C Style
An indenter for C and similar languages, such as C++, C#, Java™, JavaScript and so on.
This indenter will not work with scripting languages such as Perl or PHP.
Haskell
An indenter for the functional programming language Haskell.
Lilypond
An indenter for the Lilypond notation language for music.
Lisp
An indenter specifically for the Lisp scripting language and Lisp dialects.
Python
An indenter specifically for the python scripting language.
XML Style
An indenter specifically for XML like languages.
18
The KatePart Handbook
19
The KatePart Handbook
• To create them via mouse, use Alt + left mouse button. The modifier is configurable, see
Configure multicursor modifier
• To create via keyboard, press Ctrl+Alt+Up to create cursor above primary cursor and
Ctrl+Alt+Down to create cursor below. These shortcuts are also configurable
• To create cursors out of a selection, first select some text and then press Shift+Alt+I. This will
create a cursor at the end of each line in selection.
• Use Alt+J to find the next occurrence of the word under cursor and select it + create a cursor.
If you want to skip the current word under cursor, press Alt+K and it will mark the currently
selected word as skipped. When you press Alt+J again it will unselect the current word and
move to the next word.
• Use Ctrl+Alt+Shift+J to find all occurrences of the word under cursor and select them with a
cursor at the end of each selection. You can use Alt+J to cycle through the selected words and
use Alt+K to unselect any word as mentioned in the previous paragraph.
Once you have created a few cursors, you can perform most of the editing operations on them as
you would on a single cursor. For example, typing a letter will type it for each cursor. Similarly
you can perform text transforms e.g., capitalization for all the positions or selections.
Sometimes you will want to remove cursors. To do so, you can Alt + left mouse button on the
cursor that you want to remove. If you just want to remove cursors on lines that are empty, there
is a ready-made action for it that will do it for you. To invoke the action, open the Command
Bar using Ctrl+Alt+I and look for Remove cursors from empty lines and hit Enter. You can also
configure a shortcut for this action.
20
The KatePart Handbook
Chapter 4
21
The KatePart Handbook
N OTE
This may undo several editing commands of the same type, like typing in characters.
N OTE
If Overwrite Selection is enabled, the pasted text will overwrite the selection, if any.
22
The KatePart Handbook
Use the or button to jump to the next or previous match in the document.
Matches in the document are highlighted even when you close the search bar. To clear this
highlighting, press the Esc key.
You can choose whether the search should be case sensitive. Selecting will limit
finds to entries that match the case (upper or lower) of each of the characters in the search
pattern.
Click on the button at the right side of the incremental search bar to switch to the
power search and replace bar.
Edit → Find Variants → Find Next (F3)
This repeats the last find operation, if any, without calling the incremental search bar, and
searching forwards through the document starting from the cursor position.
Edit → Find Variants → Find Previous (Shift+F3)
This repeats the last find operation, if any, without calling the incremental search bar, and
searching backwards instead of forwards through the document.
Edit → Find Variants → Find Selected (Ctrl+H)
Finds next occurrence of selected text.
Edit → Find Variants → Find Selected Backwards (Ctrl+Shift+H)
Finds previous occurrence of selected text.
23
The KatePart Handbook
Use the or button to jump to the next or previous match in the document.
Enter the text to replace with in the text box labeled Replace and click the Replace button
to replace only the highlighted text or the Replace All button to replace the search text in
the whole document.
You can modify the search and replace behavior by selecting different options at the bottom
of the bar. Selecting will limit finds to entries that match the case (upper or lower)
of each of the characters in the search pattern. will search and replace within
the current selection only. The Find All button highlights all matches in the document and
shows the number of found matches in a small popup.
Click on the button at the right side of the power search and replace bar to switch
to the incremental search bar.
24
The KatePart Handbook
25
The KatePart Handbook
26
The KatePart Handbook
Go → Go to Line... (Ctrl+G)
This opens the goto line bar at the bottom of the window which is used to have the cursor
jump to a particular line (specified by number) in the document. The line number may be
entered directly into the text box or graphically by clicking on the up or down arrow spin
controls at the side of the text box. The little up arrow will increase the line number and the
down arrow decrease it. Close the bar with a click on the button with an icon on the left
side of the bar.
Go → Go to Previous Editing Line (Ctrl+E)
This action jumps the previous editing line in the multicursor configuration.
Go → Go to Next Editing Line (Ctrl+Shift+E)
This action jumps the next editing line in the multicursor configuration.
Go → Go to Previous Modified Line
Lines that were changed since opening the file are called modified lines. This action jumps
the previous modified line.
Go → Go to Next Modified Line
Lines that were changed since opening the file are called modified lines. This action jumps
the next modified line.
Go → Go to Matching Bracket (Ctrl+6)
Move the cursor to the associated opening or closing bracket.
Go → Select to Matching Bracket (Ctrl+Shift+6)
Selects the text between associated opening and closing brackets.
Go → Bookmarks (Ctrl+Shift+6)
Below the entries described here, one entry for each bookmark in the active document will
be available. The text will be the first few words of the marked line. Choose an item to
move the cursor to the start of that line. The editor will scroll as necessary to make that line
visible.
Go → Bookmarks → Set Bookmark (Ctrl+B)
Sets or removes a bookmark in the current line of the active document. (If it’s there, it is
removed, otherwise one is set.)
Go → Bookmarks → Clear All Bookmarks
This command will remove all the markers from the document as well as the list of markers
which is appended at the bottom of this menu item.
Go → Bookmarks → Previous (Alt+PgUp)
This will move the cursor to beginning of the first above line with a bookmark. The me-
nuitem text will include the line number and the first piece of text on the line. This item is
only available when there is a bookmark in a line above the cursor.
Go → Bookmarks → Next (Alt+PgDn)
This will move the cursor to beginning of the next line with a bookmark. The menuitem
text will include the line number and the first piece of text on the line. This item is only
available when there is a bookmark in a line below the cursor.
27
The KatePart Handbook
Tools → Mode
Choose the filetype scheme you prefer for the active document. This overwrites the global
filetype mode set in Settings → Configure Editor... in the Filetypes tab for your current
document only.
Tools → Highlighting
Choose the Highlighting scheme you prefer for the active document. This overwrites the
global highlighting mode set in Settings → Configure Editor... for your current document
only.
Tools → Indentation
Choose the style of indentation you want for your active document. This overwrites the
global indentation mode set in Settings → Configure Editor... for your current document
only.
Tools → Encoding
You can overwrite the default encoding set in Settings → Configure Editor... in the
Open/Save page to set a different encoding for your current document. The encoding you
set here will be only valid for your current document.
Tools → Add Byte Mark Order (BOM)
Checking this action you can explicitly add a byte order mark for unicode encoded docu-
ments. The byte order mark (BOM) is a Unicode character used to signal the endianness
(byte order) of a text file or stream, for more information see Byte Order Mark.
Tools → End of Line
Choose your preferred end of line mode for your active document. This overwrites the
global end of line mode set in Settings → Configure Editor... for your current document
only.
Tools → Scripts
This submenu contains a list of all scripted actions. The list can easily be modified by
writing your own scripts. This way, KatePart can be extended with user-defined tools.
28
The KatePart Handbook
29
The KatePart Handbook
Unknown word:
Here, the spellchecker indicates the word currently under consideration. This hap-
pens when the checker encounters a word not in its dictionary - a file containing a list
of correctly spelled words against which it compares each word in the editor.
Replace with:
If the checker has any similar words in its dictionary the first one will be listed here.
The user can accept the suggestion, type in his or her own correction, or choose a
different suggestion from the next box.
Language:
If you have installed multiple dictionaries, here you can select which dictionary/lan-
guage should be used.
On the right side of the dialog box are 6 buttons that allow the user to control the spellcheck
process. They are:
Add to Dictionary
Pressing this button adds the Unknown word to the checker’s dictionary. This means
that in the future the checker will always consider this word to be correctly spelled.
Suggest
The checker may list here a number of possible replacements for the word under
consideration. Clicking on any one of the suggestions will cause that word to be
entered in the Replace with box, above.
Replace
This button has the checker replace the word under consideration in the document
with the word in the Replace with box.
Replace All
This button causes the checker to replace not only the current Unknown word: but to
automatically make the same substitution for any other occurrences of this Unknown
word in the document.
Ignore
Activating this button will have the checker move on without making any changes.
Ignore All
This button tells the checker to do nothing with the current Unknown word: and to
pass over any other instances of the same word.
30
The KatePart Handbook
N OTE
This only applies to the current spellcheck run. If the checker is run again later it will stop
on this same word.
Three more buttons are located horizontally along the bottom of the spellcheck dialog. They
are:
Help
This invokes the KDE help system with the help page for this dialog.
Finished
This button ends the spellcheck process, and returns to the document.
Cancel
This button cancels the spellcheck process, all modifications are reverted, and you
will return to your document.
Tools → Spelling → Spelling (from cursor)...
This initiates the spellchecking program but it starts where your cursor is instead of at the
beginning of the document.
Tools → Spelling → Spellcheck Selection...
Spellchecks the current selection.
Tools → Spelling → Change Dictionary
Displays a drop down box with all available dictionaries for spellchecking at the bottom
of the editor window. This allows easy switching of the spellcheck dictionary e.g. for
automatic spellcheck of text in different languages.
KatePart has the common KDE Settings and Help menu items, for more information read the
sections about the Settings Menu and Help Menu of the KDE Fundamentals.
31
The KatePart Handbook
Chapter 5
Anders Lund
Dominik Haumann
5.1 Comment/Uncomment
The Comment and Uncomment commands, available from the Tools menu allow you to add or
remove comment markers to the selection, or the current line if no text is selected, if comments
are supported by the format of the text you are editing.
The rules for how commenting is done are defined in the syntax definitions, so if syntax high-
lighting is not used, commenting/uncommenting is not possible.
Some formats define single line comment markers, some multiline markers and some both. If
multiline markers are not available, commenting out a selection that does not fully include its
last line is not possible.
If a single line marker is available, commenting single lines is preferred where applicable, as this
helps to avoid problems with nested comments.
When removing comment markers, no uncommented text should be selected. When removing
multiline comment markers from a selection, any whitespace outside the comment markers is
ignored.
To place comment markers, use the Tools → Comment menu item or the related keyboard short-
cut sequence, the default is Ctrl+D.
To remove comment markers, use the Tools → Uncomment menu item or the related keyboard
shortcut, the default is Ctrl+Shift+D.
32
The KatePart Handbook
by pressing F7 it will automatically hide after a few seconds. To clear the message and enter a
new command, press F7 again.
The command line has a built-in help system; issue the command help to get started. To see a list
of all available commands issue help list; to view help for a specific command, do help command .
The command line has a built in history, so you can reuse commands already typed. To navigate
the history, use the Up and Down keys. When showing historical commands, the argument part
of the command will be selected, allowing you to easily overwrite the arguments.
BOOLEAN
This is used with commands that turns things on or off. Legal values are on, off, true,
false, 1 or 0.
INTEGER
An integer number.
STRING
A string, surrounded by single quotes (’) or double quotes (˝) when it contains spaces.
These commands are provided by the editor component, and allow you to configure the active
document and view only. This is handy if you want to use a setting different from the default
settings, for example for indentation.
33
The KatePart Handbook
indent
Indents the selected lines or the current line.
34
The KatePart Handbook
unindent
Unindents the selected lines or current line.
cleanindent
Cleans up the indentation of the selected lines or current line according to the indentation
settings in the document.
comment
Inserts comment markers to make the selection or selected lines or current line a comment
according to the text format as defined by the syntax highlight definition for the document.
uncomment
Removes comment markers from the selection or selected lines or current line according to
the text format as defined by the syntax highlight definition for the document.
kill-line
Deletes the current line.
replace STRING pattern STRING replacement
Replaces text matching pattern with replacement. If you want to include whitespace in
the pattern, you must quote both the pattern and replacement with single or double
quotes. If the arguments are unquoted, the first word is used as pattern and the rest for
replacement. If replacement is empty, each occurrence of pattern is removed.
You can set flags to configure the search by adding a colon, followed by one or more letters
each representing a configuration, giving the form replace:options pattern repla
cement. Available options are:
b
Search backwards.
c
Search from cursor position.
e
Search in the selection only.
r
Do regular expression search. If set, you may use \N where N is a number to represent
captures in the replacement string.
s
Do case sensitive search.
p
Prompt for permission to replace the next occurrence.
w
Match whole words only.
date STRING format
Inserts a date/time string as defined by the specified format, or the format ‘yyyy-MM-
dd hh:mm:ss’ if none is specified. The following translations are done when interpreting
format:
35
The KatePart Handbook
s///[ig] %s///[ig]
This command does a sed-like search/replace operation on the current line, or on the whole
file (%s///).
In short, the text is searched for text matching the search pattern, the regular expression
between the first and the second slash, and when a match is found, the matching part of
the text is replaced with the expression between the second and last slash. Parentheses
in the search pattern create back references, that is the command remembers which part of
the string matched in the parentheses; these strings can be reused in the replace pattern,
referred to as \1 for the first set of parentheses, \2 for the second and so on.
To search for a literal ( or ), you need to escape it using a backslash character: \(\)
If you put an i at the end of the expression, the matching will be case insensitive. If you
put a g at the end, all occurrences of the pattern will be replaced, otherwise only the first
occurrence is replaced.
36
The KatePart Handbook
Now you realize that this is not nice code, and decide that you want to use the const keyword for
all ‘address of’ arguments, those characterized by the & operator in front of the argument name.
You would also like to simplify the white space, so that there is only 1 whitespace character
between each word.
Launch the Editing Command Dialog, and enter: s/\s+(\w+)\s+(&)/ const \1 \2/g and
hit the OK button. The g at the end of the expression makes the regular expression recompile for
each match to save the backreferences.
Output: void MyClass::DoStringOps( const String &foo, const String &bar, String *p,
const int &a, const int &b )
Mission completed! Now, what happened? Well, we looked for some white space (\s+) followed
by one or more alphabetic characters (\w+) followed by some more whitespace (\s+) followed by
an ampersand, and in the process saved the alphabetic chunk and the ampersand for reuse in the
replace operation. Then we replaced the matching part of our line with one whitespace followed
by ‘const’ followed by one whitespace followed by our saved alphabetical chunk (\1) followed
by one whitespace followed by our saved ampersand (\2)
Now in some cases the alphabetical chunk was ‘String’, in some ‘int’, so using the character class
\w and the + quantifier proved a valuable asset.
sort
Sorts the selected text or entire document.
natsort
Sort the selected lines or entire document naturally.
moveLinesDown
Move selected lines down.
37
The KatePart Handbook
moveLinesUp
Move selected lines up.
uniq
Remove duplicated lines from the selected text or the whole document.
rtrim
Remove trailing space from the selected text or the whole document.
ltrim
Remove leading space from the selected text or the whole document.
join [STRING separator]
Join selected lines or whole document. Optionally takes a parameter defining a separator,
for example: join ’, ’
rmblank
Remove all blank spaces from the selected text or the whole document.
alignon
This command aligns lines in the selected block or whole document on the column given
by a regular expression given as an argument.
If you give 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:
alignon - will insert spaces before the first ’-’ of each lines to align them all on the same
column.
alignon :\\s+(.) will insert spaces before the first non-blank character that occurs
after a colon to align them all on the same column.
unwrap
Unwrap the selected text or the whole document.
each STRING script
Given a JavaScript function as an argument, call that for the list of selected lines and replace
them with the return value of that callback.
38
The KatePart Handbook
duplicateLinesUp
Duplicate the selected lines above the current selection.
duplicateLinesDown
Duplicate the selected lines below the current selection.
b
Search backwards.
c
Search from cursor position.
e
Search in the selection only.
r
Do regular expression search. If set, you may use \N where N is a number to represent
captures in the replacement string.
s
Do case sensitive search.
w
Match whole words only.
ifind STRING pattern
This command provides ‘as-you-type’ searching. You can configure the behavior of the
search by appending a colon followed by one or more options, like this: ifind:options
pattern. Allowed options are:
b
Search backwards.
r
Do regular expression search.
s
Do case sensitive search.
c
Search from cursor position.
39
The KatePart Handbook
5.2.1.4 Commands for Basic Editor Functions (These depend on the application the editor
component is used in)
w
Save the current document.
wa
Save all currently open documents.
q
Close the current document.
qa
Close all open documents.
wq
Save and close the current document.
wqa
Save and close all currently open documents.
x
Save and close the current document only if it has changed.
x
Save and close all currently open documents only if they have changed.
bp
Go to the previous document in the documents list.
bn
Go to the next document in the documents list.
new
Open a new document in horizontal split view.
vnew
Open a new document in vertical split view.
e
Reload the current document if it has changed on disk.
enew
Edit a new document.
print
Open the Print dialog to print the current document.
40
The KatePart Handbook
possible operation on a given region: a top down triangle means that the region is expanded,
clicking it will collapse the region and a right pointing triangle will be displayed instead.
Three commands are provided to manipulate the state of folding regions, see the menu docu-
mentation.
The folded lines are remembered when a file is closed, so when you reopen the file the folded
nodes will still be folded. This applies to reload operations as well.
If you do not want to use the code folding feature, you can disable the Show folding markers (if
available) option in the Appearance page of the editor configuration.
41
The KatePart Handbook
Chapter 6
Extending KatePart
T.C. Hollingsworth
6.1 Introduction
Like any advanced text editor component, KatePart offers a variety of ways to extend its func-
tionality. 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!
6.2.1 Overview
Syntax Highlighting is what makes the editor automatically display text in different styles/col-
ors, 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 com-
ments 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.
42
The KatePart Handbook
N OTE
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%.
This section will discuss the KatePart 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.
Whenever you open a file, one of the first things the KatePart 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 KatePart Syntax Highlighting System are XML files, contain-
ing
• Rules for detecting the role of text, organized into context blocks
• Keyword lists
• Style Item definitions
43
The KatePart Handbook
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.
6.2.2.2 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.
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 pro-
gramming languages, control statements are one concept, data type names another, and built in
functions of the language a third. The KatePart Syntax Highlighting System can use such lists to
detect and mark words in the text to emphasize concepts of the text formats.
If you open a C++ source file, a Java™ source file and an HTML document in KatePart, 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 KatePart 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 ren-
dered using the same style in all languages, you do not have to stop and think to identify them
within the text.
T IP
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.
44
The KatePart Handbook
6.2.3.1 Overview
KatePart uses the Syntax-Highlighting framework from KDE Frameworks. The default high-
lighting XML files shipped with KatePart 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 $HO
ME /.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-highligh
ting\syntax. %USERPROFILE% usually expands to C:\Users\user.
In summary, for most configurations the directory of custom XML files is as follows:
$HOME /.local/share/org.kde.syntax-hig
For local user
hlighting/syntax/
/usr/share/org.kde.syntax-highlightin
For all users
g/syntax/
$HOME /.var/app/ flatpak-package-name
For Flatpak packages /data/org.kde.syntax-highlighting/synt
ax/
$HOME /snap/ snap-package-name
For Snap packages /current/.local/share/org.kde.syntax-h
ighlighting/syntax/
%USERPROFILE%\AppData\Local\org.kde.sy
On Windows®
ntax-highlighting\syntax
If multiple files exist for the same language, the file with the highest version attribute in the
language element will be loaded.
M AIN SECTIONS OF K ATE PART H IGHLIGHT D EFINITION FILES
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.
45
The KatePart Handbook
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 " ←-
/>
46
The KatePart Handbook
< 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 informa-
tion 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 at-
tributes will be explained later.
The other sections, folding, emptyLines and spellchecking, are usually not neces-
sary 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 >
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:
47
The KatePart Handbook
name states the context name. Rules will use this name to specify the context to switch to if
the rule matches.
lineEndContext defines the context the highlight system switches to if it reaches the end
of a line. This may either be a name of another context, #stay to not switch the context
(e.g.. do nothing) or #pop which will cause 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 Inclu
deRules rules, e.g., SomeContext##JavaScript. Note that it is not possible to use this
context switch in combination with #pop, for example, #pop!SomeContext##JavaScri
pt is not valid. Context switches are also described in Section 6.2.4.
lineEmptyContext defines the context if an empty line is encountered. The nomencla-
ture 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 fallt
hroughContext if no rule matches. Note that since KDE Frameworks 5.62 this attribute
is deprecated in favor of fallthroughContext, since if the fallthroughContext at-
tribute 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 at-
tributes 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’).
48
The KatePart Handbook
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 re-
gion=˝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 posi-
tion=˝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 at-
tributes 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 Ä.
Default Styles were already explained, as a short summary: Default styles are predefined font
and color styles.
49
The KatePart Handbook
50
The KatePart Handbook
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:
Rule specific attributes varies and are described in the following sections.
C OMMON ATTRIBUTES
D YNAMIC RULES
T IP
• If the captures will not be used, both by dynamic rules and in the same regular expression, non-c
apturing groups should be used: (?:PATTERN)
The lookahead or lookbehind groups such as (?=PATTERN), (?!PATTERN) or (?<=PATTER
N) 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 respec-
tively. For more information, see Capturing matching text (back references) in Regular Expressions.
51
The KatePart Handbook
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 [=====[ commen
t ]=====].
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 "+. This
matches text as: #label˝˝˝˝inside the context˝˝˝˝#.
These captures will not be available in other contexts, such as OtherContext, FindEscapes or Some-
Context.
< 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 >
L OCAL DELIMINATORS
52
The KatePart Handbook
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 first character to match, char1 the second.
AnyChar
Detect one character of a set of specified characters.
< AnyChar String ="( string )" ( common attributes ) />
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 ) />
53
The KatePart Handbook
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 de-
fined 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 ) />
54
The KatePart Handbook
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 high-
lighting 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 "] />
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.
55
The KatePart Handbook
DetectSpaces
Detect whitespaces.
< DetectSpaces ( common attributes ) />
• If you only match two characters use Detect2Chars instead of StringDetect. The same
applies to DetectChar.
• 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 *#" />
If you want to match the regular expression ’ˆ#’ you can still use DetectChar with the at-
tribute column=˝0˝. The attribute column counts characters, so a tabulator is only one char-
acter.
• 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 con-
text 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.
< Detect2Chars attribute =" Comment " context ="# pop " char ="*" char1 ="/" ←-
lookAhead =" true " />
56
The KatePart Handbook
• 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 language.xsd mySyn-
tax.xml. The files validatehl.sh and language.xsd are 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 SYSTEM " language . dtd "
[
<! ENTITY myref "[A -Za - z_ :][\ w .:_ -]*" >
]>
6.3.1 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.
57
The KatePart Handbook
‘Breeze Light’ and ‘Breeze Dark’ color themes with the ‘C++’ syntax highlighting.
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:
58
The KatePart Handbook
6.3.3.1 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 d
ata/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 Gene
ricDataLocation 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 $HO
ME /.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-highligh
ting\themes. %USERPROFILE% usually expands to C:\Users\user-name.
In summary, for most configurations the custom themes directory is as follows:
59
The KatePart Handbook
$HOME /.local/share/org.kde.syntax-hig
For local user
hlighting/themes/
/usr/share/org.kde.syntax-highlightin
For all users
g/themes/
$HOME /.var/app/ flatpak-package-name
For Flatpak packages /data/org.kde.syntax-highlighting/them
es/
$HOME /snap/ snap-package-name
For Snap packages /current/.local/share/org.kde.syntax-h
ighlighting/themes/
%USERPROFILE%\AppData\Local\org.kde.sy
On Windows®
ntax-highlighting\themes
If multiple theme files exist with the same name, the file with the highest revision will be
loaded.
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.
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 Section 6.3.3.4.
• 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 Section 6.3.4.1.
• 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 Section 6.3.4.2.
• custom-styles: It is optional. Defines text styles for the attributes of specific syntax high-
lighting 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 Section 6.3.4.3.
60
The KatePart Handbook
The JSON language does not support comments. However, you can use the optional key _comme
nts 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 reposi-
tory.
{
" _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 " : "#94 caef ",
" BracketMatching " : "# ffff00 ",
" CurrentLine " : "# f8f7f6 ",
" IconBorder " : "# f0f0f0 ",
" IndentationLine " : "# d2d2d2 ",
" LineNumbers " : "# a0a0a0 ",
" CurrentLineNumber " : "#1 e1e1e ",
The other editor color keys ...
},
" text - styles ": {
" Normal " : {
" text - color " : "#1 f1c1b ",
" selected - text - color " : "# ffffff ",
" bold " : false ,
" italic " : false ,
" underline " : false ,
" strike - through " : false
},
" Keyword " : {
" text - color " : "#1 f1c1b ",
" selected - text - color " : "# ffffff ",
" bold " : true
},
" Function " : {
" text - color " : "#644 a9b ",
" selected - text - color " : "#452886"
},
" Variable " : {
" text - color " : "#0057 ae ",
" selected - text - color " : "#00316 e"
},
The other text style keys ...
61
The KatePart Handbook
},
" custom - styles ": {
" ISO C ++": {
" Data Type ": {
" bold ": true ,
" selected - text - color ": "#009183" ,
" text - color ": "#00 b5cf "
},
" Keyword ": {
" text - color ": "#6431 b3 "
}
},
" YAML ": {
" Attribute ": {
" selected - text - color ": "#00 b5cf ",
" text - color ": "#00 b5cf "
}
}
}
}
6.3.3.4 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 after-
wards. It is mandatory.
• revision: It is an integer number that specifies the current revision of the theme file. When-
ever 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-Licen
se-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.
62
The KatePart Handbook
63
The KatePart Handbook
Icon Border
64
The KatePart Handbook
65
The KatePart Handbook
Text Decorations
66
The KatePart Handbook
67
The KatePart Handbook
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 Section 3.6.
68
The KatePart Handbook
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.
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.
N OTE
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 Section 6.2.3.3 in the syntax highlighting documentation.
69
The KatePart Handbook
T IP
Make sure to choose readable colors with good contrast especially in combination with the Editor
Colors. See Section 6.3.6.1.
In the JSON file, the respective key text-styles has as value an object where each key cor-
responds 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 " : "#1 f1c1b ",
" selected - text - color " : "# ffffff ",
" bold " : false ,
" italic " : false ,
" underline " : false ,
" strike - through " : false
},
" Keyword " : {
" text - color " : "#1 f1c1b ",
" selected - text - color " : "# ffffff ",
" bold " : true
},
" Function " : {
" text - color " : "#644 a9b ",
" 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 TextSelec-
tion 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 se-
lected. The value is a string with a hexadecimal color code. This key is optional, by default
there is no background.
70
The KatePart Handbook
In the GUI to manage color themes of KTextEditor, these attributes can be modified in the Defa
ult 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.
71
The KatePart Handbook
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 definit
ion. 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 ": "#6431 b3 "
},
" Type Modifiers ": {
" bold ": true ,
" selected - text - color ": "#009183" ,
" text - color ": "#00 b5cf "
}
}
}
N OTE
• 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.
72
The KatePart Handbook
In the GUI to manage themes of KTextEditor, these attributes can be modified in the Highl
ighting 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.
T IP
You can adjust the KDE global color scheme in the Colors module in System Settings. You can also
change it in some applications individually such as Kate or KDevelop, from the menu Settings → Color
Scheme.
73
The KatePart Handbook
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.
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.
T IP
• 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.
6.3.5.3.1 Colors
Here the colors of the text editing area are adjusted. These are detailed in Section 6.3.4.1.
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 Section 6.3.4.2.
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.
74
The KatePart Handbook
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.
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 back-
ground 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).
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:
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.
75
The KatePart Handbook
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
• 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 KatePart version.
• required-syntax-style [optional]: The required syntax style, which matches the specified st
yle 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,
e.g.: [˝c++˝, ˝java˝].
• priority [optional]: If several indenters are suited for a certain highlighted file, the priority
decides which indenter is chosen as default indenter.
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 , e.g. 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
76
The KatePart Handbook
return -2;
}
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:
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 KatePart 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\inde
ntation. %USERPROFILE% usually expands to C:\\Users\\user.
Developing an indenter requires reloading the scripts to see whether the changes behave appro-
priately. 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 KatePart Project by contacting
the mailing list.
77
The KatePart Handbook
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
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 KatePart’s built-in command
line.
78
The KatePart Handbook
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 , e.g. range . js if you use Range
require (" range . js ") ;
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 cm
d set to the given command, i.e. cmd == ˝sort˝ . KatePart 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 be-
have appropriately. Instead of restarting the application, simply switch to the command line and
invoke the command reload-scripts.
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 "
}
]
};
79
The KatePart Handbook
• 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 KatePart Project by contacting
the mailing list.
The scripting API presented here is available to all scripts, i.e. 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 , e.g. 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 ") ;
As KatePart is a text editor, all the scripting API is based on cursors and ranges whenever pos-
sible. 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.
Cursor();
Constructor. Returns a Cursor at position (0, 0).
Example: var cursor = new Cursor();
80
The KatePart Handbook
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();
81
The KatePart Handbook
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, i.e. 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))’.
82
The KatePart Handbook
6.4.3.2.2 Debugging
6.4.3.2.3 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 KatePart
sources are automatically extracted and translatable. In other words, as a KatePart 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, i.e., 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.
83
The KatePart Handbook
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.
84
The KatePart Handbook
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.st
atus 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
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.
85
The KatePart Handbook
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, i-
nt 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(C-
ursor 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(C-
ursor 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(Curs-
or 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.
86
The KatePart Handbook
87
The KatePart Handbook
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, i.e., this
call can be nested.
void document.editEnd();
Ends an edit group. The last call of editEnd() (i.e. 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 back-
wards.
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 com-
mented 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.
88
The KatePart Handbook
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(C-
ursor 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 fal
se.
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.
89
The KatePart Handbook
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
90
The KatePart Handbook
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
91
The KatePart Handbook
Chapter 7
Configure KatePart
Selecting Settings → Configure Application... from the menu brings up the Configure dialog
box. This dialog can be used to alter a number of different settings. The settings available for
change vary according to which category the user chooses from a vertical list on the left side of
the dialog. By means of three buttons along the bottom of the box the user can control the process.
You may invoke the Help system, accept the current settings and close the dialog by means of
the OK button, or Cancel the process. The categories Appearance, Fonts & Colors, Editing,
Open/Save and Extensions are detailed below.
7.1.1 Appearance
7.1.1.1 General
Editor font
Here you can choose the font of the editor text. You can choose from any font available on
your system, and set a default size. A sample text is displayed at the bottom of the dialog,
so you can see the effect of your choices.
For more information about selecting a font, see the Choosing Fonts section of the KDE
Fundamentals documentation.
Show whitespace indicators
Never
The editor will never display dots to indicate the presence of whitespace.
At the end of a line
The editor will display dots to indicate the presence of extra whitespace at the end of
lines.
Always
The editor will always display dots to indicate the presence of whitespace.
92
The KatePart Handbook
93
The KatePart Handbook
7.1.1.2 Borders
Left side
Show marks
If this is checked, you will see an icon border on the left hand side. The icon border
shows bookmark signs for instance.
Show line numbers
If this is checked, you will see line numbers on the left hand side.
Highlight changed and unsaved lines
If this is checked, line modification markers will be visible. For more information, see
Section 3.9.
Scrollbars
Show marks
If this option is checked the current view will show marks on the vertical scrollbar.
These marks will for instance show bookmarks.
Show preview when hovering over scrollbar
If this option is checked, and you hover the scrollbar with the mouse cursor a small
text preview with several lines of the current document around the cursor position
will be displayed. This allows you to quickly switch to another part of the document.
Minimap
Show minimap
If this option is checked, every new view will show a minimap of the document on
the vertical scrollbar.
For more information on the scrollbar minimap, see Section 3.10.
Minimap Width
Adjusts the width of the scrollbar mini-map, defined in pixels.
Scrollbars visibility
Switch the scrollbar on, off or show the scrollbar only when needed. Click with the
left mouse button on the blue rectangle to display the line number range of the doc-
ument displayed on the screen. Keep the left mouse button pressed outside the blue
rectangle to automatically scroll through the document.
94
The KatePart Handbook
By date created
Each new bookmark will be added to the bottom, independently from where it is
placed in the document.
By line number
The bookmarks will be ordered by the line numbers they are placed at.
7.1.3 Editing
7.1.3.1 General
Word wrap
Word wrap is a feature that causes the editor to automatically start a new line of text and
move (wrap) the cursor to the beginning of that new line. KatePart will automatically start
a new line of text when the current line reaches the length specified by the Wrap words at:
option.
95
The KatePart Handbook
Normal
Selections will be overwritten by typed text and will be lost on cursor movement.
Persistent
Selections will stay even after cursor movement and typing.
Allow scrolling past the end of the document
This option lets you scroll past the end of the document. This can be used to vertically
centre the bottom of the document, or put it on top of the current view.
Backspace key removes character’s base with its diacritics
When selected, composed characters are removed with their diacritics instead of only
removing the base character. This is useful for Indic locales.
Multicursor modifier
This option lets you set the modifier that will be used to create multiple cursors with left
mouse button click. You need to press the modifiers and click left mouse button to create
a cursor at the desired location. See Creating multiple cursors to discover other ways to
create multiple cursors.
96
The KatePart Handbook
7.1.3.3 Indentation
Tabulators
When this is enabled the editor will insert tabulator characters when you press the
Tab key or use automatic indentation.
Spaces
When this is enabled the editor will insert a calculated number of spaces according to
the position in the text and the tab-width setting when you press the Tab key or use
automatic indentation.
Tabulators and Spaces
When this is enabled, the editor will insert spaces as describe above when indenting
or pressing Tab at the beginning of a line, but insert tabulators when the Tab key is
pressed in the middle or end of a line.
Tab width:
This configures the number of spaces that are displayed in place of a tabulator char-
acter.
Indentation width:
The indentation width is the number of spaces which is used to indent a line. If con-
figured to indent using tabulators, a tabulator character is inserted if the indentation
is divisible by the tab width.
Indentation Properties
97
The KatePart Handbook
General
7.1.3.5 Spellcheck
These configuration options are described in the documentation for the System Settings module
Spell Checker.
General
98
The KatePart Handbook
Key Mapping
Key mapping is used to change the meaning of typed keys. This allows you to move com-
mands to other keys or make special keypresses for doing a series of commands.
Example:
F2 -> I-- Esc
This will prepend I-- to a line when pressing F2.
7.1.4 Open/Save
7.1.4.1 General
File Format
Encoding
This defines the standard encoding to use to open/save files, if not changed in the
open/save dialog or by using a command line option.
Encoding Detection
Select an item from the drop down box, either to disable autodetection or use Uni-
versal to enable autodetection for all encodings. But as this may probably only detect
utf-8/utf-16, selecting a region will use custom heuristics for better results. If neither
the encoding chosen as standard above, nor the encoding specified in the open/save
dialog, nor the encoding specified on command line match the content of the file, this
detection will be run.
Fallback Encoding
This defines the fallback encoding to try for opening files if neither the encoding cho-
sen as standard above, nor the encoding specified in the open/ save dialog, nor the
encoding specified on command line match the content of the file. Before this is used,
an attempt will be made to determine the encoding to use by looking for a byte or-
der mark at start of file: if one is found, the right unicode encoding will be chosen;
otherwise encoding detection will run, if both fail fallback encoding will be tried.
End of line
Choose your preferred end of line mode for your active document. You have the
choice between UNIX® , DOS/Windows® or Macintosh.
Automatic end of line detection
Check this if you want the editor to autodetect the end of line type. The first found
end of line type will be used for the whole file.
Enable byte order mark (BOM)
The byte order mark is a special sequence at the beginning of unicode encoded doc-
uments. It helps editors to open text documents with the correct unicode encoding.
For more information see Byte Order Mark.
Line Length Limit
Unfortunately, due to deficiencies in Qt™, KatePart experiences poor performance
when working with extremely long lines. For that reason, KatePart will automatically
wrap lines when they are longer than the number of characters specified here. To
disable this, set this to 0.
Automatic Cleanups on Save
99
The KatePart Handbook
7.1.4.2 Advanced
Local files
Check this if you want backups of local files when saving.
Remote files
Check this if you want backups of remote files when saving.
Prefix for backup files
Enter the prefix to prepend to the backup file names.
Suffix for backup files
Enter the suffix to add to the backup file names.
Swap file mode
KatePart is able to recover (most) unsaved work in the case of a crash or power failure.
A swap file (.<filename>.kate-swp) is created when a document is edited. If the user
doesn’t save the changes and KatePart crashes, the swap file remains on the disk.
When opening a file, KatePart checks if there is a swap file for the document and if
it is, it asks the user whether he wants to recover the lost data or not. The user has
the possibility to view the differences between the original file and the recovered one,
too. The swap file is deleted after every save and on normal exit.
KatePart syncs the swap files on the disk every 15 seconds, but only if they have
changed since the last sync. The user can disable the swap files syncing if he wants,
by selecting Disable, but this can lead to more data loss.
When the swap file is enabled, it is possible to switch between two modes, namely
Enabled, Store in Default Directory and Enabled, Store in Custom Directory.
Store swap files in
By default, the swap files are saved in the same folder as the file. When Enabled,
Store in Custom Directory is chosen for the swap file mode, swap files are created
in the specified folder. This is useful for network file systems to avoid unnecessary
network traffic.
Save swap files every
KatePart syncs the swap files on the disk every 15 seconds, but only if they have
changed since the last sync. You can change the sync interval as you like.
100
The KatePart Handbook
This page allows you to override the default configuration for documents of specified MIME
types. When the editor loads a document, it will try if it matches the file masks or MIME types
for one of the defined filetypes, and if so apply the variables defined. If more filetypes match, the
one with the highest priority will be used.
Filetype:
The filetype with the highest priority is the one displayed in the first drop down box. If
more filetypes were found, they are also listed.
New
This is used to create a new filetype. After you click on this button, the fields below
get empty and you can fill the properties you want for the new filetype.
Delete
To remove an existing filetype, select it from the drop down box and press the Delete
button.
Properties of current filetype
The filetype with the highest priority is the one displayed in the first drop down box. If
more filetypes were found, they are also listed.
Name:
The name of the filetype will be the text of the corresponding menu item. This name
is displayed in the Tools → Filetypes menu.
Section:
The section name is used to organize the file types in menus. This is also used in the
Tools → Filetypes menu.
Variables:
This string allows you to configure KatePart’s settings for the files selected by this
MIME type using KatePart variables. You can set almost any configuration option,
such as highlight, indent-mode, etc.
Press Edit to see a list of all available variables and their descriptions. Select the check-
box on the left to enable a particular variable and then set the value of the variable
on the right. Some variables provide a drop-down box to select possible values from
while others require you to enter a valid value manually.
For complete information on these variables, see Configuring with Document Vari-
ables.
Highlighting:
If you create a new file type, this drop down box allows you to select a filetype for
highlighting.
Indentation Mode:
The drop down box specifies the indentation mode for new documents.
File extensions:
The wildcards mask allows you to select files by filename. A typical mask uses an
asterisk and the file extension, for example *.txt; *.text. The string is a semicolon-
separated list of masks.
MIME types:
Displays a wizard that helps you easily select MIME types.
Priority:
Sets a priority for this file type. If more than one file type selects the same file, the one
with the highest priority will be used.
101
The KatePart Handbook
N OTE
Only the first and last 10 lines are searched for variable lines.
Additionally, document variables can be placed in a file called .kateconfig in any directory,
and the configured settings will be applied as if the modelines were entered on every file in the
directory and its subdirectories. Document variables in .kateconfig use the same syntax as in
modelines, but with extended options.
There are variables to support almost all configurations in KatePart, and additionally plugins can
use variables, in which case it should be documented in the plugin’s documentation.
KatePart has support for reading configurations from .editorconfig files, when the editorconfig
library is installed. KatePart automatically searches for a .editorconfig whenever you open a
file. It gives priority to .kateconfig files, though.
As you can see, document variables are only overridden by changes made at runtime. Whenever
a document is saved, the document variables are reread, and will overwrite changes made using
menu items or the command line.
Any variable not listed below is stored in the document and can be queried by other objects such
as plugins, which can use them for their own purpose. For example, the variable indent mode
uses document variables for its configuration.
The variables listed here documents KatePart version 5.38. More variables may be added in the
future. There are 3 possible types of values for variables, with the following valid expressions:
• BOOL - on|off|true|false|1|0
• INTEGER - any integer number
• STRING - anything else
102
The KatePart Handbook
auto-brackets [BOOL]
Enable automatic insertion of brackets.
auto-center-lines [INT]
Set the number of autocenter lines.
background-color [STRING]
Set the document background color. The value must be something that can be evaluated to
a valid color, for example #ff0000.
backspace-indents [BOOL]
Enable or disable unindenting when Backspace is pressed.
block-selection [BOOL]
Turn block selection on or off.
bom | byte-order-mark | byte-order-marker [BOOL]
Enable/disable the byte order mark (BOM) when saving files in Unicode format (utf8, utf16,
utf32).
Since: Kate 3.4 (KDE 4.4)
bracket-highlight-color [STRING]
Set the color for the bracket highlight. The value must be something that can be evaluated
to a valid color, for example #ff0000.
current-line-color [STRING]
Set the color for the current line. The value must be something that can be evaluated to a
valid color, for example #ff0000.
default-dictionary [STRING]
Sets the default dictionary used for spellchecking.
Since: Kate 3.4 (KDE 4.4)
dynamic-word-wrap [BOOL]
Turns dynamic word wrap on or off.
eol | end-of-line [STRING]
Set the end of line mode. Valid settings are unix, mac and dos.
folding-markers [BOOL]
Set the display of folding markers on or off.
folding-preview [BOOL]
Enable folding preview in the editor border.
font-size [INT]
Set the point size of the document font.
font [STRING]
Set the font of the document. The value should be a valid font name, for example courier.
hl | syntax [STRING]
Set the syntax highlighting. Valid strings are all the names available in the menus. For
instance, for C++ simply write C++.
103
The KatePart Handbook
icon-bar-color [STRING]
Set the icon bar color. The value must be something that can be evaluated to a valid color,
for example #ff0000.
icon-border [BOOL]
Set the display of the icon border on or off.
indent-mode [STRING]
Set the auto-indentation mode. The options none, normal, cstyle, haskell, lilypond,
lisp, python, ruby and xml are recognized. See the section Section 3.8 for details.
indent-pasted-text [BOOL]
Enable/disable adjusting indentation of text pasted from the clipboard.
Since: Kate 3.11 (KDE 4.11)
indent-width [INT]
Set the indentation width.
keep-extra-spaces [BOOL]
Set whether to keep extra spaces when calculating indentation width.
line-numbers [BOOL]
Set the display of line numbers on or off.
newline-at-eof [BOOL]
Add an empty line at the end of the file (EOF) when saving the document.
Since: Kate 3.9 (KDE 4.9)
overwrite-mode [BOOL]
Set overwrite mode on or off.
persistent-selection [BOOL]
Set persistent selection on or off.
replace-tabs-save [BOOL]
Set tab to space conversion on save on or off.
replace-tabs [BOOL]
Set dynamic tab to space conversion on or off.
remove-trailing-spaces [STRING]
Removes trailing spaces when saving the document. Valid options are:
• none, - or 0: never remove trailing spaces.
• modified, mod, + or 1: remove trailing spaces only in modified lines. The modified
lines are marked by the line modification system.
• all, * or 2: remove trailing spaces in the entire document.
scrollbar-minimap [BOOL]
Show scrollbar minimap.
scrollbar-preview [BOOL]
Show scrollbar preview.
scheme [STRING]
Set the color scheme. The string must be the name of a color scheme that exists in your
configuration to have any effect.
104
The KatePart Handbook
selection-color [STRING]
Set the selection color. The value must be something that can be evaluated to a valid color,
for example #ff0000.
show-tabs [BOOL]
Set the visual tab character on or off.
smart-home [BOOL]
Set smart home navigation on or off.
tab-indents [BOOL]
Set Tab key indentation on or off.
tab-width [INT]
Set the tab character display width.
undo-steps [INT]
Set the number of undo steps to remember.
Note: Deprecated since Kate 3 in KDE4. This variable is ignored. The maximal count of
undo steps is unlimited.
word-wrap-column [INT]
Set the static word wrap width.
word-wrap-marker-color [STRING]
Set the word wrap marker color. The value must be something that can be evaluated to a
valid color, for example #ff0000.
word-wrap [BOOL]
Set static word wrapping on or off.
In this example, all files use a tab-width of 4 spaces, an indent-width of 4 spaces, and tabs are
replaced expanded to spaces. However, for all *.xml files, the indent width is set to 2 spaces.
And Makefiles use tabs, i.e. tabs are not replaced with spaces.
Wildcards are semicolon separated, i.e. you can also specify multiple file extensions as follows:
kate - wildcard (*. json ;*. xml ): indent - width 2;
Further, you can also use the MIME type to match certain files, e.g. to indent all C++ source files
with 4 spaces, you can write:
kate - mimetype ( text /x -c ++ src ): indent - width 4;
N OTE
Next to the support in .kateconfig files, wildcard and MIME type dependent document variables are
also supported in the files itself as comments.
105
The KatePart Handbook
Chapter 8
The KatePart documentation is based on the original KWrite documentation, modified to be rel-
evant to all KatePart consumers.
The original KWrite documentation was written by Thad McGinnis ctmcgin-
nis@compuserve.com, with lots of modifications from Christian Tibirna tibirna@kde.org.
Converted to docbook/proofreading by Lauri Watts lauri@kde.org and updated by Anne-Marie
Mahfouf annma@kde.org and Anders Lund anders@alweb.dk
The current KatePart documentation is maintained by T.C. Hollingsworth
tchollingsworth@gmail.com. Please send comments or suggestions to the KatePart devel-
opment mailing list at kwrite-devel@kde.org or file a bug in the KDE Bugtracking System.
This documentation is licensed under the terms of the GNU Free Documentation License.
This program is licensed under the terms of the GNU General Public License.
106
The KatePart Handbook
Chapter 9
Erlend Hamberg
N OTE
Many Vi Mode keyboard commands are case-sensitive, unlike most KDE keyboard shortcuts. That
means that y and Y are different commands. To enter the y (yank) command, make sure Caps Lock
is disabled and press Y. To enter the Y (yank to end of line) command, Shift+Y.
This does not apply to commands that use the Ctrl key, which may be entered regardless of Caps
Lock mode and without pressing Shift. However, some commands require the use of a Ctrl-key
combination followed by another key which is case sensitive. For instance, to input ‘Ctrl+W, h’ (switch
to split view right) make sure Caps Lock is disabled, press Ctrl+W, release, and then press H.
There are only a few features of KatePart’s VI mode which are incompatible with Vim (not count-
ing things missing). They are listed below together with the respective reasons.
107
The KatePart Handbook
• Normal Mode permits you to enter commands to navigate or edit a document, and is the default
mode. You can return to it from any other mode by pressing Esc.
• Visual Mode permits you to highlight text in a document. Most Normal Mode commands are
also valid in this mode. You can enter it by pressing v to select characters or V to select lines.
• Insert Mode permits you to edit the document directly. You can enter it by pressing i or one of
several other commands listed below.
• Command Mode invokes KatePart’s command line, permitting you to run many commands
available in Vi implementations as well as some specific to KatePart. For more information on
these commands, see Section 5.2. To use it, press :, enter the command, and press Enter.
• Visual Mode is entered automatically when text is selected with the mouse. It is also entered
when using functions of Kate that select text, such as Select All (either from the menu or via
Ctrl+A.)
• Vi marks and Kate bookmarks are integrated. When a mark is created in Vi Mode, a corre-
sponding Kate bookmark is created and appears in the Bookmarks menu. Conversely, when
a Kate bookmark is created, a corresponding Vi mark at the 0 column is also created.
108
The KatePart Handbook
109
The KatePart Handbook
These may be used to move around a document in Normal or Visual mode, or in conjunction
with one of the above commands. They may be preceded by a count, which indicates how many
of the appropriate movements to make.
h Left
Left Left
Backspace Left
j Down
Down Down
k Up
Up Up
l Right
Right Right
Space Right
$ End of line
End End of line
0 First character of line (Column 0)
Home First character of line
ˆ First non-blank character of line
Follow by character to move to right of
f
cursor
F Follow by character to move to left of cursor
Follow by character to move to right of
t cursor, placing the cursor on character
before it
110
The KatePart Handbook
111
The KatePart Handbook
Ctrl+D Unindent
Ctrl+T Indent
Ctrl+E Insert from below
Ctrl+Y Delete word
Ctrl+W Delete word
Ctrl+U Delete line
Ctrl+J New line
Ctrl+H Delete char backward
Ctrl+Home Move to first character in the document
Ctrl+R n Insert the contents of register n
Ctrl+O, command Enter normal mode for one command only
Ctrl+A Increment currently selected number
Ctrl+X Decrement currently selected number
112
The KatePart Handbook
This object is missing in Vim. The comma text object makes it easy to modify parameter lists in
C-like languages and other comma separated lists. It is basically the area between two commas
or between a comma and a bracket. In the line shown in the illustration, the three ranges this text
object can span are highlighted.
Comma text object ranges. If the cursor is over e.g. arg2, pressing ci, (‘change inner comma’)
would delete double arg2 and place the cursor between the two commas in insert mode. A very
convenient way to change a function’s parameters.
As stated earlier, the goal of KatePart’s VI Mode is not to support 100% of Vim’s features.
113
The KatePart Handbook
Appendix A
Regular Expressions
This Appendix contains a brief but hopefully sufficient and covering introduction
to the world of regular expressions. It documents regular expressions in
the form available within KatePart, which is not compatible with the regular
expressions of perl, nor with those of for example grep.
A.1 Introduction
Regular Expressions provides us with a way to describe some possible contents of a text string in a
way understood by a small piece of software, so that it can investigate if a text matches, and also
in the case of advanced applications with the means of saving pieces or the matching text.
An example: Say you want to search a text for paragraphs that starts with either of the names
‘Henrik’ or ‘Pernille’ followed by some form of the verb ‘say’.
With a normal search, you would start out searching for the first name, ‘Henrik’ maybe followed
by ‘sa’ like this: Henrik sa, and while looking for matches, you would have to discard those
not being the beginning of a paragraph, as well as those in which the word starting with the
letters ‘sa’ was not either ‘says’, ‘said’ or so. And then of course repeat all of that with the next
name...
With Regular Expressions, that task could be accomplished with a single search, and with a larger
degree of preciseness.
To achieve this, Regular Expressions defines rules for expressing in details a generalization of a
string to match. Our example, which we might literally express like this: ‘A line starting with
either ‘Henrik’ or ‘Pernille’ (possibly following up to 4 blanks or tab characters) followed by a
whitespace followed by ‘sa’ and then either ‘ys’ or ‘id’’ could be expressed with the following
regular expression:
ˆ[ \t]{0,4}(Henrik|Pernille) sa(ys|id)
The above example demonstrates all four major concepts of modern Regular Expressions,
namely:
• Patterns
• Assertions
• Quantifiers
• Back references
114
The KatePart Handbook
The caret (ˆ) starting the expression is an assertion, being true only if the following matching
string is at the start of a line.
The strings [ \t] and (Henrik|Pernille) sa(ys|id) are patterns. The first one is a character
class that matches either a blank or a (horizontal) tab character; the other pattern contains first
a subpattern matching either Henrik or Pernille, then a piece matching the exact string sa and
finally a subpattern matching either ys or id
The string {0,4} is a quantifier saying ‘anywhere from 0 up to 4 of the previous’.
Because regular expression software supporting the concept of back references saves the entire
matching part of the string as well as sub-patterns enclosed in parentheses, given some means of
access to those references, we could get our hands on either the whole match (when searching a
text document in an editor with a regular expression, that is often marked as selected) or either
the name found, or the last part of the verb.
All together, the expression will match where we wanted it to, and only there.
The following sections will describe in details how to construct and use patterns, character
classes, assertions, quantifiers and back references, and the final section will give a few useful
examples.
A.2 Patterns
Patterns consists of literal strings and character classes. Patterns may contain sub-patterns, which
are patterns enclosed in parentheses.
In patterns as well as in character classes, some characters have a special meaning. To literally
match any of those characters, they must be marked or escaped to let the regular expression soft-
ware know that it should interpret such characters in their literal meaning.
This is done by prepending the character with a backslash (\).
The regular expression software will silently ignore escaping a character that does not have any
special meaning in the context, so escaping for example a ‘j’ (\j) is safe. If you are in doubt
whether a character could have a special meaning, you can therefore escape it safely.
Escaping of course includes the backslash character itself, to literally match a such, you would
write \\.
115
The KatePart Handbook
It is of course possible to create a ‘negative’ class matching as ‘anything but’ To do so put a caret
(ˆ) at the beginning of the class:
[ˆabc] will match any character but ‘a’, ‘b’ or ‘c’.
In addition to literal characters, some abbreviations are defined, making life still a bit easier:
\a
This matches the ASCII bell character (BEL, 0x07).
\f
This matches the ASCII form feed character (FF, 0x0C).
\n
This matches the ASCII line feed character (LF, 0x0A, Unix newline).
\r
This matches the ASCII carriage return character (CR, 0x0D).
\t
This matches the ASCII horizontal tab character (HT, 0x09).
\v
This matches the ASCII vertical tab character (VT, 0x0B).
\xhhhh
This matches the Unicode character corresponding to the hexadecimal number hhhh (be-
tween 0x0000 and 0xFFFF). \0ooo (i.e., \zero ooo) matches the ASCII/Latin-1 character
corresponding to the octal number ooo (between 0 and 0377).
. (dot)
This matches any character (including newline).
\d
This matches a digit. Equal to [0-9]
\D
This matches a non-digit. Equal to [ˆ0-9] or [ˆ\d]
\s
This matches a whitespace character. Practically equal to [ \t\n\r]
\S
This matches a non-whitespace. Practically equal to [ˆ \t\r\n], and equal to [ˆ\s]
\w
Matches any ‘word character’ - in this case any letter, digit or underscore. Equal to [a-zA-
Z0-9_]
\W
Matches any non-word character - anything but letters, numbers or underscore. Equal to
[ˆa-zA-Z0-9_] or [ˆ\w]
The POSIX notation of classes, [:<class name>:] are also supported. For example, [:digit:
] is equivalent to \d, and [:space:] to \s. See the full list of POSIX character classes here.
The abbreviated classes can be put inside a custom class, for example to match a word character,
a blank or a dot, you could write [\w \.]
116
The KatePart Handbook
The following characters has a special meaning inside the ‘[]’ character class construct, and must
be escaped to be literally included in a class:
]
Ends the character class. Must be escaped unless it is the very first character in the class
(may follow an unescaped caret).
ˆ (caret)
Denotes a negative class, if it is the first character. Must be escaped to match literally if it is
the first character in the class.
- (dash)
Denotes a logical range. Must always be escaped within a character class.
\ (backslash)
The escape character. Must always be escaped.
If you want to match one of a set of alternative patterns, you can separate those with | (vertical
bar character).
For example to find either ‘John’ or ‘Harry’ you would use an expression John|Harry.
You may use a sub pattern to group a set of alternatives within a larger pattern. The alternatives
are separated by the character ‘|’ (vertical bar).
For example to match either of the words ‘int’, ‘float’ or ‘double’, you could use the pattern in
t|float|double. If you only want to find one if it is followed by some whitespace and then
some letters, put the alternatives inside a subpattern: (int|float|double)\s+\w+.
If you want to use a back reference, use a sub pattern (PATTERN) to have the desired part of the
pattern remembered. To prevent the sub pattern from being remembered, use a non-capturing
group (?:PATTERN).
For example, if you want to find two occurrences of the same word separated by a comma and
possibly some whitespace, you could write (\w+),\s*\1. The sub pattern \w+ would find a
chunk of word characters, and the entire expression would match if those were followed by
a comma, 0 or more whitespace and then an equal chunk of word characters. (The string \1
references the first sub pattern enclosed in parentheses.)
117
The KatePart Handbook
N OTE
To avoid ambiguities with usage of \1 with some digits behind it (e.g. \12 can be 12th subpattern or
just the first subpattern with 2) we use \{12} as syntax for multi-digit subpatterns.
Examples:
The following characters have meaning inside a pattern, and must be escaped if you want to
literally match them:
\ (backslash)
The escape character.
ˆ (caret)
Asserts the beginning of the string.
$
Asserts the end of string.
() (left and right parentheses)
Denotes sub patterns.
{} (left and right curly braces)
Denotes numeric quantifiers.
[] (left and right square brackets)
Denotes character classes.
118
The KatePart Handbook
| (vertical bar)
logical OR. Separates alternatives.
+ (plus sign)
Quantifier, 1 or more.
* (asterisk)
Quantifier, 0 or more.
? (question mark)
An optional character. Can be interpreted as a quantifier, 0 or 1.
A.3 Quantifiers
Quantifiers allows a regular expression to match a specified number or range of numbers of either
a character, character class or sub pattern.
Quantifiers are enclosed in curly brackets ({ and }) and have the general form {[minimum-occur
rences][,[maximum-occurrences]]}
The usage is best explained by example:
{1}
Exactly 1 occurrence
{0,1}
Zero or 1 occurrences
{,1}
The same, with less work;)
{5,10}
At least 5 but maximum 10 occurrences.
{5,}
At least 5 occurrences, no maximum.
* (asterisk)
similar to {0,}, find any number of occurrences.
+ (plus sign)
similar to {1,}, at least 1 occurrence.
? (question mark)
similar to {0,1}, zero or 1 occurrence.
A.3.1 Greed
When using quantifiers with no maximum, regular expressions defaults to match as much of the
searched string as possible, commonly known as greedy behavior.
Modern regular expression software provides the means of ‘turning off greediness’, though in
a graphical environment it is up to the interface to provide you with access to this feature. For
example a search dialog providing a regular expression search could have a check box labeled
‘Minimal matching’ as well as it ought to indicate if greediness is the default behavior.
119
The KatePart Handbook
ˆ\d{4,5}\s
Matches the digits in ‘1234 go’ and ‘12345 now’, but neither in ‘567 eleven’ nor in ‘223459
somewhere’.
\s+
Matches one or more whitespace characters.
(bla){1,}
Matches all of ‘blablabla’ and the ‘bla’ in ‘blackbird’ or ‘tabla’.
/?>
Matches ‘/>’ in ‘<closeditem/>’ as well as ‘>’ in ‘<openitem>’.
A.4 Assertions
Assertions allows a regular expression to match only under certain controlled conditions.
An assertion does not need a character to match, it rather investigates the surroundings of a
possible match before acknowledging it. For example the word boundary assertion does not try to
find a non word character opposite a word one at its position, instead it makes sure that there is
not a word character. This means that the assertion can match where there is no character, i.e. at
the ends of a searched string.
Some assertions actually do have a pattern to match, but the part of the string matching that will
not be a part of the result of the match of the full expression.
Regular Expressions as documented here supports the following assertions:
120
The KatePart Handbook
121
The KatePart Handbook
Appendix B
Index
C
comment, 32
R
replace, sed style
search, sed style, 36
U
uncomment, 32
122