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
Types (5)
Constants (2)
Variables (0)
This section is empty.
Procedures (29)
Procedure Groups (0)
This section is empty.
Types
Command ¶
Command :: enum u32 { None, Undo, Redo, New_Line, // multi-lines Cut, Copy, Paste, Select_All, Backspace, Delete, Delete_Word_Left, Delete_Word_Right, Left, Right, Up, // multi-lines Down, // multi-lines Word_Left, Word_Right, Start, End, Line_Start, Line_End, Select_Left, Select_Right, Select_Up, // multi-lines Select_Down, // multi-lines Select_Word_Left, Select_Word_Right, Select_Start, Select_End, Select_Line_Start, Select_Line_End, }
External examples text_edit
Command_Set ¶
Related Constants
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 translate_by_grapheme: bool, // undo undo: [dynamic]^Undo_State, redo: [dynamic]^Undo_State, undo_text_allocator: runtime.Allocator, 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, }
External examples text_edit
Related Procedures With Parameters
Translation ¶
Translation :: enum u32 { Start, End, Left, Right, Up, Down, Word_Left, Word_Right, Word_Start, Word_End, Soft_Line_Start, Soft_Line_End, }
Related Procedures With Parameters
Constants
DEFAULT_UNDO_TIMEOUT ¶
DEFAULT_UNDO_TIMEOUT: time.Duration : 300 * time.Millisecond
MULTILINE_COMMANDS ¶
MULTILINE_COMMANDS :: Command_Set{.New_Line, .Up, .Down, .Select_Up, .Select_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)
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_to ¶
select_to :: proc(s: ^State, t: Translation) {…}
Moves only the head of the selection and leaves the tail uneffected
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
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 redo stack and check if 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_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
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2026-10 (vendor "odin") Windows_amd64 @ 2026-10-18 22:42:50.976285200 +0000 UTC