@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,137 @@
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
+ /* Emacs editing mode, adapting the Emacs editing engine to the editor
26
+ (dependency-free: it knows nothing about React, Ink, or any terminal) */
27
+
28
+ /* internal dependencies */
29
+ import { literalRegExp } from "./ink-editor-text"
30
+ import { Emacs } from "./ink-editor-engine-emacs"
31
+ import type { EmacsOutcome } from "./ink-editor-engine-emacs"
32
+ import type { EditorKey, EditorBinding, EditorMode, EditorResult, EditorStatus } from "./ink-editor-mode"
33
+
34
+ /* mapping of a key press onto Emacs key tokens like "x", "C-x", "M-f", or "<up>" */
35
+ const keyOf = (input: string, key: EditorKey): string[] => {
36
+ const mods = (key.ctrl ? "C-" : "") + (key.meta ? "M-" : "")
37
+ if (key.escape) return key.meta ? [ "ESC", "ESC" ] : [ "ESC" ]
38
+ if (key.return) return [ `${mods}RET` ]
39
+
40
+ /* C-j arrives as a plain linefeed, without the Ctrl modifier */
41
+ if (input === "\n") return [ "C-j" ]
42
+ if (key.tab) return [ key.shift ? "<backtab>" : "TAB" ]
43
+ if (key.backspace) return [ `${mods}DEL` ]
44
+ if (key.delete) return [ `${mods}<deletechar>` ]
45
+ if (key.upArrow) return [ `${mods}<up>` ]
46
+ if (key.downArrow) return [ `${mods}<down>` ]
47
+ if (key.leftArrow) return [ `${mods}<left>` ]
48
+ if (key.rightArrow) return [ `${mods}<right>` ]
49
+ if (key.pageUp) return [ `${mods}<prior>` ]
50
+ if (key.pageDown) return [ `${mods}<next>` ]
51
+ if (key.home) return [ `${mods}<home>` ]
52
+ if (key.end) return [ `${mods}<end>` ]
53
+ if (input === " " && mods !== "") return [ `${mods}SPC` ]
54
+ if (key.ctrl && input.length === 1) return [ `${mods}${input.toLowerCase()}` ]
55
+ if (key.meta && input.length === 1) return [ `M-${input}` ]
56
+ return [ ...input ]
57
+ }
58
+
59
+ /* the outcome of a file operation of the host, with the messages of Emacs */
60
+ const outcome = (r: EditorResult, written: boolean): EmacsOutcome => {
61
+ if ("error" in r) {
62
+ if (r.error === "noname")
63
+ return { message: "No file name", error: true }
64
+ if (r.error === "exists")
65
+ return { message: `File ${r.detail} exists`, error: true, exists: true }
66
+ if (r.error === "read")
67
+ return { message: `Opening input file: ${r.detail}`, error: true }
68
+ return { message: `Opening output file: ${r.detail}`, error: true }
69
+ }
70
+ return { message: written ? `Wrote ${r.name}` : `Reverted buffer from file ${r.name}`, error: false }
71
+ }
72
+
73
+ /* the Emacs editing mode, on top of the Emacs editing engine */
74
+ export class EmacsMode extends Emacs implements EditorMode {
75
+ /* the highlighting of the searched text (keeping its identity while unchanged) */
76
+ private hl: { text: string | null, re: RegExp | null } = { text: null, re: null }
77
+
78
+ /* re-bind the mode to the widget and the host */
79
+ bind (binding: EditorBinding) {
80
+ const host = binding.host
81
+ this.ops = binding.ops
82
+ this.height = binding.height
83
+ this.emit = binding.emit
84
+ this.clip = binding.clip
85
+ this.files = host === null ? null : {
86
+ name: () => host.name(),
87
+ modified: () => host.modified(),
88
+ write: (file, force, rename) => outcome(host.write(file, force, rename), true),
89
+ reload: () => outcome(host.reload(), false),
90
+ quit: (failed) => { host.quit(failed) }
91
+ }
92
+ }
93
+
94
+ /* show the message of an opened file */
95
+ opened (result: EditorResult) {
96
+ if ("error" in result)
97
+ this.fail(outcome(result, false).message)
98
+ else if (result.isNew)
99
+ this.say("(New file)")
100
+ }
101
+
102
+ /* the cursor blinks, the searched text is highlighted, and the rows below the text end stay empty */
103
+ blink () {
104
+ return true
105
+ }
106
+ highlight (): RegExp | null {
107
+ const text = this.searched()
108
+ if (this.hl.text !== text)
109
+ this.hl = { text, re: text !== null ? literalRegExp(text) : null }
110
+ return this.hl.re
111
+ }
112
+ filler () {
113
+ return ""
114
+ }
115
+
116
+ /* the echo area with the prompt or message, else the mode line */
117
+ status ([ l, c ]: [ number, number ]): EditorStatus {
118
+ const name = this.files?.name() ?? ""
119
+ const buffer = name === "" ? "*scratch*" : name.replace(/^.*\//, "")
120
+ const flag = this.files?.modified() ? "**" : "--"
121
+ return {
122
+ prompt: this.prompt(),
123
+ message: this.message !== "" ? this.message : this.echo(),
124
+ error: this.message !== "" && this.error,
125
+ label: "",
126
+ note: `-:${flag}- ${buffer}`,
127
+ info: `(${l + 1},${c}) (Fundamental${this.defining() ? " Def" : ""}) `,
128
+ hints: ""
129
+ }
130
+ }
131
+
132
+ /* handle a key press (all keys are taken from the Textarea) */
133
+ key (input: string, key: EditorKey): boolean {
134
+ return this.keys(keyOf(input, key))
135
+ }
136
+ }
137
+
@@ -0,0 +1,135 @@
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
+ /* Nano editing mode, adapting the Nano editing engine to the editor
26
+ (dependency-free: it knows nothing about React, Ink, or any terminal) */
27
+
28
+ /* internal dependencies */
29
+ import { literalRegExp } from "./ink-editor-text"
30
+ import { Nano } from "./ink-editor-engine-nano"
31
+ import type { NanoOutcome } from "./ink-editor-engine-nano"
32
+ import type { EditorKey, EditorBinding, EditorMode, EditorResult, EditorStatus } from "./ink-editor-mode"
33
+
34
+ /* mapping of a key press onto Nano key tokens like "x", "^X", "M-U", or "Up" */
35
+ const keyOf = (input: string, key: EditorKey): string[] => {
36
+ const mod = key.ctrl ? "^" : key.meta ? "M-" : ""
37
+ if (key.escape) return key.meta ? [ "Esc", "Esc" ] : [ "Esc" ]
38
+ if (key.return) return [ "Enter" ]
39
+
40
+ /* Ctrl+J arrives as a plain linefeed, without the Ctrl modifier */
41
+ if (input === "\n") return [ "^J" ]
42
+ if (key.tab) return [ key.shift ? "Sh-Tab" : "Tab" ]
43
+ if (key.backspace) return [ `${mod}Bsp` ]
44
+ if (key.delete) return [ `${mod}Del` ]
45
+ if (key.upArrow) return [ `${mod}Up` ]
46
+ if (key.downArrow) return [ `${mod}Down` ]
47
+ if (key.leftArrow) return [ `${mod}Left` ]
48
+ if (key.rightArrow) return [ `${mod}Right` ]
49
+ if (key.pageUp) return [ `${mod}PgUp` ]
50
+ if (key.pageDown) return [ `${mod}PgDn` ]
51
+ if (key.home) return [ `${mod}Home` ]
52
+ if (key.end) return [ `${mod}End` ]
53
+ if (input === " " && mod !== "") return [ `${mod}Space` ]
54
+ if (mod !== "" && input.length === 1) return [ `${mod}${input.toUpperCase()}` ]
55
+ return [ ...input ]
56
+ }
57
+
58
+ /* the outcome of a file operation of the host, with the messages of Nano */
59
+ const outcome = (r: EditorResult): NanoOutcome => {
60
+ if ("error" in r) {
61
+ if (r.error === "noname")
62
+ return { message: "No file name", error: true }
63
+ if (r.error === "exists")
64
+ return { message: `File "${r.detail}" exists`, error: true, exists: true }
65
+ if (r.error === "read")
66
+ return { message: `Error reading ${r.detail}`, error: true }
67
+ return { message: `Error writing ${r.detail}`, error: true }
68
+ }
69
+ return { message: `Wrote ${r.lines} line${r.lines === 1 ? "" : "s"}`, error: false }
70
+ }
71
+
72
+ /* the Nano editing mode, on top of the Nano editing engine */
73
+ export class NanoMode extends Nano implements EditorMode {
74
+ /* the highlighting of the replaced text (keeping its identity while unchanged) */
75
+ private hl: { text: string | null, re: RegExp | null } = { text: null, re: null }
76
+
77
+ /* re-bind the mode to the widget and the host */
78
+ bind (binding: EditorBinding) {
79
+ const host = binding.host
80
+ this.ops = binding.ops
81
+ this.height = binding.height
82
+ this.emit = binding.emit
83
+ this.clip = binding.clip
84
+ this.files = host === null ? null : {
85
+ name: () => host.name(),
86
+ modified: () => host.modified(),
87
+ write: (file, force, rename) => outcome(host.write(file, force, rename)),
88
+ quit: (failed) => { host.quit(failed) }
89
+ }
90
+ }
91
+
92
+ /* show the message of an opened file */
93
+ opened (result: EditorResult) {
94
+ if ("error" in result)
95
+ this.fail(outcome(result).message)
96
+ else if (result.isNew)
97
+ this.say("New File")
98
+ else
99
+ this.say(`Read ${result.lines} line${result.lines === 1 ? "" : "s"}`)
100
+ }
101
+
102
+ /* the cursor blinks, the replaced text is highlighted, and the rows below the text end stay empty */
103
+ blink () {
104
+ return true
105
+ }
106
+ highlight (): RegExp | null {
107
+ const text = this.searched()
108
+ if (this.hl.text !== text)
109
+ this.hl = { text, re: text !== null ? literalRegExp(text, true) : null }
110
+ return this.hl.re
111
+ }
112
+ filler () {
113
+ return ""
114
+ }
115
+
116
+ /* the status bar with the prompt or message, else the file and the main shortcuts */
117
+ status (): EditorStatus {
118
+ const name = this.files?.name() || "New Buffer"
119
+ return {
120
+ prompt: this.prompt(),
121
+ message: this.message !== "" ? `[ ${this.message} ]` : "",
122
+ error: this.error,
123
+ label: "",
124
+ note: `${name}${this.files?.modified() ? " Modified" : ""}`,
125
+ info: "",
126
+ hints: " ^G Help ^O Write Out ^W Where Is ^K Cut ^U Paste ^X Exit "
127
+ }
128
+ }
129
+
130
+ /* handle a key press (all keys are taken from the Textarea) */
131
+ key (input: string, key: EditorKey): boolean {
132
+ return this.keys(keyOf(input, key))
133
+ }
134
+ }
135
+
@@ -0,0 +1,223 @@
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
+ /* Vim editing mode, adapting the Vim editing engine to the editor
26
+ (dependency-free: it knows nothing about React, Ink, or any terminal) */
27
+
28
+ /* internal dependencies */
29
+ import { lineAt, lineEnd } from "./ink-editor-text"
30
+ import { Vim, toRegExp } from "./ink-editor-engine-vim"
31
+ import type { Mode } from "./ink-editor-engine-vim"
32
+ import type { EditorKey, EditorBinding, EditorMode, EditorHost, EditorError, EditorResult, EditorStatus } from "./ink-editor-mode"
33
+
34
+ /* mapping of a key press onto Vim key tokens like "x" or "<C-r>" */
35
+ const keyOf = (input: string, key: EditorKey): string[] => {
36
+ if (key.escape) return [ "<Esc>" ]
37
+ if (key.return) return [ "<CR>" ]
38
+ if (key.tab) return [ "<Tab>" ]
39
+ if (key.backspace) return [ key.meta || key.ctrl ? "<C-w>" : "<BS>" ]
40
+ if (key.delete) return [ "<Del>" ]
41
+ if (key.upArrow) return [ "<Up>" ]
42
+ if (key.downArrow) return [ "<Down>" ]
43
+ if (key.leftArrow) return [ "<Left>" ]
44
+ if (key.rightArrow) return [ "<Right>" ]
45
+ if (key.pageUp) return [ "<PageUp>" ]
46
+ if (key.pageDown) return [ "<PageDown>" ]
47
+ if (key.home) return [ "<Home>" ]
48
+ if (key.end) return [ "<End>" ]
49
+ if (key.ctrl && input.length === 1) return [ `<C-${input.toLowerCase()}>` ]
50
+
51
+ /* an Escape typed quickly before a key arrives as Meta+key */
52
+ if (key.meta && input.length === 1) return [ "<Esc>", input ]
53
+ return [ ...input ]
54
+ }
55
+
56
+ /* the mode labels of the command line */
57
+ const LABELS: Record<Mode, string> = {
58
+ normal: "", insert: "-- INSERT --", replace: "-- REPLACE --",
59
+ visual: "-- VISUAL --", vline: "-- VISUAL LINE --", cmdline: ""
60
+ }
61
+
62
+ /* the messages of the file operations of the host */
63
+ const MODIFIED = "E37: No write since last change (add ! to override)"
64
+ const FAILURES: Record<EditorError["error"], (detail: string) => string> = {
65
+ noname: () => "E32: No file name",
66
+ exists: () => "E13: File exists (add ! to override)",
67
+ read: (detail) => `E484: Can't open file ${detail}`,
68
+ write: (detail) => `E212: Can't open file for writing: ${detail}`
69
+ }
70
+ const fileMessage = (r: EditorResult, written: boolean) => {
71
+ if ("error" in r)
72
+ return FAILURES[r.error](r.detail)
73
+ if (!written && r.isNew)
74
+ return `"${r.name}" [New]`
75
+ return `"${r.name}" ${r.isNew ? "[New] " : ""}${!written && r.crlf ? "[dos] " : ""}` +
76
+ `${r.lines}L, ${r.bytes}B${written ? " written" : ""}`
77
+ }
78
+
79
+ /* the Ex commands of files and quitting, as their required abbreviation and optional rest */
80
+ const COMMANDS = [ "w|rite", "wq|", "wqa|ll", "wa|ll", "x|it", "exi|t", "xa|ll", "q|uit", "qa|ll",
81
+ "quita|ll", "cq|uit", "up|date", "e|dit", "sav|eas", "f|ile" ]
82
+ const commandOf = (cmd: string) => COMMANDS.map((c) => c.split("|"))
83
+ .find(([ abbr, rest ]) => cmd.startsWith(abbr) && (abbr + rest).startsWith(cmd))?.join("")
84
+
85
+ /* the file argument of an Ex command, with "%" expanded and "\ ", "\%", and "\\" unescaped */
86
+ const fileArg = (host: EditorHost, arg: string): { file: string } | { error: string } => {
87
+ if (arg.startsWith("!"))
88
+ return { error: "E492: writing to a command is not supported" }
89
+ if (arg.startsWith(">>"))
90
+ return { error: "E492: appending to a file is not supported" }
91
+ let empty = false
92
+ const file = arg.replace(/\\([ %\\])|%/g, (_m: string, c?: string) => {
93
+ if (c !== undefined)
94
+ return c
95
+ empty = empty || host.name() === ""
96
+ return host.name()
97
+ })
98
+ return empty ? { error: "E499: Empty file name for '%' or '#'" } : { file }
99
+ }
100
+
101
+ /* the Ex commands of files and quitting, on top of the host
102
+ (a string is shown as message, null shows nothing, and undefined is an unknown command) */
103
+ const fileCommand = (host: EditorHost, command: string, value: string): string | null | undefined => {
104
+ const m = /^([a-zA-Z]+)(!?)\s*(.*)$/.exec(command)
105
+ if (!m)
106
+ return undefined
107
+ const cmd = commandOf(m[1])
108
+ if (cmd === undefined)
109
+ return undefined
110
+ const parsed = fileArg(host, m[3])
111
+ if ("error" in parsed)
112
+ return parsed.error
113
+ const arg = parsed.file
114
+ const force = m[2] === "!"
115
+ switch (cmd) {
116
+ case "write":
117
+ return fileMessage(host.write(arg, force), true)
118
+ case "cquit":
119
+ host.quit(true)
120
+ return null
121
+ case "quit": case "qall": case "quitall":
122
+ if (host.modified() && !force)
123
+ return MODIFIED
124
+ host.quit()
125
+ return null
126
+ case "wq": case "xit": case "exit": case "wqall": case "xall": {
127
+ if (cmd.startsWith("wq") || host.modified() || arg !== "") {
128
+ const res = host.write(arg, force)
129
+ if ("error" in res)
130
+ return fileMessage(res, true)
131
+ }
132
+ host.quit()
133
+ return null
134
+ }
135
+ case "update": case "wall":
136
+ return host.modified() ? fileMessage(host.write(arg, force), true) : null
137
+ case "edit": {
138
+ if (arg !== "" && arg !== host.name())
139
+ return "E492: editing another file is not supported"
140
+ if (host.name() === "")
141
+ return FAILURES.noname("")
142
+ if (host.modified() && !force)
143
+ return MODIFIED
144
+ return fileMessage(host.reload(), false)
145
+ }
146
+ case "saveas":
147
+ if (arg === "")
148
+ return "E471: Argument required"
149
+ return fileMessage(host.write(arg, force, true), true)
150
+ case "file": {
151
+ if (arg !== "")
152
+ return "E492: renaming the buffer is not supported"
153
+ const name = host.name() || "[No Name]"
154
+ const lines = value === "" ? 0 : value.split("\n").length
155
+ return `"${name}"${host.modified() ? " [Modified]" : ""} ${lines} line${lines === 1 ? "" : "s"}`
156
+ }
157
+ }
158
+ return undefined
159
+ }
160
+
161
+ /* the Vim editing mode, on top of the Vim editing engine */
162
+ export class VimMode extends Vim implements EditorMode {
163
+ /* the highlighting of the last search pattern (keeping its identity while unchanged) */
164
+ private hl: { key: string, re: RegExp | null } = { key: ":", re: null }
165
+
166
+ /* re-bind the mode to the widget and the host */
167
+ bind (binding: EditorBinding) {
168
+ const host = binding.host
169
+ this.ops = binding.ops
170
+ this.height = binding.height
171
+ this.emit = binding.emit
172
+ this.clip = binding.clip
173
+ this.host = host === null ? () => undefined : (command) => fileCommand(host, command, this.value)
174
+ }
175
+
176
+ /* show the message of an opened file */
177
+ opened (result: EditorResult) {
178
+ const text = fileMessage(result, false)
179
+ if ("error" in result)
180
+ this.fail(text)
181
+ else
182
+ this.say(text)
183
+ }
184
+
185
+ /* the cursor blinks while typing text, the search matches are highlighted,
186
+ and the rows below the text end are marked with "~" */
187
+ blink () {
188
+ return this.mode === "insert" || this.mode === "replace"
189
+ }
190
+ highlight (): RegExp | null {
191
+ const pattern = this.hlsearch && this.search ? this.search.pattern : ""
192
+ const icase = pattern !== "" && this.icase(pattern)
193
+ const key = `${icase ? "i" : ""}:${pattern}`
194
+ if (this.hl.key !== key)
195
+ this.hl = { key, re: pattern !== "" ? toRegExp(pattern, icase) : null }
196
+ return this.hl.re
197
+ }
198
+ filler () {
199
+ return "~"
200
+ }
201
+
202
+ /* the command line with the mode, message, pending command, and ruler */
203
+ status ([ l, c ]: [ number, number ]): EditorStatus {
204
+ const start = lineAt(this.value, l)
205
+ const empty = lineEnd(this.value, start) === start
206
+ const ruler = `${l + 1},${empty ? "0-1" : c + 1}`
207
+ return {
208
+ prompt: this.mode === "cmdline" ? this.cmdKind + this.cmdText : null,
209
+ message: this.message,
210
+ error: this.error,
211
+ label: LABELS[this.mode],
212
+ note: this.recording ? `recording @${this.recording}` : "",
213
+ info: `${this.pend.join("").padEnd(11)}${ruler.padEnd(14)}`,
214
+ hints: ""
215
+ }
216
+ }
217
+
218
+ /* handle a key press, leaving plain insert mode keys to the front end */
219
+ key (input: string, key: EditorKey): boolean {
220
+ return this.keys(keyOf(input, key), true)
221
+ }
222
+ }
223
+
@@ -0,0 +1,129 @@
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
+ /* VS Code-style editing mode, adapting the VS Code-style editing engine to the editor
26
+ (dependency-free: it knows nothing about React, Ink, or any terminal) */
27
+
28
+ /* internal dependencies */
29
+ import { literalRegExp } from "./ink-editor-text"
30
+ import { Code } from "./ink-editor-engine-vscode"
31
+ import type { CodeOutcome } from "./ink-editor-engine-vscode"
32
+ import type { EditorKey, EditorBinding, EditorMode, EditorResult, EditorStatus } from "./ink-editor-mode"
33
+
34
+ /* mapping of a key press onto VS Code key tokens like "x", "ctrl+s", or "shift+alt+down" */
35
+ const keyOf = (input: string, key: EditorKey): string[] => {
36
+ const mods = (key.ctrl ? "ctrl+" : "") + (key.shift ? "shift+" : "") + (key.meta ? "alt+" : "")
37
+ if (key.escape) return [ "escape" ]
38
+ if (key.return) return [ `${mods}enter` ]
39
+ if (key.tab) return [ `${mods}tab` ]
40
+ if (key.backspace) return [ `${mods}backspace` ]
41
+ if (key.delete) return [ `${mods}delete` ]
42
+ if (key.upArrow) return [ `${mods}up` ]
43
+ if (key.downArrow) return [ `${mods}down` ]
44
+ if (key.leftArrow) return [ `${mods}left` ]
45
+ if (key.rightArrow) return [ `${mods}right` ]
46
+ if (key.pageUp) return [ `${mods}pageup` ]
47
+ if (key.pageDown) return [ `${mods}pagedown` ]
48
+ if (key.home) return [ `${mods}home` ]
49
+ if (key.end) return [ `${mods}end` ]
50
+ if ((key.ctrl || key.meta) && input.length === 1) return [ `${mods}${input.toLowerCase()}` ]
51
+ return [ ...input ]
52
+ }
53
+
54
+ /* the outcome of a file operation of the host, with the messages of VS Code */
55
+ const outcome = (r: EditorResult): CodeOutcome => {
56
+ if ("error" in r) {
57
+ if (r.error === "noname")
58
+ return { message: "No file name", error: true }
59
+ if (r.error === "exists")
60
+ return { message: `File ${r.detail} exists`, error: true, exists: true }
61
+ if (r.error === "read")
62
+ return { message: `Failed to open ${r.detail}`, error: true }
63
+ return { message: `Failed to save: ${r.detail}`, error: true }
64
+ }
65
+ return { message: `Saved ${r.name}`, error: false }
66
+ }
67
+
68
+ /* the VS Code-style editing mode, on top of the VS Code-style editing engine */
69
+ export class CodeMode extends Code implements EditorMode {
70
+ /* the highlighting of the searched text (keeping its identity while unchanged) */
71
+ private hl: { text: string | null, re: RegExp | null } = { text: null, re: null }
72
+
73
+ /* re-bind the mode to the widget and the host */
74
+ bind (binding: EditorBinding) {
75
+ const host = binding.host
76
+ this.ops = binding.ops
77
+ this.height = binding.height
78
+ this.emit = binding.emit
79
+ this.clip = binding.clip
80
+ this.files = host === null ? null : {
81
+ name: () => host.name(),
82
+ modified: () => host.modified(),
83
+ write: (file, force, rename) => outcome(host.write(file, force, rename)),
84
+ quit: (failed) => { host.quit(failed) }
85
+ }
86
+ }
87
+
88
+ /* show the message of an opened file */
89
+ opened (result: EditorResult) {
90
+ if ("error" in result)
91
+ this.fail(outcome(result).message)
92
+ else if (result.isNew)
93
+ this.say("New file")
94
+ }
95
+
96
+ /* the cursor blinks, the searched text is highlighted, and the rows below the text end stay empty */
97
+ blink () {
98
+ return true
99
+ }
100
+ highlight (): RegExp | null {
101
+ const text = this.searched()
102
+ if (this.hl.text !== text)
103
+ this.hl = { text, re: text !== null ? literalRegExp(text, true) : null }
104
+ return this.hl.re
105
+ }
106
+ filler () {
107
+ return ""
108
+ }
109
+
110
+ /* the status bar with the prompt or message, else the file, the position, and the main shortcuts */
111
+ status ([ l, c ]: [ number, number ]): EditorStatus {
112
+ const sel = this.selection()
113
+ return {
114
+ prompt: this.prompt(),
115
+ message: this.message,
116
+ error: this.error,
117
+ label: "",
118
+ note: `${this.files?.name() || "Untitled"}${this.files?.modified() ? " ●" : ""}`,
119
+ info: `Ln ${l + 1}, Col ${c + 1}${sel ? ` (${sel[1] - sel[0]} selected)` : ""} `,
120
+ hints: "^S Save ^Q Quit ^F Find "
121
+ }
122
+ }
123
+
124
+ /* handle a key press (all keys are taken from the Textarea) */
125
+ key (input: string, key: EditorKey): boolean {
126
+ return this.keys(keyOf(input, key))
127
+ }
128
+ }
129
+