Previous: DocumentView object, Up: Scripting


5.5 Document object

Document.views()
returns list of views which display this document.
Document.active_view()
returns active view of this document. If the document has a single view, then that is returned; otherwise if the current active view belongs to this document, then that view is returned; otherwise a random view is picked.
Document.filename()
returns full file path of the document or null if the document has not been saved yet or if it can't be represented with a local path (e.g. if it is in a remote location like a web site).
Document.uri()
returns URI of the document or null if the document has not been saved yet.
Document.basename()
returns basename of the document, that is the full path minus directory part. If the document has not been saved yet, then it returns the name shown in the titlebar, e.g. "Untitled".
Document.encoding()
returns character encoding of the document.
Document.set_encoding(encoding)
set character encoding of the document, it will be used when the document is saved.
Document.reload()
reload the document.
Document.save()
save the document.
Document.save_as(filename=null)
save the document as filename. If filename is null then Save As will be shown to choose new filename.
Document.can_undo()
returns whether undo action is available.
Document.can_redo()
returns whether redo action is available.
Document.undo()
undo.
Document.redo()
redo.
Document.begin_not_undoable_action()
mark the beginning of a non-undoable operation. Undo stack will be erased and undo will not be recorded until end_not_undoable_action() call.
Document.end_not_undoable_action()
end the non-undoable operation started with begin_not_undoable_action().
Document.start_pos()
position at the beginning of the document (0 in Python, 1 in Lua, etc.)
Document.end_pos()
position at the end of the document. This is the position past the last character: it points to no character, but it is a valid position for text insertion, cursor may be put there, etc.
Document.cursor_pos()
position at the cursor.
Document.set_cursor_pos(pos)
move cursor to position pos.
Document.selection()
returns selection bounds as a list of two items, start and end. Returned list is always sorted, use cursor() and selection_bound() if you need to distinguish beginning and end of selection. If no text is is selected, then it returns pair [cursor, cursor].
Document.set_selection(bounds_as_list)
Document.set_selection(start, end)
select text.
Document.selection_bound()
returns the selection bound other than cursor position. Selection is either [cursor, selection_bound) or [selection_bound, cursor), depending on direction user dragged the mouse (or on set_selection arguments).
Document.has_selection()
whether any text is selected.
Document.char_count()
character count.
Document.line_count()
line count.
Document.line_at_pos(pos)
returns index of the line which contains position pos.
Document.pos_at_line(line)
returns position at the beginning of line line.
Document.pos_at_line(line)
returns position at the end of line line.
Document.char_at_pos(pos)
returns character at position pos as string.
Document.text()
returns whole document contents.
Document.text(start, end)
returns text in the range [start, end), end not included. Example: doc.text(doc.start_pos(), doc.end_pos()) is equivalent to doc.text().
Document.insert_text(text)
Document.insert_text(pos, text)
insert text into the document. If pos is not given, insert at cursor position.
Document.replace_text(start, end, text)
replace text in the region [start, end). Equivalent to delete_text(start, end), insert_text(start, text).
Document.delete_text(start, end)
delete text in the region [start, end). Example: doc.delete_text(doc.start(), doc.end()) will delete all text in doc.
Document.append_text(text)
append text. Equivalent to doc.insert_text(doc.end(), text).
Document.clear()
delete all text in the document.
Document.copy()
copy selected text to clipboard. If no text is selected then nothing will happen, same as Ctrl-C key combination.
Document.cut()
cut selected text to clipboard. If no text is selected then nothing will happen, same as Ctrl-X key combination.
Document.paste()
paste text from clipboard. It has the same effect as Ctrl-V key combination: nothing happens if clipboard is empty, and selected text is replaced with clipboard contents otherwise.
Document.select_text(bounds_as_list)
Document.select_text(start, end)
select text, same as set_selection().
Document.select_lines(line)
select a line.
Document.select_lines(first, last)
select lines from first to last, including last.
Document.select_lines_at_pos(bounds_as_list)
Document.select_lines_at_pos(start, end)
select lines: similar to select_text, but select whole lines.
Document.select_all()
select all.
Document.selected_text()
returns selected text.
Document.selected_lines()
returns selected lines as a list of strings, one string for each line, line terminator characters not included. If nothing is selected, then line at cursor is returned.
Document.delete_selected_text()
delete selected text, equivalent to doc.delete_text(doc.cursor(), doc.selection_bound()).
Document.delete_selected_lines()
delete selected lines. Similar to delete_selected_text() but selection is extended to include whole lines. If nothing is selected, then line at cursor is deleted.
Document.replace_selected_text(text)
replace selected text with text. If nothing is selected, text is inserted at cursor.
Document.replace_selected_lines(text)
replace selected lines with text. Similar to replace_selected_text(), but selection is extended to include whole lines. If nothing is selected, then line at cursor is replaced.