Skip to main content

Text editing

input and textArea are two views over one editing engine, EditBuffer. TextInputState is an EditBuffer holding one line, TextAreaState one holding many. Every operation below is a method on both states, so code that drives an editor directly (a test, a macro, a custom key binding) calls the same thing the keys call.

The text is stored as grapheme clusters, so no operation can split an emoji, a flag or a combining sequence. Columns count clusters, and every width is measured through CharWidth.

Keys​

KeysWhat they doState method
Shift + arrows, Home, Endextend the selectionmoveLeft(select = true) and so on
Ctrl+Left / Ctrl+Right, Alt+B / Alt+Fprevious / next wordmoveWordLeft, moveWordRight
Ctrl+Up / Ctrl+Downprevious / next blank linemoveParagraphUp, moveParagraphDown
Ctrl+Aselect everythingselectAll
Ctrl+C / Ctrl+X / Ctrl+Vcopy, cut, pastecopy(clipboard), cut, paste
Ctrl+K / Ctrl+Ukill to the end / start of the linekillToEndOfLine, killToStartOfLine
Ctrl+W, Alt+Backspace / Alt+Dkill the word before / after the caretdeleteWordBackward, deleteWordForward
Ctrl+Y / Alt+Yyank the newest kill / swap it for the one beforeyank, yankPop
Ctrl+Z / Ctrl+Shift+Zundo / redoundo, redo

Word boundaries come from java.text.BreakIterator, so they follow Unicode rules rather than splitting on spaces. Punctuation between words is skipped.

With nothing selected, Ctrl+C and Ctrl+X are not consumed and keep bubbling, so Ctrl+C still quits an app whose input is focused.

A focused text field keeps editing chords for itself

Ctrl+U, Ctrl+W, Ctrl+K, Ctrl+Y, Ctrl+Z, Ctrl+A and the other keys in the table above are consumed by a focused input or textArea before an app binding sees them, so binding("ctrl+w", "close tab") does nothing while the user is typing. Pick app keys outside that set (Ctrl+Q, function keys), or a key sequence behind a leader. A test that drives a binding with pilot.press(...) needs the same care: move focus off the field first, or the field eats the key.

Clipboard​

A widget never touches a system clipboard. It has no terminal to reach one through. copy, cut and paste take a Clipboard, and the caller decides what that is. Pass Clipboard.inMemory() in a test.

In an app the text nodes use AppServices.clipboard. A copy goes out to the system clipboard through OSC 52 and is also kept in an in-app buffer. A terminal never lets an app read the system clipboard, so Ctrl+V pastes what this app last copied or cut. The terminal's own paste (Ctrl+Shift+V in most terminals) still arrives as a bracketed paste.

Masked and hidden input fields suppress copy/cut and never send killed secrets to the shared ring. Word motion and word kills treat a secret as one word. Hidden inputs paint blank cells and report no hardware caret; their source value remains sensitive app state. A visible mask intentionally shows the number of graphemes, not their original widths.

Pasted CRLF, bare CR and LF all represent line breaks. Single-line fields fold them to spaces. An open text-area search or composer reverse search accepts only the first pasted line into its query; it never changes the document or draft.

Kill ring​

Kills go into a KillRing, which is kept apart from the clipboard: Ctrl+K does not overwrite what the user copied. Consecutive kills join into one entry, the way they do in readline, so Ctrl+K Ctrl+K yanks back as one piece. Pass the same KillRing to several states, TextInputState("", killRing = shared), and a kill in one field yanks in another.

Undo​

Undo stores diffs, not copies of the document, so a long editing session costs memory in proportion to what was typed. Consecutive single-grapheme inserts join one undo step, and so do consecutive backspaces. Typing hello and pressing Ctrl+Z removes the word. A space after a word, a caret move or a selection change starts a new step. The history keeps the newest 100 steps. Undo of a replacement restores the original selection and its direction. Call breakUndoGroup() to force a boundary from code.

Soft wrap, line numbers and syntax highlighting​

textArea(notes).wrap.lineNumbers
textArea(query).lexer(Lexer.Sql)
textArea(source, TextAreaOptions(wrap = true, lineNumbers = true, gutterStyle = Style.Default.dim))

.wrap breaks long lines at the editor's width instead of scrolling sideways. A line breaks after its last space where there is one, and between graphemes where there is not, so CJK text wraps between characters and a wide glyph is never cut in half. Up and Down move by the rows you see: from the first row of a wrapped line, Down goes to its second row at the same display column, not to the next line. Each line's wrap is cached, so typing re-wraps only the line being edited.

.lineNumbers adds a gutter of right-aligned line numbers, one column wider than the largest number. The gutter grows by a column at line 10 and again at line 100. A wrapped line's continuation rows leave it blank.

Ctrl+F opens a /query prompt on the editor's bottom row. Typing searches as you go, from where the caret was. Enter keeps the match and closes the prompt, and Esc puts the caret back where it started. Then F3 and Shift+F3 step to the next and previous match. Matches are underlined. The search ignores case unless the query has a capital letter.

.readOnly turns a textArea into a viewer. Motion, selection, copy and search keep working, edits do not, and /, n and N search the way they do in less:

textArea(logState).readOnly.lineNumbers

From code, the same search is openSearch(), updateSearch(query), closeSearch(), nextMatch() and previousMatch() on TextAreaState, with every hit in matches.

Prompt composer​

promptComposer is the input of an LLM command-line tool:

private val prompt = PromptComposerState(
completions = (trigger, query) => trigger match
case '/' => commands.filter(_.startsWith(query))
case _ => files.filter(_.contains(query)),
)

def view(using ReactiveScope, Theme): Element =
column(transcript.fill, promptComposer(prompt)(text => send(text)))
  • Enter submits. The callback receives the text, and the composer clears and records it in its history. Shift+Enter or Alt+Enter inserts a newline.
  • Up on the first visual row and Down on the last visual row walk the history. Going past the newest entry brings back the draft you were typing.
  • Ctrl+R opens a reverse search over the history, shown as (reverse-i-search)`dep': deploy prod. Ctrl+R again finds an older match, Enter loads the match for editing and Esc cancels.
  • While the caret is at the end of the draft, the newest history entry that starts with it is shown dim after the caret. Tab or Right accepts it.
  • / at the start of the prompt, or @ at the start of a word, opens a completion popup below the caret. The provider runs on a background worker once typing pauses for debounce (150 ms by default), and a newer query always replaces an older answer. Up and Down move the highlight, Enter or Tab accept it and Esc closes the popup.
  • A paste of pasteLines lines or more (10 by default) shows as [Pasted 50 lines]. The submitted text has the whole paste in its place. Repeated/localized placeholders expand longest-first in a single pass, never recursively through paste payloads. Typing the exact text of a placeholder also expands it; placeholders are text, not protected objects.
  • The composer is as tall as its draft soft-wrapped at the width it is given — a long line grows it as surely as a newline does — up to maxRows (8 by default), after which it scrolls inside them.
  • .prompt("❯ ") draws a prompt glyph before the first row; the text and every row it wraps onto start after the glyph, so continuation rows hang under the text.

The provider must not touch UI state, because it runs off the render thread. Everything else on PromptComposerState is render-thread-only, like every widget state. The state and widgets.PromptComposer painter are reusable without the runtime; the DSL spelling is an alias and its small adapter owns asynchronous provider execution. A retained state works across sequential app runs without reusing retired task scopes.

Wrapped text areas cache row-count prefix sums and construct only viewport rows on a steady frame. Single-line edits update their row count logarithmically. The initial layout, a width change or a line-count change still builds the row-count index over the document.

External editors use VISUAL, then EDITOR, then vi. Command values support quoted paths and arguments and backslash escapes; shell operators and substitutions are literal, not executed. A missing editor is a warning, not a fatal terminal-backend error.

Next: bind your own keys around the editors in Key bindings, validate what users type in Forms & validation, and drive an editor key by key in Testing.