@rse/ink-editor 0.0.0-stage → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/.claude/CLAUDE.md +1 -0
  2. package/AGENTS.md +99 -0
  3. package/CHANGELOG.md +9 -0
  4. package/README.md +116 -2
  5. package/dst-stage1/ink-editor-cli.d.ts +2 -0
  6. package/dst-stage1/ink-editor-cli.js +174 -0
  7. package/dst-stage1/ink-editor-engine-emacs.d.ts +103 -0
  8. package/dst-stage1/ink-editor-engine-emacs.js +1530 -0
  9. package/dst-stage1/ink-editor-engine-nano.d.ts +78 -0
  10. package/dst-stage1/ink-editor-engine-nano.js +884 -0
  11. package/dst-stage1/ink-editor-engine-vim.d.ts +124 -0
  12. package/dst-stage1/ink-editor-engine-vim.js +2220 -0
  13. package/dst-stage1/ink-editor-engine-vscode.d.ts +79 -0
  14. package/dst-stage1/ink-editor-engine-vscode.js +850 -0
  15. package/dst-stage1/ink-editor-mode-emacs.d.ts +12 -0
  16. package/dst-stage1/ink-editor-mode-emacs.js +143 -0
  17. package/dst-stage1/ink-editor-mode-nano.d.ts +12 -0
  18. package/dst-stage1/ink-editor-mode-nano.js +140 -0
  19. package/dst-stage1/ink-editor-mode-vim.d.ts +12 -0
  20. package/dst-stage1/ink-editor-mode-vim.js +230 -0
  21. package/dst-stage1/ink-editor-mode-vscode.d.ts +12 -0
  22. package/dst-stage1/ink-editor-mode-vscode.js +133 -0
  23. package/dst-stage1/ink-editor-mode.d.ts +72 -0
  24. package/dst-stage1/ink-editor-mode.js +24 -0
  25. package/dst-stage1/ink-editor-syntax.d.ts +14 -0
  26. package/dst-stage1/ink-editor-syntax.js +149 -0
  27. package/dst-stage1/ink-editor-text.d.ts +38 -0
  28. package/dst-stage1/ink-editor-text.js +177 -0
  29. package/dst-stage1/ink-editor-widget.d.ts +15 -0
  30. package/dst-stage1/ink-editor-widget.js +132 -0
  31. package/dst-stage1/ink-editor.d.ts +13 -0
  32. package/dst-stage1/ink-editor.js +37 -0
  33. package/dst-stage1/tsc.tsbuildinfo +1 -0
  34. package/dst-stage2/ink-editor-cli.esm.js +177 -0
  35. package/dst-stage2/ink-editor.esm.js +5972 -0
  36. package/etc/c8.json +17 -0
  37. package/etc/eslint.mts +84 -0
  38. package/etc/knip.jsonc +51 -0
  39. package/etc/stx.conf +103 -0
  40. package/etc/tsc.json +29 -0
  41. package/etc/vite.mts +79 -0
  42. package/package.d/@typescript-eslint+typescript-estree+8.74.0.patch +12 -0
  43. package/package.d/vite+8.4.1.patch +12 -0
  44. package/package.json +79 -5
  45. package/src/ink-editor-cli.ts +200 -0
  46. package/src/ink-editor-engine-emacs.ts +1648 -0
  47. package/src/ink-editor-engine-nano.ts +978 -0
  48. package/src/ink-editor-engine-vim.ts +2203 -0
  49. package/src/ink-editor-engine-vscode.ts +938 -0
  50. package/src/ink-editor-mode-emacs.ts +137 -0
  51. package/src/ink-editor-mode-nano.ts +135 -0
  52. package/src/ink-editor-mode-vim.ts +223 -0
  53. package/src/ink-editor-mode-vscode.ts +129 -0
  54. package/src/ink-editor-mode.ts +137 -0
  55. package/src/ink-editor-syntax.ts +176 -0
  56. package/src/ink-editor-text.ts +205 -0
  57. package/src/ink-editor-widget.ts +182 -0
  58. package/src/ink-editor.ts +48 -0
  59. package/tst/ink-editor-1-vim.spec.ts +177 -0
  60. package/tst/ink-editor-2-emacs.spec.ts +211 -0
  61. package/tst/ink-editor-3-nano.spec.ts +187 -0
  62. package/tst/ink-editor-4-vscode.spec.ts +187 -0
  63. package/tst/tsc.json +27 -0
@@ -0,0 +1,182 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ /* text area for Ink, driven by an editing mode, on top of the Textarea of better-ink-ui */
26
+
27
+ /* external dependencies */
28
+ import React from "react"
29
+ import { Box, Text, useInput, usePaste, useStdout, useWindowSize } from "ink"
30
+ import { Textarea, TextareaUtils, osc52 } from "better-ink-ui"
31
+
32
+ /* internal dependencies */
33
+ import type { TextOps } from "./ink-editor-text"
34
+ import type { EditorMode, EditorModeFactory, EditorHost, EditorResult } from "./ink-editor-mode"
35
+ import type { Syntax } from "./ink-editor-syntax"
36
+
37
+ const h = React.createElement
38
+
39
+ /* styles of the highlighting of the mode */
40
+ const STYLES = { highlight: { color: "black", backgroundColor: "yellow" } }
41
+
42
+ /* the properties of the widget */
43
+ export interface EditorProps {
44
+ value: string
45
+ onChange: (value: string) => void
46
+
47
+ /* rows of the text area (the status line takes one more row) */
48
+ height: number
49
+
50
+ /* columns of the text area (default: the terminal width) */
51
+ width?: number
52
+
53
+ /* whether keys go to the widget (default: true) */
54
+ focus?: boolean
55
+
56
+ /* the creation of the editing mode (called once) */
57
+ mode: EditorModeFactory
58
+
59
+ /* the file operations and the quitting for the commands of the mode */
60
+ host?: EditorHost
61
+
62
+ /* the initially opened file, reported once on the status line */
63
+ file?: EditorResult | null
64
+
65
+ /* the syntax highlighting of the text (see "ink-editor-syntax.ts"; keep its identity stable) */
66
+ syntax?: Syntax | null
67
+ }
68
+
69
+ /* the text area widget, driven by an editing mode */
70
+ export function Editor (props: EditorProps): React.ReactElement {
71
+ const [ , redraw ] = React.useReducer((n: number) => n + 1, 0)
72
+ const { stdout } = useStdout()
73
+ const { columns } = useWindowSize()
74
+ const width = Math.max(1, props.width ?? columns)
75
+
76
+ /* the wrapped rows by line of the last layout, so a layout re-wraps only the changed lines */
77
+ const cache = React.useMemo(() => new Map<string, readonly { column: number, text: string }[]>(), [ width ])
78
+
79
+ /* the text operations of the Textarea, so the mode agrees with it on graphemes, words, and rows */
80
+ const ops = React.useMemo((): TextOps => ({
81
+ previous: TextareaUtils.previous,
82
+ next: TextareaUtils.next,
83
+ wordStart: TextareaUtils.wordStart,
84
+ vertical: (v, o, direction) => {
85
+ const cursor = TextareaUtils.cursor(v, o)
86
+ const rows = TextareaUtils.rows({ value: v, width, cursor, cache })
87
+ return TextareaUtils.vertical({ value: v, rows, cursor, direction })
88
+ }
89
+ }), [ width, cache ])
90
+
91
+ /* the editing mode, created once and re-bound to the current properties on each rendering */
92
+ const ref = React.useRef<EditorMode | null>(null)
93
+ if (ref.current === null) {
94
+ ref.current = props.mode(props.value, ops)
95
+ if (props.file)
96
+ ref.current.opened(props.file)
97
+ }
98
+ const mode = ref.current
99
+ mode.bind({
100
+ ops,
101
+ height: props.height,
102
+ emit: props.onChange,
103
+ clip: (text) => { stdout.write(osc52(text)) },
104
+ host: props.host ?? null
105
+ })
106
+ mode.sync(props.value)
107
+
108
+ /* the highlighting of the mode, ahead of the syntax highlighting, as the first rule
109
+ to claim a character keeps it (keeping their identity while unchanged) */
110
+ const highlight = mode.highlight()
111
+ const syntax = props.syntax ?? null
112
+ const labels = React.useMemo(() => [
113
+ ...(highlight ? [ { pattern: highlight, label: "highlight" } ] : []),
114
+ ...(syntax ? [ { ranges: syntax } ] : [])
115
+ ], [ highlight, syntax ])
116
+
117
+ /* on a prompt of the status line, the cursor leaves the text area: the Textarea draws
118
+ no cursor without focus, so then the keys and pastes are taken here instead */
119
+ const value = mode.value
120
+ const cursor = TextareaUtils.cursor(value, mode.off)
121
+ const status = mode.status(cursor)
122
+ const focus = props.focus ?? true
123
+ const prompt = status.prompt !== null
124
+ const paste = mode.paste?.bind(mode)
125
+ useInput((input, key) => {
126
+ mode.key(input, key)
127
+ redraw()
128
+ }, { isActive: focus && prompt })
129
+ usePaste((text) => {
130
+ paste?.(text)
131
+ redraw()
132
+ }, { isActive: focus && prompt })
133
+
134
+ /* the text area, padded with filler rows below the text end */
135
+ const filler = mode.filler()
136
+ const rows = filler !== "" ? TextareaUtils.rows({ value, width, cursor, cache }).length : props.height
137
+ const shown = Math.max(1, Math.min(rows, props.height))
138
+ const area = h(Box, { height: shown, flexShrink: 0, flexDirection: "column" },
139
+ h(Textarea, {
140
+ value,
141
+ focus: focus && !prompt,
142
+ cursor,
143
+ selection: mode.selection(),
144
+ labels,
145
+ styles: STYLES,
146
+ maxRows: props.height,
147
+ cursorBlink: mode.blink(),
148
+ onChange: (v) => { mode.onText(v); redraw() },
149
+ onCursorChange: (c) => { mode.onCursor(c); redraw() },
150
+ onSelectionChange: (s) => { if (mode.onSelect(s)) redraw() },
151
+ onPaste: paste ? (text) => { paste(text); redraw() } : undefined,
152
+ onKey: (input, key) => {
153
+ const handled = mode.key(input, key)
154
+ redraw()
155
+ return handled
156
+ }
157
+ }))
158
+ const fillers = Array.from({ length: Math.max(0, props.height - shown) },
159
+ (_, i) => h(Text, { key: `~${i}`, color: "blue" }, filler))
160
+
161
+ /* the status line with the prompt, or the message resp. the label and note, and the information */
162
+ const text = status.message || status.label + status.note
163
+ const left = prompt
164
+ ? h(Text, { wrap: "truncate-start" }, status.prompt, h(Text, { inverse: true }, " "))
165
+ : status.message
166
+ ? h(Text, { wrap: "truncate", color: status.error ? "red" : undefined }, status.message)
167
+ : h(Text, { wrap: "truncate" }, h(Text, { bold: true }, status.label), status.note)
168
+
169
+ /* a long message overwrites the information and the keystroke hints (shown grey) */
170
+ let right = status.info
171
+ let hints = status.hints
172
+ if (prompt || TextareaUtils.textWidth(text) + TextareaUtils.textWidth(right + hints) >= width) {
173
+ right = ""
174
+ hints = ""
175
+ }
176
+ const line = h(Box, { height: 1, flexShrink: 0 },
177
+ h(Box, { flexGrow: 1, flexShrink: 1 }, left),
178
+ h(Box, { flexShrink: 0 }, h(Text, null, right, h(Text, { color: "gray" }, hints))))
179
+
180
+ return h(Box, { flexDirection: "column", height: props.height + 1 }, area, fillers, line)
181
+ }
182
+
@@ -0,0 +1,48 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ /* internal dependencies */
26
+ import type { EditorModeFactory } from "./ink-editor-mode"
27
+ import { VimMode } from "./ink-editor-mode-vim"
28
+ import { EmacsMode } from "./ink-editor-mode-emacs"
29
+ import { NanoMode } from "./ink-editor-mode-nano"
30
+ import { CodeMode } from "./ink-editor-mode-vscode"
31
+
32
+ /* re-export the public API */
33
+ export type * from "./ink-editor-mode"
34
+ export type { TextOps } from "./ink-editor-text"
35
+ export type { Syntax, SyntaxRange, SyntaxStyle } from "./ink-editor-syntax"
36
+ export type { EditorProps } from "./ink-editor-widget"
37
+ export { Editor } from "./ink-editor-widget"
38
+ export { syntaxLanguage, syntaxLoad } from "./ink-editor-syntax"
39
+ export { VimMode, EmacsMode, NanoMode, CodeMode }
40
+
41
+ /* the editing modes, by their name */
42
+ export const editorModes: Record<string, EditorModeFactory> = {
43
+ vim: (value, ops) => new VimMode(value, "", ops),
44
+ emacs: (value, ops) => new EmacsMode(value, ops),
45
+ nano: (value, ops) => new NanoMode(value, ops),
46
+ vscode: (value, ops) => new CodeMode(value, ops)
47
+ }
48
+
@@ -0,0 +1,177 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ /* terminal-free tests of the Vim engine */
26
+
27
+ /* external dependencies (test suite) */
28
+ import { describe, it } from "mocha"
29
+ import * as chai from "chai"
30
+
31
+ /* external dependencies */
32
+ import { TextareaUtils } from "better-ink-ui"
33
+
34
+ /* internal dependencies */
35
+ import { Vim, defaultOps } from "../src/ink-editor-engine-vim"
36
+ import { toCursor } from "../src/ink-editor-text"
37
+ import type { TextOps } from "../src/ink-editor-text"
38
+
39
+ /* setup test suite infrastructure */
40
+ chai.config.includeStack = true
41
+ const { expect } = chai
42
+
43
+ /* the text operations of the Textarea, as supplied by the Editor widget */
44
+ const textareaOps: TextOps = {
45
+ previous: TextareaUtils.previous,
46
+ next: TextareaUtils.next,
47
+ wordStart: TextareaUtils.wordStart,
48
+ vertical: (v, o, direction) => {
49
+ const cursor = TextareaUtils.cursor(v, o)
50
+ const rows = TextareaUtils.rows({ value: v, width: 80, cursor })
51
+ return TextareaUtils.vertical({ value: v, rows, cursor, direction })
52
+ }
53
+ }
54
+
55
+ /* a test case: name, initial text, keys (with "<...>" tokens), expected text, and optional expected cursor */
56
+ type Case = [ string, string, string, string, [ number, number ]? ]
57
+ const cases: Case[] = [
58
+ /* motions and operators */
59
+ [ "dw", "hello world foo", "dw", "world foo", [ 0, 0 ] ],
60
+ [ "dw at line end", "foo bar\nbaz", "wdw", "foo \nbaz" ],
61
+ [ "cw + dot", "two two three", "cwone<Esc>w.", "one one three", [ 0, 6 ] ],
62
+ [ "3x", "abcdef", "3x", "def", [ 0, 0 ] ],
63
+ [ "X", "abc", "$X", "ac" ],
64
+ [ "e", "foo bar", "ex", "fo bar" ],
65
+ [ "b", "foo bar", "$bx", "foo ar" ],
66
+ [ "ge", "foo bar", "$gex", "fo bar" ],
67
+ [ "G with count", "1\n2\n3", "2Gx", "1\n\n3" ],
68
+ [ "%", "(a [b] c)", "%x", "(a [b] c" ],
69
+ [ "f ; ,", "a-b-c-d", "f-;;,x", "a-bc-d" ],
70
+ [ "t ;", "a-b-c", "t-;x", "a--c" ],
71
+ [ "d}", "a\nb\n\nc", "d}", "\nc" ],
72
+ [ "marks", "a\nb\nc", "majj'ax", "\nb\nc" ],
73
+
74
+ /* graphemes */
75
+ [ "x on emoji", "a👍🏽b", "lx", "ab", [ 0, 1 ] ],
76
+ [ "l over emoji", "👍🏽x", "lx", "👍🏽" ],
77
+ [ "h over combining", "e\u{301}e", "$hx", "e" ],
78
+ [ "$ on flag", "a🇩🇪", "$x", "a" ],
79
+
80
+ /* linewise operations */
81
+ [ "dd with count", "1\n2\n3\n4", "j2dd", "1\n4", [ 1, 0 ] ],
82
+ [ "dd on last line", "1\n2", "jdd", "1", [ 0, 0 ] ],
83
+ [ "yyp", "a\nb", "yyp", "a\na\nb", [ 1, 0 ] ],
84
+ [ "p on last line", "a\nb", "yyjp", "a\nb\na", [ 2, 0 ] ],
85
+ [ "J", "a\n b\nc", "J", "a b\nc", [ 0, 1 ] ],
86
+ [ "3J", "a\nb\nc", "3J", "a b c" ],
87
+ [ ">>", "a", ">>", "\ta" ],
88
+ [ "cc", " a\nb", "ccx<Esc>", "x\nb" ],
89
+ [ "S", "a\nb", "jSz<Esc>", "a\nz" ],
90
+ [ "gUU", "abc", "gUU", "ABC" ],
91
+
92
+ /* text objects */
93
+ [ "ci(", "f(a, b) x", "faci(z<Esc>", "f(z) x" ],
94
+ [ "da\"", "x \"q\" y", "fqda\"", "x y" ],
95
+ [ "diw", "foo bar baz", "wdiw", "foo baz" ],
96
+ [ "daw", "foo bar baz", "wdaw", "foo baz" ],
97
+ [ "di{ over lines", "f {\n a\n b\n}", "jdi{", "f {\n}" ],
98
+ [ "guiw", "ABC DEF", "guiw", "abc DEF" ],
99
+
100
+ /* simple changes */
101
+ [ "~", "abc", "~~", "ABc", [ 0, 2 ] ],
102
+ [ "r with count", "abcd", "3rx", "xxxd", [ 0, 2 ] ],
103
+ [ "Ctrl-A", "x 41 y", "<C-a>", "x 42 y", [ 0, 3 ] ],
104
+ [ "Ctrl-X with count", "x 5", "7<C-x>", "x -2" ],
105
+
106
+ /* insert and replace mode */
107
+ [ "R + BS", "hello", "Rab<BS><BS>cd<Esc>", "cdllo", [ 0, 1 ] ],
108
+ [ "o with count", "a", "3ox<Esc>", "a\nx\nx\nx", [ 3, 0 ] ],
109
+ [ "i with count", "b", "3ia<Esc>", "aaab", [ 0, 2 ] ],
110
+ [ "Ctrl-W", "", "iabc def<C-w>x<Esc>", "abc x" ],
111
+ [ "Ctrl-W + dot", "abc def\nabc def", "A<C-w>x<Esc>j.", "abc x\nabc x" ],
112
+ [ "Ctrl-U", " ab", "A cd<C-u>x<Esc>", " x" ],
113
+ [ "BS", "", "iab<BS>c<Esc>", "ac" ],
114
+ [ "BS on emoji", "", "ia👍🏽<BS>b<Esc>", "ab" ],
115
+ [ "Up", "abc\nde", "jA<Up>x<Esc>", "abxc\nde" ],
116
+
117
+ /* undo, repeating, registers, and macros */
118
+ [ "A + u", "ab\ncd", "jAx<Esc>u", "ab\ncd", [ 1, 1 ] ],
119
+ [ "u + Ctrl-R", "ab", "xu<C-r>", "b" ],
120
+ [ "dot with count", "a b c d e", "dw2.", "d e" ],
121
+ [ "named register", "foo bar", "\"ayiww\"aP", "foo foobar" ],
122
+ [ "macro", "a\nb\nc", "qaA!<Esc>jq2@a", "a!\nb!\nc!" ],
123
+
124
+ /* visual mode */
125
+ [ "v d", "abcdef", "lvlld", "aef", [ 0, 1 ] ],
126
+ [ "V >", "a\nb\nc", "Vj>", "\ta\n\tb\nc" ],
127
+ [ "v ~", "abc def", "ve~", "ABC def" ],
128
+ [ "v iw y P", "foo bar", "wviwy0P", "barfoo bar" ],
129
+ [ "v p", "foo bar", "yiwwviwp", "foo foo" ],
130
+
131
+ /* search and Ex commands */
132
+ [ "/ n", "x a x b x", "/x<CR>nD", "x a x b " ],
133
+ [ "/ wrapping", "x a x", "$/x<CR>", "x a x", [ 0, 0 ] ],
134
+ [ "*", "foo bar foo", "*D", "foo bar " ],
135
+ [ ":%s", "aa\naa", ":%s/a/b/g<CR>", "bb\nbb" ],
136
+ [ ":s with groups", "ab cd", ":s/\\(\\w\\+\\) \\(\\w\\+\\)/\\2 \\1/<CR>", "cd ab" ],
137
+ [ ":d with range", "1\n2\n3\n4", ":2,3d<CR>", "1\n4" ],
138
+ [ ":'<,'>s", "a\na\na", "Vj:s/a/b/<CR>", "b\nb\na" ],
139
+
140
+ /* regressions */
141
+ [ "* on Unicode word", "über x über", "*D", "über x " ],
142
+ [ "count before reg", "foo", "\"ayl3\"ap", "ffffoo" ],
143
+ [ "want after ~", "abcd\nefgh", "~~~j", "ABCd\nefgh", [ 1, 3 ] ],
144
+ [ ":s with n flag", "aa\na", ":%s/a/b/gn<CR>", "aa\na" ],
145
+ [ ":s with c flag", "aa", ":s/a/b/c<CR>", "aa" ],
146
+ [ "macro aborts", "a\nb\nc", "qaA!<Esc>jq5@a", "a!\nb!\nc!" ],
147
+ [ "Ctrl-R register", "foo", "\"ayiwA <C-r>a<Esc>", "foo foo" ],
148
+ [ "Ctrl-R + count", "x", "yl3i<C-r>\"<Esc>", "xxxx" ],
149
+ [ "Ctrl-O consumed", "ab", "A<C-o>xy<Esc>", "aby" ],
150
+ [ ":s Ctrl-W Unicode", "x", ":s/x/über<C-w>y/<CR>", "y" ],
151
+ [ "v d + dot", "abcdef", "xvld.", "f" ],
152
+ [ "v $ d", "abc\ndef", "v$d", "def" ],
153
+ [ "dot replaces op count", "a b c d e f g h i", "d3w2.", "f g h i" ],
154
+ [ "append to linewise", "a\nb", "\"ayyj\"Aylj\"ap", "a\nb\na\nb" ]
155
+ ]
156
+
157
+ /* feed the keys of a case into a fresh engine, one key press at a time */
158
+ const run = (ops: TextOps, text: string, keys: string) => {
159
+ const vim = new Vim(text, "", ops)
160
+ for (const token of keys.match(/<[^>]+>|./gsu) ?? [])
161
+ vim.keys([ token ])
162
+ return vim
163
+ }
164
+
165
+ for (const [ label, ops ] of [ [ "default text operations", defaultOps ], [ "Textarea text operations", textareaOps ] ] as const) {
166
+ describe(`Vim engine with ${label}`, () => {
167
+ for (const [ name, text, keys, expected, cursor ] of cases) {
168
+ it(name, () => {
169
+ const vim = run(ops, text, keys)
170
+ expect(vim.value).to.equal(expected)
171
+ if (cursor)
172
+ expect(toCursor(vim.value, vim.off)).to.deep.equal(cursor)
173
+ })
174
+ }
175
+ })
176
+ }
177
+
@@ -0,0 +1,211 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ /* terminal-free tests of the Emacs engine */
26
+
27
+ /* external dependencies (test suite) */
28
+ import { describe, it } from "mocha"
29
+ import * as chai from "chai"
30
+
31
+ /* external dependencies */
32
+ import { TextareaUtils } from "better-ink-ui"
33
+
34
+ /* internal dependencies */
35
+ import { Emacs } from "../src/ink-editor-engine-emacs"
36
+ import type { EmacsFiles } from "../src/ink-editor-engine-emacs"
37
+ import { toCursor } from "../src/ink-editor-text"
38
+ import type { TextOps } from "../src/ink-editor-text"
39
+
40
+ /* setup test suite infrastructure */
41
+ chai.config.includeStack = true
42
+ const { expect } = chai
43
+
44
+ /* the text operations of the Textarea, as supplied by the Editor widget */
45
+ const textareaOps: TextOps = {
46
+ previous: TextareaUtils.previous,
47
+ next: TextareaUtils.next,
48
+ wordStart: TextareaUtils.wordStart,
49
+ vertical: (v, o, direction) => {
50
+ const cursor = TextareaUtils.cursor(v, o)
51
+ const rows = TextareaUtils.rows({ value: v, width: 80, cursor })
52
+ return TextareaUtils.vertical({ value: v, rows, cursor, direction })
53
+ }
54
+ }
55
+
56
+ /* feed the keys of a case into a fresh engine, one key press at a time
57
+ (with "{...}" for named keys like "{C-x}", "{M-f}", or "{RET}") */
58
+ const run = (text: string, keys: string, files?: EmacsFiles) => {
59
+ const emacs = new Emacs(text, textareaOps)
60
+ if (files)
61
+ emacs.files = files
62
+ for (const token of keys.match(/\{[^}]+\}|./gsu) ?? [])
63
+ emacs.keys([ token.length > 2 && token.startsWith("{") ? token.slice(1, -1) : token ])
64
+ return emacs
65
+ }
66
+
67
+ /* a test case: name, initial text, keys, expected text, and optional expected cursor */
68
+ type Case = [ string, string, string, string, [ number, number ]? ]
69
+ const fill = (n: number) => Array(n).fill("word").join(" ")
70
+ const cases: Case[] = [
71
+ /* motion */
72
+ [ "C-f C-b", "abc", "{C-f}{C-f}{C-b}X", "aXbc", [ 0, 2 ] ],
73
+ [ "C-a C-e", "abc\ndef", "{C-n}{C-e}X{C-a}Y", "abc\nYdefX" ],
74
+ [ "C-n goal column", "abcdef\nab\nabcdef", "{C-e}{C-b}{C-n}{C-n}X", "abcdef\nab\nabcdeXf", [ 2, 6 ] ],
75
+ [ "M-f M-b", "foo bar baz", "{M-f}{M-f}{M-b}X", "foo Xbar baz" ],
76
+ [ "M-< M->", "abc\ndef", "{M->}X{M-<}Y", "Yabc\ndefX" ],
77
+ [ "M-g g", "1\n2\n3", "{M-g}g3{RET}X", "1\n2\nX3" ],
78
+ [ "C-<down> paragraph", "a\nb\n\nc", "{C-<down>}X", "a\nb\nX\nc" ],
79
+ [ "ESC as Meta", "foo bar", "{ESC}fX", "fooX bar" ],
80
+
81
+ /* prefix arguments */
82
+ [ "C-u 3 C-d", "abcdef", "{C-u}3{C-d}", "def" ],
83
+ [ "M-3 C-f", "abcdef", "{M-3}{C-f}X", "abcXdef" ],
84
+ [ "C-u C-u x", "", "{C-u}{C-u}x", "x".repeat(16) ],
85
+
86
+ /* inserting and deleting */
87
+ [ "C-o", "ab", "{C-f}{C-o}", "a\nb", [ 0, 1 ] ],
88
+ [ "C-q TAB", "", "{C-q}{TAB}", "\t" ],
89
+ [ "DEL with region", "abcd", "{C-SPC}{C-f}{C-f}{DEL}", "cd" ],
90
+ [ "M-\\", "a b", "{C-f}{C-f}{M-\\}", "ab" ],
91
+ [ "M-SPC", "a b", "{C-f}{C-f}{M-SPC}", "a b" ],
92
+ [ "M-^", "a\n b", "{C-n}{M-^}", "a b" ],
93
+ [ "C-x C-o", "a\n\n\n\nb", "{C-n}{C-x}{C-o}", "a\n\nb" ],
94
+
95
+ /* changing */
96
+ [ "C-t", "abc", "{C-f}{C-t}", "bac", [ 0, 2 ] ],
97
+ [ "C-t at line end", "abc", "{C-e}{C-t}", "acb" ],
98
+ [ "M-t", "foo bar", "{M-f}{M-t}", "bar foo" ],
99
+ [ "C-x C-t", "a\nb\nc", "{C-n}{C-x}{C-t}", "b\na\nc", [ 2, 0 ] ],
100
+ [ "M-u M-l M-c", "foo BAR baz", "{M-u}{M-l}{M-c}", "FOO bar Baz" ],
101
+ [ "M-q", fill(20), "{M-q}", fill(14) + "\n" + fill(6) ],
102
+
103
+ /* killing and yanking */
104
+ [ "M-d", "foo bar", "{M-d}", " bar" ],
105
+ [ "M-DEL", "foo bar", "{C-e}{M-DEL}", "foo " ],
106
+ [ "C-k", "abc\ndef", "{C-f}{C-k}", "a\ndef" ],
107
+ [ "C-k at line end", "abc\ndef", "{C-e}{C-k}", "abcdef" ],
108
+ [ "C-k C-k C-y", "abc\ndef", "{C-k}{C-k}{C-y}", "abc\ndef", [ 1, 0 ] ],
109
+ [ "C-SPC C-w C-y", "foo bar", "{C-SPC}{M-f}{C-w}{C-e}{C-y}", " barfoo" ],
110
+ [ "M-w C-y", "ab", "{C-SPC}{C-e}{M-w}{C-y}", "abab" ],
111
+ [ "C-y M-y", "a b", "{M-d}{C-g}{M-d}{C-y}{M-y}", "a" ],
112
+ [ "M-z", "abc,def", "{M-z},X", "Xdef" ],
113
+ [ "C-x h C-w", "abc", "{C-x}h{C-w}", "" ],
114
+ [ "C-x C-x", "abc", "{C-SPC}{C-e}{C-x}{C-x}X", "Xabc" ],
115
+
116
+ /* undoing */
117
+ [ "undo amalgamated", "", "abc{C-_}", "" ],
118
+ [ "undo, quit, redo", "", "ab{RET}cd{C-_}{C-_}{C-g}{C-_}", "ab\n" ],
119
+
120
+ /* searching and replacing */
121
+ [ "C-s", "foo bar foo", "{C-s}foo{C-s}{RET}X", "foo bar fooX" ],
122
+ [ "C-r", "foo bar foo", "{M->}{C-r}foo{RET}X", "foo bar Xfoo" ],
123
+ [ "C-s failing C-g", "abc", "{C-s}bx{C-g}{RET}X", "abXc" ],
124
+ [ "C-s abort", "abc", "{C-s}c{C-g}X", "Xabc" ],
125
+ [ "C-s C-s last search", "a b a b", "{C-s}b{RET}{C-s}{C-s}{RET}X", "a b a bX" ],
126
+ [ "C-s exit by key", "abc", "{C-s}b{C-a}X", "Xabc" ],
127
+ [ "M-% y n !", "a a a a", "{M-%}a{RET}b{RET}yn!", "b a b b" ],
128
+ [ "M-% q", "a a a", "{M-%}a{RET}b{RET}yq", "b a a" ],
129
+ [ "M-x replace-string", "a.a", "{M-x}replace-string{RET}.{RET}-{RET}", "a-a" ],
130
+ [ "M-x replace-regexp", "ab ab", "{M-x}replace-regexp{RET}(a)(b){RET}\\2\\1{RET}", "ba ba" ],
131
+
132
+ /* keyboard macros */
133
+ [ "C-x ( C-x ) C-x e e", "a\nb\nc", "{C-x}({C-a}-{C-n}{C-x}){C-x}ee", "-a\n-b\n-c" ],
134
+
135
+ /* regressions */
136
+ [ "M-- DEL", "abc", "{M--}{DEL}", "bc" ],
137
+ [ "M-- M-DEL", "foo bar", "{M--}{M-DEL}", " bar" ],
138
+ [ "M-t inside word", "foo bar", "{C-f}{M-t}", "bar foo" ],
139
+ [ "replace-regexp ^", "a\nb", "{M-x}replace-regexp{RET}^{RET}>{RET}", ">a\n>b" ],
140
+ [ "C-u C-y", "a b c d", "{M-d}{C-g}{M-d}{C-g}{M-d}{C-g}{M-d}{C-g}{C-u}{C-y}X", "X d" ],
141
+ [ "C-x e without macro", "", "{C-x}ee", "e" ]
142
+ ]
143
+
144
+ describe("Emacs engine", () => {
145
+ for (const [ name, text, keys, expected, cursor ] of cases) {
146
+ it(name, () => {
147
+ const emacs = run(text, keys)
148
+ expect(emacs.value).to.equal(expected)
149
+ if (cursor)
150
+ expect(toCursor(emacs.value, emacs.off)).to.deep.equal(cursor)
151
+ })
152
+ }
153
+ it("undefined key", () => {
154
+ const emacs = run("", "{C-x}{C-y}")
155
+ expect(emacs.message).to.equal("C-x C-y is undefined")
156
+ expect(emacs.error).to.equal(true)
157
+ })
158
+ it("M-x with completion", () => {
159
+ const emacs = run("a\nb", "{C-n}{M-x}what-li{TAB}{RET}")
160
+ expect(emacs.message).to.equal("Line 2")
161
+ })
162
+ it("echo of pending keys", () => {
163
+ expect(run("", "{C-x}").echo()).to.equal("C-x-")
164
+ expect(run("", "{C-u}5").echo()).to.equal("C-u 5-")
165
+ })
166
+ })
167
+
168
+ /* a fake of the file operations, logging the calls */
169
+ const fake = (exists = false) => {
170
+ const log: string[] = []
171
+ let modified = true
172
+ const files: EmacsFiles = {
173
+ name: () => "f.txt",
174
+ modified: () => modified,
175
+ write: (file, force, rename) => {
176
+ log.push(`write ${file}|${force}|${rename}`)
177
+ if (exists && !force)
178
+ return { message: `File ${file} exists`, error: true, exists: true }
179
+ modified = false
180
+ return { message: `Wrote ${file || "f.txt"}`, error: false }
181
+ },
182
+ reload: () => { log.push("reload"); return { message: "", error: false } },
183
+ quit: (failed) => { log.push(`quit ${failed ? 1 : 0}`) }
184
+ }
185
+ return { log, files }
186
+ }
187
+
188
+ describe("Emacs engine files", () => {
189
+ it("C-x C-s", () => {
190
+ const { log, files } = fake()
191
+ const emacs = run("", "{C-x}{C-s}", files)
192
+ expect(log).to.deep.equal([ "write |false|false" ])
193
+ expect(emacs.message).to.equal("Wrote f.txt")
194
+ })
195
+ it("C-x C-c saving", () => {
196
+ const { log, files } = fake()
197
+ run("", "{C-x}{C-c}y", files)
198
+ expect(log).to.deep.equal([ "write |false|false", "quit 0" ])
199
+ })
200
+ it("C-x C-c without saving", () => {
201
+ const { log, files } = fake()
202
+ run("", "{C-x}{C-c}nyes{RET}", files)
203
+ expect(log).to.deep.equal([ "quit 0" ])
204
+ })
205
+ it("C-x C-w overwriting", () => {
206
+ const { log, files } = fake(true)
207
+ run("", "{C-x}{C-w}g.txt{RET}y", files)
208
+ expect(log).to.deep.equal([ "write g.txt|false|true", "write g.txt|true|true" ])
209
+ })
210
+ })
211
+