@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,2203 @@
|
|
|
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 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, toCursor, toOffset, isSpace, isIdent } from "./ink-editor-text"
|
|
30
|
+
import type { TextOps } from "./ink-editor-text"
|
|
31
|
+
|
|
32
|
+
/* the editing modes */
|
|
33
|
+
export type Mode = "normal" | "insert" | "replace" | "visual" | "vline" | "cmdline"
|
|
34
|
+
|
|
35
|
+
/* a register, an undo snapshot, a text range, and the result of a motion */
|
|
36
|
+
type Reg = { text: string, linewise: boolean }
|
|
37
|
+
type Snap = { value: string, off: number }
|
|
38
|
+
type Range = { start: number, end: number, linewise: boolean }
|
|
39
|
+
type Motion = { to: number, linewise: boolean, inclusive: boolean, vertical?: boolean, want?: number }
|
|
40
|
+
|
|
41
|
+
/* a parsed normal or visual mode command */
|
|
42
|
+
type Cmd = {
|
|
43
|
+
reg: string
|
|
44
|
+
count: number
|
|
45
|
+
count2: number
|
|
46
|
+
op: string
|
|
47
|
+
arg: string
|
|
48
|
+
motion: string
|
|
49
|
+
marg: string
|
|
50
|
+
tokens: string[]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* the command vocabulary */
|
|
54
|
+
const MOTIONS = new Set([
|
|
55
|
+
"h", "j", "k", "l", "w", "b", "e", "W", "B", "E", "ge", "gE", "0", "^", "$", "g_", "|",
|
|
56
|
+
"G", "gg", "{", "}", "%", "f", "F", "t", "T", ";", ",", "n", "N", "*", "#", "+", "-", "_",
|
|
57
|
+
"`", "'", "<Left>", "<Right>", "<Up>", "<Down>", "<Home>", "<End>", "<BS>", "<C-h>", " ",
|
|
58
|
+
"<CR>", "<C-n>", "<C-p>", "<C-j>"
|
|
59
|
+
])
|
|
60
|
+
const NORMAL = new Set([
|
|
61
|
+
"x", "X", "D", "C", "s", "S", "Y", "p", "P", "J", "gJ", "u", "<C-r>", ".", "i", "a", "I", "A",
|
|
62
|
+
"gI", "gi", "o", "O", "R", "v", "V", "gv", "<C-v>", ":", "/", "?", "~", "&", "r", "m", "q",
|
|
63
|
+
"@", "ZZ", "ZQ", "zz", "zt", "zb", "z.", "z<CR>", "z-", "<C-d>", "<C-u>", "<C-f>", "<C-b>",
|
|
64
|
+
"<PageDown>", "<PageUp>", "<C-e>", "<C-y>", "<C-g>", "<C-a>", "<C-x>", "<C-c>", "<C-l>", "<Del>"
|
|
65
|
+
])
|
|
66
|
+
const VISUAL = new Set([
|
|
67
|
+
"d", "x", "X", "D", "<Del>", "y", "Y", "c", "s", "C", "S", "R", "r", "~", "u", "U", ">", "<",
|
|
68
|
+
"J", "gJ", "p", "P", "o", "O", "I", "A", ":", "v", "V", "<C-v>", "<Esc>", "<C-c>", "/", "?"
|
|
69
|
+
])
|
|
70
|
+
const OPERATORS = new Set([ "d", "c", "y", "<", ">", "gu", "gU", "g~" ])
|
|
71
|
+
const ARGS = new Set([ "f", "F", "t", "T", "`", "'", "r", "m", "q", "@" ])
|
|
72
|
+
const PREFIXES = new Set([ "g", "Z", "z" ])
|
|
73
|
+
const JUMPS = new Set([ "G", "gg", "{", "}", "%", "n", "N", "*", "#", "`", "'" ])
|
|
74
|
+
const OBJECT = /^[ia][wWp"'`()bB{}[\]<>]$/
|
|
75
|
+
const PASSABLE = new Set([ "<CR>", "<BS>", "<Del>", "<C-w>", "<Left>", "<Right>", "<Up>", "<Down>", "<Home>", "<End>" ])
|
|
76
|
+
const PAIRS: Record<string, [ string, string ]> = {
|
|
77
|
+
"(": [ "(", ")" ], ")": [ "(", ")" ], "b": [ "(", ")" ],
|
|
78
|
+
"{": [ "{", "}" ], "}": [ "{", "}" ], "B": [ "{", "}" ],
|
|
79
|
+
"[": [ "[", "]" ], "]": [ "[", "]" ],
|
|
80
|
+
"<": [ "<", ">" ], ">": [ "<", ">" ]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* line geometry of a text */
|
|
84
|
+
const lineEndNl = (v: string, o: number) => { const e = lineEnd(v, o); return e < v.length ? e + 1 : e }
|
|
85
|
+
const lastLine = (v: string) => lineNo(v, v.length)
|
|
86
|
+
const isEmpty = (v: string, l: number) => { const s = lineAt(v, l); return lineEnd(v, s) === s }
|
|
87
|
+
|
|
88
|
+
/* grapheme stepping (a newline is always a grapheme of its own, so segment within lines only) */
|
|
89
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" })
|
|
90
|
+
const prevGrapheme = (v: string, o: number) => {
|
|
91
|
+
if (o <= 0)
|
|
92
|
+
return 0
|
|
93
|
+
const s = lineStart(v, o)
|
|
94
|
+
if (s === o)
|
|
95
|
+
return o - 1
|
|
96
|
+
let last = s
|
|
97
|
+
for (const { index } of segmenter.segment(v.slice(s, o)))
|
|
98
|
+
last = s + index
|
|
99
|
+
return last
|
|
100
|
+
}
|
|
101
|
+
const nextGrapheme = (v: string, o: number) => {
|
|
102
|
+
if (o >= v.length)
|
|
103
|
+
return v.length
|
|
104
|
+
const e = lineEnd(v, o)
|
|
105
|
+
if (e === o)
|
|
106
|
+
return o + 1
|
|
107
|
+
const first = segmenter.segment(v.slice(o, e))[Symbol.iterator]().next().value
|
|
108
|
+
return o + (first ? first.segment.length : 1)
|
|
109
|
+
}
|
|
110
|
+
const firstNonBlank = (v: string, o: number) => {
|
|
111
|
+
let i = lineStart(v, o)
|
|
112
|
+
const e = lineEnd(v, i)
|
|
113
|
+
while (i < e && isSpace(v[i]))
|
|
114
|
+
i++
|
|
115
|
+
return i
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/* character classes of word motions: 0 = blank, 1 = punctuation, 2 = keyword */
|
|
119
|
+
const cls = (c: string | undefined, big = false) =>
|
|
120
|
+
c === undefined || /\s/.test(c) ? 0 : big ? 1 : isIdent(c) ? 2 : 1
|
|
121
|
+
const emptyAt = (v: string, i: number) =>
|
|
122
|
+
(i === v.length || v[i] === "\n") && (i === 0 || v[i - 1] === "\n")
|
|
123
|
+
|
|
124
|
+
/* word motions "w", "e", "b", and "ge" */
|
|
125
|
+
const wordFwd = (v: string, o: number, big: boolean) => {
|
|
126
|
+
let i = o
|
|
127
|
+
const c = cls(v[i], big)
|
|
128
|
+
if (c !== 0)
|
|
129
|
+
while (i < v.length && cls(v[i], big) === c)
|
|
130
|
+
i++
|
|
131
|
+
while (i < v.length && cls(v[i], big) === 0) {
|
|
132
|
+
if (i > o && emptyAt(v, i))
|
|
133
|
+
break
|
|
134
|
+
i++
|
|
135
|
+
}
|
|
136
|
+
return i
|
|
137
|
+
}
|
|
138
|
+
const wordEnd = (v: string, o: number, big: boolean) => {
|
|
139
|
+
let i = o + 1
|
|
140
|
+
while (i < v.length && cls(v[i], big) === 0)
|
|
141
|
+
i++
|
|
142
|
+
if (i >= v.length)
|
|
143
|
+
return Math.max(0, v.length - 1)
|
|
144
|
+
const c = cls(v[i], big)
|
|
145
|
+
while (i + 1 < v.length && cls(v[i + 1], big) === c)
|
|
146
|
+
i++
|
|
147
|
+
return i
|
|
148
|
+
}
|
|
149
|
+
const wordBack = (v: string, o: number, big: boolean) => {
|
|
150
|
+
let i = o - 1
|
|
151
|
+
while (i > 0 && cls(v[i], big) === 0 && !emptyAt(v, i))
|
|
152
|
+
i--
|
|
153
|
+
if (i <= 0 || emptyAt(v, i))
|
|
154
|
+
return Math.max(0, i)
|
|
155
|
+
const c = cls(v[i], big)
|
|
156
|
+
while (i > 0 && cls(v[i - 1], big) === c)
|
|
157
|
+
i--
|
|
158
|
+
return i
|
|
159
|
+
}
|
|
160
|
+
const wordEndBack = (v: string, o: number, big: boolean) => {
|
|
161
|
+
let i = o
|
|
162
|
+
const c = cls(v[i], big)
|
|
163
|
+
if (c !== 0)
|
|
164
|
+
while (i >= 0 && cls(v[i], big) === c)
|
|
165
|
+
i--
|
|
166
|
+
while (i >= 0 && cls(v[i], big) === 0) {
|
|
167
|
+
if (i < o && emptyAt(v, i))
|
|
168
|
+
return i
|
|
169
|
+
i--
|
|
170
|
+
}
|
|
171
|
+
return Math.max(0, i)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/* the bracket matching of "%" */
|
|
175
|
+
const matchPair = (v: string, i: number) => {
|
|
176
|
+
const [ open, close ] = PAIRS[v[i]]
|
|
177
|
+
const fwd = v[i] === open
|
|
178
|
+
let depth = 0
|
|
179
|
+
for (let j = i; fwd ? j < v.length : j >= 0; j += fwd ? 1 : -1) {
|
|
180
|
+
if (v[j] === v[i])
|
|
181
|
+
depth++
|
|
182
|
+
else if (v[j] === (fwd ? close : open) && --depth === 0)
|
|
183
|
+
return j
|
|
184
|
+
}
|
|
185
|
+
return -1
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* case conversion of "~" and "g~" */
|
|
189
|
+
const swapCase = (s: string) =>
|
|
190
|
+
s.replace(/\p{L}/gu, (c) => c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase())
|
|
191
|
+
|
|
192
|
+
/* conversion of a Vim "magic" pattern into a JavaScript regular expression */
|
|
193
|
+
export const toRegExp = (pattern: string, icase = false): RegExp | null => {
|
|
194
|
+
let flags = icase ? "gmi" : "gm"
|
|
195
|
+
let src = ""
|
|
196
|
+
let brace = false
|
|
197
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
198
|
+
const c = pattern[i]
|
|
199
|
+
if (c === "\\" && i + 1 < pattern.length) {
|
|
200
|
+
const d = pattern[++i]
|
|
201
|
+
if (d === "<" || d === ">") {
|
|
202
|
+
|
|
203
|
+
/* Unicode-aware word boundaries, consistent with "isIdent" */
|
|
204
|
+
const w = "[\\p{L}\\p{N}_]"
|
|
205
|
+
src += d === "<" ? `(?<!${w})(?=${w})` : `(?<=${w})(?!${w})`
|
|
206
|
+
flags = flags.includes("u") ? flags : flags + "u"
|
|
207
|
+
}
|
|
208
|
+
else if (d === "=")
|
|
209
|
+
src += "?"
|
|
210
|
+
else if ("(|)+?{}".includes(d)) {
|
|
211
|
+
src += d
|
|
212
|
+
brace = d === "{"
|
|
213
|
+
}
|
|
214
|
+
else if (d === "c")
|
|
215
|
+
flags = flags.includes("i") ? flags : flags + "i"
|
|
216
|
+
else if (d === "C")
|
|
217
|
+
flags = flags.replace("i", "")
|
|
218
|
+
else
|
|
219
|
+
src += "\\" + d
|
|
220
|
+
}
|
|
221
|
+
else if (c === "}" && brace) {
|
|
222
|
+
src += c
|
|
223
|
+
brace = false
|
|
224
|
+
}
|
|
225
|
+
else if ("(|)+?{}".includes(c))
|
|
226
|
+
src += "\\" + c
|
|
227
|
+
else
|
|
228
|
+
src += c
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
return new RegExp(src, flags)
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return null
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* escaping of a literal text for a Vim pattern */
|
|
239
|
+
const escapeVim = (s: string) => s.replace(/[\\/.*$^~[\]]/g, (c) => "\\" + c)
|
|
240
|
+
|
|
241
|
+
/* expansion of a ":s" replacement (with "&", "\0"-"\9", "\n", and "\t") */
|
|
242
|
+
const expandRep = (rep: string, args: unknown[]) => {
|
|
243
|
+
const idx = args.findIndex((a, i) => i > 0 && typeof a === "number")
|
|
244
|
+
const groups = (idx > 0 ? args.slice(0, idx) : [ args[0] ]) as (string | undefined)[]
|
|
245
|
+
let out = ""
|
|
246
|
+
for (let i = 0; i < rep.length; i++) {
|
|
247
|
+
const c = rep[i]
|
|
248
|
+
if (c === "\\" && i + 1 < rep.length) {
|
|
249
|
+
const d = rep[++i]
|
|
250
|
+
if (/[0-9]/.test(d))
|
|
251
|
+
out += groups[parseInt(d, 10)] ?? ""
|
|
252
|
+
else if (d === "n" || d === "r")
|
|
253
|
+
out += "\n"
|
|
254
|
+
else if (d === "t")
|
|
255
|
+
out += "\t"
|
|
256
|
+
else
|
|
257
|
+
out += d
|
|
258
|
+
}
|
|
259
|
+
else if (c === "&")
|
|
260
|
+
out += groups[0] ?? ""
|
|
261
|
+
else
|
|
262
|
+
out += c
|
|
263
|
+
}
|
|
264
|
+
return out
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/* splitting of a ":s" body at its unescaped delimiters */
|
|
268
|
+
const splitDelim = (body: string, delim: string) => {
|
|
269
|
+
const parts = [ "" ]
|
|
270
|
+
for (let i = 0; i < body.length; i++) {
|
|
271
|
+
if (body[i] === "\\" && body[i + 1] === delim)
|
|
272
|
+
parts[parts.length - 1] += body[++i]
|
|
273
|
+
else if (body[i] === "\\" && i + 1 < body.length)
|
|
274
|
+
parts[parts.length - 1] += body[i] + body[++i]
|
|
275
|
+
else if (body[i] === delim)
|
|
276
|
+
parts.push("")
|
|
277
|
+
else
|
|
278
|
+
parts[parts.length - 1] += body[i]
|
|
279
|
+
}
|
|
280
|
+
return parts
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/* the start of the word before an offset, past any blanks in between (insert mode "<C-w>") */
|
|
284
|
+
const wordStartBack = (v: string, o: number) => {
|
|
285
|
+
let i = o
|
|
286
|
+
while (i > 0 && cls(v[i - 1]) === 0)
|
|
287
|
+
i--
|
|
288
|
+
const c = cls(v[i - 1])
|
|
289
|
+
while (i > 0 && c !== 0 && cls(v[i - 1]) === c)
|
|
290
|
+
i--
|
|
291
|
+
return i
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/* whether a token is a printable text (a character or a bracketed paste) */
|
|
295
|
+
const isText = (t: string) => [ ...t ].length === 1 || t.startsWith("\u0000")
|
|
296
|
+
|
|
297
|
+
/* the default text operations, for a front end without its own */
|
|
298
|
+
export const defaultOps: TextOps = {
|
|
299
|
+
previous: prevGrapheme,
|
|
300
|
+
next: nextGrapheme,
|
|
301
|
+
wordStart: wordStartBack,
|
|
302
|
+
vertical: (v, o, direction) => {
|
|
303
|
+
const [ l, c ] = toCursor(v, o)
|
|
304
|
+
const t = l + direction
|
|
305
|
+
return t < 0 ? 0 : t > lastLine(v) ? v.length : toOffset(v, [ t, c ])
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/* the Vim editing engine, operating on a plain string value and a cursor offset */
|
|
310
|
+
export class Vim {
|
|
311
|
+
/* bindings to the outside */
|
|
312
|
+
emit: (value: string) => void = () => {}
|
|
313
|
+
host: (command: string) => string | null | undefined = () => undefined
|
|
314
|
+
clip: (text: string) => void = () => {}
|
|
315
|
+
ops: TextOps
|
|
316
|
+
height = 24
|
|
317
|
+
|
|
318
|
+
/* editing state */
|
|
319
|
+
mode: Mode = "normal"
|
|
320
|
+
value: string
|
|
321
|
+
off = 0
|
|
322
|
+
want = 0
|
|
323
|
+
anchor = 0
|
|
324
|
+
pend: string[] = []
|
|
325
|
+
message = ""
|
|
326
|
+
error = false
|
|
327
|
+
cmdKind = ":"
|
|
328
|
+
cmdText = ""
|
|
329
|
+
cmdFrom: Mode = "normal"
|
|
330
|
+
cmdHist: string[] = []
|
|
331
|
+
cmdIdx = -1
|
|
332
|
+
search: { pattern: string, forward: boolean } | null = null
|
|
333
|
+
hlsearch = true
|
|
334
|
+
ignorecase = false
|
|
335
|
+
smartcase = false
|
|
336
|
+
recording = ""
|
|
337
|
+
|
|
338
|
+
/* internal state */
|
|
339
|
+
private seen: string
|
|
340
|
+
private cur: [ number, number ] = [ 0, 0 ]
|
|
341
|
+
private regs = new Map<string, Reg>()
|
|
342
|
+
private marks = new Map<string, number>()
|
|
343
|
+
private macros = new Map<string, string[]>()
|
|
344
|
+
private macro: string[] = []
|
|
345
|
+
private lastMacro = ""
|
|
346
|
+
private lastEx = ""
|
|
347
|
+
private lastFind: { cmd: string, ch: string } | null = null
|
|
348
|
+
private lastSub: { pat: string, rep: string, flags: string } | null = null
|
|
349
|
+
private lastVisual: { mode: Mode, anchor: number, off: number } | null = null
|
|
350
|
+
private lastInsert = 0
|
|
351
|
+
private undos: Snap[] = []
|
|
352
|
+
private redos: Snap[] = []
|
|
353
|
+
private redoSave: Snap[] = []
|
|
354
|
+
private change: string[] = []
|
|
355
|
+
private recChange = false
|
|
356
|
+
private lastChange: string[] = []
|
|
357
|
+
private insCount = 1
|
|
358
|
+
private insOpen = false
|
|
359
|
+
private insTokens: string[] = []
|
|
360
|
+
private replaced: { off: number, orig: string, len: number }[] = []
|
|
361
|
+
private depth = 0
|
|
362
|
+
private failed = false
|
|
363
|
+
|
|
364
|
+
constructor (value: string, message = "", ops: TextOps = defaultOps) {
|
|
365
|
+
this.value = value
|
|
366
|
+
this.seen = value
|
|
367
|
+
this.message = message
|
|
368
|
+
this.ops = ops
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/* the offset of the last character of a line, and an offset clamped onto a character */
|
|
372
|
+
private lastChar (v: string, o: number) {
|
|
373
|
+
const s = lineStart(v, o)
|
|
374
|
+
const e = lineEnd(v, o)
|
|
375
|
+
return e > s ? this.ops.previous(v, e) : s
|
|
376
|
+
}
|
|
377
|
+
private clampLine (v: string, o: number) {
|
|
378
|
+
return Math.max(lineStart(v, o), Math.min(o, this.lastChar(v, o)))
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/* ==== EXTERNAL INTERFACE ==== */
|
|
382
|
+
|
|
383
|
+
/* take over an externally changed value */
|
|
384
|
+
sync (value: string) {
|
|
385
|
+
if (value === this.seen)
|
|
386
|
+
return
|
|
387
|
+
this.seen = value
|
|
388
|
+
if (value === this.value)
|
|
389
|
+
return
|
|
390
|
+
this.value = value
|
|
391
|
+
this.off = this.clampLine(value, Math.min(this.off, value.length))
|
|
392
|
+
this.pend = []
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/* handle the key tokens of a key press (like [ "x" ] or [ "<C-r>" ]); with "delegate",
|
|
396
|
+
plain insert mode keys are left to the front end, signaled by returning false */
|
|
397
|
+
keys (tokens: string[], delegate = false): boolean {
|
|
398
|
+
if (this.recording)
|
|
399
|
+
this.macro.push(...tokens)
|
|
400
|
+
if (tokens.length === 1)
|
|
401
|
+
return this.feed(tokens[0], delegate)
|
|
402
|
+
for (const t of tokens)
|
|
403
|
+
this.feed(t, false)
|
|
404
|
+
return true
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/* handle a bracketed paste */
|
|
408
|
+
paste (text: string) {
|
|
409
|
+
const t = "\u0000" + text
|
|
410
|
+
if (this.recording)
|
|
411
|
+
this.macro.push(t)
|
|
412
|
+
this.feed(t, false)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/* take over a text change of the front end (typing in insert mode) */
|
|
416
|
+
onText (value: string) {
|
|
417
|
+
this.value = value
|
|
418
|
+
this.off = toOffset(value, this.cur)
|
|
419
|
+
this.emit(value)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/* take over a cursor change of the front end (typing or mouse click) */
|
|
423
|
+
onCursor (cursor: [ number, number ]) {
|
|
424
|
+
this.cur = cursor
|
|
425
|
+
this.off = toOffset(this.value, cursor)
|
|
426
|
+
if (this.mode === "normal" || this.mode === "visual" || this.mode === "vline") {
|
|
427
|
+
this.off = this.clampLine(this.value, this.off)
|
|
428
|
+
this.want = this.off - lineStart(this.value, this.off)
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/* take over a mouse selection as a visual selection */
|
|
433
|
+
onSelect (sel: [ number, number ] | null): boolean {
|
|
434
|
+
if (!sel || sel[0] === sel[1] || !(this.mode === "normal" || this.mode === "visual"))
|
|
435
|
+
return false
|
|
436
|
+
this.mode = "visual"
|
|
437
|
+
this.anchor = sel[0]
|
|
438
|
+
this.off = this.clampLine(this.value, sel[1] > sel[0] ? this.ops.previous(this.value, sel[1]) : sel[1])
|
|
439
|
+
return true
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/* the visual selection to be drawn */
|
|
443
|
+
selection (): [ number, number ] | null {
|
|
444
|
+
if (this.mode !== "visual" && this.mode !== "vline")
|
|
445
|
+
return null
|
|
446
|
+
const r = this.selRange()
|
|
447
|
+
return [ r.start, r.end ]
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/* whether a pattern has to be matched ignoring case */
|
|
451
|
+
icase (pattern: string) {
|
|
452
|
+
return this.ignorecase && !(this.smartcase && /[A-Z]/.test(pattern))
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/* ==== KEY DISPATCHING ==== */
|
|
456
|
+
|
|
457
|
+
/* dispatch a key token by the mode (false for a key left to the front end) */
|
|
458
|
+
private feed (t: string, live = false): boolean {
|
|
459
|
+
if (this.mode === "insert" || this.mode === "replace")
|
|
460
|
+
return this.feedInsert(t, live)
|
|
461
|
+
if (this.mode === "cmdline")
|
|
462
|
+
this.feedCmdline(t)
|
|
463
|
+
else
|
|
464
|
+
this.feedNormal(t)
|
|
465
|
+
return true
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/* handle a key token in insert and replace mode */
|
|
469
|
+
private feedInsert (t: string, live: boolean): boolean {
|
|
470
|
+
if (this.recChange)
|
|
471
|
+
this.change.push(t)
|
|
472
|
+
this.insTokens.push(t)
|
|
473
|
+
|
|
474
|
+
/* a pending "<C-r>" inserts a register (recorded as its text), a pending "<C-o>" is refused */
|
|
475
|
+
if (this.pend.length > 0) {
|
|
476
|
+
const p = this.pend[0]
|
|
477
|
+
this.pend = []
|
|
478
|
+
this.insTokens.splice(-2)
|
|
479
|
+
if (this.recChange)
|
|
480
|
+
this.change.splice(-2)
|
|
481
|
+
const reg = p === "<C-r>" ? this.getReg(t) : undefined
|
|
482
|
+
if (p === "<C-o>")
|
|
483
|
+
this.fail("<C-o> is not supported")
|
|
484
|
+
else if (reg) {
|
|
485
|
+
const tok = "\u0000" + reg.text
|
|
486
|
+
this.insTokens.push(tok)
|
|
487
|
+
if (this.recChange)
|
|
488
|
+
this.change.push(tok)
|
|
489
|
+
this.insertKey(tok)
|
|
490
|
+
}
|
|
491
|
+
return true
|
|
492
|
+
}
|
|
493
|
+
if (t === "<Esc>" || t === "<C-c>" || t === "<C-[>") {
|
|
494
|
+
this.leaveInsert()
|
|
495
|
+
return true
|
|
496
|
+
}
|
|
497
|
+
if (t === "<C-o>" || t === "<C-r>") {
|
|
498
|
+
this.pend = [ t ]
|
|
499
|
+
return true
|
|
500
|
+
}
|
|
501
|
+
if (t === "<C-z>" || t === "<C-y>")
|
|
502
|
+
return true
|
|
503
|
+
|
|
504
|
+
/* leave plain typing to the front end, unless replaying */
|
|
505
|
+
if (live && this.mode === "insert" && (t.length === 1 || PASSABLE.has(t)))
|
|
506
|
+
return false
|
|
507
|
+
this.insertKey(t)
|
|
508
|
+
return true
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/* handle a key token on the command line */
|
|
512
|
+
private feedCmdline (t: string) {
|
|
513
|
+
const text = this.cmdText
|
|
514
|
+
switch (t) {
|
|
515
|
+
case "<Esc>": case "<C-c>":
|
|
516
|
+
this.mode = this.cmdFrom
|
|
517
|
+
break
|
|
518
|
+
case "<CR>": case "<C-j>": case "<C-m>":
|
|
519
|
+
this.mode = this.cmdFrom
|
|
520
|
+
if (text !== "" && this.cmdHist[this.cmdHist.length - 1] !== text)
|
|
521
|
+
this.cmdHist.push(text)
|
|
522
|
+
if (this.cmdKind === ":") {
|
|
523
|
+
const [ v, o ] = [ this.value, this.off ]
|
|
524
|
+
this.ex(text)
|
|
525
|
+
if (this.off !== o || this.value !== v)
|
|
526
|
+
this.want = this.off - lineStart(this.value, this.off)
|
|
527
|
+
}
|
|
528
|
+
else
|
|
529
|
+
this.searchCmd(text)
|
|
530
|
+
break
|
|
531
|
+
case "<BS>": case "<C-h>":
|
|
532
|
+
if (text === "")
|
|
533
|
+
this.mode = this.cmdFrom
|
|
534
|
+
else
|
|
535
|
+
this.cmdText = text.slice(0, this.ops.previous(text, text.length))
|
|
536
|
+
break
|
|
537
|
+
case "<C-u>":
|
|
538
|
+
this.cmdText = ""
|
|
539
|
+
break
|
|
540
|
+
case "<C-w>":
|
|
541
|
+
this.cmdText = text.replace(/([\p{L}\p{N}_]+|[^\p{L}\p{N}_\s]+)\s*$/u, "")
|
|
542
|
+
break
|
|
543
|
+
case "<Up>": case "<Down>": {
|
|
544
|
+
const n = this.cmdHist.length
|
|
545
|
+
if (n === 0)
|
|
546
|
+
break
|
|
547
|
+
const i = this.cmdIdx < 0 ? n : this.cmdIdx
|
|
548
|
+
this.cmdIdx = Math.max(0, Math.min(n, i + (t === "<Up>" ? -1 : 1)))
|
|
549
|
+
this.cmdText = this.cmdHist[this.cmdIdx] ?? ""
|
|
550
|
+
break
|
|
551
|
+
}
|
|
552
|
+
default:
|
|
553
|
+
if (t.startsWith("\u0000"))
|
|
554
|
+
this.cmdText += t.slice(1).split("\n")[0]
|
|
555
|
+
else if (t === "<Tab>")
|
|
556
|
+
this.cmdText += "\t"
|
|
557
|
+
else if (isText(t))
|
|
558
|
+
this.cmdText += t
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/* collect the key tokens of a normal or visual mode command and execute it once complete */
|
|
563
|
+
private feedNormal (t: string) {
|
|
564
|
+
|
|
565
|
+
/* a bracketed paste is inserted before the cursor at once */
|
|
566
|
+
if (t.startsWith("\u0000")) {
|
|
567
|
+
if (this.mode !== "normal")
|
|
568
|
+
this.leaveVisual()
|
|
569
|
+
this.pend = []
|
|
570
|
+
this.begin()
|
|
571
|
+
this.insertText(t.slice(1))
|
|
572
|
+
this.off = this.clampLine(this.value, this.ops.previous(this.value, this.off))
|
|
573
|
+
this.end()
|
|
574
|
+
return
|
|
575
|
+
}
|
|
576
|
+
if (t === "<Esc>" && this.pend.length > 0) {
|
|
577
|
+
this.pend = []
|
|
578
|
+
return
|
|
579
|
+
}
|
|
580
|
+
if (t === "<Esc>" && this.mode === "normal")
|
|
581
|
+
return
|
|
582
|
+
this.pend.push(t)
|
|
583
|
+
const c = this.parse(this.pend)
|
|
584
|
+
if (c === "more")
|
|
585
|
+
return
|
|
586
|
+
this.pend = []
|
|
587
|
+
if (c === "bad")
|
|
588
|
+
return
|
|
589
|
+
const v = this.value
|
|
590
|
+
const o = this.off
|
|
591
|
+
this.exec(c)
|
|
592
|
+
|
|
593
|
+
/* a moving non-motion command sets the wanted column (motions do it themselves) */
|
|
594
|
+
if ((this.off !== o || this.value !== v) && !MOTIONS.has(c.op)
|
|
595
|
+
&& !(c.op === "@" && c.arg !== ":") && c.op !== "<C-e>" && c.op !== "<C-y>"
|
|
596
|
+
&& (this.mode === "normal" || this.mode === "visual" || this.mode === "vline"))
|
|
597
|
+
this.want = this.off - lineStart(this.value, this.off)
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/* parse "[\"x][count]cmd[arg]" resp. "[\"x][count]op[count]motion[arg]" */
|
|
601
|
+
private parse (t: string[]): Cmd | "more" | "bad" {
|
|
602
|
+
const visual = this.mode === "visual" || this.mode === "vline"
|
|
603
|
+
const digits = (i: number): [ string, number ] => {
|
|
604
|
+
let s = ""
|
|
605
|
+
while (i < t.length && /^[0-9]$/.test(t[i]) && (t[i] !== "0" || s !== ""))
|
|
606
|
+
s += t[i++]
|
|
607
|
+
return [ s, i ]
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/* a count may stand before and after the register (and both multiply) */
|
|
611
|
+
const [ c0, i0 ] = digits(0)
|
|
612
|
+
let i = i0
|
|
613
|
+
let reg = ""
|
|
614
|
+
if (t[i] === "\"") {
|
|
615
|
+
if (i + 1 >= t.length)
|
|
616
|
+
return "more"
|
|
617
|
+
reg = t[i + 1]
|
|
618
|
+
i += 2
|
|
619
|
+
}
|
|
620
|
+
const [ c1, i1 ] = digits(i)
|
|
621
|
+
i = i1
|
|
622
|
+
if (i >= t.length)
|
|
623
|
+
return "more"
|
|
624
|
+
let k = t[i++]
|
|
625
|
+
if (PREFIXES.has(k) || (visual && (k === "i" || k === "a"))) {
|
|
626
|
+
if (i >= t.length)
|
|
627
|
+
return "more"
|
|
628
|
+
k += t[i++]
|
|
629
|
+
}
|
|
630
|
+
let arg = ""
|
|
631
|
+
if (ARGS.has(k) && !(k === "q" && this.recording !== "")) {
|
|
632
|
+
if (i >= t.length)
|
|
633
|
+
return "more"
|
|
634
|
+
arg = t[i++]
|
|
635
|
+
}
|
|
636
|
+
const cmd: Cmd = {
|
|
637
|
+
reg, count: c0 || c1 ? Math.max(1, +c0) * Math.max(1, +c1) : 0, count2: 0,
|
|
638
|
+
op: k, arg, motion: "", marg: "", tokens: t.slice()
|
|
639
|
+
}
|
|
640
|
+
if (visual)
|
|
641
|
+
return MOTIONS.has(k) || OBJECT.test(k) || VISUAL.has(k) || k === "gv" ? cmd : "bad"
|
|
642
|
+
if (OPERATORS.has(k)) {
|
|
643
|
+
const [ c2, i2 ] = digits(i)
|
|
644
|
+
i = i2
|
|
645
|
+
if (i >= t.length)
|
|
646
|
+
return "more"
|
|
647
|
+
let m = t[i++]
|
|
648
|
+
if (m === "g" || m === "i" || m === "a") {
|
|
649
|
+
if (i >= t.length)
|
|
650
|
+
return "more"
|
|
651
|
+
m += t[i++]
|
|
652
|
+
}
|
|
653
|
+
if (m === k || m === k.slice(-1))
|
|
654
|
+
m = "line"
|
|
655
|
+
else if (!MOTIONS.has(m) && !OBJECT.test(m))
|
|
656
|
+
return "bad"
|
|
657
|
+
else if (ARGS.has(m)) {
|
|
658
|
+
if (i >= t.length)
|
|
659
|
+
return "more"
|
|
660
|
+
cmd.marg = t[i]
|
|
661
|
+
}
|
|
662
|
+
cmd.motion = m
|
|
663
|
+
cmd.count2 = c2 ? parseInt(c2, 10) : 0
|
|
664
|
+
return cmd
|
|
665
|
+
}
|
|
666
|
+
return MOTIONS.has(k) || NORMAL.has(k) ? cmd : "bad"
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/* ==== COMMAND EXECUTION ==== */
|
|
670
|
+
|
|
671
|
+
/* execute a parsed normal mode command */
|
|
672
|
+
private exec (c: Cmd) {
|
|
673
|
+
const n = Math.max(1, c.count) * Math.max(1, c.count2)
|
|
674
|
+
const given = c.count > 0 || c.count2 > 0
|
|
675
|
+
if (this.mode === "visual" || this.mode === "vline") {
|
|
676
|
+
this.execVisual(c, n, given)
|
|
677
|
+
return
|
|
678
|
+
}
|
|
679
|
+
const v = this.value
|
|
680
|
+
const o = this.off
|
|
681
|
+
|
|
682
|
+
/* a change is recorded for "." until it is finished */
|
|
683
|
+
const chg = (fn: () => void) => {
|
|
684
|
+
this.change = c.tokens.slice()
|
|
685
|
+
this.recChange = true
|
|
686
|
+
fn()
|
|
687
|
+
if (this.mode !== "insert" && this.mode !== "replace") {
|
|
688
|
+
this.lastChange = this.change
|
|
689
|
+
this.recChange = false
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
if (OPERATORS.has(c.op)) {
|
|
693
|
+
if (c.op === "y")
|
|
694
|
+
this.operate(c.op, c.motion, c.marg, n, given, c.reg)
|
|
695
|
+
else
|
|
696
|
+
chg(() => this.operate(c.op, c.motion, c.marg, n, given, c.reg))
|
|
697
|
+
return
|
|
698
|
+
}
|
|
699
|
+
switch (c.op) {
|
|
700
|
+
case "x": case "<Del>": chg(() => this.operate("d", "l", "", n, given, c.reg)); return
|
|
701
|
+
case "X": chg(() => this.operate("d", "h", "", n, given, c.reg)); return
|
|
702
|
+
case "D": chg(() => this.operate("d", "$", "", n, given, c.reg)); return
|
|
703
|
+
case "C": chg(() => this.operate("c", "$", "", n, given, c.reg)); return
|
|
704
|
+
case "s": chg(() => this.operate("c", "l", "", n, given, c.reg)); return
|
|
705
|
+
case "S": chg(() => this.operate("c", "line", "", n, given, c.reg)); return
|
|
706
|
+
case "Y": this.operate("y", "line", "", n, given, c.reg); return
|
|
707
|
+
case "p": case "P": chg(() => this.put(c.reg, n, c.op === "P")); return
|
|
708
|
+
case "r": chg(() => this.replaceChars(c.arg, n)); return
|
|
709
|
+
case "~": chg(() => this.toggleCase(n)); return
|
|
710
|
+
case "<C-a>": chg(() => this.increment(n)); return
|
|
711
|
+
case "<C-x>": chg(() => this.increment(-n)); return
|
|
712
|
+
case "&":
|
|
713
|
+
chg(() => {
|
|
714
|
+
if (this.lastSub)
|
|
715
|
+
this.substitute(lineNo(v, o), lineNo(v, o), "\u0001", `${this.lastSub.pat}\u0001${this.lastSub.rep}\u0001`)
|
|
716
|
+
})
|
|
717
|
+
return
|
|
718
|
+
case "J": case "gJ":
|
|
719
|
+
chg(() => {
|
|
720
|
+
this.begin()
|
|
721
|
+
this.join(lineNo(v, o), Math.max(2, n), c.op === "J")
|
|
722
|
+
this.end()
|
|
723
|
+
})
|
|
724
|
+
return
|
|
725
|
+
case "i": case "a": case "I": case "A": case "gI": case "gi": case "o": case "O":
|
|
726
|
+
chg(() => {
|
|
727
|
+
if (c.op === "a")
|
|
728
|
+
this.off = o < lineEnd(v, o) ? this.ops.next(v, o) : o
|
|
729
|
+
else if (c.op === "I")
|
|
730
|
+
this.off = firstNonBlank(v, o)
|
|
731
|
+
else if (c.op === "gI")
|
|
732
|
+
this.off = lineStart(v, o)
|
|
733
|
+
else if (c.op === "A")
|
|
734
|
+
this.off = lineEnd(v, o)
|
|
735
|
+
else if (c.op === "gi")
|
|
736
|
+
this.off = Math.min(this.lastInsert, v.length)
|
|
737
|
+
this.begin()
|
|
738
|
+
if (c.op === "o") {
|
|
739
|
+
const e = lineEnd(v, o)
|
|
740
|
+
this.replace(e, e, "\n")
|
|
741
|
+
this.off = e + 1
|
|
742
|
+
}
|
|
743
|
+
else if (c.op === "O") {
|
|
744
|
+
const s = lineStart(v, o)
|
|
745
|
+
this.replace(s, s, "\n")
|
|
746
|
+
this.off = s
|
|
747
|
+
}
|
|
748
|
+
this.enterInsert(n, c.op === "o" || c.op === "O")
|
|
749
|
+
})
|
|
750
|
+
return
|
|
751
|
+
case "R":
|
|
752
|
+
chg(() => {
|
|
753
|
+
this.begin()
|
|
754
|
+
this.enterInsert(n, false)
|
|
755
|
+
this.mode = "replace"
|
|
756
|
+
})
|
|
757
|
+
return
|
|
758
|
+
case "u": this.undo(n); return
|
|
759
|
+
case "<C-r>": this.redo(n); return
|
|
760
|
+
case ".": this.dot(c); return
|
|
761
|
+
case "v": case "V":
|
|
762
|
+
this.mode = c.op === "v" ? "visual" : "vline"
|
|
763
|
+
this.anchor = o
|
|
764
|
+
this.message = ""
|
|
765
|
+
return
|
|
766
|
+
case "gv":
|
|
767
|
+
if (this.lastVisual) {
|
|
768
|
+
this.mode = this.lastVisual.mode
|
|
769
|
+
this.anchor = Math.min(this.lastVisual.anchor, v.length)
|
|
770
|
+
this.off = this.clampLine(v, Math.min(this.lastVisual.off, v.length))
|
|
771
|
+
}
|
|
772
|
+
return
|
|
773
|
+
case "<C-v>":
|
|
774
|
+
this.fail("visual block mode is not supported")
|
|
775
|
+
return
|
|
776
|
+
case ":":
|
|
777
|
+
this.openCmd(":", given ? (n > 1 ? `.,.+${n - 1}` : ".") : "")
|
|
778
|
+
return
|
|
779
|
+
case "/": case "?":
|
|
780
|
+
this.openCmd(c.op, "")
|
|
781
|
+
return
|
|
782
|
+
case "m":
|
|
783
|
+
if (/^[a-zA-Z]$/.test(c.arg))
|
|
784
|
+
this.marks.set(c.arg, o)
|
|
785
|
+
return
|
|
786
|
+
case "q":
|
|
787
|
+
this.macroRecord(c.arg)
|
|
788
|
+
return
|
|
789
|
+
case "@":
|
|
790
|
+
this.macroPlay(c.arg, n)
|
|
791
|
+
return
|
|
792
|
+
case "ZZ": this.runHost("x"); return
|
|
793
|
+
case "ZQ": this.runHost("q!"); return
|
|
794
|
+
case "<C-g>": this.runHost("file"); return
|
|
795
|
+
case "<C-c>":
|
|
796
|
+
this.say("Type :qa! and press <Enter> to abandon all changes and exit Vim")
|
|
797
|
+
return
|
|
798
|
+
case "<C-d>": case "<C-u>": case "<C-f>": case "<C-b>":
|
|
799
|
+
case "<PageDown>": case "<PageUp>": case "<C-e>": case "<C-y>":
|
|
800
|
+
this.scroll(c.op, n, given)
|
|
801
|
+
return
|
|
802
|
+
case "zz": case "zt": case "zb": case "z.": case "z<CR>": case "z-": case "<C-l>":
|
|
803
|
+
return
|
|
804
|
+
}
|
|
805
|
+
const m = this.motion(c.op, c.arg, n, given, false)
|
|
806
|
+
if (m)
|
|
807
|
+
this.go(m, JUMPS.has(c.op))
|
|
808
|
+
else
|
|
809
|
+
this.failed = true
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/* execute a parsed visual mode command */
|
|
813
|
+
private execVisual (c: Cmd, n: number, given: boolean) {
|
|
814
|
+
const k = c.op
|
|
815
|
+
const v = this.value
|
|
816
|
+
if (MOTIONS.has(k)) {
|
|
817
|
+
const m = this.motion(k, c.arg, n, given, false)
|
|
818
|
+
if (m)
|
|
819
|
+
this.go(m, JUMPS.has(k))
|
|
820
|
+
else
|
|
821
|
+
this.failed = true
|
|
822
|
+
return
|
|
823
|
+
}
|
|
824
|
+
if (OBJECT.test(k)) {
|
|
825
|
+
const r = this.object(k, n)
|
|
826
|
+
if (r) {
|
|
827
|
+
this.anchor = r.start
|
|
828
|
+
this.off = r.end > r.start ? this.ops.previous(v, r.end) : r.start
|
|
829
|
+
if (r.linewise)
|
|
830
|
+
this.mode = "vline"
|
|
831
|
+
}
|
|
832
|
+
return
|
|
833
|
+
}
|
|
834
|
+
switch (k) {
|
|
835
|
+
case "<Esc>": case "<C-c>":
|
|
836
|
+
this.leaveVisual()
|
|
837
|
+
return
|
|
838
|
+
case "v": case "V": {
|
|
839
|
+
const mode = k === "v" ? "visual" : "vline"
|
|
840
|
+
if (this.mode === mode)
|
|
841
|
+
this.leaveVisual()
|
|
842
|
+
else
|
|
843
|
+
this.mode = mode
|
|
844
|
+
return
|
|
845
|
+
}
|
|
846
|
+
case "<C-v>":
|
|
847
|
+
this.fail("visual block mode is not supported")
|
|
848
|
+
return
|
|
849
|
+
case "o": case "O":
|
|
850
|
+
[ this.anchor, this.off ] = [ this.off, this.anchor ]
|
|
851
|
+
return
|
|
852
|
+
case "gv":
|
|
853
|
+
return
|
|
854
|
+
case ":":
|
|
855
|
+
this.leaveVisual()
|
|
856
|
+
this.openCmd(":", "'<,'>")
|
|
857
|
+
return
|
|
858
|
+
case "/": case "?":
|
|
859
|
+
this.openCmd(k, "")
|
|
860
|
+
return
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/* operators act on the selection at once */
|
|
864
|
+
const r = this.selRange()
|
|
865
|
+
const lines = { start: lineStart(v, r.start), end: lineEndNl(v, Math.max(r.start, r.end - 1)), linewise: true }
|
|
866
|
+
const vline = this.mode === "vline"
|
|
867
|
+
this.leaveVisual()
|
|
868
|
+
|
|
869
|
+
/* a change is recorded for "." with a selection of the same extent from the cursor */
|
|
870
|
+
this.change = [ ...this.selKeys(r, vline), ...c.tokens ]
|
|
871
|
+
this.recChange = k !== "y" && k !== "Y"
|
|
872
|
+
switch (k) {
|
|
873
|
+
case "d": case "x": case "<Del>": this.apply("d", r, c.reg); break
|
|
874
|
+
case "X": case "D": this.apply("d", lines, c.reg); break
|
|
875
|
+
case "y": this.apply("y", r, c.reg); break
|
|
876
|
+
case "Y": this.apply("y", lines, c.reg); break
|
|
877
|
+
case "c": case "s": this.apply("c", r, c.reg); break
|
|
878
|
+
case "C": case "S": case "R": this.apply("c", lines, c.reg); break
|
|
879
|
+
case ">": case "<": this.apply(k, lines, c.reg, n); break
|
|
880
|
+
case "~": this.apply("g~", r, c.reg); break
|
|
881
|
+
case "u": this.apply("gu", r, c.reg); break
|
|
882
|
+
case "U": this.apply("gU", r, c.reg); break
|
|
883
|
+
case "J": case "gJ": {
|
|
884
|
+
const l1 = lineNo(v, r.start), l2 = lineNo(v, Math.max(r.start, r.end - 1))
|
|
885
|
+
this.begin()
|
|
886
|
+
this.join(l1, Math.max(2, l2 - l1 + 1), k === "J")
|
|
887
|
+
this.end()
|
|
888
|
+
break
|
|
889
|
+
}
|
|
890
|
+
case "r": {
|
|
891
|
+
const ch = c.arg === "<Tab>" ? "\t" : c.arg
|
|
892
|
+
if (!isText(ch) || ch.startsWith("\u0000"))
|
|
893
|
+
break
|
|
894
|
+
this.begin()
|
|
895
|
+
let out = ""
|
|
896
|
+
for (let i = r.start; i < r.end; i = this.ops.next(v, i))
|
|
897
|
+
out += v[i] === "\n" ? "\n" : ch
|
|
898
|
+
this.replace(r.start, r.end, out)
|
|
899
|
+
this.off = this.clampLine(this.value, r.start)
|
|
900
|
+
this.end()
|
|
901
|
+
break
|
|
902
|
+
}
|
|
903
|
+
case "p": case "P": {
|
|
904
|
+
const reg = this.getReg(c.reg)
|
|
905
|
+
if (!reg) {
|
|
906
|
+
this.fail(`E353: Nothing in register ${c.reg || "\""}`)
|
|
907
|
+
break
|
|
908
|
+
}
|
|
909
|
+
this.begin()
|
|
910
|
+
|
|
911
|
+
/* a linewise selection up to the text end lacks its final newline */
|
|
912
|
+
const old = v.slice(r.start, r.end)
|
|
913
|
+
const tail = r.linewise && !old.endsWith("\n")
|
|
914
|
+
this.setReg("", tail ? old + "\n" : old, r.linewise, "delete")
|
|
915
|
+
let text = r.linewise
|
|
916
|
+
? (reg.linewise ? reg.text : reg.text + "\n")
|
|
917
|
+
: (reg.linewise ? "\n" + reg.text : reg.text)
|
|
918
|
+
if (tail)
|
|
919
|
+
text = text.slice(0, -1)
|
|
920
|
+
this.replace(r.start, r.end, text)
|
|
921
|
+
this.off = this.clampLine(this.value, r.start + (r.linewise || !reg.linewise ? 0 : 1))
|
|
922
|
+
this.end()
|
|
923
|
+
break
|
|
924
|
+
}
|
|
925
|
+
case "I":
|
|
926
|
+
this.begin()
|
|
927
|
+
this.off = vline ? firstNonBlank(v, r.start) : r.start
|
|
928
|
+
this.enterInsert(1, false)
|
|
929
|
+
break
|
|
930
|
+
case "A":
|
|
931
|
+
this.begin()
|
|
932
|
+
this.off = vline ? lineEnd(v, Math.max(r.start, r.end - 1)) : Math.min(r.end, lineEnd(v, Math.max(r.start, r.end - 1)))
|
|
933
|
+
this.enterInsert(1, false)
|
|
934
|
+
break
|
|
935
|
+
}
|
|
936
|
+
if (this.recChange && this.mode !== "insert") {
|
|
937
|
+
this.lastChange = this.change
|
|
938
|
+
this.recChange = false
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/* the keys re-creating a visual selection of the same extent from the cursor (for ".") */
|
|
943
|
+
private selKeys (r: Range, vline: boolean) {
|
|
944
|
+
const v = this.value
|
|
945
|
+
const e = Math.max(r.start, r.end - 1)
|
|
946
|
+
const dl = lineNo(v, e) - lineNo(v, r.start)
|
|
947
|
+
const keys = [ vline ? "V" : "v" ]
|
|
948
|
+
if (dl > 0)
|
|
949
|
+
keys.push(...String(dl), "j")
|
|
950
|
+
if (vline)
|
|
951
|
+
return keys
|
|
952
|
+
if (v[e] === "\n")
|
|
953
|
+
keys.push("$")
|
|
954
|
+
else {
|
|
955
|
+
let n = 0
|
|
956
|
+
for (let i = dl > 0 ? lineStart(v, e) : r.start; i < e; i = this.ops.next(v, i))
|
|
957
|
+
n++
|
|
958
|
+
if (dl > 0)
|
|
959
|
+
keys.push("0")
|
|
960
|
+
if (n > 0)
|
|
961
|
+
keys.push(...String(n), "l")
|
|
962
|
+
}
|
|
963
|
+
return keys
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/* ==== MOTIONS AND TEXT OBJECTS ==== */
|
|
967
|
+
|
|
968
|
+
/* the target of a motion from the cursor (with "op" for an operator-pending motion) */
|
|
969
|
+
private motion (k: string, arg: string, n: number, given: boolean, op: boolean): Motion | null {
|
|
970
|
+
const v = this.value
|
|
971
|
+
const o = this.off
|
|
972
|
+
const ls = lineStart(v, o)
|
|
973
|
+
const le = lineEnd(v, o)
|
|
974
|
+
const ex = (to: number): Motion => ({ to, linewise: false, inclusive: false })
|
|
975
|
+
const inc = (to: number): Motion => ({ to, linewise: false, inclusive: true })
|
|
976
|
+
const lines = (to: number): Motion => ({ to, linewise: true, inclusive: false })
|
|
977
|
+
switch (k) {
|
|
978
|
+
case "h": case "<Left>": case "<BS>": case "<C-h>": {
|
|
979
|
+
if (o <= ls)
|
|
980
|
+
return null
|
|
981
|
+
let p = o
|
|
982
|
+
for (let i = 0; i < n && p > ls; i++)
|
|
983
|
+
p = this.ops.previous(v, p)
|
|
984
|
+
return ex(p)
|
|
985
|
+
}
|
|
986
|
+
case "l": case "<Right>": case " ": {
|
|
987
|
+
let p = o
|
|
988
|
+
for (let i = 0; i < n && p < le; i++)
|
|
989
|
+
p = this.ops.next(v, p)
|
|
990
|
+
if (!op)
|
|
991
|
+
p = Math.min(p, this.lastChar(v, o))
|
|
992
|
+
return p === o ? null : ex(p)
|
|
993
|
+
}
|
|
994
|
+
case "j": case "<Down>": case "<C-n>": case "<C-j>":
|
|
995
|
+
case "k": case "<Up>": case "<C-p>": {
|
|
996
|
+
const down = k === "j" || k === "<Down>" || k === "<C-n>" || k === "<C-j>"
|
|
997
|
+
const l = lineNo(v, o)
|
|
998
|
+
const t = down ? Math.min(lastLine(v), l + n) : Math.max(0, l - n)
|
|
999
|
+
if (t === l)
|
|
1000
|
+
return null
|
|
1001
|
+
return { to: this.column(v, t, this.want), linewise: true, inclusive: false, vertical: true }
|
|
1002
|
+
}
|
|
1003
|
+
case "+": case "<CR>": case "-": case "_": {
|
|
1004
|
+
const t = lineNo(v, o) + (k === "_" ? n - 1 : k === "-" ? -n : n)
|
|
1005
|
+
if (t < 0 || t > lastLine(v))
|
|
1006
|
+
return null
|
|
1007
|
+
return lines(firstNonBlank(v, lineAt(v, t)))
|
|
1008
|
+
}
|
|
1009
|
+
case "0": case "<Home>":
|
|
1010
|
+
return ex(ls)
|
|
1011
|
+
case "^":
|
|
1012
|
+
return ex(firstNonBlank(v, o))
|
|
1013
|
+
case "$": case "<End>": case "g_": {
|
|
1014
|
+
const s = lineAt(v, Math.min(lastLine(v), lineNo(v, o) + n - 1))
|
|
1015
|
+
let e = lineEnd(v, s)
|
|
1016
|
+
if (k === "g_") {
|
|
1017
|
+
while (e > s && isSpace(v[e - 1]))
|
|
1018
|
+
e--
|
|
1019
|
+
return inc(e > s ? this.ops.previous(v, e) : s)
|
|
1020
|
+
}
|
|
1021
|
+
return { to: op ? e : (e > s ? this.ops.previous(v, e) : s), linewise: false, inclusive: false, want: Infinity }
|
|
1022
|
+
}
|
|
1023
|
+
case "|":
|
|
1024
|
+
return ex(Math.min(ls + n - 1, this.lastChar(v, o)))
|
|
1025
|
+
case "w": case "W": case "e": case "E": case "b": case "B": case "ge": case "gE":
|
|
1026
|
+
return this.wordMotion(k, n, op)
|
|
1027
|
+
case "G": case "gg": {
|
|
1028
|
+
const l = given ? Math.min(n - 1, lastLine(v)) : k === "G" ? lastLine(v) : 0
|
|
1029
|
+
return lines(firstNonBlank(v, lineAt(v, l)))
|
|
1030
|
+
}
|
|
1031
|
+
case "f": case "F": case "t": case "T": case ";": case ",":
|
|
1032
|
+
return this.findMotion(k, arg, n)
|
|
1033
|
+
case "%": {
|
|
1034
|
+
let i = o
|
|
1035
|
+
while (i < le && !"()[]{}".includes(v[i]))
|
|
1036
|
+
i++
|
|
1037
|
+
if (i >= le)
|
|
1038
|
+
return null
|
|
1039
|
+
const q = matchPair(v, i)
|
|
1040
|
+
return q < 0 ? null : inc(q)
|
|
1041
|
+
}
|
|
1042
|
+
case "}": case "{":
|
|
1043
|
+
return this.paragraphMotion(k === "}", n)
|
|
1044
|
+
case "n": case "N": case "*": case "#":
|
|
1045
|
+
return this.searchMotion(k, n)
|
|
1046
|
+
case "`": case "'": {
|
|
1047
|
+
const p = this.marks.get(arg === "`" ? "'" : arg)
|
|
1048
|
+
if (p === undefined) {
|
|
1049
|
+
this.fail("E20: Mark not set")
|
|
1050
|
+
return null
|
|
1051
|
+
}
|
|
1052
|
+
const q = Math.min(p, v.length)
|
|
1053
|
+
return k === "`" ? ex(q) : lines(firstNonBlank(v, q))
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
return null
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/* the word motions "w", "W", "e", "E", "b", "B", "ge", and "gE" */
|
|
1060
|
+
private wordMotion (k: string, n: number, op: boolean): Motion | null {
|
|
1061
|
+
const v = this.value
|
|
1062
|
+
const o = this.off
|
|
1063
|
+
const big = k === "W" || k === "E" || k === "B" || k === "gE"
|
|
1064
|
+
let p = o
|
|
1065
|
+
if (k === "w" || k === "W") {
|
|
1066
|
+
let q = o
|
|
1067
|
+
for (let i = 0; i < n && p < v.length; i++) {
|
|
1068
|
+
q = p
|
|
1069
|
+
p = wordFwd(v, p, big)
|
|
1070
|
+
}
|
|
1071
|
+
if (p === o)
|
|
1072
|
+
return null
|
|
1073
|
+
|
|
1074
|
+
/* "dw" on the last word of a line stops at its end */
|
|
1075
|
+
if (op && lineNo(v, p) > lineNo(v, q) && !emptyAt(v, q))
|
|
1076
|
+
p = lineEnd(v, q)
|
|
1077
|
+
return { to: p, linewise: false, inclusive: false }
|
|
1078
|
+
}
|
|
1079
|
+
const step = k === "e" || k === "E" ? wordEnd : k === "b" || k === "B" ? wordBack : wordEndBack
|
|
1080
|
+
for (let i = 0; i < n; i++) {
|
|
1081
|
+
const q = step(v, p, big)
|
|
1082
|
+
if (q === p)
|
|
1083
|
+
break
|
|
1084
|
+
p = q
|
|
1085
|
+
}
|
|
1086
|
+
return p === o ? null : { to: p, linewise: false, inclusive: k !== "b" && k !== "B" }
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/* the character find motions "f", "F", "t", "T", ";", and "," */
|
|
1090
|
+
private findMotion (k: string, arg: string, n: number): Motion | null {
|
|
1091
|
+
const v = this.value
|
|
1092
|
+
const o = this.off
|
|
1093
|
+
let cmd = k
|
|
1094
|
+
let ch = arg === "<Tab>" ? "\t" : arg
|
|
1095
|
+
const repeat = k === ";" || k === ","
|
|
1096
|
+
if (repeat) {
|
|
1097
|
+
if (!this.lastFind)
|
|
1098
|
+
return null
|
|
1099
|
+
ch = this.lastFind.ch
|
|
1100
|
+
cmd = this.lastFind.cmd
|
|
1101
|
+
if (k === ",")
|
|
1102
|
+
cmd = cmd === cmd.toLowerCase() ? cmd.toUpperCase() : cmd.toLowerCase()
|
|
1103
|
+
}
|
|
1104
|
+
else if (isText(ch) && !ch.startsWith("\u0000"))
|
|
1105
|
+
this.lastFind = { cmd: k, ch }
|
|
1106
|
+
else
|
|
1107
|
+
return null
|
|
1108
|
+
const fwd = cmd === "f" || cmd === "t"
|
|
1109
|
+
const till = cmd === "t" || cmd === "T"
|
|
1110
|
+
const ls = lineStart(v, o)
|
|
1111
|
+
const le = lineEnd(v, o)
|
|
1112
|
+
let p = o
|
|
1113
|
+
for (let i = 0; i < n; i++) {
|
|
1114
|
+
const skip = i === 0 && repeat && till ? 2 : 1
|
|
1115
|
+
const start = fwd ? p + skip : p - skip
|
|
1116
|
+
if (start < 0)
|
|
1117
|
+
return null
|
|
1118
|
+
const q = fwd ? v.indexOf(ch, start) : v.lastIndexOf(ch, start)
|
|
1119
|
+
if (q < 0 || (fwd ? q >= le : q < ls))
|
|
1120
|
+
return null
|
|
1121
|
+
p = q
|
|
1122
|
+
}
|
|
1123
|
+
if (till)
|
|
1124
|
+
p = fwd ? this.ops.previous(v, p) : p + ch.length
|
|
1125
|
+
return { to: p, linewise: false, inclusive: fwd }
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/* the paragraph motions "}" and "{" */
|
|
1129
|
+
private paragraphMotion (fwd: boolean, n: number): Motion | null {
|
|
1130
|
+
const v = this.value
|
|
1131
|
+
const last = lastLine(v)
|
|
1132
|
+
let l = lineNo(v, this.off)
|
|
1133
|
+
for (let i = 0; i < n && (fwd ? l < last : l > 0); i++) {
|
|
1134
|
+
while (fwd ? l < last : l > 0) {
|
|
1135
|
+
if (!isEmpty(v, l))
|
|
1136
|
+
break
|
|
1137
|
+
l += fwd ? 1 : -1
|
|
1138
|
+
}
|
|
1139
|
+
while (fwd ? l < last : l > 0) {
|
|
1140
|
+
l += fwd ? 1 : -1
|
|
1141
|
+
if (isEmpty(v, l))
|
|
1142
|
+
break
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
const to = isEmpty(v, l) ? lineAt(v, l) : fwd ? v.length : 0
|
|
1146
|
+
return to === this.off ? null : { to, linewise: false, inclusive: false }
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/* the search motions "n", "N", "*", and "#" */
|
|
1150
|
+
private searchMotion (k: string, n: number): Motion | null {
|
|
1151
|
+
let p = this.off
|
|
1152
|
+
if (k === "*" || k === "#") {
|
|
1153
|
+
const [ w, s ] = this.wordUnder()
|
|
1154
|
+
if (!w) {
|
|
1155
|
+
this.fail("E348: No string under cursor")
|
|
1156
|
+
return null
|
|
1157
|
+
}
|
|
1158
|
+
this.search = { pattern: `\\<${escapeVim(w)}\\>`, forward: k === "*" }
|
|
1159
|
+
this.hlsearch = true
|
|
1160
|
+
p = s
|
|
1161
|
+
}
|
|
1162
|
+
if (!this.search) {
|
|
1163
|
+
this.fail("E35: No previous regular expression")
|
|
1164
|
+
return null
|
|
1165
|
+
}
|
|
1166
|
+
const fwd = k === "N" ? !this.search.forward : k === "n" ? this.search.forward : k === "*"
|
|
1167
|
+
this.say((fwd ? "/" : "?") + this.search.pattern)
|
|
1168
|
+
for (let i = 0; i < n; i++) {
|
|
1169
|
+
const q = this.find(this.search.pattern, p, fwd)
|
|
1170
|
+
if (q === null)
|
|
1171
|
+
return null
|
|
1172
|
+
p = q
|
|
1173
|
+
}
|
|
1174
|
+
return { to: p, linewise: false, inclusive: false }
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/* the offset on a line closest to a wanted column */
|
|
1178
|
+
private column (v: string, line: number, want: number) {
|
|
1179
|
+
const s = lineAt(v, line)
|
|
1180
|
+
const e = lineEnd(v, s)
|
|
1181
|
+
if (e === s)
|
|
1182
|
+
return s
|
|
1183
|
+
const t = Math.min(s + want, this.ops.previous(v, e))
|
|
1184
|
+
return this.ops.next(v, this.ops.previous(v, t)) === t ? t : this.ops.previous(v, t)
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/* move the cursor by a motion */
|
|
1188
|
+
private go (m: Motion, jump: boolean) {
|
|
1189
|
+
if (jump)
|
|
1190
|
+
this.marks.set("'", this.off)
|
|
1191
|
+
this.off = this.clampLine(this.value, Math.min(m.to, this.value.length))
|
|
1192
|
+
if (m.want !== undefined)
|
|
1193
|
+
this.want = m.want
|
|
1194
|
+
else if (!m.vertical)
|
|
1195
|
+
this.want = this.off - lineStart(this.value, this.off)
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
/* the keyword under or after the cursor and its start (for "*" and "#") */
|
|
1199
|
+
private wordUnder (): [ string, number ] {
|
|
1200
|
+
const v = this.value
|
|
1201
|
+
let s = this.off
|
|
1202
|
+
const e = lineEnd(v, s)
|
|
1203
|
+
while (s < e && cls(v[s]) !== 2)
|
|
1204
|
+
s++
|
|
1205
|
+
if (s >= e)
|
|
1206
|
+
return [ "", s ]
|
|
1207
|
+
while (s > 0 && cls(v[s - 1]) === 2)
|
|
1208
|
+
s--
|
|
1209
|
+
let t = s
|
|
1210
|
+
while (t < e && cls(v[t]) === 2)
|
|
1211
|
+
t++
|
|
1212
|
+
return [ v.slice(s, t), s ]
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/* the text objects "iw", "aw", "i\"", "a(", "ip", etc. */
|
|
1216
|
+
private object (k: string, n: number): Range | null {
|
|
1217
|
+
const v = this.value
|
|
1218
|
+
const o = this.off
|
|
1219
|
+
const inner = k[0] === "i"
|
|
1220
|
+
const t = k[1]
|
|
1221
|
+
const ls = lineStart(v, o)
|
|
1222
|
+
const le = lineEnd(v, o)
|
|
1223
|
+
const outer = (s: number, e: number): Range => {
|
|
1224
|
+
let e2 = e
|
|
1225
|
+
while (e2 < le && isSpace(v[e2]))
|
|
1226
|
+
e2++
|
|
1227
|
+
if (e2 > e)
|
|
1228
|
+
e = e2
|
|
1229
|
+
else
|
|
1230
|
+
while (s > ls && isSpace(v[s - 1]))
|
|
1231
|
+
s--
|
|
1232
|
+
return { start: s, end: e, linewise: false }
|
|
1233
|
+
}
|
|
1234
|
+
if (t === "w" || t === "W") {
|
|
1235
|
+
if (le === ls)
|
|
1236
|
+
return null
|
|
1237
|
+
const big = t === "W"
|
|
1238
|
+
const c = cls(v[o], big)
|
|
1239
|
+
let s = o, e = o
|
|
1240
|
+
while (s > ls && cls(v[s - 1], big) === c)
|
|
1241
|
+
s--
|
|
1242
|
+
while (e < le && cls(v[e], big) === c)
|
|
1243
|
+
e++
|
|
1244
|
+
for (let i = 1; i < n && e < le; i++) {
|
|
1245
|
+
const c2 = cls(v[e], big)
|
|
1246
|
+
while (e < le && cls(v[e], big) === c2)
|
|
1247
|
+
e++
|
|
1248
|
+
}
|
|
1249
|
+
if (inner)
|
|
1250
|
+
return { start: s, end: e, linewise: false }
|
|
1251
|
+
if (c === 0) {
|
|
1252
|
+
const c2 = cls(v[e], big)
|
|
1253
|
+
while (e < le && c2 !== 0 && cls(v[e], big) === c2)
|
|
1254
|
+
e++
|
|
1255
|
+
return { start: s, end: e, linewise: false }
|
|
1256
|
+
}
|
|
1257
|
+
return outer(s, e)
|
|
1258
|
+
}
|
|
1259
|
+
if (t === "\"" || t === "'" || t === "`") {
|
|
1260
|
+
const qs: number[] = []
|
|
1261
|
+
for (let i = ls; i < le; i++)
|
|
1262
|
+
if (v[i] === t && v[i - 1] !== "\\")
|
|
1263
|
+
qs.push(i)
|
|
1264
|
+
let pair: [ number, number ] | null = null
|
|
1265
|
+
for (let i = 0; i + 1 < qs.length && !pair; i += 2)
|
|
1266
|
+
if (qs[i] <= o && o <= qs[i + 1])
|
|
1267
|
+
pair = [ qs[i], qs[i + 1] ]
|
|
1268
|
+
for (let i = 0; i + 1 < qs.length && !pair; i += 2)
|
|
1269
|
+
if (qs[i] > o)
|
|
1270
|
+
pair = [ qs[i], qs[i + 1] ]
|
|
1271
|
+
if (!pair)
|
|
1272
|
+
return null
|
|
1273
|
+
return inner ? { start: pair[0] + 1, end: pair[1], linewise: false } : outer(pair[0], pair[1] + 1)
|
|
1274
|
+
}
|
|
1275
|
+
if (t === "p") {
|
|
1276
|
+
const last = lastLine(v)
|
|
1277
|
+
const empty = isEmpty(v, lineNo(v, o))
|
|
1278
|
+
let a = lineNo(v, o), b = a
|
|
1279
|
+
while (a > 0 && isEmpty(v, a - 1) === empty)
|
|
1280
|
+
a--
|
|
1281
|
+
while (b < last && isEmpty(v, b + 1) === empty)
|
|
1282
|
+
b++
|
|
1283
|
+
if (!inner)
|
|
1284
|
+
while (b < last && isEmpty(v, b + 1) !== empty)
|
|
1285
|
+
b++
|
|
1286
|
+
return { start: lineAt(v, a), end: lineEndNl(v, lineAt(v, b)), linewise: true }
|
|
1287
|
+
}
|
|
1288
|
+
const pair = PAIRS[t]
|
|
1289
|
+
return pair ? this.pairObject(pair, n, inner) : null
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
/* the bracket text objects "i(", "a)", "iB", "a<", etc. */
|
|
1293
|
+
private pairObject ([ open, close ]: [ string, string ], n: number, inner: boolean): Range | null {
|
|
1294
|
+
const v = this.value
|
|
1295
|
+
const o = this.off
|
|
1296
|
+
let a = o
|
|
1297
|
+
for (let i = 0; i < n; i++) {
|
|
1298
|
+
let depth = 0
|
|
1299
|
+
let j = i === 0 ? a : a - 1
|
|
1300
|
+
for (; j >= 0; j--) {
|
|
1301
|
+
if (v[j] === close && !(i === 0 && j === o))
|
|
1302
|
+
depth++
|
|
1303
|
+
else if (v[j] === open) {
|
|
1304
|
+
if (depth === 0)
|
|
1305
|
+
break
|
|
1306
|
+
depth--
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
if (j < 0)
|
|
1310
|
+
return null
|
|
1311
|
+
a = j
|
|
1312
|
+
}
|
|
1313
|
+
let b = -1
|
|
1314
|
+
let depth = 0
|
|
1315
|
+
for (let j = a; j < v.length && b < 0; j++) {
|
|
1316
|
+
if (v[j] === open)
|
|
1317
|
+
depth++
|
|
1318
|
+
else if (v[j] === close && --depth === 0)
|
|
1319
|
+
b = j
|
|
1320
|
+
}
|
|
1321
|
+
if (b < 0)
|
|
1322
|
+
return null
|
|
1323
|
+
if (!inner)
|
|
1324
|
+
return { start: a, end: b + 1, linewise: false }
|
|
1325
|
+
let s = a + 1, e = b
|
|
1326
|
+
if (v[s] === "\n")
|
|
1327
|
+
s++
|
|
1328
|
+
const lb = lineStart(v, b)
|
|
1329
|
+
if (lb > s && v.slice(lb, b).trim() === "")
|
|
1330
|
+
e = lb
|
|
1331
|
+
return { start: s, end: Math.max(s, e), linewise: false }
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
/* the range of a motion, with the exclusive/linewise rules of Vim */
|
|
1335
|
+
private rangeOf (m: Motion): Range {
|
|
1336
|
+
const v = this.value
|
|
1337
|
+
const s = Math.min(this.off, m.to)
|
|
1338
|
+
const e = Math.max(this.off, m.to)
|
|
1339
|
+
if (m.linewise)
|
|
1340
|
+
return { start: lineStart(v, s), end: lineEndNl(v, e), linewise: true }
|
|
1341
|
+
if (m.inclusive)
|
|
1342
|
+
return { start: s, end: Math.min(v.length, this.ops.next(v, e)), linewise: false }
|
|
1343
|
+
if (e > s && e === lineStart(v, e) && lineNo(v, e) > lineNo(v, s)) {
|
|
1344
|
+
if (s <= firstNonBlank(v, s))
|
|
1345
|
+
return { start: lineStart(v, s), end: e, linewise: true }
|
|
1346
|
+
return { start: s, end: e - 1, linewise: false }
|
|
1347
|
+
}
|
|
1348
|
+
return { start: s, end: e, linewise: false }
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
/* the range of "n" whole lines from the cursor */
|
|
1352
|
+
private lineRange (n: number): Range {
|
|
1353
|
+
const v = this.value
|
|
1354
|
+
const l = lineNo(v, this.off)
|
|
1355
|
+
const e = Math.min(lastLine(v), l + n - 1)
|
|
1356
|
+
return { start: lineAt(v, l), end: lineEndNl(v, lineAt(v, e)), linewise: true }
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
/* the current visual selection range */
|
|
1360
|
+
private selRange (): Range {
|
|
1361
|
+
const v = this.value
|
|
1362
|
+
const a = Math.min(this.anchor, this.off)
|
|
1363
|
+
const b = Math.max(this.anchor, this.off)
|
|
1364
|
+
if (this.mode === "vline")
|
|
1365
|
+
return { start: lineStart(v, a), end: lineEndNl(v, b), linewise: true }
|
|
1366
|
+
|
|
1367
|
+
/* after "$" the selection includes the line end (with its newline) */
|
|
1368
|
+
const end = b < v.length ? this.ops.next(v, b) : b
|
|
1369
|
+
return { start: a, end: b === this.off && this.want === Infinity ? Math.max(end, lineEndNl(v, b)) : end, linewise: false }
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
/* leave visual mode, remembering the selection for "'<", "'>", and "gv" */
|
|
1373
|
+
private leaveVisual () {
|
|
1374
|
+
const r = this.selRange()
|
|
1375
|
+
this.marks.set("<", r.start)
|
|
1376
|
+
this.marks.set(">", Math.max(r.start, r.end - 1))
|
|
1377
|
+
this.lastVisual = { mode: this.mode, anchor: this.anchor, off: this.off }
|
|
1378
|
+
this.mode = "normal"
|
|
1379
|
+
this.off = this.clampLine(this.value, this.off)
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
/* ==== OPERATORS ==== */
|
|
1383
|
+
|
|
1384
|
+
/* apply an operator onto the range of a motion or text object */
|
|
1385
|
+
private operate (op: string, key: string, marg: string, n: number, given: boolean, reg: string) {
|
|
1386
|
+
const v = this.value
|
|
1387
|
+
const o = this.off
|
|
1388
|
+
let r: Range | null = null
|
|
1389
|
+
if (key === "line")
|
|
1390
|
+
r = this.lineRange(n)
|
|
1391
|
+
else if (OBJECT.test(key))
|
|
1392
|
+
r = this.object(key, n)
|
|
1393
|
+
else if (op === "c" && (key === "w" || key === "W") && cls(v[o]) !== 0) {
|
|
1394
|
+
|
|
1395
|
+
/* "cw" on a word acts like "ce", but stays on the current word */
|
|
1396
|
+
const big = key === "W"
|
|
1397
|
+
let p = o
|
|
1398
|
+
while (p + 1 < v.length && cls(v[p + 1], big) === cls(v[p], big))
|
|
1399
|
+
p++
|
|
1400
|
+
for (let i = 1; i < n; i++)
|
|
1401
|
+
p = wordEnd(v, p, big)
|
|
1402
|
+
r = { start: o, end: Math.min(v.length, this.ops.next(v, p)), linewise: false }
|
|
1403
|
+
}
|
|
1404
|
+
else {
|
|
1405
|
+
const m = this.motion(key, marg, n, given, true)
|
|
1406
|
+
if (m)
|
|
1407
|
+
r = this.rangeOf(m)
|
|
1408
|
+
}
|
|
1409
|
+
if (r)
|
|
1410
|
+
this.apply(op, r, reg)
|
|
1411
|
+
else if (op === "c" && key === "l" && lineEnd(v, o) === o) {
|
|
1412
|
+
|
|
1413
|
+
/* "cl" resp. "s" on an empty line just starts inserting */
|
|
1414
|
+
this.begin()
|
|
1415
|
+
this.enterInsert(1, false)
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
/* apply an operator onto a range (with "n" as the shift count) */
|
|
1420
|
+
private apply (op: string, r: Range, reg: string, n = 1) {
|
|
1421
|
+
const v = this.value
|
|
1422
|
+
const text = v.slice(r.start, r.end)
|
|
1423
|
+
const regText = r.linewise && !text.endsWith("\n") ? text + "\n" : text
|
|
1424
|
+
const nlines = r.linewise ? regText.split("\n").length - 1 : 0
|
|
1425
|
+
if (op === "y") {
|
|
1426
|
+
this.setReg(reg, regText, r.linewise, "yank")
|
|
1427
|
+
if (nlines > 2)
|
|
1428
|
+
this.say(`${nlines} lines yanked`)
|
|
1429
|
+
if (!r.linewise || lineNo(v, r.start) !== lineNo(v, this.off))
|
|
1430
|
+
this.off = this.clampLine(v, r.start)
|
|
1431
|
+
return
|
|
1432
|
+
}
|
|
1433
|
+
this.begin()
|
|
1434
|
+
if (op === "d") {
|
|
1435
|
+
this.setReg(reg, regText, r.linewise, "delete")
|
|
1436
|
+
if (r.linewise && r.end === v.length && !text.endsWith("\n") && r.start > 0) {
|
|
1437
|
+
this.replace(r.start - 1, r.end, "")
|
|
1438
|
+
this.off = firstNonBlank(this.value, r.start - 1)
|
|
1439
|
+
}
|
|
1440
|
+
else {
|
|
1441
|
+
this.replace(r.start, r.end, "")
|
|
1442
|
+
this.off = r.linewise ? firstNonBlank(this.value, r.start) : r.start
|
|
1443
|
+
}
|
|
1444
|
+
this.off = this.clampLine(this.value, Math.min(this.off, this.value.length))
|
|
1445
|
+
if (nlines > 2)
|
|
1446
|
+
this.say(`${nlines} fewer lines`)
|
|
1447
|
+
}
|
|
1448
|
+
else if (op === "c") {
|
|
1449
|
+
this.setReg(reg, regText, r.linewise, "delete")
|
|
1450
|
+
this.replace(r.start, r.end, r.linewise && text.endsWith("\n") ? "\n" : "")
|
|
1451
|
+
this.off = r.start
|
|
1452
|
+
this.enterInsert(1, false)
|
|
1453
|
+
return
|
|
1454
|
+
}
|
|
1455
|
+
else if (op === ">" || op === "<") {
|
|
1456
|
+
const l1 = lineNo(v, r.start)
|
|
1457
|
+
const l2 = lineNo(v, Math.max(r.start, r.end - 1))
|
|
1458
|
+
const ls = v.split("\n")
|
|
1459
|
+
for (let l = l1; l <= l2; l++)
|
|
1460
|
+
for (let i = 0; i < n; i++)
|
|
1461
|
+
ls[l] = op === ">" ? (ls[l] !== "" ? "\t" + ls[l] : ls[l]) : ls[l].replace(/^(\t| {1,8})/, "")
|
|
1462
|
+
this.setText(ls.join("\n"))
|
|
1463
|
+
this.off = firstNonBlank(this.value, lineAt(this.value, l1))
|
|
1464
|
+
if (l2 - l1 + 1 > 2)
|
|
1465
|
+
this.say(`${l2 - l1 + 1} lines ${op}ed ${n} time${n > 1 ? "s" : ""}`)
|
|
1466
|
+
}
|
|
1467
|
+
else {
|
|
1468
|
+
const t = op === "gu" ? text.toLowerCase() : op === "gU" ? text.toUpperCase() : swapCase(text)
|
|
1469
|
+
this.replace(r.start, r.end, t)
|
|
1470
|
+
this.off = this.clampLine(this.value, r.start)
|
|
1471
|
+
}
|
|
1472
|
+
this.want = this.off - lineStart(this.value, this.off)
|
|
1473
|
+
this.end()
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
/* ==== SIMPLE CHANGES ==== */
|
|
1477
|
+
|
|
1478
|
+
/* put a register after or before the cursor ("p" and "P") */
|
|
1479
|
+
private put (reg: string, n: number, before: boolean) {
|
|
1480
|
+
const r = this.getReg(reg)
|
|
1481
|
+
if (!r) {
|
|
1482
|
+
this.fail(`E353: Nothing in register ${reg || "\""}`)
|
|
1483
|
+
return
|
|
1484
|
+
}
|
|
1485
|
+
if (r.text.length * n > 100_000_000) {
|
|
1486
|
+
this.fail("E1240: Resulting text too long")
|
|
1487
|
+
return
|
|
1488
|
+
}
|
|
1489
|
+
const v = this.value
|
|
1490
|
+
const o = this.off
|
|
1491
|
+
const text = r.text.repeat(n)
|
|
1492
|
+
this.begin()
|
|
1493
|
+
if (r.linewise) {
|
|
1494
|
+
let at = lineStart(v, o)
|
|
1495
|
+
let ins = text
|
|
1496
|
+
if (!before) {
|
|
1497
|
+
const e = lineEnd(v, o)
|
|
1498
|
+
if (e === v.length) {
|
|
1499
|
+
at = e
|
|
1500
|
+
ins = "\n" + text.slice(0, -1)
|
|
1501
|
+
}
|
|
1502
|
+
else
|
|
1503
|
+
at = e + 1
|
|
1504
|
+
}
|
|
1505
|
+
this.replace(at, at, ins)
|
|
1506
|
+
this.off = firstNonBlank(this.value, ins.startsWith("\n") ? at + 1 : at)
|
|
1507
|
+
}
|
|
1508
|
+
else {
|
|
1509
|
+
const at = before || lineEnd(v, o) === o ? o : this.ops.next(v, o)
|
|
1510
|
+
this.replace(at, at, text)
|
|
1511
|
+
this.off = text.includes("\n") ? at : Math.max(at, this.ops.previous(this.value, at + text.length))
|
|
1512
|
+
}
|
|
1513
|
+
this.end()
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
/* replace the characters under the cursor ("r") */
|
|
1517
|
+
private replaceChars (arg: string, n: number) {
|
|
1518
|
+
const ch = arg === "<Tab>" ? "\t" : arg === "<CR>" ? "\n" : arg
|
|
1519
|
+
if (!isText(ch) || ch.startsWith("\u0000"))
|
|
1520
|
+
return
|
|
1521
|
+
const v = this.value
|
|
1522
|
+
const o = this.off
|
|
1523
|
+
const le = lineEnd(v, o)
|
|
1524
|
+
let e = o
|
|
1525
|
+
for (let i = 0; i < n; i++) {
|
|
1526
|
+
if (e >= le)
|
|
1527
|
+
return
|
|
1528
|
+
e = this.ops.next(v, e)
|
|
1529
|
+
}
|
|
1530
|
+
this.begin()
|
|
1531
|
+
if (ch === "\n") {
|
|
1532
|
+
this.replace(o, e, "\n")
|
|
1533
|
+
this.off = o + 1
|
|
1534
|
+
}
|
|
1535
|
+
else {
|
|
1536
|
+
this.replace(o, e, ch.repeat(n))
|
|
1537
|
+
this.off = o + ch.length * (n - 1)
|
|
1538
|
+
}
|
|
1539
|
+
this.end()
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
/* toggle the case of the characters under the cursor ("~") */
|
|
1543
|
+
private toggleCase (n: number) {
|
|
1544
|
+
const v = this.value
|
|
1545
|
+
const o = this.off
|
|
1546
|
+
const le = lineEnd(v, o)
|
|
1547
|
+
let e = o
|
|
1548
|
+
for (let i = 0; i < n && e < le; i++)
|
|
1549
|
+
e = this.ops.next(v, e)
|
|
1550
|
+
if (e === o)
|
|
1551
|
+
return
|
|
1552
|
+
this.begin()
|
|
1553
|
+
this.replace(o, e, swapCase(v.slice(o, e)))
|
|
1554
|
+
this.off = this.clampLine(this.value, e)
|
|
1555
|
+
this.end()
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
/* add to the number under or after the cursor ("<C-a>" and "<C-x>") */
|
|
1559
|
+
private increment (n: number) {
|
|
1560
|
+
const v = this.value
|
|
1561
|
+
const ls = lineStart(v, this.off)
|
|
1562
|
+
const line = v.slice(ls, lineEnd(v, this.off))
|
|
1563
|
+
const col = this.off - ls
|
|
1564
|
+
for (const m of line.matchAll(/-?\d+/g)) {
|
|
1565
|
+
const s = m.index
|
|
1566
|
+
const e = s + m[0].length
|
|
1567
|
+
if (e <= col)
|
|
1568
|
+
continue
|
|
1569
|
+
const out = String(BigInt(m[0]) + BigInt(n))
|
|
1570
|
+
this.begin()
|
|
1571
|
+
this.replace(ls + s, ls + e, out)
|
|
1572
|
+
this.off = ls + s + out.length - 1
|
|
1573
|
+
this.end()
|
|
1574
|
+
return
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
/* join lines, with or without inserting spaces ("J" and "gJ") */
|
|
1579
|
+
private join (l: number, count: number, spaces: boolean) {
|
|
1580
|
+
let v = this.value
|
|
1581
|
+
const s0 = lineAt(v, l)
|
|
1582
|
+
let pos = -1
|
|
1583
|
+
for (let i = 1; i < count; i++) {
|
|
1584
|
+
const le = lineEnd(v, s0)
|
|
1585
|
+
if (le >= v.length)
|
|
1586
|
+
break
|
|
1587
|
+
let k = le + 1
|
|
1588
|
+
if (spaces)
|
|
1589
|
+
while (k < v.length && isSpace(v[k]))
|
|
1590
|
+
k++
|
|
1591
|
+
const sep = spaces && k < v.length && v[k] !== "\n" && v[k] !== ")" && le > s0 && !isSpace(v[le - 1]) ? " " : ""
|
|
1592
|
+
v = v.slice(0, le) + sep + v.slice(k)
|
|
1593
|
+
pos = le
|
|
1594
|
+
}
|
|
1595
|
+
if (pos < 0)
|
|
1596
|
+
return
|
|
1597
|
+
this.setText(v)
|
|
1598
|
+
this.off = this.clampLine(v, pos)
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
/* move the cursor by lines, half pages, or pages (the scroll commands) */
|
|
1602
|
+
private scroll (k: string, n: number, given: boolean) {
|
|
1603
|
+
const v = this.value
|
|
1604
|
+
const half = Math.max(1, Math.floor(this.height / 2))
|
|
1605
|
+
const page = Math.max(1, this.height - 2)
|
|
1606
|
+
const amount = k === "<C-d>" || k === "<C-u>" ? (given ? n : half)
|
|
1607
|
+
: k === "<C-e>" || k === "<C-y>" ? n : page * n
|
|
1608
|
+
const down = k === "<C-d>" || k === "<C-f>" || k === "<PageDown>" || k === "<C-e>"
|
|
1609
|
+
const l = lineNo(v, this.off)
|
|
1610
|
+
const t = Math.max(0, Math.min(lastLine(v), l + (down ? amount : -amount)))
|
|
1611
|
+
if (t === l)
|
|
1612
|
+
return
|
|
1613
|
+
if (k === "<C-e>" || k === "<C-y>")
|
|
1614
|
+
this.off = this.column(v, t, this.want)
|
|
1615
|
+
else {
|
|
1616
|
+
this.off = firstNonBlank(v, lineAt(v, t))
|
|
1617
|
+
this.want = this.off - lineAt(v, t)
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
/* ==== INSERT MODE ==== */
|
|
1622
|
+
|
|
1623
|
+
/* enter insert mode, with a count and whether the count repeats on new lines */
|
|
1624
|
+
private enterInsert (count: number, open: boolean) {
|
|
1625
|
+
this.mode = "insert"
|
|
1626
|
+
this.insCount = count
|
|
1627
|
+
this.insOpen = open
|
|
1628
|
+
this.insTokens = []
|
|
1629
|
+
this.replaced = []
|
|
1630
|
+
this.message = ""
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/* leave insert mode, repeating the inserted keys for a count */
|
|
1634
|
+
private leaveInsert () {
|
|
1635
|
+
if (this.insCount > 1) {
|
|
1636
|
+
const tokens = this.insTokens.filter((t) => t !== "<Esc>" && t !== "<C-c>" && t !== "<C-[>")
|
|
1637
|
+
for (let i = 1; i < this.insCount; i++) {
|
|
1638
|
+
if (this.insOpen)
|
|
1639
|
+
this.insertKey("<CR>")
|
|
1640
|
+
for (const t of tokens)
|
|
1641
|
+
this.insertKey(t)
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
const v = this.value
|
|
1645
|
+
this.mode = "normal"
|
|
1646
|
+
this.lastInsert = this.off
|
|
1647
|
+
if (this.off > lineStart(v, this.off))
|
|
1648
|
+
this.off = this.ops.previous(v, this.off)
|
|
1649
|
+
this.off = this.clampLine(v, this.off)
|
|
1650
|
+
this.want = this.off - lineStart(v, this.off)
|
|
1651
|
+
this.end()
|
|
1652
|
+
if (this.recChange) {
|
|
1653
|
+
this.lastChange = this.change
|
|
1654
|
+
this.recChange = false
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
/* perform an insert mode key ourselves (when replaying or not handled by the front end) */
|
|
1659
|
+
private insertKey (t: string) {
|
|
1660
|
+
const v = this.value
|
|
1661
|
+
const o = this.off
|
|
1662
|
+
if (t.startsWith("\u0000")) {
|
|
1663
|
+
this.insertText(t.slice(1))
|
|
1664
|
+
return
|
|
1665
|
+
}
|
|
1666
|
+
if (isText(t)) {
|
|
1667
|
+
if (this.mode === "replace")
|
|
1668
|
+
this.overwrite(t)
|
|
1669
|
+
else
|
|
1670
|
+
this.insertText(t)
|
|
1671
|
+
return
|
|
1672
|
+
}
|
|
1673
|
+
switch (t) {
|
|
1674
|
+
case "<CR>": case "<C-j>": case "<C-m>":
|
|
1675
|
+
this.insertText("\n")
|
|
1676
|
+
break
|
|
1677
|
+
case "<Tab>": case "<C-i>":
|
|
1678
|
+
if (this.mode === "replace")
|
|
1679
|
+
this.overwrite("\t")
|
|
1680
|
+
else
|
|
1681
|
+
this.insertText("\t")
|
|
1682
|
+
break
|
|
1683
|
+
case "<BS>": case "<C-h>": {
|
|
1684
|
+
const r = this.mode === "replace" ? this.replaced.pop() : undefined
|
|
1685
|
+
if (r) {
|
|
1686
|
+
this.replace(r.off, r.off + r.len, r.orig)
|
|
1687
|
+
this.off = r.off
|
|
1688
|
+
}
|
|
1689
|
+
else if (o > 0) {
|
|
1690
|
+
const p = this.ops.previous(v, o)
|
|
1691
|
+
if (this.mode !== "replace")
|
|
1692
|
+
this.replace(p, o, "")
|
|
1693
|
+
this.off = p
|
|
1694
|
+
}
|
|
1695
|
+
break
|
|
1696
|
+
}
|
|
1697
|
+
case "<Del>":
|
|
1698
|
+
if (o < v.length)
|
|
1699
|
+
this.replace(o, this.ops.next(v, o), "")
|
|
1700
|
+
break
|
|
1701
|
+
case "<C-w>": {
|
|
1702
|
+
const p = this.ops.wordStart(v, o)
|
|
1703
|
+
this.replace(p, o, "")
|
|
1704
|
+
this.off = p
|
|
1705
|
+
break
|
|
1706
|
+
}
|
|
1707
|
+
case "<C-u>": {
|
|
1708
|
+
const f = firstNonBlank(v, o)
|
|
1709
|
+
const p = o > f ? f : lineStart(v, o)
|
|
1710
|
+
this.replace(p, o, "")
|
|
1711
|
+
this.off = p
|
|
1712
|
+
break
|
|
1713
|
+
}
|
|
1714
|
+
case "<Left>": this.off = o > lineStart(v, o) ? this.ops.previous(v, o) : o; break
|
|
1715
|
+
case "<Right>": this.off = o < lineEnd(v, o) ? this.ops.next(v, o) : o; break
|
|
1716
|
+
case "<Home>": this.off = lineStart(v, o); break
|
|
1717
|
+
case "<End>": this.off = lineEnd(v, o); break
|
|
1718
|
+
case "<Up>": case "<Down>":
|
|
1719
|
+
this.off = this.ops.vertical(v, o, t === "<Up>" ? -1 : 1)
|
|
1720
|
+
break
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/* insert a text at the cursor and move the cursor behind it */
|
|
1725
|
+
private insertText (text: string) {
|
|
1726
|
+
const o = this.off
|
|
1727
|
+
this.replace(o, o, text)
|
|
1728
|
+
this.off = o + text.length
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
/* overwrite the character under the cursor (replace mode) */
|
|
1732
|
+
private overwrite (ch: string) {
|
|
1733
|
+
const v = this.value
|
|
1734
|
+
const o = this.off
|
|
1735
|
+
const e = o < lineEnd(v, o) ? this.ops.next(v, o) : o
|
|
1736
|
+
this.replaced.push({ off: o, orig: v.slice(o, e), len: ch.length })
|
|
1737
|
+
this.replace(o, e, ch)
|
|
1738
|
+
this.off = o + ch.length
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
/* ==== COMMAND LINE ==== */
|
|
1742
|
+
|
|
1743
|
+
/* open the command line for an Ex command or a search */
|
|
1744
|
+
private openCmd (kind: string, text: string) {
|
|
1745
|
+
this.cmdFrom = this.mode
|
|
1746
|
+
this.mode = "cmdline"
|
|
1747
|
+
this.cmdKind = kind
|
|
1748
|
+
this.cmdText = text
|
|
1749
|
+
this.cmdIdx = -1
|
|
1750
|
+
this.message = ""
|
|
1751
|
+
this.error = false
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
/* execute a search command line ("/" and "?") */
|
|
1755
|
+
private searchCmd (text: string) {
|
|
1756
|
+
const pattern = text || this.search?.pattern
|
|
1757
|
+
if (!pattern) {
|
|
1758
|
+
this.fail("E35: No previous regular expression")
|
|
1759
|
+
return
|
|
1760
|
+
}
|
|
1761
|
+
const forward = this.cmdKind === "/"
|
|
1762
|
+
this.search = { pattern, forward }
|
|
1763
|
+
this.hlsearch = true
|
|
1764
|
+
this.say(this.cmdKind + pattern)
|
|
1765
|
+
const q = this.find(pattern, this.off, forward)
|
|
1766
|
+
if (q !== null)
|
|
1767
|
+
this.go({ to: q, linewise: false, inclusive: false }, true)
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
/* find the next match of a pattern, wrapping around the text end */
|
|
1771
|
+
private find (pattern: string, from: number, fwd: boolean): number | null {
|
|
1772
|
+
const re = toRegExp(pattern, this.icase(pattern))
|
|
1773
|
+
if (!re) {
|
|
1774
|
+
this.fail(`E486: Pattern not found: ${pattern}`)
|
|
1775
|
+
return null
|
|
1776
|
+
}
|
|
1777
|
+
const all = [ ...this.value.matchAll(re) ].map((m) => m.index)
|
|
1778
|
+
if (all.length === 0) {
|
|
1779
|
+
this.fail(`E486: Pattern not found: ${pattern}`)
|
|
1780
|
+
return null
|
|
1781
|
+
}
|
|
1782
|
+
if (fwd) {
|
|
1783
|
+
const q = all.find((i) => i > from)
|
|
1784
|
+
if (q !== undefined)
|
|
1785
|
+
return q
|
|
1786
|
+
this.fail("search hit BOTTOM, continuing at TOP", false)
|
|
1787
|
+
return all[0]
|
|
1788
|
+
}
|
|
1789
|
+
const q = all.findLast((i) => i < from)
|
|
1790
|
+
if (q !== undefined)
|
|
1791
|
+
return q
|
|
1792
|
+
this.fail("search hit TOP, continuing at BOTTOM", false)
|
|
1793
|
+
return all[all.length - 1]
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
/* parse a line address like "12", ".", "$", "'a", or ".+3" */
|
|
1797
|
+
private addr (s: string, i: number): [ number, number ] | null {
|
|
1798
|
+
const v = this.value
|
|
1799
|
+
const rest = s.slice(i)
|
|
1800
|
+
let line: number | null = null
|
|
1801
|
+
let m = /^(\d+|\.|\$|'[a-zA-Z<>'])/.exec(rest)
|
|
1802
|
+
if (m) {
|
|
1803
|
+
if (m[1] === ".")
|
|
1804
|
+
line = lineNo(v, this.off)
|
|
1805
|
+
else if (m[1] === "$")
|
|
1806
|
+
line = lastLine(v)
|
|
1807
|
+
else if (m[1].startsWith("'")) {
|
|
1808
|
+
const p = this.marks.get(m[1][1])
|
|
1809
|
+
if (p === undefined)
|
|
1810
|
+
return null
|
|
1811
|
+
line = lineNo(v, Math.min(p, v.length))
|
|
1812
|
+
}
|
|
1813
|
+
else
|
|
1814
|
+
line = parseInt(m[1], 10) - 1
|
|
1815
|
+
}
|
|
1816
|
+
let len = m ? m[0].length : 0
|
|
1817
|
+
m = /^([+-])(\d*)/.exec(rest.slice(len))
|
|
1818
|
+
if (m) {
|
|
1819
|
+
line = (line ?? lineNo(v, this.off)) + (m[1] === "+" ? 1 : -1) * (m[2] ? parseInt(m[2], 10) : 1)
|
|
1820
|
+
len += m[0].length
|
|
1821
|
+
}
|
|
1822
|
+
return line === null ? null : [ line, i + len ]
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
/* execute an Ex command line */
|
|
1826
|
+
private ex (input: string) {
|
|
1827
|
+
this.lastEx = input
|
|
1828
|
+
const v = this.value
|
|
1829
|
+
const last = lastLine(v)
|
|
1830
|
+
let s = input.replace(/^[\s:]+/, "")
|
|
1831
|
+
let from = lineNo(v, this.off)
|
|
1832
|
+
let to = from
|
|
1833
|
+
let ranged = false
|
|
1834
|
+
if (s.startsWith("%")) {
|
|
1835
|
+
from = 0
|
|
1836
|
+
to = last
|
|
1837
|
+
ranged = true
|
|
1838
|
+
s = s.slice(1)
|
|
1839
|
+
}
|
|
1840
|
+
else {
|
|
1841
|
+
const a = this.addr(s, 0)
|
|
1842
|
+
if (a) {
|
|
1843
|
+
from = to = a[0]
|
|
1844
|
+
ranged = true
|
|
1845
|
+
let i = a[1]
|
|
1846
|
+
if (s[i] === "," || s[i] === ";") {
|
|
1847
|
+
const b = this.addr(s, i + 1)
|
|
1848
|
+
if (b) {
|
|
1849
|
+
to = b[0]
|
|
1850
|
+
i = b[1]
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
s = s.slice(i)
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
s = s.trim()
|
|
1857
|
+
if (from > to)
|
|
1858
|
+
[ from, to ] = [ to, from ]
|
|
1859
|
+
if (from < 0 || to > last) {
|
|
1860
|
+
this.fail("E16: Invalid range")
|
|
1861
|
+
return
|
|
1862
|
+
}
|
|
1863
|
+
if (s === "") {
|
|
1864
|
+
if (ranged)
|
|
1865
|
+
this.go({ to: firstNonBlank(v, lineAt(v, to)), linewise: true, inclusive: false }, true)
|
|
1866
|
+
return
|
|
1867
|
+
}
|
|
1868
|
+
const sub = /^(?:s|substitute)([^\w\s"|])(.*)$/.exec(s)
|
|
1869
|
+
if (sub) {
|
|
1870
|
+
this.substitute(from, to, sub[1], sub[2])
|
|
1871
|
+
return
|
|
1872
|
+
}
|
|
1873
|
+
const m = /^([a-zA-Z]+|[<>&]+)(!?)\s*(.*)$/.exec(s)
|
|
1874
|
+
const name = m ? m[1] : s
|
|
1875
|
+
const arg = m ? m[3] : ""
|
|
1876
|
+
const lines = { start: lineAt(v, from), end: lineEndNl(v, lineAt(v, to)), linewise: true }
|
|
1877
|
+
switch (name) {
|
|
1878
|
+
case "d": case "de": case "del": case "delete":
|
|
1879
|
+
this.apply("d", lines, arg)
|
|
1880
|
+
return
|
|
1881
|
+
case "y": case "ya": case "yank": {
|
|
1882
|
+
const o = this.off
|
|
1883
|
+
this.apply("y", lines, arg)
|
|
1884
|
+
this.off = o
|
|
1885
|
+
return
|
|
1886
|
+
}
|
|
1887
|
+
case "j": case "join":
|
|
1888
|
+
this.begin()
|
|
1889
|
+
this.join(from, Math.max(2, to - from + 1), m?.[2] !== "!")
|
|
1890
|
+
this.end()
|
|
1891
|
+
return
|
|
1892
|
+
case "u": case "undo":
|
|
1893
|
+
this.undo(1)
|
|
1894
|
+
return
|
|
1895
|
+
case "red": case "redo":
|
|
1896
|
+
this.redo(1)
|
|
1897
|
+
return
|
|
1898
|
+
case "noh": case "nohl": case "nohlsearch":
|
|
1899
|
+
this.hlsearch = false
|
|
1900
|
+
return
|
|
1901
|
+
case "&": case "&&":
|
|
1902
|
+
if (this.lastSub)
|
|
1903
|
+
this.substitute(from, to, "\u0001",
|
|
1904
|
+
`${this.lastSub.pat}\u0001${this.lastSub.rep}\u0001${name === "&&" ? this.lastSub.flags : ""}`)
|
|
1905
|
+
return
|
|
1906
|
+
case "set": case "se":
|
|
1907
|
+
this.setOption(arg)
|
|
1908
|
+
return
|
|
1909
|
+
case "reg": case "registers": case "di": case "display":
|
|
1910
|
+
this.say([ ...this.regs.entries() ]
|
|
1911
|
+
.map(([ k, r ]) => `"${k} ${JSON.stringify(r.text).slice(1, -1).slice(0, 29)}`).join(" ") || "no registers")
|
|
1912
|
+
return
|
|
1913
|
+
}
|
|
1914
|
+
if (/^[<>]+$/.test(name)) {
|
|
1915
|
+
this.apply(name[0], lines, "", name.length)
|
|
1916
|
+
return
|
|
1917
|
+
}
|
|
1918
|
+
this.runHost(s)
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
/* pass an Ex command to the host and show its result */
|
|
1922
|
+
private runHost (command: string) {
|
|
1923
|
+
const res = this.host(command)
|
|
1924
|
+
if (res === undefined)
|
|
1925
|
+
this.fail(`E492: Not an editor command: ${command}`)
|
|
1926
|
+
else if (res !== null) {
|
|
1927
|
+
if (/^E\d+:/.test(res))
|
|
1928
|
+
this.fail(res)
|
|
1929
|
+
else
|
|
1930
|
+
this.say(res)
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
/* set the options of ":set" */
|
|
1935
|
+
private setOption (arg: string) {
|
|
1936
|
+
for (const opt of arg.split(/\s+/).filter((o) => o !== "")) {
|
|
1937
|
+
const on = !opt.startsWith("no")
|
|
1938
|
+
const name = on ? opt : opt.slice(2)
|
|
1939
|
+
if (name === "hlsearch" || name === "hls")
|
|
1940
|
+
this.hlsearch = on
|
|
1941
|
+
else if (name === "ignorecase" || name === "ic")
|
|
1942
|
+
this.ignorecase = on
|
|
1943
|
+
else if (name === "smartcase" || name === "scs")
|
|
1944
|
+
this.smartcase = on
|
|
1945
|
+
else {
|
|
1946
|
+
this.fail(`E518: Unknown option: ${opt}`)
|
|
1947
|
+
return
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
/* substitute a pattern on a line range (":s") */
|
|
1953
|
+
private substitute (from: number, to: number, delim: string, body: string) {
|
|
1954
|
+
const [ p = "", rep = "", flags = "" ] = splitDelim(body, delim)
|
|
1955
|
+
const pat = p || this.search?.pattern || ""
|
|
1956
|
+
if (!pat) {
|
|
1957
|
+
this.fail("E35: No previous regular expression")
|
|
1958
|
+
return
|
|
1959
|
+
}
|
|
1960
|
+
if (flags.includes("c")) {
|
|
1961
|
+
this.fail("confirm flag \"c\" is not supported")
|
|
1962
|
+
return
|
|
1963
|
+
}
|
|
1964
|
+
this.lastSub = { pat, rep, flags }
|
|
1965
|
+
this.search = { pattern: pat, forward: true }
|
|
1966
|
+
this.hlsearch = true
|
|
1967
|
+
const icase = flags.includes("I") ? false : flags.includes("i") || this.icase(pat)
|
|
1968
|
+
const re = toRegExp(pat, icase)
|
|
1969
|
+
if (!re) {
|
|
1970
|
+
this.fail(`E486: Pattern not found: ${pat}`)
|
|
1971
|
+
return
|
|
1972
|
+
}
|
|
1973
|
+
const once = new RegExp(re.source, re.flags.replace("g", ""))
|
|
1974
|
+
const lines = this.value.split("\n")
|
|
1975
|
+
let subs = 0, changed = 0, lastL = -1
|
|
1976
|
+
for (let l = from; l <= to; l++) {
|
|
1977
|
+
let hit = false
|
|
1978
|
+
const out = lines[l].replace(flags.includes("g") ? re : once, (...args: unknown[]) => {
|
|
1979
|
+
subs++
|
|
1980
|
+
hit = true
|
|
1981
|
+
return expandRep(rep, args)
|
|
1982
|
+
})
|
|
1983
|
+
if (hit) {
|
|
1984
|
+
lines[l] = out
|
|
1985
|
+
changed++
|
|
1986
|
+
lastL = l
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
if (subs === 0) {
|
|
1990
|
+
this.fail(`E486: Pattern not found: ${pat}`)
|
|
1991
|
+
return
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
/* the "n" flag just reports the number of matches */
|
|
1995
|
+
if (flags.includes("n")) {
|
|
1996
|
+
this.say(`${subs} match${subs > 1 ? "es" : ""} on ${changed} line${changed > 1 ? "s" : ""}`)
|
|
1997
|
+
return
|
|
1998
|
+
}
|
|
1999
|
+
this.begin()
|
|
2000
|
+
this.setText(lines.join("\n"))
|
|
2001
|
+
this.off = firstNonBlank(this.value, lineAt(this.value, lastL))
|
|
2002
|
+
this.end()
|
|
2003
|
+
if (subs > 2)
|
|
2004
|
+
this.say(`${subs} substitutions on ${changed} line${changed > 1 ? "s" : ""}`)
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
/* ==== REPEATING, MACROS, AND UNDO ==== */
|
|
2008
|
+
|
|
2009
|
+
/* repeat the last change, optionally with a new count (".") */
|
|
2010
|
+
private dot (c: Cmd) {
|
|
2011
|
+
let tokens = this.lastChange
|
|
2012
|
+
if (tokens.length === 0)
|
|
2013
|
+
return
|
|
2014
|
+
if (c.count > 0) {
|
|
2015
|
+
|
|
2016
|
+
/* the new count replaces the counts before and after the register and the operator */
|
|
2017
|
+
const old = tokens
|
|
2018
|
+
const skip = (i: number) => {
|
|
2019
|
+
if (/^[1-9]$/.test(old[i] ?? ""))
|
|
2020
|
+
while (/^[0-9]$/.test(old[i] ?? ""))
|
|
2021
|
+
i++
|
|
2022
|
+
return i
|
|
2023
|
+
}
|
|
2024
|
+
let i = skip(0)
|
|
2025
|
+
const head: string[] = []
|
|
2026
|
+
if (old[i] === "\"") {
|
|
2027
|
+
head.push(...old.slice(i, i + 2))
|
|
2028
|
+
i = skip(i + 2)
|
|
2029
|
+
}
|
|
2030
|
+
const len = PREFIXES.has(old[i] ?? "") ? 2 : 1
|
|
2031
|
+
const cmd = old.slice(i, i + len)
|
|
2032
|
+
const rest = OPERATORS.has(cmd.join("")) ? old.slice(skip(i + len)) : old.slice(i + len)
|
|
2033
|
+
tokens = [ ...head, ...String(c.count).split(""), ...cmd, ...rest ]
|
|
2034
|
+
}
|
|
2035
|
+
this.replay(tokens)
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/* replay key tokens, guarded against endless recursion and optionally aborted
|
|
2039
|
+
by a failing command (returning whether no command failed) */
|
|
2040
|
+
private replay (tokens: string[], abort = false) {
|
|
2041
|
+
if (this.depth > 100)
|
|
2042
|
+
return false
|
|
2043
|
+
this.depth++
|
|
2044
|
+
this.failed = false
|
|
2045
|
+
for (const t of tokens) {
|
|
2046
|
+
this.feed(t, false)
|
|
2047
|
+
if (abort && this.failed)
|
|
2048
|
+
break
|
|
2049
|
+
}
|
|
2050
|
+
this.depth--
|
|
2051
|
+
return !this.failed
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
/* start or stop recording a macro into a register ("q") */
|
|
2055
|
+
private macroRecord (reg: string) {
|
|
2056
|
+
if (this.recording) {
|
|
2057
|
+
if (this.macro[this.macro.length - 1] === "q")
|
|
2058
|
+
this.macro.pop()
|
|
2059
|
+
this.macros.set(this.recording, this.macro)
|
|
2060
|
+
const text = this.macro.filter((t) => isText(t)).map((t) => t.startsWith("\u0000") ? t.slice(1) : t).join("")
|
|
2061
|
+
this.regs.set(this.recording, { text, linewise: false })
|
|
2062
|
+
this.recording = ""
|
|
2063
|
+
return
|
|
2064
|
+
}
|
|
2065
|
+
if (!/^[a-zA-Z0-9]$/.test(reg))
|
|
2066
|
+
return
|
|
2067
|
+
const lower = reg.toLowerCase()
|
|
2068
|
+
this.macro = reg !== lower ? [ ...(this.macros.get(lower) ?? []) ] : []
|
|
2069
|
+
this.recording = lower
|
|
2070
|
+
this.message = ""
|
|
2071
|
+
}
|
|
2072
|
+
|
|
2073
|
+
/* play a macro or the last Ex command ("@") */
|
|
2074
|
+
private macroPlay (reg: string, n: number) {
|
|
2075
|
+
if (reg === ":") {
|
|
2076
|
+
for (let i = 0; i < n && this.lastEx; i++)
|
|
2077
|
+
this.ex(this.lastEx)
|
|
2078
|
+
return
|
|
2079
|
+
}
|
|
2080
|
+
const r = (reg === "@" ? this.lastMacro : reg).toLowerCase()
|
|
2081
|
+
if (!r)
|
|
2082
|
+
return
|
|
2083
|
+
this.lastMacro = r
|
|
2084
|
+
const tokens = this.macros.get(r) ?? [ ...(this.regs.get(r)?.text ?? "") ].map((c) => c === "\n" ? "<CR>" : c)
|
|
2085
|
+
for (let i = 0; i < n; i++)
|
|
2086
|
+
if (!this.replay(tokens, true))
|
|
2087
|
+
break
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
/* open an undo step */
|
|
2091
|
+
private begin () {
|
|
2092
|
+
this.undos.push({ value: this.value, off: this.off })
|
|
2093
|
+
if (this.undos.length > 1000)
|
|
2094
|
+
this.undos.shift()
|
|
2095
|
+
this.redoSave = this.redos
|
|
2096
|
+
this.redos = []
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
/* close an undo step, dropping it where nothing changed */
|
|
2100
|
+
private end () {
|
|
2101
|
+
const top = this.undos[this.undos.length - 1]
|
|
2102
|
+
if (top && top.value === this.value) {
|
|
2103
|
+
this.undos.pop()
|
|
2104
|
+
this.redos = this.redoSave
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
/* undo up to "n" changes */
|
|
2109
|
+
private undo (n: number) {
|
|
2110
|
+
let done = 0
|
|
2111
|
+
for (let i = 0; i < n && this.undos.length > 0; i++, done++) {
|
|
2112
|
+
const s = this.undos.pop()!
|
|
2113
|
+
this.redos.push({ value: this.value, off: s.off })
|
|
2114
|
+
this.value = s.value
|
|
2115
|
+
this.off = s.off
|
|
2116
|
+
}
|
|
2117
|
+
if (done === 0) {
|
|
2118
|
+
this.say("Already at oldest change")
|
|
2119
|
+
return
|
|
2120
|
+
}
|
|
2121
|
+
this.emit(this.value)
|
|
2122
|
+
this.off = this.clampLine(this.value, Math.min(this.off, this.value.length))
|
|
2123
|
+
this.say(`${done} change${done > 1 ? "s" : ""}; before #${this.undos.length + 1}`)
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
/* redo up to "n" undone changes */
|
|
2127
|
+
private redo (n: number) {
|
|
2128
|
+
let done = 0
|
|
2129
|
+
for (let i = 0; i < n && this.redos.length > 0; i++, done++) {
|
|
2130
|
+
const s = this.redos.pop()!
|
|
2131
|
+
this.undos.push({ value: this.value, off: s.off })
|
|
2132
|
+
this.value = s.value
|
|
2133
|
+
this.off = s.off
|
|
2134
|
+
}
|
|
2135
|
+
if (done === 0) {
|
|
2136
|
+
this.say("Already at newest change")
|
|
2137
|
+
return
|
|
2138
|
+
}
|
|
2139
|
+
this.emit(this.value)
|
|
2140
|
+
this.off = this.clampLine(this.value, Math.min(this.off, this.value.length))
|
|
2141
|
+
this.say(`${done} change${done > 1 ? "s" : ""}; after #${this.undos.length}`)
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
/* ==== REGISTERS AND HELPERS ==== */
|
|
2145
|
+
|
|
2146
|
+
/* store a yanked or deleted text into a register and the unnamed and numbered ones */
|
|
2147
|
+
private setReg (reg: string, text: string, linewise: boolean, kind: "yank" | "delete") {
|
|
2148
|
+
if (reg === "_")
|
|
2149
|
+
return
|
|
2150
|
+
let r: Reg = { text, linewise }
|
|
2151
|
+
if (/^[A-Z]$/.test(reg)) {
|
|
2152
|
+
const old = this.regs.get(reg.toLowerCase())
|
|
2153
|
+
|
|
2154
|
+
/* mixing a linewise and a charwise text makes the latter whole lines */
|
|
2155
|
+
if (old) {
|
|
2156
|
+
const mix = old.linewise !== linewise
|
|
2157
|
+
r = { text: old.text + (mix && linewise ? "\n" : "") + text + (mix && old.linewise ? "\n" : ""),
|
|
2158
|
+
linewise: old.linewise || linewise }
|
|
2159
|
+
}
|
|
2160
|
+
this.regs.set(reg.toLowerCase(), r)
|
|
2161
|
+
}
|
|
2162
|
+
else if (reg !== "" && reg !== "\"")
|
|
2163
|
+
this.regs.set(reg, r)
|
|
2164
|
+
else if (kind === "yank")
|
|
2165
|
+
this.regs.set("0", r)
|
|
2166
|
+
else
|
|
2167
|
+
this.regs.set(linewise || text.includes("\n") ? "1" : "-", r)
|
|
2168
|
+
this.macros.delete(reg.toLowerCase())
|
|
2169
|
+
if (reg === "+" || reg === "*")
|
|
2170
|
+
this.clip(text)
|
|
2171
|
+
this.regs.set("\"", r)
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
/* the register of a name (the unnamed one for an empty name) */
|
|
2175
|
+
private getReg (reg: string): Reg | undefined {
|
|
2176
|
+
return this.regs.get(reg === "" ? "\"" : reg.toLowerCase())
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
/* replace a range of the text */
|
|
2180
|
+
private replace (s: number, e: number, text: string) {
|
|
2181
|
+
this.setText(this.value.slice(0, s) + text + this.value.slice(e))
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
/* set the entire text and emit it to the front end */
|
|
2185
|
+
private setText (value: string) {
|
|
2186
|
+
this.value = value
|
|
2187
|
+
this.emit(value)
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
/* show an informational message */
|
|
2191
|
+
protected say (text: string) {
|
|
2192
|
+
this.message = text
|
|
2193
|
+
this.error = false
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
/* show an error message (aborting a replay, unless just a warning) */
|
|
2197
|
+
protected fail (text: string, abort = true) {
|
|
2198
|
+
this.message = text
|
|
2199
|
+
this.error = true
|
|
2200
|
+
if (abort)
|
|
2201
|
+
this.failed = true
|
|
2202
|
+
}
|
|
2203
|
+
}
|