KatePart gained scripting support in version 2.5 (KDE 3.5) using the common language JavaScript (ECMAScript).
First some basics: A katepart represents a document and one or more views - that’s all. Scripts can access the document and the active view. As JavaScript supports functions and properties every function will be marked with a [function] and every property with a [property:read only].
debug( string ); [function]
Outputs the string to STDERR using kdDebug(). A dedicated output area is used for the output, which will be prefixed Kate (KJS Scripts):.
The document provides basic functions to modify itself, like get or set a specific text line. The following list explains all available functions and properties, each one can be accessed by document.function:
document.attribute( uint line, uint column ); [function]
Returns the numeric ID of the attribute for the document position [line,col]. The attribute represents the visual appearance or style of the text, and is also used to calculate the syntax highlight for a specific part of the text in mixed formats like HTML or PHP.
document.canBreakAt( Char c, uint attribute ); [function]
Returns whether it is allowed to break the line at a character c with attribute attribute. The result is decided by querying the highlight owning attribute for which characters allow breaking the line.
document.canComment( uint start_attribute, uint end_attribute ); [function]
Returns whether startattribute and endattribute belongs to the same syntax highlight system. If they do, it is sane to make a range with startattribute at the start and endattribute at the end into a comment.
if ( document.canComment( document.attribute(1,0), document.attribute(5,0) ) ) { // 1,0 and 5,0 belongs to the same syntax highlighting system }
document.clear(); [function]
Clears the document.
document.commentStart( uint attribute ); [function]
Returns the string required to start a multiline comment for a text with attribute, or an empty string if multiline comments are not supported for that text.
document.commentMarker( uint attribute ); [function]
Returns the string used to mark the rest of the line as a comment for a text with attribute or an empty string if single line comments are not supported for that text.
document.commentEnd( uint attribute ); [function]
Returns the string required to end a multiline comment for a text with attribute, or an empty string if multiline comments are not supported for that text.
document.editBegin(); [function]
Start an editing group. All actions done until the call of editEnd() will be grouped as one undo-action.
document.editEnd(); [function]
Finish an editing group.
document.highlightMode; [property:read only]
The name of the document’s highlight mode, such as JavaScript or C++. If no syntax highlight mode is set for the document, the value is None. Notice that you need to use the English name in cases where it differs from the translated one.
document.indentMode; [property:read only]
The name of the document indent mode, such as <code>normal</code>' orcstyle’. Remember that if no indent mode is set, the value is `none’.
document.indentWidth; [property:read only]
The indentation width set for the document. This is used if space indenting is enabled.
document.insertLine( uint line, string text ); [function]
Inserts the text at the line.
document.insertText( uint line, uint column, string text ); [function]
Inserts the text in line and column.
document.length(); [function]
Returns the document’s size in bytes.
document.lineLength( uint line ); [function]
Returns the number of characters in line.
document.lines(); [function]
Returns the number of lines in the document.
document.mixedIndent; [property:read only]
A boolean telling whether the mixed-indent setting is enabled for the document. If so, indentation is optimized to contain a mix of tab characters and spaces like used by the Emacs editor.
document.removeLine( uint line ); [function]
Removes the document line.
document.removeText( uint startLine, uint startColumn,
uint endLine, uint endColumn ); [function]
Removes the text range from line startLine and column startColumn up to line endLine and column endColumn.
document.setText( string text ); [function]
Sets the entire document content.
document.spaceIndent; [property:read only]
A boolean telling whether space-indent is enabled for the document. If so, the document is indented with indentWidth spaces per level, otherwise indentation is one tab character per level.
document.textFull(); [function]
Returns the full document text. If the text spans over multiple lines the linefeed character is ‘\n’.
document.textLine( uint line ); [function]
Returns the text of line.
document.textRange( uint startLine, uint startColumn,
uint endLine, uint endColumn ); [function]
Returns the specified text range. If the range spans over multiple lines the linefeed character is ‘\n’.
A document has one or more views. The object view represents the active view and exports the following functions and properties:
view.clearSelection(); [function]
Deselects all text.
view.cursorColumn(); [function]
Returns the current cursor column.
view.cursorColumnReal(); [function]
Returns the current real cursor column.
view.cursorLine(); [function]
Returns the current cursor line.
view.hasSelection(); [function]
Returns true if the view contains selected text, otherwise false.
view.removeSelectedText(); [function]
Removes the selected text, if the view has a selection.
view.selectAll(); [function]
Selects all text.
view.selection(); [function]
Returns the selected text. If the selection spans over multiple lines the linefeed character is ‘\n’.
view.selectionEndColumn; [property:read only]
Returns the ending column of the selection.
view.selectionEndLine; [property:read only]
Returns the ending line of the selection.
view.selectionStartColumn; [property:read only]
Returns the starting column of the selection.
view.selectionStartLine; [property:read only]
Returns the starting line of the selection.
view.setCursorPosition( uint line, uint column ); [function]
Sets the input cursor position in the view to [line,col]. This sets the cursor position by visual means, that is the a TAB character counts up to <tabwidth> depending on the position inside the line. The cursor position is made visible. Both line and column are zero-based.
view.setCursorPositionReal( uint line, uint column ); [function]
Sets the input cursor position to [line,col]. This sets the string position, that is a TAB character counts for 1. The cursor position is made visible. Both line and column are zero-based.
view.setSelection( uint startLine, uint startColumn,
uint endLine, uint endColumn ); [function]
Sets a selection from line startLine and column startColumn up to line endLine and column endColumn.
As an example we will create a small script that uppercases the selection. It is obvious that we first need to check whether a selection exists, if so we get the text, change the case and then replace it with the new one.
An implementation could look like this:
if ( view.hasSelection() )
{
// uppercase selection
column = view.selectionStartColumn;
line = view.selectionStartLine;
selection = view.selection().toUpperCase();
document.editBegin();
view.removeSelectedText();
document.insertText( line, column, selection );
document.editEnd();
}
To group this action together we encapsulate the lines view.removeSelectedText() and document.insertText() with a document.editBegin() and document.editEnd().
Now save the file as for example uppercase.js and put it into the scripts folder (see next chapter).
The katepart searches the folders $KDEDIR/share/apps/katepart/scripts/ and ~/.kde/share/apps/katepart/scripts/ for .js files. For every file it checks whether there is a corresponding .desktop file, like for uppercase.js it would look for uppercase.desktop.
If a .desktop file can not be found the script will be registered in katepart’s command line with the filename without the ending .js, so in our example this would be uppercase. If the command-name is fine and you don’t need the extra features a .desktop file provides you do not need a .desktop file at all.
If a .desktop file exists katepart will read the name under which the script will be registered from the .desktop-entry X-Kate-Command, for example X-Kate-Command=uppercase-selection.
As already explained, with X-Kate-Command you can change the command line name. However, you can provide much more information, available settings are:
# Example of a .desktop file
[Desktop Entry]
Encoding=UTF-8
Name=Kate Part JavaScript Uppercase
Comment=Script to uppercase the selection
X-Kate-Command=uppercase-selection
X-Kate-Help=<p>Usage: <code>uppercase-selection</code></p>
So you can define the Encoding, set a Name, a Comment, a help text using X-Kate-Help and the command line name via X-Kate-Command.
The entries Name, Comment and X-Kate-Help are automatically translated into other languages, if the files are in KDE’s SVN repository.
Comments
Base64 encode/decode
I have preserved a comment block written by the author of the encode64/decode64 functions. My contributions are also public domain.
base64.js
unbase64.js
desktop file
What is the effect of the desktop file from above? Is it possible to add a menu item by such a desktop file?
bind the script to a shortcut
Same question here: Is there a way I could bind the script to a shortcut, and/or let it appear in a menu?
serlis http://blog.lsmnetworks.com/
Can you call external shell command from the script?
I would like to reformat the entire document so that the line width is, say, 60 characters. If I have file.txt, the following shell command will do:
fmt -w 60 file.txt
or alternatively using pipe
cat file.txt | fmt -w 60
How to do this in Kate? Ideally, I would like to be able to select text, pipe it into fmt, and replace the selection with the output of fmt.
Invoking the script
I can invoke the script by pressing F7 (the default key binding), writing its name, and pressing Enter.
This is rather lengthy. Is there a way I could bind the script to a shortcut, and/or let it appear in a menu (in Kate, for example)?
Invoking the script
It would be really simple to extend kate with scripts, although it is useless too, cause some simple scripts like deleting/copying/inserting a line aren’t useful without keybindings. I would really appreciate the option for keybindings to my own scripts.
Post new comment