package core:text/edit
Overview
Text edit primitives to use in a text box.
Based off the articles by rxi:
External Examples 1 ¶
The programs in odin-lang/examples about this package, and how many of its declarations each uses.
Index
Constants (3)
Variables (0)
This section is empty.
Procedures (38)
- begin
- clear_all
- copy
- current_selected_text
- cut
- delete_to
- destroy
- end
- has_selection
- init
input_… (3)
- insert
- is_continuation_byte
- move_to
- new_line
- paste
- perform_command
- remove
select_… (5)
- selection_delete
- setup_once
- sorted_selection
- translate_position
- undo
- update_time
- word_class_ascii
- word_class_unicode
Procedure Groups (0)
This section is empty.
Types
Command ¶
Command :: enum u32 { None, Undo, Redo, New_Line, // multi-lines Cut, Copy, Paste, Select_All, Select_Word, Select_Line, Select_Hard_Line, Backspace, Delete, Delete_Word_Left, Delete_Word_Right, Left, Right, Up, // multi-lines Down, // multi-lines Page_Up, // multi-lines Page_Down, // multi-lines Word_Left, Word_Right, Start, End, Line_Start, Line_End, Hard_Line_Start, Hard_Line_End, Smart_Line_Start, Smart_Line_End, Select_Left, Select_Right, Select_Up, // multi-lines Select_Down, // multi-lines Select_Page_Up, // multi-lines Select_Page_Down, // multi-lines Select_Word_Left, Select_Word_Right, Select_Start, Select_End, Select_Line_Start, Select_Line_End, Select_Hard_Line_Start, Select_Hard_Line_End, Select_Smart_Line_Start, Select_Smart_Line_End, }
External examples text_edit
Command_Set ¶
Related Constants
Option ¶
Option :: enum u32 { Keep_Indent, // `New_Line` copies the hard line's leading spaces and tabs, up to the caret Line_Clipboard, // with nothing selected, `Copy` and `Cut` take the caret's hard line; `Paste` puts such a line above the caret's }
opt-in behaviours, for State.options
State ¶
State :: struct { selection: [2]int, line_start: int, line_end: int, // initialized each "frame" with `begin` builder: ^strings.Builder, // let the caller store the text buffer data up_index: int, down_index: int, // multi-lines page_up_index: int, page_down_index: int, // set by up, down, and page moves: while set, keep aiming them at the same column (multi-lines) // - edits and other moves clear it; clear it yourself on a click moved_vertically: bool, translate_by_grapheme: bool, // classifies runes for word moves and selection. // nil: only spaces, tabs, and newlines separate words word_class: proc(r: rune) -> Word_Class, options: Option_Set, // undo undo: [dynamic]^Undo_State, redo: [dynamic]^Undo_State, undo_text_allocator: runtime.Allocator, undo_breaks: Undo_Break_Set, // what starts a new undo step, besides a pause of `undo_timeout` id: u64, // Timeout information current_time: time.Tick, last_edit_time: time.Tick, undo_timeout: time.Duration, // Set these if you want cut/copy/paste functionality set_clipboard: proc(user_data: rawptr, text: string) -> (ok: bool), get_clipboard: proc(user_data: rawptr) -> (text: string, ok: bool), clipboard_user_data: rawptr, // owned by the package _last_edit: Edit_Kind, _last_caret: int, _edit_depth: int, _undo_groups: int, _undo_break: bool, _clipboard_line_hash: u64, }
External examples text_edit
Related Procedures With Parameters
- begin
- clear_all
- copy
- current_selected_text
- cut
- delete_to
- destroy
- end
- has_selection
- init
- input_rune
- input_runes
- input_text
- insert
- move_to
- new_line
- paste
- perform_command
- remove
- select_all
- select_hard_line
- select_line
- select_to
- select_word
- selection_delete
- setup_once
- sorted_selection
- translate_position
- undo
- undo_check
- undo_clear
- undo_group_begin
- undo_group_end
- undo_state_push
- update_time
Translation ¶
Translation :: enum u32 { Start, End, Left, Right, Up, Down, Page_Up, Page_Down, Word_Left, Word_Right, Word_Start, Word_End, Soft_Line_Start, Soft_Line_End, Hard_Line_Start, Hard_Line_End, Smart_Line_Start, Smart_Line_End, }
Related Procedures With Parameters
Undo_Break ¶
Undo_Break :: enum u32 { Kind, // switching between typing, deleting, pasting, and cutting; every paste and cut is its own step Word, // typing the first rune of a word, by `State.word_class` Caret_Move, // editing away from where the last edit left the caret Every_Edit, }
what starts a new undo step, besides a pause of State.undo_timeout
Undo_Break_Set ¶
Undo_Break_Set :: distinct bit_set[Undo_Break; u32]
Word_Class ¶
Word_Class :: enum u32 { Space, Punctuation, Word, }
a rune's class for word moves: a word is a run of one class, and word moves skip Space
Related Procedures With Returns
Constants
DEFAULT_UNDO_TIMEOUT ¶
DEFAULT_UNDO_TIMEOUT: time.Duration : 300 * time.Millisecond
MULTILINE_COMMANDS ¶
MULTILINE_COMMANDS :: Command_Set{.New_Line, .Up, .Down, .Page_Up, .Page_Down, .Select_Up, .Select_Down, .Select_Page_Up, .Select_Page_Down}
VERTICAL_TRANSLATIONS ¶
VERTICAL_TRANSLATIONS :: Translation_Set{.Up, .Down, .Page_Up, .Page_Down}
Variables
This section is empty.
Procedures
begin ¶
Call at the beginning of each frame
clear_all ¶
returns true when the builder had content to be cleared clear builder&selection and the undo|redo stacks
copy ¶
try and copy the currently selected text to the clipboard State.set_clipboard needs to be assigned
current_selected_text ¶
return the currently selected text
cut ¶
copy & delete the current selection when copy() succeeds
delete_to ¶
delete_to :: proc(s: ^State, t: Translation) {…}
Deletes everything between the caret and resultant position
destroy ¶
destroy :: proc(s: ^State) {…}
clear undo|redo strings and delete their stacks
External examples text_edit
has_selection ¶
true if selection head and tail dont match and form a selection of multiple characters
init ¶
init :: proc( s: ^State, undo_text_allocator, undo_state_allocator: runtime.Allocator, undo_timeout: time.Duration = DEFAULT_UNDO_TIMEOUT, ) {…}
init the state to some timeout and set the respective allocators
- undo_state_allocator dictates the dynamic undo|redo arrays allocators
- undo_text_allocator is the allocator which allocates strings only
External examples text_edit
input_rune ¶
insert a single rune into the edit state - deletes the current selection
External examples text_edit
input_runes ¶
insert slice of runes into the edit state - deletes the current selection
input_text ¶
insert text into the edit state - deletes the current selection
insert ¶
insert a single rune into the edit state - deletes the current selection
move_to ¶
move_to :: proc(s: ^State, t: Translation) {…}
Moves the position of the caret (both sides of the selection)
new_line ¶
new_line :: proc(s: ^State) {…}
Inserts a line break, followed by the hard line's indentation with .Keep_Indent
paste ¶
reinsert whatever the get_clipboard would return State.get_clipboard needs to be assigned
perform_command ¶
External examples text_edit
remove ¶
remove the wanted range withing, usually the selection within byte indices
select_hard_line ¶
select_hard_line :: proc(s: ^State) {…}
Selects the caret's hard line and its line break
select_to ¶
select_to :: proc(s: ^State, t: Translation) {…}
Moves only the head of the selection and leaves the tail uneffected
select_word ¶
select_word :: proc(s: ^State) {…}
Selects the word at the caret, or the spaces around it if there is no word
selection_delete ¶
selection_delete :: proc(s: ^State) {…}
delete the current selection range and set the proper selection afterwards
setup_once ¶
setup the builder, selection and undo|redo state once allowing to retain selection
External examples text_edit
sorted_selection ¶
return the clamped lo/hi of the current selection since the selection[0] moves around and could be ahead of selection[1] useful when rendering and needing left->right
External examples text_edit
translate_position ¶
translate_position :: proc(s: ^State, t: Translation) -> int {…}
translates the caret position
- soft line: the caret's line as laid out, from
State.line_starttoState.line_end - hard line: the caret's line as written, up to a
\nor\r\n - smart start/end: skip the soft line's leading/trailing spaces and tabs, or go to its start/end if already there or the line is blank
undo ¶
undo :: proc(s: ^State, undo, redo: ^[dynamic]^Undo_State) {…}
pop undo|redo state - push to redo|undo - set selection & text
undo_check ¶
undo_check :: proc(s: ^State) {…}
clear the redo stack, and push an undo step when the undo stack is empty or the undo timeout gets hit
undo_clear ¶
undo_clear :: proc(s: ^State, undo: ^[dynamic]^Undo_State) {…}
iteratively clearn the undo|redo stack and free each allocated text state
undo_group_begin ¶
undo_group_begin :: proc(s: ^State) {…}
Starts a group of edits that undo as one step until the matching undo_group_end
undo_group_end ¶
undo_group_end :: proc(s: ^State) {…}
Ends a group of edits started by undo_group_begin
undo_state_push ¶
undo_state_push :: proc(s: ^State, undo: ^[dynamic]^Undo_State) -> runtime.Allocator_Error {…}
push current text state to the wanted undo|redo stack
update_time ¶
update_time :: proc(s: ^State) {…}
update current time so "insert" can check for timeouts
External examples text_edit
word_class_ascii ¶
@(require_results) word_class_ascii :: proc(r: rune) -> Word_Class {…}
Word classes for code: ASCII punctuation separates words, except _
word_class_unicode ¶
@(require_results) word_class_unicode :: proc(r: rune) -> Word_Class {…}
Word classes for any text: Unicode spaces, punctuation, and symbols separate words, except _
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2026-11 (vendor "odin") Windows_amd64 @ 2026-11-30 21:05:49.814964300 +0000 UTC