Skip to content
docsv0.3.0

WordNavigation

Word-boundary navigation helpers. The PHP port of pi-tui's `word-navigation.ts`. Pure functions — no state, no mutation. A `text-input` or `editor` key handler calls them to compute the new cursor column after the user presses `Ctrl+Left` / `Ctrl+Right` / `Alt+B` / `Alt+F`. "Word" is defined the Emacs way: a run of word characters, OR a run of punctuation, OR a run of whitespace. Each is one atomic jump. Skipping trailing whitespace before the jump matches the Emacs "skip-then-jump" feel: `foo |bar` (| = cursor) with `Alt+B` lands at `foo| bar`, not at `|foo bar`. The implementation is Unicode-aware via {@see preg_match()} with the `u` flag, so accented letters and CJK are treated as word characters by `\w` (which is locale-independent in PCRE with `/u`). Punctuation is the complement of `\w` and `\s` — the same tri-state split pi-tui uses via `Intl.Segmenter`'s `isWordLike` flag.

WordNavigation::findWordBackward()

public static function findWordBackward(string $text, int $cursor): int

Returns the cursor position after moving one word backward from `$cursor` in `$text`. Skips trailing whitespace, then jumps one word/punctuation boundary. Returns 0 when the cursor is already at or before column 0.

Parameters

Parameters of findWordBackward()
NameTypeDescription
$textstring
$cursorint

WordNavigation::findWordForward()

public static function findWordForward(string $text, int $cursor): int

Returns the cursor position after moving one word forward from `$cursor` in `$text`. Skips leading whitespace, then jumps one word/punctuation boundary. Returns `mb_strlen($text)` when the cursor is already at or past the end.

Parameters

Parameters of findWordForward()
NameTypeDescription
$textstring
$cursorint