The scripting API presented here is available in all scripts. Before the contents of a script is loaded, Kile first adds several prototypes and functions to the scripting context. This convenience API contains prototypes like text cursors and text ranges and is located in the folder KILE_APP_DIR/script-plugins/.
Kile scripts differ slightly from Kate scripts, which use another design, as they also can be started from the command-line. But all functions of the Kate scripting API are also available in Kile's scripting API, so porting JavaScript code from Kate to Kile should be very easy. But as Kile is a very rich featured LATEX editor, its own scripting API offers much more possibilities than Kate's one.
Remark: Description of API calls, which are also available in Kate scripting, have been taken from Kate documentation.
This section lists global functions.
void debug(String);textPrints
texttostdoutin the console. The printed text is colored to distance it from other debug output.
As Kile 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.
Cursor();
Constructor: Returns a Cursor at position
(0,0).Example:
var cursor = new Cursor();
Cursor(
int,lineint);columnConstructor: Returns a Cursor at position (line,column).
Example:
var cursor = new Cursor(3,42);
Cursor(Cursor);otherCopy 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();
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 an new invalid cursor located at
(-1,-1).Example:
var invalidCursor = cursor.invalid();
int Cursor.compareTo(Cursor);otherCompares 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);otherReturns
true, if this cursor and the cursorotherare equal, otherwisefalse.
String Cursor.toString();
Returns the cursor as a string of the form
Cursor(line,column).
As Kile is a text editor, all the scripting API is based on cursors and ranges whenever possible. As 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.
Range();
Constructor: Calling
new Range()returns a Range at(0,0) - (0,0).
Range(
Cursor,startCursor);endConstructor: Calling
new Range(returns the range from cursorstart,end)startto cursorend.
Range(
int,startLineint,startColumnint,endLineint);endColumnConstructor: Calling
new Range(returns the Range from (startLine,startColumn,endLine,endColumn)startLine,startColumn) to (endLine,endColumn).
Range(Range);otherCopy constructor: Returns a copy of Range
other.
Range Range.clone();
Returns a clone of the range.
Example:
var clone = range.clone();
bool Range.isValid();
Returns
true, if both start and end cursor are valid, otherwisefalse.Example:
var valid = range.isValid();
bool Range.invalid();
Returns the Range from (-1,-1) to (-1,-1).
bool Range.contains(Cursor);cursorReturns
true, if this range contains the cursor position, otherwisefalse.
bool Range.contains(Range);otherReturns
true, if this range contains the Rangeother, otherwisefalse.
bool Range.containsColumn(int);columnReturns
true, ifcolumnis in the half open interval[start.column, end.column), otherwisefalse.
bool Range.containsLine(int);lineReturns
true, iflineis in the half open interval[start.line, end.line), otherwisefalse.
bool Range.overlaps(Range);otherReturns
true, if this range and the rangeothershare a common region, otherwisefalse.
bool Range.overlapsLine(int);lineReturns
true, iflineis in the interval[start.line, end.line], otherwisefalse.
bool Range.overlapsColumn(int);columnReturns
true, ifcolumnis in the interval[start.column, end.column], otherwisefalse.
bool Range.equals(Range);otherReturns
true, if this range and the Rangeotherare equal, otherwisefalse.
String Range.toString();
Returns the range as a string of the form
Range(Cursor(line,column) - Cursor(line,column)).
Whenever a script is being executed, there is a global object (variable) view representing the current active editor view. All functions of view work with cursor positions or selected text. The following is a list of all available view functions.
void view.backspace();
Programmatically Performs the equivalent of pressing the backspace key.
Cursor view.cursorPosition();
Returns the current cursor position in the view.
void view.setCursorPosition(
int,lineint); void view.setCursorPosition(columnCursor);cursorSet the current cursor position to either
line,columnor to the givencursor.
void view.cursorLeft();
Moves the cursor one position backward in the text.
void view.cursorRight();
Moves the cursor one position forward in the text.
void view.cursorUp();
Moves the cursor one line up in the document.
void view.cursorDown();
Moves the cursor one line down in the document.
int view.cursorLine();
Returns the line which the cursor is currently located at.
int view.cursorColumn();
Returns the column which the cursor is currently located at.
void view.setCursorLine(int);lineSet the cursor line to the given
line.
void view.setCursorColumn(int);columnSet the cursor column to the given
column.
Cursor view.virtualCursorPosition();
Get the current virtual cursor position. Virtual means the tabulator character (TAB) counts multiple characters, as configured by the user (e.g. one TAB is 8 spaces). The virtual cursor position provides access to the user visible values of the current cursor position.
bool view.hasSelection();
Returns
true, if the view has selected text, otherwisefalse.
String view.selectedText();
Returns the selected text. If no text is selected, the returned string is empty.
Range view.selectionRange();
Returns the selected text range. The returned range is invalid if there is no selected text.
void view.setSelection(Range);rangeSet the selected text to the given
range.
void view.selectAll();
Selects the entire text in the document.
void view.clearSelection();
Clears the text selection without removing the text.
void view.removeSelectedText();
Remove the selected text. If the view does not have any selected text, this does nothing.
void view.selectLine();
Selects the text in the current line.
void view.selectLine(int);lineSelects the text in the given
line.
void view.selectLines(
int,fromint);toSelects the entire text from line
fromto lineto.
void view.selectWord();
Selects the current word. If no word was found at the current cursor position, nothing is done.
void view.selectLatexCommand();
Selects the current LATEX command. If no command was found at the current cursor position, nothing is done.
void view.selectEnvironment(bool);inside = falseSelects the entire text of the current LATEX environment. If
insideisfalse, the environment text including the surrounding LATEX tags\begin{...}...\end{...}will be selected, else without these tags. If no parameter is given,insideis set tofalse.
void view.selectTexgroup(bool);inside = trueSelects the text of the current LATEX group. If
insideistrue, only the texgroup without the surrounding braces will be selected. If no parameter is given,insideis set totrue.
void view.selectMathgroup();
Selects the text of the current math group.
void view.selectParagraph(bool wholeLines = true);
Selects the entire text of the current LATEX paragraph. If
wholeLinesistrue, the first and the last lines of the paragraph will be included in the selection entirely (including the end-of-line character); otherwise, the selection will only contain non-whitespace characters.
Whenever a script is being executed, there is a global object (variable) document representing the current active document. The following is a list of all available document functions.
void document.insertText(String);textInserts the
textat the current cursor position.
void document.insertText(
int,lineint,columnString); void document.insertText(textCursor,cursorString);textInserts the
textat the given cursor position.
bool document.removeText(
int,fromLineint,fromColumnint,toLineint); bool document.removeText(toColumnCursor,fromCursor); bool document.removeText(toRange);rangeRemoves the text in the given range. Returns
trueon success, orfalse, if the document is in read-only mode.
bool document.replaceText(
Range,rangeString);textReplace the text of the given range with the specified text.
int document.lines();
Returns the amount of lines in the document.
int document.length();
Returns the number of characters in the document.
Range document.documentRange();
Returns a range which encompasses the whole document.
Cursor document.documentEnd();
Returns the current cursor position of the document's end.
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,fromLineint,fromColumnint,toLineint); String document.text(toColumnCursor,fromCursor); String document.text(toRange);rangeReturns the text in the given range. It is recommended to use the cursor and range based version for better readability of the source code.
bool document.setText(String);textSets the entire document text.
bool document.clear();
Removes the entire text in the document.
String document.line();
Returns the current text line as string.
String document.line(int);lineReturns the given text line as string. The string is empty if the requested line is out of range.
int document.lineLength();
Returns the length of the current line.
int document.lineLength(int);lineReturns the
line's length.
bool document.insertLine(String);sInserts text in the current line. Returns
trueon success, orfalse, if the document is in read-only mode or the line is not in the document range.
bool document.insertLine(
int,lineString);sInserts text in the given line. Returns
trueon success, orfalse, if the document is in read-only mode or the line is not in the document range.
bool document.removeLine();
Removes the current text line. Returns
trueon success, orfalse, if the document is in read-only mode.
bool document.removeLine(int);lineRemoves the given text line. Returns
trueon success, orfalse, if the document is in read-only mode or the line is not in the document range.
bool document.replaceLine(String);textReplace the text of the current line with the specified text.
bool document.replaceLine(
int,lineString);textReplace the text of the given line with the specified text.
bool document.truncateLine();
Truncate the current line at the given column or cursor position. Returns
trueon success, orfalseif the given line is not part of the document range.
bool document.truncate(
int,lineint); bool document.truncate(columnCursor);cursorTruncate the given line at the given column or cursor position. Returns
trueon success, orfalseif the given line is not part of the document range.
String document.word();
Returns the word at the current cursor position. If no word at this cursor position is found, the returned string is empty.
String document.wordAt(
int,lineint); String document.wordAt(columnCursor);cursorReturns the word at the given cursor position. If no word at this cursor position is found, the returned string is empty.
Range document.wordRange();
Returns the range of the word at the given cursor position. If no word was found,
Range.invalid()is returned, which can be tested withRange.isValid().
String document.latexCommand();
Returns the LATEX command at the current cursor position. If no command at this cursor position is found, the returned string is empty.
String document.latexCommandAt(
int,lineint); String document.latexCommandAt(columnCursor);cursorReturns the LATEX command at the given cursor position. If no command at this cursor position is found, the returned string is empty.
Range document.latexCommandRange();
Returns the range of the LATEX command at the given cursor position. If no LATEX command was found,
Range.invalid()is returned, which can be tested withRange.isValid().
String document.charAt(
int,lineint); String document.charAt(columnCursor);cursorReturns the character at the given cursor position.
String document.firstChar(int);lineReturns the first character in the given
linethat 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);lineReturns the last character in the given
linethat is not a whitespace. If the line is empty or only contains whitespace characters, the returned string is empty.
bool document.isSpace(
int,lineint); bool document.isSpace(columnCursor);cursorReturns
true, if the character at the given cursor position is a whitespace, otherwisefalse.
void document.insertBullet();
Insert a Kile bullet. Remember that you can easily jump to the next or previous bullet. This will also highlight this bullet so that it will be deleted automatically, when you enter your first letter.
void document.nextBullet();
Jump to the next bullet in the text if there is one.
void document.previousBullet();
Jump to the previous bullet in the text if there is one.
bool document.hasEnvironment();
Returns
trueif a surrounding LATEX environment was found, elsefalse.
String document.environment(bool);inside = falseReturns the entire text of the surrounding LATEX environment. If
insideisfalse, the environment text including the surrounding LATEX tags\begin{...}...\end{...}will be returned, else without these tags. If no parameter is given,insideis set tofalse. If no environment was found, the returned string is empty.
Range document.environmentRange(bool);inside = falseReturns the range of the surrounding LATEX environment. If
insideisfalse, the range including the surrounding LATEX tags\begin{...}...\end{...}will be returned, else without these tags. If no parameter is given,insideis set tofalse. If no environment was found,Range.invalid()is returned, which can be tested withRange.isValid().
String document.environmentName();
Returns the name of the surrounding LATEX environment or an empty string.
void document.removeEnvironment(bool);inside = falseRemoves the text of the surrounding LATEX environment. If
insideisfalse, the environment text including the surrounding LATEX tags\begin{...}...\end{...}will be removed, else without these tags. If no parameter is given,insideis set tofalse.
void document.closeEnvironment();
Insert a closing environment tag, if an opened LATEX environment was found at the current cursor position.
void document.closeAllEnvironments();
Insert closing environment tags for all opened LATEX environments, which were found at the current cursor position.
bool document.hasTexgroup();
Returns
trueif a surrounding LATEX group was found at the current cursor position, elsefalse.
String document.texgroup(bool);inside = trueReturns the text of the surrounding LATEX group. If
insideisfalse, the text of this LATEX group including the surrounding braces{...}will be returned, else without them. If no parameter is given,insideis set tofalse. The returned string is empty, if no surrounding LATEX group was found at the current cursor position.
Range document.texgroupRange(bool);inside = trueReturns the range of the surrounding LATEX group. If
insideisfalse, the range including the surrounding braces{...}will be returned, else without them. If no parameter is given,insideis set tofalse. If no group was found,Range.invalid()is returned, which can be tested withRange.isValid().
void document.removeTexgroup(bool);inside = trueRemoves the text of the surrounding LATEX group. If
insideisfalse, the text of this LATEX group including the surrounding braces{...}will be removed, else without them. If no parameter is given,insideis set tofalse.
bool document.hasMathgroup();
Returns
trueif a surrounding LATEX mathgroup was found at the current cursor position, elsefalse.
String document.mathgroup();
Returns the text of the surrounding LATEX mathgroup. The returned string is empty, if no surrounding LATEX mathgroup was found at the current cursor position.
Range document.mathgroupRange();
Returns the range of the surrounding LATEX mathgroup. If no group was mathgroup,
Range.invalid()is returned, which can be tested withRange.isValid().
void document.removeMathgroup();
Removes the text of the surrounding LATEX mathgroup.
String document.paragraph();
Returns the text of the current LATEX paragraph.
Range document.paragraphRange();
Returns the range of the surrounding LATEX paragraph.
void document.removeParagraph();
Removes the text of the current LATEX paragraph.
bool document.matchesAt(
int,lineint,columnString); bool document.matchesAt(textCursor,cursorString);textReturns
true, if the giventextmatches at the corresponding cursor position, otherwisefalse.
bool document.startsWith(
int,lineString,patternbool);skipWhiteSpaces = trueReturns
true, if the line starts withpattern, otherwisefalse. The argumentskipWhiteSpacescontrols whether leading whitespaces are ignored.
bool document.endsWith(
int,lineString,patternbool);skipWhiteSpaces = trueReturns
true, if the line ends withpattern, otherwisefalse. The argumentskipWhiteSpacescontrols whether trailing whitespaces are ignored.
int document.firstColumn(int);lineReturns 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);lineReturns 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,lineint); int document.prevNonSpaceColumn(columnCursor);cursorReturns the column with a non-whitespace characters starting at the given cursor position and searching backwards.
int document.nextNonSpaceColumn(
int,lineint); int document.nextNonSpaceColumn(columnCursor);cursorReturns the column with a non-whitespace characters starting at the given cursor position and searching forwards.
int document.prevNonEmptyLine(int);lineReturns the next non-empty line containing non-whitespace characters searching backwards.
int document.nextNonEmptyLine(int);lineReturns the next non-empty line containing non-whitespace characters searching forwards.
void document.gotoBeginEnv();
Go to the start of a surrounding LATEX environment.
void document.gotoEndEnv();
Go to the end of a surrounding LATEX environment.
void document.gotoBeginTexgroup();
Go to the start of a surrounding LATEX group.
void document.gotoEndTexgroup();
Go to the end of a surrounding LATEX group.
void document.gotoNextParagraph();
Go to the next LATEX Paragraph.
void document.gotoPrevParagraph();
Go to the previous LATEX Paragraph.
void document.gotoNextSectioning();
Go to the next LATEX section.
void document.gotoPrevSectioning();
Go to the previous LATEX section.
void document.gotoLine(int);lineGo to the given
line.
void document.insertChapter();
Insert a
\chaptercommand (see alsodocument.insertSection()).
void document.insertSection();
Insert a
\sectioncommand. Like choosing the menu entry → → a dialog will appear, where you can choose the title and an optional label for this sectioning command.
void document.insertSubsection();
Insert a
\subsectioncommand (see alsodocument.insertSection()).
void document.insertSubsubsection();
Insert a
\subsubsectioncommand (see alsodocument.insertSection()).
void document.insertParagraph();
Insert a
\paragraphcommand (see alsodocument.insertSection()).
void document.insertSubparagraph();
Insert a
\subparagraphcommand (see alsodocument.insertSection()).
void document.insertLabel();
Insert a
\labelcommand.
void document.insertReference();
Insert a
\refcommand. Like choosing the menu entry → → a dialog will appear, where you can choose from already defined labels, which are listed in a combobox.
void document.insertPageref();
Insert a
\pagerefcommand (see alsodocument.insertReference()).
void document.insertCitation();
Insert a
\citecommand.
void document.insertIndex();
Insert a
\indexcommand.
void document.insertFootnote();
Insert a
\footnotecommand.
void document.comment();
Inserts comment markers to make the selection or current line a comment.
void document.uncomment();
Removes comment markers from the selection or current line.
void document.uppercase();
Put the selected text or the letter after the cursor in uppercase.
void document.lowercase();
Put the selected text or the letter after the cursor in lowercase.
void document.capitalize();
Capitalize the selected text or the current word.
void document.joinLines();
Joins the lines of the current selection. Two succeeding text lines are always separated with a single space.
void document.insertIntelligentNewline();
Insert a smart newline (see Smart Newline).
void document.insertIntelligentTabulator();
Insert a smart tabulator (see Smart Tabulator).
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.
StringList document.labelList();
Returns all defined labels as a
StringList, which can be used in JavaScript as an array of strings.
StringList document.bibitemList();
Returns all defined bibitems as a
StringList, which can be used in JavaScript as an array of strings.
void document.refreshStructure();
Refresh the structure view (see Navigating the LATEX Source).
The global object (variable) kile is used to handle top level interactions with the outside world, input message and dialog interfaces. These API calls are divided into subobjects to structure this part of the scripting API. Conceptually kile is a bit like window in a browser API.
kile.alert: message boxeskile.input: get user inputkile.wizard: call one of Kile's wizardskile.script: get info about a running scriptkile.file: file operations like read and write
void kile.alert.information(
String,textString);captionDisplay an Information dialog.
textis the message string andcaptionthe title of the message box. The default title is the script name.
void kile.alert.sorry(
String,textString);captionDisplay a Sorry dialog.
textis the message string andcaptionthe title of the message box. The default title is the script name.
void kile.alert.error(
String,textString);captionDisplay an Error dialog.
textis the message string andcaptionthe title of the message box. The default title is the script name.
String kile.alert.question(
String,textString);captionDisplay a simple question dialog.
textis the message string andcaptionthe title of the message box. The default title is the script name. The returned string is eitheryesorno.
String kile.alert.warning(
String,textString);captionDisplay a simple warning dialog.
textis the message string andcaptionthe title of the message box. The default title is the script name. The returned string is eithercontinueorcancel.
String kile.input.getListboxItem(
String,captionString,labelStringList);listFunction to let the user select an item from a list, which is shown as listbox.
captionis the text that is displayed in the title bar,labelis the text that appears as the label for the list andlistis the string list which is inserted into the list.
String kile.input.getComboboxItem(
String,captionString,labelStringList);listFunction to let the user select an item from a list, which is shown as combobox.
captionis the text that is displayed in the title bar,labelis the text that appears as the label for the list andlistis the string list which is inserted into the list.
String kile.input.getText(
String,captionString);labelFunction to get a string from the user.
captionis the text that is displayed in the title bar andlabelthe text that appears as a label for the line edit.
String kile.input.getLatexCommand(
String,captionString);labelFunction to get a LATEX command from the user. This means that only lower- and uppercase letters are allowed.
captionis the text that is displayed in the title bar andlabelthe text that appears as a label for the line edit.
int kile.input.getInteger(
String,captionString,labelint,min = INT_MINint);max = INT_MAXFunction to get an integer from the user.
captionis the text that is displayed in the title bar.labelis the text that appears as the label for the spin box.minandmaxare the minimum and maximum allowable values the user may choose. Default values areINT_MINandINT_MAX.
int kile.input.getPosInteger(
String,captionString,labelint,min = 1int);max = INT_MAXFunction to get a positive integer from the user.
captionis the text that is displayed in the title bar.labelis the text that appears as the label for the spin box.minandmaxare the minimum and maximum allowable values the user may choose. Default values are1andINT_MAX.
void kile.wizard.tabular();
Calls the Tabular wizard, which helps to write a tabular environment (see Arrays and tabulars).
void kile.wizard.array();
Calls the Array wizard, which helps to write an array environment (see Arrays and tabulars).
void kile.wizard.tabbing();
Calls the Tabbing wizard, which helps to write a tabbing environment (see Arrays and tabulars).
void kile.wizard.floatEnvironment();
Calls the Floats wizard, which helps to insert floating elements (see Inserting floating elements).
void kile.wizard.mathEnvironment();
Calls the Math wizard, which helps to insert math environments (see Inserting Math environments).
void kile.wizard.postscript();
Calls the Postscript Tools wizard, which may help to manipulate or rearrange Postscript documents (see PostScript® Utilities).
String kile.script.name();
Returns the basename of a running script (without path and extension).
String kile.script.caption();
Returns a string, which can be used as captions of alert boxes. It looks like
Script: scriptname.js.
Object kile.file.read(String);filenameRead the contents of a textfile. It is used like
Example:
var res = kile.file.read("path/to/file.txt");The return value
resis an object (better: a map) with three properties:status: Gives the status code of the operation, which can be 0 (no error), 1 (access failed) or 2 (access denied). So, if no error occurred, the value of
res.statusorres["status"]will be 0.result: Contains the text of the given file.
message: Contains an error message, if an error occurred.
Object kile.file.read();
Like
read(filename), but no filename is given. A dialog will appear to select the file to read.
Object kile.file.write(
String,filenameString);textWrite the given text into a file. It is used like
Example:
var res = kile.file.write("path/to/file.txt","Some text...");The return value
resis an object (better: a map) with two properties:statusandmessage(seeread()for more information).
Object kile.file.write(String);textLike
write(filename,text), but no filename is given. A dialog will appear to choose a filename.
String kile.file.getOpenFileName(
String,startDirString);filterCreates a modal file dialog and return the selected filename or an empty string if none was chosen. Note that with this method the user must select an existing filename.
Parameters:
startDir: Starting directory of the open dialog.
filter: A shell glob or a mime-type-filter that specifies which files to display. Refer to the KFileDialog documentation for more information on this parameter.
Both parameters are optional. If you omit
filter, all files will be displayed. If additionallystartDiris omitted, the dialog will take the current document directory as starting point.
String kile.file.getSaveFileName(
String,startDirString);filterCreates a modal file dialog and returns the selected filename or an empty string if none was chosen. Note that with this method the user need not select an existing filename. See
getOpenFileName()for an explanation of the parameters.