@rse/ink-editor 0.0.0-stage → 0.9.1
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.
- package/.claude/CLAUDE.md +1 -0
- package/AGENTS.md +99 -0
- package/CHANGELOG.md +9 -0
- package/README.md +116 -2
- package/dst-stage1/ink-editor-cli.d.ts +2 -0
- package/dst-stage1/ink-editor-cli.js +174 -0
- package/dst-stage1/ink-editor-engine-emacs.d.ts +103 -0
- package/dst-stage1/ink-editor-engine-emacs.js +1530 -0
- package/dst-stage1/ink-editor-engine-nano.d.ts +78 -0
- package/dst-stage1/ink-editor-engine-nano.js +884 -0
- package/dst-stage1/ink-editor-engine-vim.d.ts +124 -0
- package/dst-stage1/ink-editor-engine-vim.js +2220 -0
- package/dst-stage1/ink-editor-engine-vscode.d.ts +79 -0
- package/dst-stage1/ink-editor-engine-vscode.js +850 -0
- package/dst-stage1/ink-editor-mode-emacs.d.ts +12 -0
- package/dst-stage1/ink-editor-mode-emacs.js +143 -0
- package/dst-stage1/ink-editor-mode-nano.d.ts +12 -0
- package/dst-stage1/ink-editor-mode-nano.js +140 -0
- package/dst-stage1/ink-editor-mode-vim.d.ts +12 -0
- package/dst-stage1/ink-editor-mode-vim.js +230 -0
- package/dst-stage1/ink-editor-mode-vscode.d.ts +12 -0
- package/dst-stage1/ink-editor-mode-vscode.js +133 -0
- package/dst-stage1/ink-editor-mode.d.ts +72 -0
- package/dst-stage1/ink-editor-mode.js +24 -0
- package/dst-stage1/ink-editor-syntax.d.ts +14 -0
- package/dst-stage1/ink-editor-syntax.js +149 -0
- package/dst-stage1/ink-editor-text.d.ts +38 -0
- package/dst-stage1/ink-editor-text.js +177 -0
- package/dst-stage1/ink-editor-widget.d.ts +15 -0
- package/dst-stage1/ink-editor-widget.js +132 -0
- package/dst-stage1/ink-editor.d.ts +13 -0
- package/dst-stage1/ink-editor.js +37 -0
- package/dst-stage1/tsc.tsbuildinfo +1 -0
- package/dst-stage2/ink-editor-cli.esm.js +177 -0
- package/dst-stage2/ink-editor.esm.js +5972 -0
- package/etc/c8.json +17 -0
- package/etc/eslint.mts +84 -0
- package/etc/knip.jsonc +51 -0
- package/etc/stx.conf +103 -0
- package/etc/tsc.json +29 -0
- package/etc/vite.mts +79 -0
- package/package.d/@typescript-eslint+typescript-estree+8.74.0.patch +12 -0
- package/package.d/vite+8.4.1.patch +12 -0
- package/package.json +79 -5
- package/src/ink-editor-cli.ts +200 -0
- package/src/ink-editor-engine-emacs.ts +1648 -0
- package/src/ink-editor-engine-nano.ts +978 -0
- package/src/ink-editor-engine-vim.ts +2203 -0
- package/src/ink-editor-engine-vscode.ts +938 -0
- package/src/ink-editor-mode-emacs.ts +137 -0
- package/src/ink-editor-mode-nano.ts +135 -0
- package/src/ink-editor-mode-vim.ts +223 -0
- package/src/ink-editor-mode-vscode.ts +129 -0
- package/src/ink-editor-mode.ts +137 -0
- package/src/ink-editor-syntax.ts +176 -0
- package/src/ink-editor-text.ts +205 -0
- package/src/ink-editor-widget.ts +182 -0
- package/src/ink-editor.ts +48 -0
- package/tst/ink-editor-1-vim.spec.ts +177 -0
- package/tst/ink-editor-2-emacs.spec.ts +211 -0
- package/tst/ink-editor-3-nano.spec.ts +187 -0
- package/tst/ink-editor-4-vscode.spec.ts +187 -0
- package/tst/tsc.json +27 -0
|
@@ -0,0 +1,938 @@
|
|
|
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 engine, operating on a plain string value and key tokens
|
|
26
|
+
(dependency-free: it knows nothing about React, Ink, or any terminal) */
|
|
27
|
+
|
|
28
|
+
/* internal dependencies */
|
|
29
|
+
import { lineStart, lineEnd, lineNo, lineAt, toOffset, isIdent, isSpace, wordEnd, wordBegin,
|
|
30
|
+
escapeRegExp, literalRegExp, findNext, findPrevious, goalColumn, rowMove } from "./ink-editor-text"
|
|
31
|
+
import type { TextOps } from "./ink-editor-text"
|
|
32
|
+
|
|
33
|
+
/* the outcome of a file operation: a message, whether it is an error,
|
|
34
|
+
and whether forcing it would overwrite an existing other file */
|
|
35
|
+
export type CodeOutcome = { message: string, error: boolean, exists?: boolean }
|
|
36
|
+
|
|
37
|
+
/* the file operations of the outside, used by the file commands */
|
|
38
|
+
export interface CodeFiles {
|
|
39
|
+
/* the name of the edited file ("" for none), and whether the text is modified */
|
|
40
|
+
name (): string
|
|
41
|
+
modified (): boolean
|
|
42
|
+
|
|
43
|
+
/* write the text to a file ("" for the edited one), overwriting another existing file
|
|
44
|
+
only with "force", and with "rename" making the written file the edited one */
|
|
45
|
+
write (file: string, force: boolean, rename: boolean): CodeOutcome
|
|
46
|
+
|
|
47
|
+
/* quit the editor */
|
|
48
|
+
quit (failed?: boolean): void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* an undo snapshot, a text input (with optional own keys and live changes), a question, and a replacing */
|
|
52
|
+
type Snap = { value: string, off: number, anchor: number | null }
|
|
53
|
+
type Input = { prompt: () => string, text: string, done: (text: string) => void,
|
|
54
|
+
change?: (text: string) => void, key?: (token: string) => boolean, close?: () => void }
|
|
55
|
+
type Question = { prompt: string, take: (token: string) => void }
|
|
56
|
+
type Replace = { from: string, to: string, pos: number, stop: number, wrapped: boolean, match: [ number, number ], count: number }
|
|
57
|
+
|
|
58
|
+
/* the key bindings, from keys to command names (the "shift+" variants of the cursor commands select) */
|
|
59
|
+
const BINDINGS = new Map(Object.entries({
|
|
60
|
+
"ctrl+s": "save",
|
|
61
|
+
"ctrl+q": "quit",
|
|
62
|
+
"ctrl+w": "quit",
|
|
63
|
+
"ctrl+z": "undo",
|
|
64
|
+
"ctrl+y": "redo",
|
|
65
|
+
"ctrl+shift+z": "redo",
|
|
66
|
+
"ctrl+x": "cut",
|
|
67
|
+
"ctrl+c": "copy",
|
|
68
|
+
"ctrl+v": "paste",
|
|
69
|
+
"ctrl+a": "selectAll",
|
|
70
|
+
"ctrl+f": "find",
|
|
71
|
+
"ctrl+h": "replace",
|
|
72
|
+
"ctrl+r": "replace",
|
|
73
|
+
"ctrl+g": "gotoLine",
|
|
74
|
+
"ctrl+d": "selectNextMatch",
|
|
75
|
+
"ctrl+l": "selectLine",
|
|
76
|
+
"ctrl+shift+k": "deleteLines",
|
|
77
|
+
"ctrl+enter": "insertLineAfter",
|
|
78
|
+
"ctrl+shift+enter": "insertLineBefore",
|
|
79
|
+
"alt+up": "moveLinesUp",
|
|
80
|
+
"alt+down": "moveLinesDown",
|
|
81
|
+
"shift+alt+up": "copyLinesUp",
|
|
82
|
+
"shift+alt+down": "copyLinesDown",
|
|
83
|
+
"ctrl+/": "commentLine",
|
|
84
|
+
"ctrl+_": "commentLine",
|
|
85
|
+
"ctrl+]": "indentLines",
|
|
86
|
+
"tab": "tab",
|
|
87
|
+
"shift+tab": "outdentLines",
|
|
88
|
+
"escape": "cancelSelection",
|
|
89
|
+
"enter": "newline",
|
|
90
|
+
"backspace": "deleteLeft",
|
|
91
|
+
"delete": "deleteRight",
|
|
92
|
+
"ctrl+backspace": "deleteWordLeft",
|
|
93
|
+
"alt+backspace": "deleteWordLeft",
|
|
94
|
+
"ctrl+delete": "deleteWordRight",
|
|
95
|
+
"alt+delete": "deleteWordRight",
|
|
96
|
+
"left": "cursorLeft",
|
|
97
|
+
"right": "cursorRight",
|
|
98
|
+
"up": "cursorUp",
|
|
99
|
+
"down": "cursorDown",
|
|
100
|
+
"home": "cursorHome",
|
|
101
|
+
"end": "cursorEnd",
|
|
102
|
+
"pageup": "cursorPageUp",
|
|
103
|
+
"pagedown": "cursorPageDown",
|
|
104
|
+
"ctrl+home": "cursorTop",
|
|
105
|
+
"ctrl+end": "cursorBottom",
|
|
106
|
+
"ctrl+left": "cursorWordLeft",
|
|
107
|
+
"alt+left": "cursorWordLeft",
|
|
108
|
+
"ctrl+right": "cursorWordRight",
|
|
109
|
+
"alt+right": "cursorWordRight"
|
|
110
|
+
} as const))
|
|
111
|
+
type Command = NonNullable<ReturnType<typeof BINDINGS.get>> | "type"
|
|
112
|
+
|
|
113
|
+
/* the line comment prefixes, by file extension (default: "#") */
|
|
114
|
+
const COMMENTS: Record<string, string> = Object.fromEntries([
|
|
115
|
+
...[ "js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx", "c", "h", "cc", "cpp", "hpp", "cxx", "java",
|
|
116
|
+
"cs", "go", "rs", "swift", "kt", "kts", "scala", "php", "dart", "json5", "jsonc", "groovy", "vue" ]
|
|
117
|
+
.map((ext) => [ ext, "//" ]),
|
|
118
|
+
...[ "sql", "lua", "hs", "elm", "ada" ].map((ext) => [ ext, "--" ]),
|
|
119
|
+
...[ "tex", "sty", "erl", "m" ].map((ext) => [ ext, "%" ]),
|
|
120
|
+
...[ "ini", "lisp", "el", "clj", "scm", "asm" ].map((ext) => [ ext, ";" ]),
|
|
121
|
+
...[ "vim" ].map((ext) => [ ext, "\"" ])
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
/* whether a key token is a printable character, and the plural of a count */
|
|
125
|
+
const isChar = (t: string) => [ ...t ].length === 1 && t >= " "
|
|
126
|
+
const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? "" : "s"}`
|
|
127
|
+
|
|
128
|
+
/* the VS Code-style editing engine, operating on a plain string value, a cursor offset,
|
|
129
|
+
and the anchor of the selection */
|
|
130
|
+
export class Code {
|
|
131
|
+
/* bindings to the outside */
|
|
132
|
+
emit: (value: string) => void = () => {}
|
|
133
|
+
clip: (text: string) => void = () => {}
|
|
134
|
+
files: CodeFiles | null = null
|
|
135
|
+
ops: TextOps
|
|
136
|
+
height = 24
|
|
137
|
+
|
|
138
|
+
/* editing state */
|
|
139
|
+
value: string
|
|
140
|
+
off = 0
|
|
141
|
+
anchor: number | null = null
|
|
142
|
+
message = ""
|
|
143
|
+
error = false
|
|
144
|
+
|
|
145
|
+
/* internal state */
|
|
146
|
+
private seen: string
|
|
147
|
+
private cmd = ""
|
|
148
|
+
private lastCmd = ""
|
|
149
|
+
private recorded = false
|
|
150
|
+
private undos: Snap[] = []
|
|
151
|
+
private redos: Snap[] = []
|
|
152
|
+
private clipboard = ""
|
|
153
|
+
private clipLine = false
|
|
154
|
+
private goal: number | null = null
|
|
155
|
+
private input: Input | null = null
|
|
156
|
+
private question: Question | null = null
|
|
157
|
+
private rep: Replace | null = null
|
|
158
|
+
private searching = ""
|
|
159
|
+
private lastSearch = ""
|
|
160
|
+
|
|
161
|
+
constructor (value: string, ops: TextOps) {
|
|
162
|
+
this.value = value
|
|
163
|
+
this.seen = value
|
|
164
|
+
this.ops = ops
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/* ==== EXTERNAL INTERFACE ==== */
|
|
168
|
+
|
|
169
|
+
/* take over an externally changed value */
|
|
170
|
+
sync (value: string) {
|
|
171
|
+
if (value === this.seen)
|
|
172
|
+
return
|
|
173
|
+
this.seen = value
|
|
174
|
+
if (value === this.value)
|
|
175
|
+
return
|
|
176
|
+
this.value = value
|
|
177
|
+
this.off = Math.min(this.off, value.length)
|
|
178
|
+
this.anchor = null
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/* handle the key tokens of a key press (like [ "x" ], [ "ctrl+s" ], or [ "shift+left" ]) */
|
|
182
|
+
keys (tokens: string[]): boolean {
|
|
183
|
+
for (const t of tokens)
|
|
184
|
+
this.dispatch(t)
|
|
185
|
+
return true
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* handle a bracketed paste */
|
|
189
|
+
paste (text: string) {
|
|
190
|
+
if (this.input !== null) {
|
|
191
|
+
this.input.text += text.replace(/\r?\n/g, " ")
|
|
192
|
+
this.input.change?.(this.input.text)
|
|
193
|
+
}
|
|
194
|
+
else if (this.question === null && this.rep === null) {
|
|
195
|
+
this.recorded = false
|
|
196
|
+
this.type(text)
|
|
197
|
+
this.lastCmd = "paste"
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/* take over a text change of the front end */
|
|
202
|
+
onText (value: string) {
|
|
203
|
+
this.recorded = false
|
|
204
|
+
this.record()
|
|
205
|
+
this.value = value
|
|
206
|
+
this.off = Math.min(this.off, value.length)
|
|
207
|
+
this.anchor = null
|
|
208
|
+
this.emit(value)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/* take over a cursor change of the front end (a mouse click) */
|
|
212
|
+
onCursor (cursor: [ number, number ]) {
|
|
213
|
+
if (this.prompt() === null) {
|
|
214
|
+
this.off = toOffset(this.value, cursor)
|
|
215
|
+
this.lastCmd = ""
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/* take over a mouse selection */
|
|
220
|
+
onSelect (sel: [ number, number ] | null): boolean {
|
|
221
|
+
if (this.prompt() !== null)
|
|
222
|
+
return false
|
|
223
|
+
this.lastCmd = ""
|
|
224
|
+
if (!sel || sel[0] === sel[1]) {
|
|
225
|
+
if (this.anchor === null)
|
|
226
|
+
return false
|
|
227
|
+
this.anchor = null
|
|
228
|
+
return true
|
|
229
|
+
}
|
|
230
|
+
this.anchor = sel[0]
|
|
231
|
+
this.off = sel[1]
|
|
232
|
+
return true
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/* the selection to be drawn */
|
|
236
|
+
selection (): [ number, number ] | null {
|
|
237
|
+
if (this.rep !== null)
|
|
238
|
+
return this.rep.match
|
|
239
|
+
return this.range()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/* the prompt of the status line (with its input), or null without one */
|
|
243
|
+
prompt (): string | null {
|
|
244
|
+
if (this.question !== null)
|
|
245
|
+
return this.question.prompt
|
|
246
|
+
if (this.input !== null)
|
|
247
|
+
return this.input.prompt() + this.input.text
|
|
248
|
+
if (this.rep !== null)
|
|
249
|
+
return "Replace? (y)es (n)o (a)ll (esc) "
|
|
250
|
+
return null
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* the text being searched resp. replaced, to be highlighted */
|
|
254
|
+
searched (): string | null {
|
|
255
|
+
if (this.rep !== null)
|
|
256
|
+
return this.rep.from
|
|
257
|
+
return this.searching !== "" ? this.searching : null
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/* ==== KEY DISPATCHING ==== */
|
|
261
|
+
|
|
262
|
+
/* dispatch a key token by the current input state */
|
|
263
|
+
private dispatch (t: string) {
|
|
264
|
+
this.recorded = false
|
|
265
|
+
if (this.question !== null) {
|
|
266
|
+
const q = this.question
|
|
267
|
+
this.question = null
|
|
268
|
+
q.take(t)
|
|
269
|
+
}
|
|
270
|
+
else if (this.input !== null)
|
|
271
|
+
this.inputKey(t)
|
|
272
|
+
else if (this.rep !== null)
|
|
273
|
+
this.replaceKey(t)
|
|
274
|
+
else
|
|
275
|
+
this.bufferKey(t)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/* handle a key of the buffer */
|
|
279
|
+
private bufferKey (t: string) {
|
|
280
|
+
this.message = ""
|
|
281
|
+
this.error = false
|
|
282
|
+
let cmd: Command | undefined = BINDINGS.get(t)
|
|
283
|
+
let select = false
|
|
284
|
+
if (cmd === undefined && t.includes("shift+")) {
|
|
285
|
+
const base = BINDINGS.get(t.replace("shift+", ""))
|
|
286
|
+
if (base !== undefined && base.startsWith("cursor")) {
|
|
287
|
+
cmd = base
|
|
288
|
+
select = true
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (cmd === undefined && isChar(t))
|
|
292
|
+
cmd = "type"
|
|
293
|
+
if (cmd === undefined) {
|
|
294
|
+
this.lastCmd = ""
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
this.cmd = cmd
|
|
298
|
+
this.exec(cmd, t, select)
|
|
299
|
+
this.cmd = ""
|
|
300
|
+
this.lastCmd = cmd
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/* ==== COMMANDS ==== */
|
|
304
|
+
|
|
305
|
+
/* execute a command with its key and whether it extends the selection */
|
|
306
|
+
private exec (cmd: Command, t: string, select: boolean) {
|
|
307
|
+
const v = this.value
|
|
308
|
+
const o = this.off
|
|
309
|
+
const r = this.range()
|
|
310
|
+
switch (cmd) {
|
|
311
|
+
/* ---- typing and deleting ---- */
|
|
312
|
+
case "type":
|
|
313
|
+
this.type(t)
|
|
314
|
+
break
|
|
315
|
+
case "newline": {
|
|
316
|
+
const s = r ? r[0] : o
|
|
317
|
+
const indent = /^[ \t]*/.exec(v.slice(lineStart(v, s), s))?.[0] ?? ""
|
|
318
|
+
this.type("\n" + indent)
|
|
319
|
+
break
|
|
320
|
+
}
|
|
321
|
+
case "tab":
|
|
322
|
+
if (r !== null && lineNo(v, r[0]) !== lineNo(v, r[1]))
|
|
323
|
+
this.indent(true)
|
|
324
|
+
else {
|
|
325
|
+
const unit = this.indentUnit()
|
|
326
|
+
const col = (r ? r[0] : o) - lineStart(v, r ? r[0] : o)
|
|
327
|
+
this.type(unit === "\t" ? "\t" : " ".repeat(unit.length - col % unit.length))
|
|
328
|
+
}
|
|
329
|
+
break
|
|
330
|
+
case "indentLines": case "outdentLines":
|
|
331
|
+
this.indent(cmd === "indentLines")
|
|
332
|
+
break
|
|
333
|
+
case "deleteLeft": case "deleteRight": case "deleteWordLeft": case "deleteWordRight": {
|
|
334
|
+
if (r !== null) {
|
|
335
|
+
this.cut(r[0], r[1])
|
|
336
|
+
break
|
|
337
|
+
}
|
|
338
|
+
const p = cmd === "deleteLeft" ? this.ops.previous(v, o)
|
|
339
|
+
: cmd === "deleteRight" ? this.ops.next(v, o)
|
|
340
|
+
: cmd === "deleteWordLeft" ? wordBegin(v, o, isIdent)
|
|
341
|
+
: wordEnd(v, o, isIdent)
|
|
342
|
+
this.cut(Math.min(o, p), Math.max(o, p))
|
|
343
|
+
break
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/* ---- the clipboard ---- */
|
|
347
|
+
case "cut": case "copy": {
|
|
348
|
+
const [ s, e ] = r ?? this.lineRange(o)
|
|
349
|
+
const text = r ? v.slice(s, e) : this.lineText(o)
|
|
350
|
+
this.clipboard = text
|
|
351
|
+
this.clipLine = r === null
|
|
352
|
+
this.clip(text)
|
|
353
|
+
|
|
354
|
+
/* a line cut of the last line removes its preceding newline (like deleteLines) */
|
|
355
|
+
if (cmd === "cut")
|
|
356
|
+
this.cut(r === null && e === v.length && s > 0 ? s - 1 : s, e)
|
|
357
|
+
break
|
|
358
|
+
}
|
|
359
|
+
case "paste":
|
|
360
|
+
if (this.clipboard === "")
|
|
361
|
+
break
|
|
362
|
+
if (this.clipLine && r === null) {
|
|
363
|
+
const s = lineStart(v, o)
|
|
364
|
+
this.replace(s, s, this.clipboard)
|
|
365
|
+
this.off = o + this.clipboard.length
|
|
366
|
+
}
|
|
367
|
+
else
|
|
368
|
+
this.type(this.clipboard)
|
|
369
|
+
break
|
|
370
|
+
|
|
371
|
+
/* ---- undoing ---- */
|
|
372
|
+
case "undo": case "redo":
|
|
373
|
+
this.undo(cmd === "undo")
|
|
374
|
+
break
|
|
375
|
+
|
|
376
|
+
/* ---- selecting ---- */
|
|
377
|
+
case "selectAll":
|
|
378
|
+
this.anchor = 0
|
|
379
|
+
this.off = v.length
|
|
380
|
+
break
|
|
381
|
+
case "cancelSelection":
|
|
382
|
+
this.anchor = null
|
|
383
|
+
break
|
|
384
|
+
case "selectLine": {
|
|
385
|
+
if (r === null)
|
|
386
|
+
this.anchor = lineStart(v, o)
|
|
387
|
+
const e = lineEnd(v, o)
|
|
388
|
+
this.off = e < v.length ? e + 1 : e
|
|
389
|
+
break
|
|
390
|
+
}
|
|
391
|
+
case "selectNextMatch": {
|
|
392
|
+
if (r === null) {
|
|
393
|
+
let s = o
|
|
394
|
+
let e = o
|
|
395
|
+
while (s > 0 && isIdent(v[s - 1]))
|
|
396
|
+
s--
|
|
397
|
+
while (e < v.length && isIdent(v[e]))
|
|
398
|
+
e++
|
|
399
|
+
if (e > s) {
|
|
400
|
+
this.anchor = s
|
|
401
|
+
this.off = e
|
|
402
|
+
}
|
|
403
|
+
break
|
|
404
|
+
}
|
|
405
|
+
const m = findNext(v, v.slice(r[0], r[1]), r[1], false)
|
|
406
|
+
if (m !== null) {
|
|
407
|
+
this.anchor = m.match[0]
|
|
408
|
+
this.off = m.match[1]
|
|
409
|
+
}
|
|
410
|
+
break
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/* ---- lines ---- */
|
|
414
|
+
case "deleteLines": {
|
|
415
|
+
const [ first, last ] = this.lines()
|
|
416
|
+
const s = lineAt(v, first)
|
|
417
|
+
const e = lineEnd(v, lineAt(v, last))
|
|
418
|
+
if (e < v.length)
|
|
419
|
+
this.cut(s, e + 1)
|
|
420
|
+
else
|
|
421
|
+
this.cut(s > 0 ? s - 1 : s, e)
|
|
422
|
+
this.off = lineStart(this.value, Math.min(s, this.value.length))
|
|
423
|
+
break
|
|
424
|
+
}
|
|
425
|
+
case "insertLineAfter": case "insertLineBefore": {
|
|
426
|
+
const s = lineStart(v, o)
|
|
427
|
+
const indent = /^[ \t]*/.exec(v.slice(s, lineEnd(v, o)))?.[0] ?? ""
|
|
428
|
+
this.anchor = null
|
|
429
|
+
if (cmd === "insertLineAfter") {
|
|
430
|
+
const e = lineEnd(v, o)
|
|
431
|
+
this.replace(e, e, "\n" + indent)
|
|
432
|
+
this.off = e + 1 + indent.length
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
this.replace(s, s, indent + "\n")
|
|
436
|
+
this.off = s + indent.length
|
|
437
|
+
}
|
|
438
|
+
break
|
|
439
|
+
}
|
|
440
|
+
case "moveLinesUp": case "moveLinesDown": case "copyLinesUp": case "copyLinesDown":
|
|
441
|
+
this.moveLines(cmd)
|
|
442
|
+
break
|
|
443
|
+
case "commentLine":
|
|
444
|
+
this.comment()
|
|
445
|
+
break
|
|
446
|
+
|
|
447
|
+
/* ---- moving ---- */
|
|
448
|
+
case "cursorLeft": case "cursorRight":
|
|
449
|
+
if (r !== null && !select)
|
|
450
|
+
this.moveTo(cmd === "cursorLeft" ? r[0] : r[1], false)
|
|
451
|
+
else
|
|
452
|
+
this.moveTo(cmd === "cursorLeft" ? this.ops.previous(v, o) : this.ops.next(v, o), select)
|
|
453
|
+
break
|
|
454
|
+
case "cursorUp": case "cursorDown": case "cursorPageUp": case "cursorPageDown": {
|
|
455
|
+
if (![ "cursorUp", "cursorDown", "cursorPageUp", "cursorPageDown" ].includes(this.lastCmd))
|
|
456
|
+
this.goal = goalColumn(this.ops, v, o)
|
|
457
|
+
const dir = cmd === "cursorUp" || cmd === "cursorPageUp" ? -1 : 1
|
|
458
|
+
const rows = cmd.includes("Page") ? Math.max(1, this.height - 2) : 1
|
|
459
|
+
let p = o
|
|
460
|
+
for (let i = 0; i < rows; i++)
|
|
461
|
+
p = rowMove(this.ops, v, p, dir, this.goal)
|
|
462
|
+
this.moveTo(p, select)
|
|
463
|
+
break
|
|
464
|
+
}
|
|
465
|
+
case "cursorHome": {
|
|
466
|
+
const s = lineStart(v, o)
|
|
467
|
+
let p = s
|
|
468
|
+
while (p < lineEnd(v, o) && isSpace(v[p]))
|
|
469
|
+
p++
|
|
470
|
+
this.moveTo(o === p ? s : p, select)
|
|
471
|
+
break
|
|
472
|
+
}
|
|
473
|
+
case "cursorEnd":
|
|
474
|
+
this.moveTo(lineEnd(v, o), select)
|
|
475
|
+
break
|
|
476
|
+
case "cursorTop":
|
|
477
|
+
this.moveTo(0, select)
|
|
478
|
+
break
|
|
479
|
+
case "cursorBottom":
|
|
480
|
+
this.moveTo(v.length, select)
|
|
481
|
+
break
|
|
482
|
+
case "cursorWordLeft":
|
|
483
|
+
this.moveTo(wordBegin(v, o, isIdent), select)
|
|
484
|
+
break
|
|
485
|
+
case "cursorWordRight":
|
|
486
|
+
this.moveTo(wordEnd(v, o, isIdent), select)
|
|
487
|
+
break
|
|
488
|
+
|
|
489
|
+
/* ---- finding and replacing ---- */
|
|
490
|
+
case "find":
|
|
491
|
+
this.find()
|
|
492
|
+
break
|
|
493
|
+
case "replace":
|
|
494
|
+
this.replaceStart()
|
|
495
|
+
break
|
|
496
|
+
case "gotoLine":
|
|
497
|
+
this.read(() => "Go to line: ", "", (text) => {
|
|
498
|
+
const m = /^\s*(\d+)\s*(?::\s*(\d+))?\s*$/.exec(text)
|
|
499
|
+
if (!m)
|
|
500
|
+
return
|
|
501
|
+
const s = lineAt(this.value, Math.max(0, parseInt(m[1], 10) - 1))
|
|
502
|
+
const c = m[2] !== undefined ? parseInt(m[2], 10) - 1 : 0
|
|
503
|
+
this.moveTo(s + Math.max(0, Math.min(c, lineEnd(this.value, s) - s)), false)
|
|
504
|
+
})
|
|
505
|
+
break
|
|
506
|
+
|
|
507
|
+
/* ---- files and quitting ---- */
|
|
508
|
+
case "save":
|
|
509
|
+
this.save(() => {})
|
|
510
|
+
break
|
|
511
|
+
case "quit": {
|
|
512
|
+
const f = this.need()
|
|
513
|
+
if (f === null)
|
|
514
|
+
break
|
|
515
|
+
if (!f.modified()) {
|
|
516
|
+
f.quit()
|
|
517
|
+
break
|
|
518
|
+
}
|
|
519
|
+
this.ask("Save changes before closing? (y)es (n)o (esc) ",
|
|
520
|
+
() => this.save(() => f.quit()), () => f.quit())
|
|
521
|
+
break
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/* ==== HELPERS ==== */
|
|
527
|
+
|
|
528
|
+
/* the ordered selection, or null without one */
|
|
529
|
+
private range (): [ number, number ] | null {
|
|
530
|
+
if (this.anchor === null || this.anchor === this.off)
|
|
531
|
+
return null
|
|
532
|
+
return this.anchor < this.off ? [ this.anchor, this.off ] : [ this.off, this.anchor ]
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/* move the cursor, extending resp. dropping the selection */
|
|
536
|
+
private moveTo (p: number, select: boolean) {
|
|
537
|
+
if (select && this.anchor === null)
|
|
538
|
+
this.anchor = this.off
|
|
539
|
+
else if (!select)
|
|
540
|
+
this.anchor = null
|
|
541
|
+
this.off = p
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/* the range of the line of an offset with its newline, and its text with a newline */
|
|
545
|
+
private lineRange (o: number): [ number, number ] {
|
|
546
|
+
const e = lineEnd(this.value, o)
|
|
547
|
+
return [ lineStart(this.value, o), e < this.value.length ? e + 1 : e ]
|
|
548
|
+
}
|
|
549
|
+
private lineText (o: number) {
|
|
550
|
+
return this.value.slice(lineStart(this.value, o), lineEnd(this.value, o)) + "\n"
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/* the first and last line of the selection resp. of the cursor
|
|
554
|
+
(a selection ending at a line start excludes that line) */
|
|
555
|
+
private lines (): [ number, number ] {
|
|
556
|
+
const v = this.value
|
|
557
|
+
const [ s, e ] = this.range() ?? [ this.off, this.off ]
|
|
558
|
+
const last = e > s && e === lineStart(v, e) ? lineNo(v, e) - 1 : lineNo(v, e)
|
|
559
|
+
return [ lineNo(v, s), last ]
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/* the unit of indentation: a tab if more lines are indented by tabs than by spaces, else 4 spaces */
|
|
563
|
+
private indentUnit () {
|
|
564
|
+
const tabs = (this.value.match(/^\t/gm) ?? []).length
|
|
565
|
+
const spaces = (this.value.match(/^ {2}/gm) ?? []).length
|
|
566
|
+
return tabs > spaces ? "\t" : " "
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/* type a text, replacing the selection */
|
|
570
|
+
private type (text: string) {
|
|
571
|
+
const r = this.range()
|
|
572
|
+
const [ s, e ] = r ?? [ this.off, this.off ]
|
|
573
|
+
this.replace(s, e, text)
|
|
574
|
+
this.off = s + text.length
|
|
575
|
+
this.anchor = null
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/* delete a range of the text */
|
|
579
|
+
private cut (s: number, e: number) {
|
|
580
|
+
this.replace(s, e, "")
|
|
581
|
+
this.off = s
|
|
582
|
+
this.anchor = null
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/* replace a range of the text, keeping the selection on the surrounding text */
|
|
586
|
+
private replace (s: number, e: number, text: string) {
|
|
587
|
+
if (s === e && text === "")
|
|
588
|
+
return
|
|
589
|
+
this.record()
|
|
590
|
+
const d = text.length - (e - s)
|
|
591
|
+
const adjust = (p: number) => p > e || (p === e && e > s) ? p + d : p > s ? s : p
|
|
592
|
+
this.value = this.value.slice(0, s) + text + this.value.slice(e)
|
|
593
|
+
if (this.anchor !== null)
|
|
594
|
+
this.anchor = adjust(this.anchor)
|
|
595
|
+
this.emit(this.value)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/* replace lines from bottom to top, keeping the cursor and the selection on their characters */
|
|
599
|
+
private eachLine (first: number, last: number, change: (line: string) => string) {
|
|
600
|
+
for (let l = last; l >= first; l--) {
|
|
601
|
+
const s = lineAt(this.value, l)
|
|
602
|
+
const e = lineEnd(this.value, s)
|
|
603
|
+
const line = this.value.slice(s, e)
|
|
604
|
+
const text = change(line)
|
|
605
|
+
if (text === line)
|
|
606
|
+
continue
|
|
607
|
+
const d = text.length - line.length
|
|
608
|
+
const adjust = (p: number) => p <= s ? p : p >= e ? p + d : Math.max(s, p + d)
|
|
609
|
+
const off = adjust(this.off)
|
|
610
|
+
const anchor = this.anchor !== null ? adjust(this.anchor) : null
|
|
611
|
+
this.replace(s, e, text)
|
|
612
|
+
this.off = off
|
|
613
|
+
this.anchor = anchor
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/* indent resp. outdent the lines of the selection resp. of the cursor */
|
|
618
|
+
private indent (indent: boolean) {
|
|
619
|
+
const [ first, last ] = this.lines()
|
|
620
|
+
const unit = this.indentUnit()
|
|
621
|
+
const lead = new RegExp(`^(\\t| {1,${unit.length === 1 ? 4 : unit.length}})`)
|
|
622
|
+
this.eachLine(first, last, (line) =>
|
|
623
|
+
indent ? (line === "" ? line : unit + line) : line.replace(lead, ""))
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/* toggle the line comments of the lines of the selection resp. of the cursor */
|
|
627
|
+
private comment () {
|
|
628
|
+
const name = this.files?.name() ?? ""
|
|
629
|
+
const prefix = COMMENTS[/\.([^./]+)$/.exec(name)?.[1]?.toLowerCase() ?? ""] ?? "#"
|
|
630
|
+
const [ first, last ] = this.lines()
|
|
631
|
+
const lines: string[] = []
|
|
632
|
+
for (let l = first; l <= last; l++)
|
|
633
|
+
lines.push(this.value.slice(lineAt(this.value, l), lineEnd(this.value, lineAt(this.value, l))))
|
|
634
|
+
const used = lines.filter((line) => !/^\s*$/.test(line))
|
|
635
|
+
if (used.length === 0)
|
|
636
|
+
return
|
|
637
|
+
const commented = used.every((line) => line.trimStart().startsWith(prefix))
|
|
638
|
+
const column = Math.min(...used.map((line) => /^[ \t]*/.exec(line)?.[0].length ?? 0))
|
|
639
|
+
const re = new RegExp(`^([ \\t]*)${escapeRegExp(prefix)} ?`)
|
|
640
|
+
this.eachLine(first, last, (line) => /^\s*$/.test(line) ? line
|
|
641
|
+
: commented ? line.replace(re, "$1")
|
|
642
|
+
: line.slice(0, column) + prefix + " " + line.slice(column))
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/* move resp. copy the lines of the selection resp. of the cursor up or down */
|
|
646
|
+
private moveLines (cmd: Command) {
|
|
647
|
+
const v = this.value
|
|
648
|
+
const [ first, last ] = this.lines()
|
|
649
|
+
const bs = lineAt(v, first)
|
|
650
|
+
const be = lineEnd(v, lineAt(v, last))
|
|
651
|
+
const block = v.slice(bs, be)
|
|
652
|
+
const shift = (d: number) => {
|
|
653
|
+
this.off += d
|
|
654
|
+
if (this.anchor !== null)
|
|
655
|
+
this.anchor += d
|
|
656
|
+
}
|
|
657
|
+
const anchor = this.anchor
|
|
658
|
+
if (cmd === "moveLinesUp" && first > 0) {
|
|
659
|
+
const as = lineAt(v, first - 1)
|
|
660
|
+
const above = v.slice(as, bs - 1)
|
|
661
|
+
this.replace(as, be, block + "\n" + above)
|
|
662
|
+
this.anchor = anchor
|
|
663
|
+
shift(-(above.length + 1))
|
|
664
|
+
}
|
|
665
|
+
else if (cmd === "moveLinesDown" && be < v.length) {
|
|
666
|
+
const ne = lineEnd(v, be + 1)
|
|
667
|
+
const below = v.slice(be + 1, ne)
|
|
668
|
+
this.replace(bs, ne, below + "\n" + block)
|
|
669
|
+
this.anchor = anchor
|
|
670
|
+
shift(below.length + 1)
|
|
671
|
+
}
|
|
672
|
+
else if (cmd === "copyLinesUp") {
|
|
673
|
+
this.replace(bs, bs, block + "\n")
|
|
674
|
+
this.anchor = anchor
|
|
675
|
+
}
|
|
676
|
+
else if (cmd === "copyLinesDown") {
|
|
677
|
+
this.replace(be, be, "\n" + block)
|
|
678
|
+
this.anchor = anchor
|
|
679
|
+
shift(block.length + 1)
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/* record the text for undoing, once per key and amalgamating typed characters */
|
|
684
|
+
private record () {
|
|
685
|
+
if (this.recorded)
|
|
686
|
+
return
|
|
687
|
+
this.recorded = true
|
|
688
|
+
this.redos = []
|
|
689
|
+
if (this.cmd === "type" && this.lastCmd === "type")
|
|
690
|
+
return
|
|
691
|
+
this.undos.push({ value: this.value, off: this.off, anchor: this.anchor })
|
|
692
|
+
if (this.undos.length > 2000)
|
|
693
|
+
this.undos.shift()
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/* undo resp. redo a change */
|
|
697
|
+
private undo (undo: boolean) {
|
|
698
|
+
const from = undo ? this.undos : this.redos
|
|
699
|
+
const to = undo ? this.redos : this.undos
|
|
700
|
+
const snap = from.pop()
|
|
701
|
+
if (snap === undefined)
|
|
702
|
+
return
|
|
703
|
+
to.push({ value: this.value, off: this.off, anchor: this.anchor })
|
|
704
|
+
this.value = snap.value
|
|
705
|
+
this.off = Math.min(snap.off, snap.value.length)
|
|
706
|
+
this.anchor = snap.anchor !== null ? Math.min(snap.anchor, snap.value.length) : null
|
|
707
|
+
this.emit(this.value)
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/* ==== FINDING AND REPLACING ==== */
|
|
711
|
+
|
|
712
|
+
/* the initial text of finding: the single-line selection, else the last searched text */
|
|
713
|
+
private findInit () {
|
|
714
|
+
const r = this.range()
|
|
715
|
+
const sel = r ? this.value.slice(r[0], r[1]) : ""
|
|
716
|
+
return sel !== "" && !sel.includes("\n") ? sel : this.lastSearch
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/* the number of the current match and the number of all matches */
|
|
720
|
+
private findCount (text: string) {
|
|
721
|
+
const all = [ ...this.value.matchAll(literalRegExp(text, true)) ]
|
|
722
|
+
if (all.length === 0)
|
|
723
|
+
return "No results"
|
|
724
|
+
const r = this.range()
|
|
725
|
+
const i = r ? all.findIndex((m) => m.index === r[0]) : -1
|
|
726
|
+
return `${i >= 0 ? i + 1 : "?"} of ${all.length}`
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/* find a text incrementally, with Enter resp. Down for the next match, Up for the previous one,
|
|
730
|
+
and Escape closing the find */
|
|
731
|
+
private find () {
|
|
732
|
+
const origin = this.range()?.[0] ?? this.off
|
|
733
|
+
const select = (m: { match: [ number, number ] } | null) => {
|
|
734
|
+
if (m !== null) {
|
|
735
|
+
this.anchor = m.match[0]
|
|
736
|
+
this.off = m.match[1]
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
const init = this.findInit()
|
|
740
|
+
this.searching = init
|
|
741
|
+
this.read(() => this.searching !== "" ? `Find (${this.findCount(this.searching)}): ` : "Find: ", init,
|
|
742
|
+
() => {}, {
|
|
743
|
+
change: (text) => {
|
|
744
|
+
this.searching = text
|
|
745
|
+
if (text !== "")
|
|
746
|
+
select(findNext(this.value, text, origin, true))
|
|
747
|
+
},
|
|
748
|
+
key: (t) => {
|
|
749
|
+
if (this.searching === "" || ![ "enter", "down", "up", "shift+enter" ].includes(t))
|
|
750
|
+
return false
|
|
751
|
+
const r = this.range() ?? [ this.off, this.off ]
|
|
752
|
+
select(t === "up" || t === "shift+enter"
|
|
753
|
+
? findPrevious(this.value, this.searching, r[0], true)
|
|
754
|
+
: findNext(this.value, this.searching, r[1], true))
|
|
755
|
+
return true
|
|
756
|
+
},
|
|
757
|
+
close: () => {
|
|
758
|
+
if (this.searching !== "")
|
|
759
|
+
this.lastSearch = this.searching
|
|
760
|
+
this.searching = ""
|
|
761
|
+
}
|
|
762
|
+
})
|
|
763
|
+
if (init !== "")
|
|
764
|
+
select(findNext(this.value, init, origin, true))
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/* replace a text, read with its replacement, match by match from the cursor, wrapping around up to it */
|
|
768
|
+
private replaceStart () {
|
|
769
|
+
this.read(() => "Find: ", this.findInit(), (from) => {
|
|
770
|
+
if (from === "")
|
|
771
|
+
return
|
|
772
|
+
this.lastSearch = from
|
|
773
|
+
this.read(() => `Replace ${from} with: `, "", (to) => {
|
|
774
|
+
const start = this.range()?.[0] ?? this.off
|
|
775
|
+
this.anchor = null
|
|
776
|
+
this.rep = { from, to, pos: start, stop: start, wrapped: false, match: [ start, start ], count: 0 }
|
|
777
|
+
this.replaceNext()
|
|
778
|
+
})
|
|
779
|
+
})
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/* find the next match to replace, or end the replacing */
|
|
783
|
+
private replaceNext () {
|
|
784
|
+
const q = this.rep
|
|
785
|
+
if (q === null)
|
|
786
|
+
return
|
|
787
|
+
const find = (from: number) => {
|
|
788
|
+
const re = literalRegExp(q.from, true)
|
|
789
|
+
re.lastIndex = from
|
|
790
|
+
const m = re.exec(this.value)
|
|
791
|
+
return m === null || (q.wrapped && m.index + m[0].length > q.stop) ? null : m
|
|
792
|
+
}
|
|
793
|
+
let m = find(q.pos)
|
|
794
|
+
if (m === null && !q.wrapped) {
|
|
795
|
+
q.wrapped = true
|
|
796
|
+
m = find(0)
|
|
797
|
+
}
|
|
798
|
+
if (m === null) {
|
|
799
|
+
this.rep = null
|
|
800
|
+
this.say(`Replaced ${plural(q.count, "occurrence")}`)
|
|
801
|
+
return
|
|
802
|
+
}
|
|
803
|
+
q.match = [ m.index, m.index + m[0].length ]
|
|
804
|
+
this.off = q.match[1]
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/* handle a key of the replace question */
|
|
808
|
+
private replaceKey (t: string) {
|
|
809
|
+
const q = this.rep
|
|
810
|
+
if (q === null)
|
|
811
|
+
return
|
|
812
|
+
const one = () => {
|
|
813
|
+
const [ s, e ] = q.match
|
|
814
|
+
this.replace(s, e, q.to)
|
|
815
|
+
q.pos = s + q.to.length
|
|
816
|
+
if (q.wrapped)
|
|
817
|
+
q.stop += q.to.length - (e - s)
|
|
818
|
+
q.count++
|
|
819
|
+
this.off = q.pos
|
|
820
|
+
}
|
|
821
|
+
if (t === "y" || t === "enter") {
|
|
822
|
+
one()
|
|
823
|
+
this.replaceNext()
|
|
824
|
+
}
|
|
825
|
+
else if (t === "n") {
|
|
826
|
+
q.pos = q.match[1]
|
|
827
|
+
this.replaceNext()
|
|
828
|
+
}
|
|
829
|
+
else if (t === "a") {
|
|
830
|
+
while (this.rep !== null) {
|
|
831
|
+
one()
|
|
832
|
+
this.replaceNext()
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
else if (t === "escape") {
|
|
836
|
+
this.rep = null
|
|
837
|
+
this.say(`Replaced ${plural(q.count, "occurrence")}`)
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/* ==== PROMPTS ==== */
|
|
842
|
+
|
|
843
|
+
/* read a text on the status line */
|
|
844
|
+
private read (prompt: () => string, init: string, done: (text: string) => void,
|
|
845
|
+
hooks: Pick<Input, "change" | "key" | "close"> = {}) {
|
|
846
|
+
this.input = { prompt, text: init, done, ...hooks }
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/* ask a yes/no question (Escape cancels) */
|
|
850
|
+
private ask (question: string, yes: () => void, no: () => void) {
|
|
851
|
+
this.question = { prompt: question, take: (t) => {
|
|
852
|
+
if (t === "y" || t === "Y")
|
|
853
|
+
yes()
|
|
854
|
+
else if (t === "n" || t === "N")
|
|
855
|
+
no()
|
|
856
|
+
else if (t !== "escape" && t !== "ctrl+c")
|
|
857
|
+
this.ask(question, yes, no)
|
|
858
|
+
} }
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/* handle a key of the text input */
|
|
862
|
+
private inputKey (t: string) {
|
|
863
|
+
const p = this.input
|
|
864
|
+
if (p === null)
|
|
865
|
+
return
|
|
866
|
+
if (p.key?.(t))
|
|
867
|
+
return
|
|
868
|
+
if (isChar(t)) {
|
|
869
|
+
p.text += t
|
|
870
|
+
p.change?.(p.text)
|
|
871
|
+
}
|
|
872
|
+
else if (t === "backspace") {
|
|
873
|
+
p.text = p.text.slice(0, this.ops.previous(p.text, p.text.length))
|
|
874
|
+
p.change?.(p.text)
|
|
875
|
+
}
|
|
876
|
+
else if (t === "escape" || t === "ctrl+c") {
|
|
877
|
+
this.input = null
|
|
878
|
+
p.close?.()
|
|
879
|
+
}
|
|
880
|
+
else if (t === "enter") {
|
|
881
|
+
this.input = null
|
|
882
|
+
p.close?.()
|
|
883
|
+
p.done(p.text)
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/* ==== FILES ==== */
|
|
888
|
+
|
|
889
|
+
/* the file operations, or an error without them */
|
|
890
|
+
private need (): CodeFiles | null {
|
|
891
|
+
if (this.files === null)
|
|
892
|
+
this.fail("No file operations available")
|
|
893
|
+
return this.files
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/* save the text (asking for a file name without one), and continue after a successful save */
|
|
897
|
+
private save (then: () => void) {
|
|
898
|
+
const f = this.need()
|
|
899
|
+
if (f === null)
|
|
900
|
+
return
|
|
901
|
+
const done = (res: CodeOutcome) => {
|
|
902
|
+
if (res.error)
|
|
903
|
+
this.fail(res.message)
|
|
904
|
+
else {
|
|
905
|
+
this.say(res.message)
|
|
906
|
+
then()
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
if (f.name() !== "") {
|
|
910
|
+
done(f.write("", false, false))
|
|
911
|
+
return
|
|
912
|
+
}
|
|
913
|
+
this.read(() => "Save as: ", "", (file) => {
|
|
914
|
+
if (file === "")
|
|
915
|
+
return
|
|
916
|
+
const res = f.write(file, false, true)
|
|
917
|
+
if (res.exists)
|
|
918
|
+
this.ask(`File ${file} exists. Overwrite? (y)es (n)o `, () => done(f.write(file, true, true)), () => {})
|
|
919
|
+
else
|
|
920
|
+
done(res)
|
|
921
|
+
})
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/* ==== MESSAGES ==== */
|
|
925
|
+
|
|
926
|
+
/* show an informational message */
|
|
927
|
+
protected say (text: string) {
|
|
928
|
+
this.message = text
|
|
929
|
+
this.error = false
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/* show an error message */
|
|
933
|
+
protected fail (text: string) {
|
|
934
|
+
this.message = text
|
|
935
|
+
this.error = true
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|