# Extending Kate with Scripts

As mentioned in one of my last blogs, there has been quite some work for JavaScript scripting support in trunk recently. So what will Kate 3.4 (KDE 4.4) will bring? This is explained in detail in the sections:

• Indentation Scripting
• Command Line Scripting
• Some Remarks

The scripting allows to extend Kate with lots of little helper functions. All users can add scripts as they wish. The documentation here is copied from the official Kate handbook. So instead of using this blog as reference, please use the handbook later for an up-to-date version. To script something useful, you’ll need to know the scripting API. All the available functions are documented in the section Scripting API in the handbook as well. Enough said, here we go:

## Extending Kate with Scripts

Since Kate 3.4 in KDE 4.4 the Kate editor component is easily extensible by writing scripts. The scripting language is ECMAScript (widely known as JavaScript). Kate supports two kinds of scripts: indentation and command line scripts.

### Indentation Scripts

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

The following sections describe step by step how to create the skeleton for a simple indenter. As first step, create a new *.js file called e.g. javascript.js in the local home folder KDEHOME/share/apps/katepart/script. #### The Indentation Script Header <p> The header of the file <code class="filename">javascript.js</code> is embedded in a comment and is of the following form </p> <pre>/* kate-script • name: JavaScript • author: Example Name • license: BSD • revision: 1 • kate-version: 3.4 • type: indentation • required-syntax-style: javascript • indent-languages: javascript • priority: 0 * • A line without colon ‘:’ stops header parsing. That is, you can add optional • text here such as a detailed license. */ Each entry is explained in detail now: • kate-script [required]: This text string has to appear in the first line of the *.js file, otherwise Kate skips the script. • 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 or LGPLv3. • revision [required]: The revision of the script. This number should be increased whenever the script is modified. • kate-version [required]: Minimal required Kate version. • type [required]: The type must be “indentation”, otherwise Kate skips this script. • required-syntax-style [optional]: Comma separated list of required syntax highlighting styles. 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]: Comma separated list 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. Kate reads all pairs of the form “key:value” until it cannot fine a colon anymore. This implies that the header can contain arbitrary text such as a license as shown in the example. #### The Indenter Source Code <p> Having specified the header this section explains how the indentation scripting itself works. The basic skeleton of the body looks like this: </p> <pre>triggerCharacters = "{}/:;"; function indent(line, indentWidth, ch) { // called for each newline (ch == ‘\n’) and all characters specified in // the global variable triggerCharacters. When calling Tools -> Align // the variable ch is empty, i.e. ch == “. // // see also: Scripting API return -2; } <p> The function <code class="function">indent()</code> has three parameters: </p> <div class="itemizedlist"> <ul type="disc"> <li> <code class="literal">line</code>: the line that has to be indented </li> <li> <code class="literal">indentWidth</code>: the indentation width in amount of spaces </li> <li> <code class="literal">ch</code>: either a newline character (<code class="literal">ch == '\n'</code>), the trigger character specified in <code class="literal">triggerCharacters</code> or empty if the user invoked the action <span class="guiitem"><span class="guimenu">Tools</span></span>-><span class="guiitem"><span class="guimenuitem">Align</span></span>. </li> </ul> </div> <p> The return value of the <code class="function">indent()</code> function specifies how the line will be indented. If the return value is a simple integer number, it is interpreted as follows: </p> <div class="itemizedlist"> <ul type="disc"> <li> return value <code class="literal">-2</code>: do nothing </li> <li> return value <code class="literal">-1</code>: keep indentation (searches for previous non-blank line) </li> <li> return value <code class="literal"> 0</code>: numbers >= 0 specify the indentation depth in spaces </li> </ul> </div> <p> Alternatively, an array of two elements can be returned: </p> <div class="itemizedlist"> <ul type="disc"> <li> <code class="literal">return [ indent, align ];</code> </li> </ul> </div> <p> In this case, the first element is the indentation depth like above with the same meaning of the special values. However, the second element is an absolute value representing a column for “<span class="quote">alignment</span>”. 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 “<span class="quote">mixed mode</span>”. </p> <p> Consider the following example: Assume using tabs to indent, and tab width is set to 4. Here, <tab> represents a tab and &#8216;.&#8217; a space: </p> <pre>1: &lt;tab&gt;&lt;tab&gt;foobar("hello", 2: <tab><tab>…….“world”); <p> When indenting line 2, the <code class="function">indent()</code> 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. </p> <p> A default KDE installation ships <span class="application">Kate</span> with several indenters. The corresponding JavaScript source code can be found in <code class="filename">KDRDIR/share/apps/katepart/script</code>.
</p>

<p>
Developing an indenter requires to reload the scripts to see whether the changes behave appropriately. Instead of restarting the application, simply switch to the command line and invoke the command <span class="command"><strong class="command">reload-scripts</strong></span>.
</p>

<p>
If you develop useful scripts please consider contributing to the <span class="application">Kate</span> Project by <a href="mailto:kwrite-devel@kde.org" target="_top">contacting the mailing list</a>.
</p>

### Command Line Scripts

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

Command line scripts are located in the save folder as indentation scripts. So as first step, create a new *.js file called myutils.js in the local home folder \$KDEHOME/share/apps/katepart/script.

#### The Command Line Script Header

<p>
The header of each command line script is embedded in a comment and is of the following form
</p>

<pre>/* kate-script
• author: Example Name
• revision: 1
• kate-version: 3.4
• type: commands
• functions: sort, format-paragraph *
• A line without colon ‘:’ stops header parsing. That is, you can add optional
• text here such as a detailed license. */

Each entry is explained in detail now:

• kate-script [required]: This text string has to appear in the first line of the *.js file, otherwise Kate skips the script.
• author [optional]: The author’s name and contact information.
• license [optional]: Short form of the license, such as BSD or LGPLv3.
• revision [required]: The revision of the script. This number should be increased whenever the script is modified.
• kate-version [required]: Minimal required Kate version.
• type [required]: The type must be ‘commands’, otherwise Kate skips this script.
• functions [required]: Comma separated list of commands in the script.

Kate reads all pairs of the form “key:value” until it cannot fine a colon anymore. This implies that the header can contain arbitrary text such as a license as shown in the example. The value of the key functions is a comma separated list of command line commands. This means a single script contains an arbitrary amount of command line commands. Each function is available through Kate‘s built-in command line.

#### The Script Source Code

<p>
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 <span class="command"><strong class="command">sort</strong></span> and <span class="command"><strong class="command">format-paragraph</strong></span>. All functions have the following syntax:
</p>

<pre class="programlisting">function (arg1, arg2, ...) {

<p>
Arguments in the command line are passed to the function as <span class="parameter"><em class="parameter"><code>arg1</code></em></span>, <span class="parameter"><em class="parameter"><code>arg2</code></em></span>, etc. In order to provide documentation for each command, simply implement the &#8216;<code class="function">help</code>&#8216; function as follows:
</p>

<pre class="programlisting">function help(cmd)

{ if (cmd == “sort”) { return “Sort the selected text.”; } else if (cmd == “…”) { // … } }

<p>
Executing <span class="command"><strong class="command">help sort</strong></span> in the command line then calls this help function with the argument <span class="parameter"><em class="parameter"><code>cmd</code></em></span> set to the given command, i.e. <span class="parameter"><em class="parameter"><code>cmd == "sort"</code></em></span>. <span class="application">Kate</span> then presents the returned text as documentation to the user.
</p>

<p>
Developing a command line script requires to reload the scripts to see whether the changes behave appropriately. Instead of restarting the application, simply switch to the command line and invoke the command <span class="command"><strong class="command">reload-scripts</strong></span>.
</p>

<p>
If you develop useful scripts please consider contributing to the <span class="application">Kate</span> Project by <a href="mailto:kwrite-devel@kde.org" target="_top">contacting the mailing list</a>.
</p>

<h2 class="title" style="clear: both;">
Final Remarks
</h2>

<p>
The command line scripting can be accessed for all KTextEditor users through the <a href="http://api.kde.org/4.x-api/kdelibs-apidocs/interfaces/ktexteditor/html/classKTextEditor_1_1CommandInterface.html">KTextEditor::CommandInterface</a>. That is, you can query a specific command and <a href="http://api.kde.org/4.x-api/kdelibs-apidocs/interfaces/ktexteditor/html/classKTextEditor_1_1Command.html#afcf59107ca87d860fc7a17e507df02d2">execute it</a> with arbitrary parameters (The parameter cmd contains the command itself including all arguments. Example: cmd = &#8220;goto 65&#8221;).
</p>

<p>
Kate&#8217;s command line itself is actually a quite powerful tool. It&#8217;s a little bit sad that it&#8217;s rather unknown. If you want to know more, just invoke &#8220;View -> Swith to command line&#8221; (shortcut: F7) and start typing text. More details are in the <a title="Kate Command Line" href="http://docs.kde.org/stable/en/kde-baseapps/kate/advanced-editing-tools-commandline.html" target="_blank" rel="noopener">Kate handbook</a> as well.
</p>

<p>
The Kate scripting API can be found <a title="Kate Scripting API" href="http://docs.kde.org/stable/en/kde-baseapps/kate/advanced-editing-tools-scripting.html#advanced-editing-tools-scripting-api" target="_blank" rel="noopener">here</a>.
</p>