Grapheme-aware text editing buffer with cursor, selection, and undo.
Grapheme-aware text editing buffer.
A mutable buffer that tracks cursor position, selection, and undo history in terms of Unicode grapheme clusters. Content may contain newlines for multi-line editing.
create ?max_length ?width_method ?tab_width initial is a buffer whose initial content is initial, with cursor at the end.
max_length is the maximum number of grapheme clusters the buffer may hold. initial is truncated to max_length grapheme clusters if it exceeds that limit. max_length defaults to 1000 and is clamped to >= 0.
width_method controls how grapheme display widths are computed. Defaults to `Unicode.
tab_width is the display width of tab characters. Defaults to 2 and is clamped to >= 1.
s is truncated to max_lengtht grapheme clusters if it exceeds that limit. The cursor moves to the end and any active selection is cleared. No undo point is saved.
set_cursor t pos moves the cursor to pos, clamped to [0;length t]. Clears any active selection.
Sourceval set_cursor_offset : ?select:bool ->t->int -> unit
set_cursor_offset ?select t pos moves the cursor to pos, clamped to [0;length t].
When select is true, extends or creates a selection anchored at the previous cursor position. When select is false (default), any active selection is cleared.
insert t s is true iff inserting s changed the text of t.
If a selection is active it is deleted first. Then s is inserted at the cursor position. The result is truncated to max_lengtht grapheme clusters. An undo point is saved before any modification.
delete_backward t is true iff a deletion changed the text of t.
If a selection is active the selected text is deleted. Otherwise the grapheme cluster immediately before the cursor (Backspace behaviour) is deleted. An undo point is saved before any modification.
delete_forward t is true iff a deletion changed the text of t.
If a selection is active the selected text is deleted. Otherwise the grapheme cluster immediately after the cursor (Delete behaviour) is deleted. An undo point is saved before any modification.
delete_to_line_end t is true iff a deletion changed the text of t.
Deletes from the cursor to the end of the current logical line (before any newline). Returns false when the cursor is already at the line end. An undo point is saved before any modification.
delete_line t is true iff a deletion changed the text of t.
Deletes the current logical line including its trailing line separator. An undo point is saved before any modification.
Cursor movement
Movement operations return true iff the cursor position changed.
When select is true the operation extends or creates a selection. When select is false (default) any active selection is cleared. If a selection was active and select is false, the cursor moves to the selection edge in the direction of movement.
set_max_length t n sets the maximum grapheme cluster count of t to n, clamped to >= 0. If the current content exceeds n grapheme clusters it is truncated to fit.
line_end t line is the grapheme offset past the last character on line, before the newline (or at buffer end for the last line). line is clamped to [0;line_count t - 1].
byte_offset t ~grapheme is the byte offset of grapheme cluster grapheme in the buffer's content string. Returns String.length (text t) when grapheme >= length t.