The KatePart editor component is easily extensible by writing scripts. The scripting language is ECMAScript (widely known as JavaScript). KatePart supports two kinds of scripts: indentation and command line scripts.
Indentation scripts - also referred as indenters - automatically indent the source code while typing text. As an example, after hitting the return key the indentation level often increases.
The following sections describe step by step how to create the skeleton for a
simple indenter. As a first step, create a new *.js
file
called e.g. javascript.js
in the local home folder
$
.
Therein, the environment variable XDG_DATA_HOME
/katepart5/script/indentationXDG_DATA_HOME
typically expands to
either ~/.local
or ~/.local/share
.
On Windows® these files are located in %USERPROFILE%\AppData\Local\katepart5\script\indentation
.
%USERPROFILE%
usually expands to C:\\Users\\
.user
The header of the file javascript.js
is embedded as JSON at the beginning of the document as follows:
var katescript = { "name": "JavaScript", "author": "Example Name <example.name@some.address.org>", "license": "BSD License", "revision": 1, "kate-version": "5.1", "required-syntax-style": "javascript", "indent-languages": ["javascript"], "priority": 0, }; // kate-script-header, must be at the start of the file without comments
Each entry is explained in detail now:
name
[required]: This is the indenter name that appears in the menu → 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 specifiedstyle
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 filesruby.js
andruby.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 → // the variable ch is empty, i.e. ch == ''. // // see also: Scripting API return -2; }
The function indent()
has three parameters:
line
: the line that has to be indentedindentWidth
: the indentation width in number of spacesch
: either a newline character (ch == '\n'
), the trigger character specified intriggerCharacters
or empty if the user invoked the action → .
The return value of the indent()
function specifies how
the line will be indented. If the return value is a simple integer number, it
is interpreted as follows:
return value
-2
: do nothingreturn value
-1
: keep indentation (searches for previous non-blank line)return value
0
: numbers >= 0 specify the indentation depth in spaces
Alternatively, an array of two elements can be returned:
return [ indent, align ];
In this case, the first element is the indentation depth as above with the same meaning of the special values. However, the second element is an absolute value representing a column for “alignment”. If this value is higher than the indent value, the difference represents a number of spaces to be added after the indentation of the first parameter. Otherwise, the second number is ignored. Using tabs and spaces for indentation is often referred to as “mixed mode”.
Consider the following example: Assume using tabs to indent, and tab width is set to 4. Here, <tab> represents a tab and '.' a space:
1: <tab><tab>foobar("hello", 2: <tab><tab>......."world");
When indenting line 2, the indent()
function returns [8, 15]. As result, two
tabs are inserted to indent to column 8, and 7 spaces are added to align the
second parameter under the first, so that it stays aligned if the file is viewed
with a different tab width.
A default KDE installation ships 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\indentation
.
%USERPROFILE%
usually expands to C:\\Users\\
.
user
Developing an indenter requires reloading the scripts to see whether the changes behave appropriately. Instead of restarting the application, simply switch to the command line and invoke the command reload-scripts.
If you develop useful scripts please consider contributing to the KatePart Project by contacting the mailing list.
As it is hard to satisfy everyone's needs, KatePart supports little helper tools
for quick text manipulation through the
built-in command line.
For instance, the command
sort is implemented as a script. This section explains how to create
*.js
files to extend KatePart with arbitrary helper scripts.
Command line scripts are located in the same folder as indentation scripts.
So as a first step, create a new *.js
file called
myutils.js
in the local home folder
$
.
Therein, the environment variable XDG_DATA_HOME
/katepart5/script/commandsXDG_DATA_HOME
typically expands to
either ~/.local
or ~/.local/share
.
On Windows® these files are located in %USERPROFILE%\AppData\Local\katepart5\script\commands
.
%USERPROFILE%
usually expands to C:\\Users\\
.
user
The header of each command line script is embedded in JSON at the beginning of the script as follows:
var katescript = { "author": "Example Name <example.name@some.address.org>", "license": "LGPLv2+", "revision": 1, "kate-version": "5.1", "functions": ["sort", "moveLinesDown"], "actions": [ { "function": "sort", "name": "Sort Selected Text", "category": "Editing", "interactive": "false" }, { "function": "moveLinesDown", "name": "Move Lines Down", "category": "Editing", "shortcut": "Ctrl+Shift+Down", "interactive": "false" } ] }; // kate-script-header, must be at the start of the file without comments
Each entry is explained in detail now:
author
[optional]: The author's name and contact information.license
[optional]: Short form of the license, such as BSD License or LGPLv2.revision
[required]: The revision of the script. This number should be increased whenever the script is modified.kate-version
[required]: Minimum required KatePart version.functions
[required]: JSON array of commands in the script.actions
[optional]: JSON Array of JSON objects that defines the actions that appear in the application menu. Detailed information is provided in the section Binding Shortcuts.
Since the value of functions
is a JSON array, a single script is able to contain an arbitrary number
of command line commands. Each function is available through KatePart's
built-in command line.
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"); function <name>(arg1, arg2, ...) { // ... implementation, see also: Scripting API }
Arguments in the command line are passed to the function as
arg1
, arg2
, etc.
In order to provide documentation for each command, simply implement the
'help
' function as follows:
function help(cmd) { if (cmd == "sort") { return i18n("Sort the selected text."); } else if (cmd == "...") { // ... } }
Executing help sort in the command line then calls this help function with
the argument cmd
set to the given command, 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 behave 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" } ] };
The fields for one action are as follows:
function
[required]: The function that should appear in the menu → .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 totrue
.
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 $
.
If you want to use them in your script, which needs to use some of the XDG_DATA_DIRS
/katepart5/librariesDocument
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
$
and include it into your script using:
XDG_DATA_HOME
/katepart5/libraries
require ("myscriptnamehere.js");
On Windows® these files are located in %USERPROFILE%\AppData\Local\katepart5\libraries
.
%USERPROFILE%
usually expands to C:\\Users\\
.user
To extend existing prototypes like Cursor
or
Range
, the recommended way is to
not modify the global *.js
files.
Instead, change the Cursor
prototype in JavaScript after the cursor.js
is included into your
script via require
.
As KatePart is a text editor, all the scripting API is based on cursors and
ranges whenever possible. A Cursor is a simple (line, column)
tuple representing a text position in the document. A Range spans text from a
starting cursor position to an ending cursor position. The API is explained in
detail in the next sections.
Cursor();
Constructor. Returns a Cursor at position
(0, 0)
.Example:
var cursor = new Cursor();
Cursor(
int
,line
int
);column
Constructor. Returns a Cursor at position (line, column).
Example:
var cursor = new Cursor(3, 42);
Cursor(
Cursor
);other
Copy constructor. Returns a copy of the cursor
other
.Example:
var copy = new Cursor(other);
Cursor Cursor.clone();
Returns a clone of the cursor.
Example:
var clone = cursor.clone();
Cursor.setPosition(
int
,line
int
);column
Sets the cursor position to
line
andcolumn
.Since: KDE 4.11
bool Cursor.isValid();
Check whether the cursor is valid. The cursor is invalid, if line and/or column are set to
-1
.Example:
var valid = cursor.isValid();
Cursor Cursor.invalid();
Returns a new invalid cursor located at
(-1, -1)
.Example:
var invalidCursor = cursor.invalid();
int Cursor.compareTo(
Cursor
);other
Compares this cursor to the cursor
other
. Returns-1
, if this cursor is located before the cursorother
,0
, if both cursors equal and+1
, if this cursor is located after the cursorother
.
bool Cursor.equals(
Cursor
);other
Returns
true
, if this cursor and the cursorother
are equal, otherwisefalse
.String Cursor.toString();
Returns the cursor as a string of the form “
Cursor(line, column)
”.
Range();
Constructor. Calling
new Range()
returns a Range at (0, 0) - (0, 0).Range(
Cursor
,start
Cursor
);end
Constructor. Calling
new Range(
returns the Range (start
,end
)start
,end
).Range(
int
,startLine
int
,startColumn
int
,endLine
int
);endColumn
Constructor. Calling
new Range(
returns the Range from (startLine
,startColumn
,endLine
,endColumn
)startLine
,startColumn
) to (endLine
,endColumn
).Range(
Range
);other
Copy constructor. Returns a copy of Range
other
.Range Range.clone();
Returns a clone of the range.
Example:
var clone = range.clone();
bool Range.isEmpty();
Returns
true
, if the start and end cursors are equal.Example:
var empty = range.isEmpty();
Since: KDE 4.11
bool Range.isValid();
Returns
true
, if both start and end cursor are valid, otherwisefalse
.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, otherwisefalse
.bool Range.contains(
Range
);other
Returns
true
, if this range contains the Rangeother
, otherwisefalse
.bool Range.containsColumn(
int
);column
Returns
true
, ifcolumn
is in the half open interval[start.column, end.column)
, otherwisefalse
.bool Range.containsLine(
int
);line
Returns
true
, ifline
is in the half open interval[start.line, end.line)
, otherwisefalse
.bool Range.overlaps(
Range
);other
Returns
true
, if this range and the rangeother
share a common region, otherwisefalse
.bool Range.overlapsLine(
int
);line
Returns
true
, ifline
is in the interval[start.line, end.line]
, otherwisefalse
.bool Range.overlapsColumn(
int
);column
Returns
true
, ifcolumn
is in the interval[start.column, end.column]
, otherwisefalse
.bool Range.onSingleLine();
Returns
true
, if the range starts and ends at the same line, i.e. ifRange.start.line == Range.end.line
.Since: KDE 4.9
bool Range.equals(
Range
);other
Returns
true
, if this range and the Rangeother
are equal, otherwisefalse
.String Range.toString();
Returns the range as a string of the form “
Range(Cursor(line, column), Cursor(line, column))
”.
This section lists all global functions.
String read(
String
);file
Will search the given
file
relative to thekatepart5/script/files
directory and return its content as a string.
void require(
String
);file
Will search the given
file
relative to thekatepart5/script/libraries
directory and evaluate it.require
is internally guarded against multiple inclusions of the samefile
.Since: KDE 4.10
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.
void i18n(
String
,text
arg1
, ...);Translates
text
into the language used by the application. The argumentsarg1
, ..., are optional and used to replace the placeholders%1
,%2
, etc.void i18nc(
String
,context
String
,text
arg1
, ...);Translates
text
into the language used by the application. Additionally, the stringcontext
is visible to translators so they can provide a better translation. The argumentsarg1
, ..., are optional and used to replace the placeholders%1
,%2
, etc.void i18np(
String
,singular
String
,plural
int
,number
arg1
, ...);Translates either
singular
orplural
into the language used by the application, depending on the givennumber
. The argumentsarg1
, ..., are optional and used to replace the placeholders%1
,%2
, etc.void i18ncp(
String
,context
String
,singular
String
,plural
int
,number
arg1
, ...);Translates either
singular
orplural
into the language used by the application, depending on the givennumber
. Additionally, the stringcontext
is visible to translators so they can provide a better translation. The argumentsarg1
, ..., are optional and used to replace the placeholders%1
,%2
, etc.
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
); void view.setCursorPosition(column
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
); void view.setVirtualCursorPosition(column
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, otherwisefalse
.Range view.selection();
Returns the selected text range. The returned range is invalid if there is no selected text.
void view.setSelection(
Range
);range
Set the selected text to the given range.
void view.removeSelectedText();
Remove the selected text. If the view does not have any selected text, this does nothing.
void view.selectAll();
Selects the entire text in the document.
void view.clearSelection();
Clears the text selection without removing the text.
void view.setBlockSelection(bool on);
Set block selection mode on or off.
bool view.blockSelection();
Returns
true
, if block selection mode is on, otherwisefalse
.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 expressionpattern
. With an emptypattern
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 argumentsargs
and the optionalrange
. The returnedobject
has a boolean propertyobject.ok
that indicates whether execution of thecommand
was successful. In case of an error, the stringobject.status
contains an error message.Since: KDE Frameworks™ 5.50
Range view.searchText(
Range
,range
String
,pattern
bool
);backwards
= falseSearch for the first occurrence of
pattern
inrange
and returns the matched range. Search is performed backwards if the optional boolean parameterbackwards
is set totrue
.The returned range is invalid (see Range.isValid()) if
pattern
is not found inrange
.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.
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), otherwisefalse
.String document.text();
Returns the entire content of the document in a single text string. Newlines are marked with the newline character “
\n
”.String document.text(
int
,fromLine
int
,fromColumn
int
,toLine
int
); String document.text(toColumn
Cursor
,from
Cursor
); String document.text(to
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
); String document.wordAt(column
Cursor
);cursor
Returns the word at the given cursor position.
-
Range document.wordRangeAt(
int
,line
int
); Range document.wordRangeAt(column
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
); String document.charAt(column
Cursor
);cursor
Returns the character at the given cursor position.
String document.firstChar(
int
);line
Returns the first character in the given
line
that is not a whitespace. The first character is at column 0. If the line is empty or only contains whitespace characters, the returned string is empty.String document.lastChar(
int
);line
Returns the last character in the given
line
that is not a whitespace. If the line is empty or only contains whitespace characters, the returned string is empty.bool document.isSpace(
int
,line
int
); bool document.isSpace(column
Cursor
);cursor
Returns
true
, if the character at the given cursor position is a whitespace, otherwisefalse
.bool document.matchesAt(
int
,line
int
,column
String
); bool document.matchesAt(text
Cursor
,cursor
String
);text
Returns
true
, if the giventext
matches at the corresponding cursor position, otherwisefalse
.bool document.startsWith(
int
,line
String
,text
bool
);skipWhiteSpaces
Returns
true
, if the line starts withtext
, otherwisefalse
. The argumentskipWhiteSpaces
controls whether leading whitespaces are ignored.bool document.endsWith(
int
,line
String
,text
bool
);skipWhiteSpaces
Returns
true
, if the line ends withtext
, otherwisefalse
. The argumentskipWhiteSpaces
controls whether trailing whitespaces are ignored.bool document.setText(
String
);text
Sets the entire document text.
bool document.clear();
Removes the entire text in the document.
bool document.truncate(
int
,line
int
); bool document.truncate(column
Cursor
);cursor
Truncate the given line at the given column or cursor position. Returns
true
on success, orfalse
if the given line is not part of the document range.bool document.insertText(
int
,line
int
,column
String
); bool document.insertText(text
Cursor
,cursor
String
);text
Inserts the
text
at the given cursor position. Returnstrue
on success, orfalse
, if the document is in read-only mode.bool document.removeText(
int
,fromLine
int
,fromColumn
int
,toLine
int
); bool document.removeText(toColumn
Cursor
,from
Cursor
); bool document.removeText(to
Range
);range
Removes the text in the given range. Returns
true
on success, orfalse
, if the document is in read-only mode.bool document.insertLine(
int
,line
String
);text
Inserts text in the given line. Returns
true
on success, orfalse
, if the document is in read-only mode or the line is not in the document range.bool document.removeLine(
int
);line
Removes the given text line. Returns
true
on success, orfalse
, if the document is in read-only mode or the line is not in the document range.bool document.wrapLine(
int
,line
int
); bool document.wrapLine(column
Cursor
);cursor
Wraps the line at the given cursor position. Returns
true
on success, otherwisefalse
, e.g. if line < 0.Since: KDE 4.9
void document.joinLines(
int
,startLine
int
);endLine
Joins the lines from
startLine
toendLine
. Two succeeding text lines are always separated with a single space.int document.lines();
Returns the number of lines in the document.
bool document.isLineModified(
int
);line
Returns
true
, ifline
currently contains unsaved data.Since: KDE 5.0
bool document.isLineSaved(
int
);line
Returns
true
, ifline
was changed, but the document was saved. Hence, the line currently does not contain any unsaved data.Since: KDE 5.0
bool document.isLineTouched(
int
);line
Returns
true
, ifline
currently contains unsaved data or was changed before.Since: KDE 5.0
bool document.findTouchedLine(
int
,startLine
bool
);down
Search for the next touched line starting at
line
. The search is performed either upwards or downwards depending on the search direction specified indown
.Since: KDE 5.0
int document.length();
Returns the number of characters in the document.
int document.lineLength(
int
);line
Returns the
line
's length.void document.editBegin();
Starts an edit group for undo/redo grouping. Make sure to always call
editEnd()
as often as you calleditBegin()
. CallingeditBegin()
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 ofeditBegin()
) 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
); int document.prevNonSpaceColumn(column
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
); int document.nextNonSpaceColumn(column
Cursor
);cursor
Returns the column with a non-whitespace character starting at the given cursor position and searching forwards.
int document.prevNonEmptyLine(
int
);line
Returns the next non-empty line containing non-whitespace characters searching backwards.
int document.nextNonEmptyLine(
int
);line
Returns the next non-empty line containing non-whitespace characters searching forwards.
bool document.isInWord(
String
,character
int
);attribute
Returns
true
, if the givencharacter
with the givenattribute
can be part of a word, otherwisefalse
.bool document.canBreakAt(
String
,character
int
);attribute
Returns
true
, if the givencharacter
with the givenattribute
is suited to wrap a line, otherwisefalse
.bool document.canComment(
int
,startAttribute
int
);endAttribute
Returns
true
, if a range starting and ending with the given attributes is suited to be commented out, otherwisefalse
.String document.commentMarker(
int
);attribute
Returns the comment marker for single line comments for a given
attribute
.String document.commentStart(
int
);attribute
Returns the comment marker for the start of multi-line comments for a given
attribute
.String document.commentEnd(
int
);attribute
Returns the comment marker for the end of multi-line comments for a given
attribute
.Range document.documentRange();
Returns a range that encompasses the whole document.
Cursor documentEnd();
Returns a cursor positioned at the last column of the last line in the document.
bool isValidTextPosition(
int
,line
int
); bool isValidTextPosition(column
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
); int document.attribute(column
Cursor
);cursor
Returns the attribute at the given cursor position.
bool document.isAttribute(
int
,line
int
,column
int
); bool document.isAttribute(attribute
Cursor
,cursor
int
);attribute
Returns
true
, if the attribute at the given cursor position equalsattribute
, otherwisefalse
.String document.attributeName(
int
,line
int
); String document.attributeName(column
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
); bool document.isAttributeName(name
Cursor
,cursor
String
);name
Returns
true
, if the attribute name at a certain cursor position matches the givenname
, otherwisefalse
.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
); int document.toVirtualColumn(column
Cursor
); Cursor document.toVirtualCursor(cursor
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
); int document.fromVirtualColumn(virtualColumn
Cursor
); Cursor document.fromVirtualCursor(virtualCursor
Cursor
);virtualCursor
Converts the given virtual cursor position to a “real” cursor position, either returning an int or a Cursor object.
Cursor document.anchor(
int
,line
int
,column
Char
); Cursor document.anchor(character
Cursor
,cursor
Char
);character
Searches backward for the given character starting from the given cursor. As an example, if '(' is passed as character, this function will return the position of the opening '('. This reference counting, i.e. other '(...)' are ignored.
Cursor document.rfind(
int
,line
int
,column
String
,text
int
); Cursor document.rfind(attribute
= -1Cursor
,cursor
String
,text
int
);attribute
= -1Find searching backwards the given text with the appropriate
attribute
. The argumentattribute
is ignored if it is set to-1
. The returned cursor is invalid, if the text could not be found.int document.defStyleNum(
int
,line
int
); int document.defStyleNum(column
Cursor
);cursor
Returns the default style used at the given cursor position.
bool document.isCode(
int
,line
int
); bool document.isCode(column
Cursor
);cursor
Returns
true
, if the attribute at the given cursor position is not equal to all of the following styles:dsComment
,dsString
,dsRegionMarker
,dsChar
,dsOthers
.bool document.isComment(
int
,line
int
); bool document.isComment(column
Cursor
);cursor
Returns
true
, if the attribute of the character at the cursor position isdsComment
, otherwisefalse
.bool document.isString(
int
,line
int
); bool document.isString(column
Cursor
);cursor
Returns
true
, if the attribute of the character at the cursor position isdsString
, otherwisefalse
.bool document.isRegionMarker(
int
,line
int
); bool document.isRegionMarker(column
Cursor
);cursor
Returns
true
, if the attribute of the character at the cursor position isdsRegionMarker
, otherwisefalse
.bool document.isChar(
int
,line
int
); bool document.isChar(column
Cursor
);cursor
Returns
true
, if the attribute of the character at the cursor position isdsChar
, otherwisefalse
.bool document.isOthers(
int
,line
int
); bool document.isOthers(column
Cursor
);cursor
Returns
true
, if the attribute of the character at the cursor position isdsOthers
, otherwisefalse
.void document.indent(
Range
,range
int
);change
Indents all lines in
range
bychange
tabs orchange
timestabSize
spaces depending on the users preferences. Thechange
parameter can be negative.
In addition to the document and view API, there is a general editor API that provides functions for general editor scripting functionality.
String editor.clipboardText();
Returns the text that currently is in the global clipboard.
Since: KDE Frameworks™ 5.50
String editor.clipboardHistory();
The editor holds a clipboard history that contains up to 10 clipboard entries. This function returns all entries that currently are in the clipboard history.
Since: KDE Frameworks™ 5.50
void editor.setClipboardText(
String
);text
Set the contents of the clipboard to
text
. Thetext
will be added to the clipboard history.Since: KDE Frameworks™ 5.50