Module Stk.TextbufferSource
Text buffer sharable by several views.
A textbuffer holds a (Utf-8) string and associated tags in a Rope.t. The buffer also maintains cursors, updating their position when text is modified.
A widget wanting to display the buffer contents must register by providing functions to call when buffer is modified (text or tags).
A history is kept for undoing/redoing actions.
Create a new text buffer. Options arguments:
source_langage: sets a source language by name.word_char_re: regular expression of word characters. Default is default_word_char_re.
lines t returns an array with the range of each line of t.
line_count t returns the number of lines in t.
size t returns the number of utf8 characters in t.
Get the modified flag of t. This flag is set to true when t is modified. The history keeps track of this flag, so undoing or redoing can change this flag.
Sourceval set_modified : t -> bool -> unit set_modified t b sets modified flag of t to b. A typical use is to set the flag to false after saving the contents of buffer to a file.
set_map_in t f sets a function (or removes it) used to map inserted contents. When such a function is set, it is applied on strings passed to insertion functions (insert, insert_at_cursor, set_text, ...
set_map_out t f sets a function (or removes it) used to map retrieved contents. When such a function is set, it is applied when retrieving contents from the buffer (to_string, chars, ...).
Pretty-print contents of text buffer.
Logging
This module has its own Logs source "stk.textbuffer". See Log.
Positions and ranges
All position and offset are 0-based and expressed in UTF8 characters (not bytes).
Sourcetype line_offset = {line : int;bol : int;offset of beginning of line
offset : int;offset from the beginning of line
} A line_offset defines a line number and a character position on this line.
order_line_offsets lo1 lo2 returns (lo1, lo2) if lo1 is before or equal to lo2, else returns (lo2, lo1).
Range accross lines, defined by two line_offset positions.
line_range ~start ~stop creates a line_range from start to stop characters.
Pretty-prints a line range.
range_of_line_range lr converts lr to a Rope.range.
Sourceval line_of_offset : t -> int -> int line_of_offset t o returns the line number of t corresponding to offset o.
Sourceval line_char_of_offset : t -> int -> int * int line_char_of_offset t o returns the pair (line, char) corresponding to offset o.
Sourceval offset_of_line_char : t -> line:int -> char:int -> int offset_of_line_char t ~line ~char returns the absolute offset in t corresponding to line and character char.
line_offset_of_offset t o returns a line_offset position from the given absolute offset o.
line_ranges_of_ranges t ranges returns a list of line_range from a list of Rope.range. Returnd ranges are merged when possible.
Cursors
A text buffer can handle several cursors. Some cursors can be associated to a widget. In this case, they will be removed when the widget is unregistered, and changes of these cursors will be signaled only to this widget.
Cursor positions are automatically updated after each change (insertion, deletion) in the buffer.
Sourcetype cursor_gravity = [ | `Left| `Right
] Cursor gravity defines how a cursor moves when text is inserted at its position. `Left gravity means that the cursor stays at the same position, `Right means that the cursor moves to the right of the inserted text, as when user types text.
Pretty-prints a cursor (with its position).
Pretty-prints a cursor id.
A map with cursors as keys. This is useful for views on a text buffer (like Textview.textview widget).
create_cursor t creates a new cursor in buffer. Optional arguments are:
widget: indicates a widget id. Changes regarding this cursor will be signaled only to this widget.gravity: the gravity of the cursor. Default is `Right.line, char and offset: indicate position of cursor. If offset is given, it is used. Else line and char are used, with default value 0 for each.
Get last used cursor field of textbuffer. This can be useful for editors handling several cursors.
Set last used cursor for textbuffer.
remove_cursor t c removes cursor c from t. This cursor cannot be used anymore.
create_insert_cursor t creates a new cursor in t. If last_used_cursor of t is not None and is a valid cursor, then this cursor is duplicated, setting gravity of new cursor to `Right. Else a new cursor is created at offset 0. Optonal argument widget indicates a widget id. Changes regarding this cursor will be signaled only to this widget.
dup_cursor t c returns a new cursor, if c is a valid cursor. The new cursor is at the same position than c. The cursor can be associated to widget id, if provided. gravity argument allows to force the gravity of new cursor rather than having the same as c.
cursor_offset t c returns absolute offset of c, or 0 if cursor is invalid.
cursor_line_offset t c returns position of c as a line_offset.
Moving cursors
All these functions returns the new absolute offset of the cursor, or None if the cursor is invalid.
Sourceval move_cursor :
t ->
?line:int ->
?char:int ->
?offset:int ->
cursor ->
int option move_cursor t c moves the cursor c according to optional parameters:
offset: move cursor to absolute offset.line and char: if offset is not provided, then move cursor to the given line (default is 0) and char (default is 0).
move_cursor_to_cursor t ~src ~dst moves cursor src at the same position as dst. Returns the new absolute offset of the cursor, or None if src or dst is invalid.
Moves cursor to the start of the current line of the cursor.
Moves cursor to the end of the current line of the cursor.
line_forward_cursor t c n moves cursor n lines forward. If possible, keep the same char position on line.
Sourceval line_backward_cursor : t -> cursor -> int -> int option forward_cursor t c n moves c n characters forward.
backward_cursor t c n moves c n characters backward.
forward_cursor_to_word_end t c moves c to the next word end.
Sourceval backward_cursor_to_word_start : t -> cursor -> int option backward_cursor_to_word_start t c moves c to the previous word start.
Regions
create_region ~start ~stop t creates a new region defined by two cursors at the given absolute positions. start_gravity (default is `Left) and stop_gravity (default is `Right) can be used to specify different gravities for cursors.
Removes the given region from t.
Events
Textbuffer events:
Delete (range, string): A range was deleted which corresponds to string.Insert_text (range, string): A string was inserted, now at range.Cursor_moved (c, line_offset): The given cursor moved to a new position.Modified_changed bool: The modified status changed to the given boolean.Source_language_changed lang: the source language changed to Some name, or None for no language.
connect t ev cb connects callback cb to be called when event ev is triggered by t.
disconnect t id disconnect callback with id from t.
Retrieving contents
Sourceval to_string : ?start:int -> ?size:int -> t -> string to_string t returns contents of t as a string. Optional arguments:
start indicates a start position (default is 0).size indicates a size of string to get (default is to get the contents of t until the end, i.e. size t - start.
Same as to_string but returns a list of characters with their associated tags: for each character, a pair (tag set, language tag option). Argument map_out indicates if the characters must be mapped using buffer'a map_out function if present.
get_line t n returns the range of line n in t.
Same as chars but returns chars for the given line.
Sourceval line_to_string : t -> int -> string line_to_string t n returns line n as a string.
Inserting and deleting
insert_at_cursor t c s inserts the string s at the position of the cursor c. Optional arguments:
readonly: a function taking tags of characters before and after the insertion position and returning whether can be inserted here. Can be used to define readonly ranges in a buffer based on tags.tags: tags associated to each inserted character.
insert t offset s is the same as insert_at_cursor but inserts at the given offset.
Sourceval set_text : t -> string -> unit set_text t s replaces contents of buffer with string s.
delete t deletes contents from t. Options arguments:
readonly: a function taking the tags of a character and returning whether this character can be deleted. Only characters which are not readonly will be deleted.start: start of range to delete. Default is 0.size: size of range to delete. Default is size t - start.
The function returns the deleted text.
Tags are associated to each character of the underlying rope used by a text buffer. The text buffer does not associate properties to these tags. The theme of the Textview displaying a text buffer associated properties (color, font, ...) to tags.
add_tag t tag () adds the given tag to contents of t. Optional arguments:
start: indicates the start of range to tag (default is 0).size: indicate the size of range to tag (default is size t - start).
remove_tag t tag () removes the given tag from contents of t. Optional arguments:
start: indicates the start of range to tag (default is 0).size: indicate the size of range to tag (default is size t - start).
Source language
A text buffer can be given a source language, using its name. The name if used as key for language lexers registered in Higlo library. Higlo lexeme are mapped to Texttag.Lang tags. These tags are given properties in the Texttag.Theme used in Textview, so that each lexeme can be styled.
Sourceval source_language : t -> string option Returns the source language of the buffer, if any.
Sourceval set_source_language : t -> string option -> unit Sets the source language of the buffer, or None for no language.
Word characters
A regular expression is used in buffer to determine what is a Word character. This is used by functions acting on words in the buffer.
A word is defined by default by this regular PCRE expression: "(*UCP)\\w", with iflags contains `UTF8, as buffer contains UTF8 strings.
Sets the word character regular expression of t.
Sourceval set_word_char_re_string : t -> string -> unit Same as set_word_char_re but regular expression is given as string. It is compiled to a regular expression with the `UTF8 flag.
History
A textbuffer keeps tracks of changes in a history, allowing undo/redo.
Sourceval max_undo_levels : t -> int Sourceval set_max_undo_levels : t -> int -> unit set_max_undo_levels t n sets max undo levels of t to n.
Sourceval reset_history : t -> unit reset_history t clears the list of stored changes in t.
Begins a action. An action will be considered will be considered as one change (as a list of changes) in the history.
Ends the current action. All the changes of the action are added to the history as a single change.
Interfacing with views
register_widget t ~widget ~on_change ~on_cursor_change ~on_tag_change registers the given widget as view on t. on_change will be called when contents of the textbuffer changes. on_cursor_change will be called when a cursor not associated to a widget or associated to this widget moves. on_tag_change will be called when tags of a part of the contents changed. If the widget is already registered, then returns None, else returns Some c where c is a new cursor created with create_insert_cursor ~widget t.
unregister_widget t widget unregisters the given widget from the views of t. Cursor of this widget are removed (and not usable any more).