Category Archives: Users

Kate Scripting: Indentation

Kate Part in KDE4 supports the ECMAScript (JavaScript) language by using kjs. In KDE3 we had several hard-coded indenters in C++, the idea is to let scripts do all the indentation in KDE4.
How does it work? It is similar to vim: You simply create a script in the directory $KDEDIR/share/apps/katepart/jscript. An indentation script has to follow several rules:

  1. it must have a valid script header (the first line must include the string kate-script and indentation scripts must have the type: indentation)
  2. it must define some variables and functions

Whenever the user types a character, the flow in Kate Part works like this

  1. check the indentation script’s trigger characters, i.e. whether the script wants to indent code for the typed character
  2. if yes, call the indentation function
  3. the return value of the indentation function is an integer value representing the new indentation depth in spaces.

In the 3rd step there are 2 special cases for the return value:

  1. return value = -1: Kate keeps the indentation, that is it searches for the last non-empty line and uses its indentation for the current line
  2. return value < -1 tells Kate to do nothing

So how does a script look like exactly?
The name does not really matter, so let’s call it foobar.js:

/* kate-script
 * name: MyLanguage Indenter
 * license: LGPL
 * author: Foo Bar
 * version: 1
 * kate-version: 3.0
 * type: indentation
 *
 * optional bla bla here
 */

// specifies the characters which should trigger indent() beside the default '\n'
triggerCharacters = "{}";

// called for the triggerCharacters {} and
function indent(line, indentWidth, typedChar)
{
  // do calculations here
  // if typedChar is an empty string, the user hit enter/return

  // todo: Implement your indentation algorithms here.
  return -1; // keep indentation
}

More details on the header:

  • name [required]: the name will appear in Kate’s menus
  • license [optional]: not visible in gui, but should be specified in js-files. it is always better to have a defined license
  • author [optional]: name
  • version [optional]: recommended. an integer
  • kate-version [required]: the minimum required kate-version (e.g. for api changes)
  • type [required]: must be set to indentation

The only missing part is the API which Kate Part exports to access the document and the view. Right now, there is no API documentation, so you have to look at the code:

You will find, that the current document exports functions like

  • document.fileName()
  • document.isModified()
  • document.charAt(line, column)
  • etc…

The view exports functions like

  • view.cursorPosition()
  • view.hasSelection()
  • view.clearSelection()

That’s the boring part of this blog. The interesting one is unfortunately shorter: we are looking for contributors who want to write scripts or help in the C++ implementation 🙂

.kateconfig Files

Kate allows you to specify local document variables by setting the corresponding modelines. The hidden file .kateconfig provides exactly the same functionality, but for all files in the directory.

Enable the Feature

To use a .kateconfig file you first have to enable the feature by invoking Settings > Configure Kate. In the config page Open/Save you can find the option Search depth for config file, which defaults to Do not use a config file. Change the value to an appropriate number,

  • 0 means look in the current folder
  • 1 means look in the current folder, and if there is no .kateconfig file, look in the parent folder
  • etc…

In short – Kate will search the number of given folder levels upwards for a .kateconfig file and load the settings from it.

Fill the .kateconfig File

The .kateconfig file simply contains modelines.

The following example will cause all documents to indent with 4 spaces with a tab width of 4. Tabs will be replaced during editing text and the end-of-line symbol is a linefeed (\n).

kate: space-indent on; tab-width 4; indent-width 4; replace-tabs on; eol unix;

Extended Options in KDE 3.5.x, KDE 4 and Kate 5

Kate in KDE 4 as well as later versions of Kate always search for a .kateconfig file for local files (not remote files). In addition, it is now possible to set options based on wildcards (file extensions) as follows:

kate: tab-width 4; indent-width 4; replace-tabs on;
kate-wildcard(*.xml): indent-width 2;
kate-wildcard(Makefile): replace-tabs 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 mimetype 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;

Note: Next to the support in .kateconfig files, wildcard and mimetype dependent document variables are also supported in the files itself as comments.

Kate Modelines

Kate Part’s modelines – also called Kate document variables – are Kate Part’s implementation of document variables, similar to Emacs and vim modelines.

Document variables can be used to set Kate settings local to a file. This is for example for using another indenter than the one defined in the GUI settings. Or assume you work on two projects, the first indents with spaces and the second with tabs, then you can simply add a modeline that contains the specific document variables that set the specific indentation properties.

The following contents is mainly from the Kate Handbook.

Modeline Format

A modeline in the Kate Part has the following format:

kate: VARIABLENAME VALUE; [ VARIABLENAME VALUE; ... ] 

The lines can be in a comment, in C++ for example in /*...*/ sections. Multiple modelines are supported, i.e. modelines can be split over several lines for better visibility. Variable names are single words (no whitespace), and anything up to the next semicolon is the value. The trailing semicolon (;) is required.

Here is an example of how a modeline could look like, forcing indentation settings for a C++, Java or JavaScript file:

// kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle; 

NOTE: Only the first and last 10 lines in a file are searched for modelines.

There are variables to support almost all options in Kate Part, and additionally plugins can use variables, in which case it should be documented in the plugin’s documentation.

How Kate Part uses Variables

When reading settings, Kate Part looks in the following places in the order

  1. the global configuration, i.e. settings made in the user interface
  2. the Modes & Filetypes configuration
  3. the .kateconfig file
  4. the modelines in the document itself

This means Kate Part’s modelines have actually the highest precedence. 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.

Available Modelines

The variables listed here documents Kate version 2.4. More variables may be added in the future. There are defied 3 types of values for variables, with the following valid expressions:

  • BOOL – on|off|true|false|1|0
  • INTEGER – any integer number
  • STRING – anything else

Available modelines are, in detail:

  • auto-brackets [BOOL] Set auto insertion of brackets on or off.
  • auto-center-lines [INT] Set the number of autocenter lines.
  • auto-insert-doxygen [BOOL] Turn insertion of the leading asterisk in doxygen comments on or off. This has no effect unless you use the cstyle auto-indenter.
  • background-color [STRING] Sets the document background color. The value must be something that can be evaluated to a valid color, for example #ff0000.
  • backspace-indents [BOOL]Turn backspace indenting on or off.
  • block-selection [BOOL] Turn block selection on or off.
  • bom | byte-order-marker [BOOL] (new in Kate 3.4/KDE 4.4)
    Enable/disable the byte order marker when saving files in unicode format (utf-8/utf-16/utf-32).
  • bracket-highlight-color [STRING] Sets 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] Sets 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] (new in Kate 3.4/KDE 4.4)Sets the default dictionary used for spell checking.
  • dynamic-word-wrap [BOOL] Turns dynamic word wrap on or off.
  • eol | end-of-line [STRING] Sets the end of line mode. Valid settings are unix, mac and dos
  • folding-markers [BOOL] Set the display of folding markers on or off.
  • font-size [INT]Sets the point size of the document font.
  • font [STRING] Sets 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 you can find in the menus. For instance, for C++ you write C++.
  • icon-bar-color [STRING] Sets 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, python, lilypond, lisp are recognized. See the section Using Automatic Indenting for details.
  • indent-width [INT] Sets the indentation width.
  • keep-extra-spaces [BOOL] Set weather to keep extra spaces when calculating indentation width.
  • keep-indent-profile [BOOL] (Removed in Kate 3, i.e. since KDE4)
    If enabled, prevents unindenting a block if at least one line has no indentation.
  • line-numbers [BOOL] Set the display of line numbers on or off.
  • mixed-indent [BOOL] Set mixed indentation ala Emacs on or off.
    DEPRECATED for Kate 3.0 From version 3.0 this variable is ignored, set replace-tabs on; instead for that version.
  • overwrite-mode [BOOL] Set overwrite mode on or off.
  • persistent-selection [BOOL] Set persistent selection on or off.
  • remove-trailing-space [BOOL] Set dynamic end of line cleanup on or off.
  • replace-tabs-save [BOOL] Set tab->space conversion on save on or off.
  • replace-tabs [BOOL] Set dynamic tab->space conversion on or off.
  • replace-trailing-space-save [BOOL] Set end of line cleanup on save on or off.
  • 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.
  • 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.
  • space-indent [BOOL] Set indentation with spaces on or off.
    DEPRECATED for Kate 3.0 — From Kate 3.0 mixed-indent is the default behavior, set replace-tabs on; to achieve space-only indentation.
  • tab-indents [BOOL] Set the TAB key indentation on or off.
  • tab-width [INT] Set the tab display width.
  • undo-steps [INT] Set the number of undo steps to remember.
  • word-wrap-column [INT] Set the hard word wrap width.
  • word-wrap-marker-color [STRING] Set the work wrap marker color. The value must be something that can be evaluated to a valid color, for example #ff0000.
  • word-wrap [BOOL] Set hard word wrapping on or off.
  • wrap-cursor [BOOL] Set cursor wrapping on or off.