@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,1530 @@
|
|
|
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
|
+
/* Emacs editing engine, operating on a plain string value and key tokens
|
|
25
|
+
(dependency-free: it knows nothing about React, Ink, or any terminal) */
|
|
26
|
+
/* internal dependencies */
|
|
27
|
+
import { lineStart, lineEnd, lineNo, lineAt, toCursor, toOffset, isSpace, isBlank, wordEnd, wordBegin, paragraphEnd, paragraphBegin, literalRegExp, goalColumn, rowMove, fillParagraph } from "./ink-editor-text";
|
|
28
|
+
/* the key bindings, from key sequences to command names */
|
|
29
|
+
const BINDINGS = new Map(Object.entries({
|
|
30
|
+
/* motion */
|
|
31
|
+
"C-f": "forward-char",
|
|
32
|
+
"<right>": "forward-char",
|
|
33
|
+
"C-b": "backward-char",
|
|
34
|
+
"<left>": "backward-char",
|
|
35
|
+
"C-n": "next-line",
|
|
36
|
+
"<down>": "next-line",
|
|
37
|
+
"C-p": "previous-line",
|
|
38
|
+
"<up>": "previous-line",
|
|
39
|
+
"C-a": "move-beginning-of-line",
|
|
40
|
+
"<home>": "move-beginning-of-line",
|
|
41
|
+
"C-e": "move-end-of-line",
|
|
42
|
+
"<end>": "move-end-of-line",
|
|
43
|
+
"M-f": "forward-word",
|
|
44
|
+
"C-<right>": "forward-word",
|
|
45
|
+
"M-<right>": "forward-word",
|
|
46
|
+
"M-b": "backward-word",
|
|
47
|
+
"C-<left>": "backward-word",
|
|
48
|
+
"M-<left>": "backward-word",
|
|
49
|
+
"M-}": "forward-paragraph",
|
|
50
|
+
"C-<down>": "forward-paragraph",
|
|
51
|
+
"M-{": "backward-paragraph",
|
|
52
|
+
"C-<up>": "backward-paragraph",
|
|
53
|
+
"M-m": "back-to-indentation",
|
|
54
|
+
"M-<": "beginning-of-buffer",
|
|
55
|
+
"C-<home>": "beginning-of-buffer",
|
|
56
|
+
"M->": "end-of-buffer",
|
|
57
|
+
"C-<end>": "end-of-buffer",
|
|
58
|
+
"C-v": "scroll-up-command",
|
|
59
|
+
"<next>": "scroll-up-command",
|
|
60
|
+
"M-v": "scroll-down-command",
|
|
61
|
+
"<prior>": "scroll-down-command",
|
|
62
|
+
"C-l": "recenter-top-bottom",
|
|
63
|
+
"M-g g": "goto-line",
|
|
64
|
+
"M-g M-g": "goto-line",
|
|
65
|
+
"M-g c": "goto-char",
|
|
66
|
+
/* inserting, deleting, and changing */
|
|
67
|
+
"RET": "newline",
|
|
68
|
+
"C-j": "newline",
|
|
69
|
+
"C-o": "open-line",
|
|
70
|
+
"TAB": "indent-for-tab-command",
|
|
71
|
+
"M-i": "tab-to-tab-stop",
|
|
72
|
+
"C-q": "quoted-insert",
|
|
73
|
+
"C-d": "delete-char",
|
|
74
|
+
"<deletechar>": "delete-forward-char",
|
|
75
|
+
"DEL": "delete-backward-char",
|
|
76
|
+
"M-\\": "delete-horizontal-space",
|
|
77
|
+
"M-SPC": "just-one-space",
|
|
78
|
+
"M-^": "delete-indentation",
|
|
79
|
+
"C-x C-o": "delete-blank-lines",
|
|
80
|
+
"C-t": "transpose-chars",
|
|
81
|
+
"M-t": "transpose-words",
|
|
82
|
+
"C-x C-t": "transpose-lines",
|
|
83
|
+
"M-u": "upcase-word",
|
|
84
|
+
"M-l": "downcase-word",
|
|
85
|
+
"M-c": "capitalize-word",
|
|
86
|
+
"C-x C-u": "upcase-region",
|
|
87
|
+
"C-x C-l": "downcase-region",
|
|
88
|
+
"M-q": "fill-paragraph",
|
|
89
|
+
/* killing and yanking */
|
|
90
|
+
"C-k": "kill-line",
|
|
91
|
+
"M-d": "kill-word",
|
|
92
|
+
"M-DEL": "backward-kill-word",
|
|
93
|
+
"C-DEL": "backward-kill-word",
|
|
94
|
+
"C-w": "kill-region",
|
|
95
|
+
"M-w": "kill-ring-save",
|
|
96
|
+
"C-y": "yank",
|
|
97
|
+
"M-y": "yank-pop",
|
|
98
|
+
"M-z": "zap-to-char",
|
|
99
|
+
/* the mark and the region */
|
|
100
|
+
"C-SPC": "set-mark-command",
|
|
101
|
+
"C-@": "set-mark-command",
|
|
102
|
+
"C-x C-x": "exchange-point-and-mark",
|
|
103
|
+
"C-x h": "mark-whole-buffer",
|
|
104
|
+
"M-h": "mark-paragraph",
|
|
105
|
+
"M-@": "mark-word",
|
|
106
|
+
/* undoing, quitting, searching, replacing, and commands */
|
|
107
|
+
"C-/": "undo",
|
|
108
|
+
"C-_": "undo",
|
|
109
|
+
"C-x u": "undo",
|
|
110
|
+
"C-g": "keyboard-quit",
|
|
111
|
+
"C-s": "isearch-forward",
|
|
112
|
+
"C-r": "isearch-backward",
|
|
113
|
+
"M-%": "query-replace",
|
|
114
|
+
"M-x": "execute-extended-command",
|
|
115
|
+
/* files and quitting */
|
|
116
|
+
"C-x C-s": "save-buffer",
|
|
117
|
+
"C-x C-w": "write-file",
|
|
118
|
+
"C-x s": "save-some-buffers",
|
|
119
|
+
"C-x C-f": "find-file",
|
|
120
|
+
"C-x C-c": "save-buffers-kill-terminal",
|
|
121
|
+
"C-z": "suspend-frame",
|
|
122
|
+
"C-x C-z": "suspend-frame",
|
|
123
|
+
/* information */
|
|
124
|
+
"C-x =": "what-cursor-position",
|
|
125
|
+
"M-=": "count-words-region",
|
|
126
|
+
/* keyboard macros */
|
|
127
|
+
"C-x (": "kmacro-start-macro",
|
|
128
|
+
"C-x )": "kmacro-end-macro",
|
|
129
|
+
"C-x e": "kmacro-end-and-call-macro"
|
|
130
|
+
}));
|
|
131
|
+
/* the prefix keys of the key bindings */
|
|
132
|
+
const PREFIXES = new Set([...BINDINGS.keys()].flatMap((seq) => {
|
|
133
|
+
const keys = seq.split(" ");
|
|
134
|
+
return keys.slice(0, -1).map((_, i) => keys.slice(0, i + 1).join(" "));
|
|
135
|
+
}));
|
|
136
|
+
/* the commands of "M-x" */
|
|
137
|
+
const COMMANDS = [...new Set([...BINDINGS.values(),
|
|
138
|
+
"replace-string", "replace-regexp", "revert-buffer", "kill-emacs", "count-words",
|
|
139
|
+
"what-line", "delete-trailing-whitespace", "keyboard-escape-quit"
|
|
140
|
+
])].sort();
|
|
141
|
+
/* the commands whose kills are appended to by a directly following kill */
|
|
142
|
+
const KILLS = new Set(["kill-line", "kill-word", "backward-kill-word", "kill-region", "zap-to-char"]);
|
|
143
|
+
/* the column of filling paragraphs */
|
|
144
|
+
const FILL_COLUMN = 70;
|
|
145
|
+
/* the limit of numeric arguments, against runaway repetitions */
|
|
146
|
+
const MAX_ARG = 1000000;
|
|
147
|
+
/* whether a key token is a printable character */
|
|
148
|
+
const isChar = (t) => [...t].length === 1 && t >= " ";
|
|
149
|
+
/* the longest common prefix of strings */
|
|
150
|
+
const commonPrefix = (list) => list.reduce((a, b) => { let i = 0; while (i < a.length && a[i] === b[i])
|
|
151
|
+
i++; return a.slice(0, i); });
|
|
152
|
+
/* a count with its word, in plural form unless the count is one */
|
|
153
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
154
|
+
/* the Emacs editing engine, operating on a plain string value and a point offset */
|
|
155
|
+
export class Emacs {
|
|
156
|
+
constructor(value, ops) {
|
|
157
|
+
/* bindings to the outside */
|
|
158
|
+
this.emit = () => { };
|
|
159
|
+
this.clip = () => { };
|
|
160
|
+
this.files = null;
|
|
161
|
+
this.height = 24;
|
|
162
|
+
this.off = 0;
|
|
163
|
+
this.mark = null;
|
|
164
|
+
this.active = false;
|
|
165
|
+
this.message = "";
|
|
166
|
+
this.error = false;
|
|
167
|
+
this.pend = [];
|
|
168
|
+
this.prefix = null;
|
|
169
|
+
this.bare = false;
|
|
170
|
+
this.esc = false;
|
|
171
|
+
this.cmd = "";
|
|
172
|
+
this.lastCmd = "";
|
|
173
|
+
this.recorded = false;
|
|
174
|
+
this.amalgam = 0;
|
|
175
|
+
this.undos = [];
|
|
176
|
+
this.undoPtr = 0;
|
|
177
|
+
this.kills = [];
|
|
178
|
+
this.yankRange = [0, 0];
|
|
179
|
+
this.yankIdx = 0;
|
|
180
|
+
this.markRing = [];
|
|
181
|
+
this.goal = null;
|
|
182
|
+
this.mini = null;
|
|
183
|
+
this.reader = null;
|
|
184
|
+
this.is = null;
|
|
185
|
+
this.qr = null;
|
|
186
|
+
this.lastSearch = "";
|
|
187
|
+
this.lastReplace = null;
|
|
188
|
+
this.history = new Map();
|
|
189
|
+
this.macroRec = null;
|
|
190
|
+
this.macroStart = 0;
|
|
191
|
+
this.macro = [];
|
|
192
|
+
this.depth = 0;
|
|
193
|
+
this.value = value;
|
|
194
|
+
this.seen = value;
|
|
195
|
+
this.ops = ops;
|
|
196
|
+
}
|
|
197
|
+
/* ==== EXTERNAL INTERFACE ==== */
|
|
198
|
+
/* take over an externally changed value */
|
|
199
|
+
sync(value) {
|
|
200
|
+
if (value === this.seen)
|
|
201
|
+
return;
|
|
202
|
+
this.seen = value;
|
|
203
|
+
if (value === this.value)
|
|
204
|
+
return;
|
|
205
|
+
this.value = value;
|
|
206
|
+
this.off = Math.min(this.off, value.length);
|
|
207
|
+
if (this.mark !== null)
|
|
208
|
+
this.mark = Math.min(this.mark, value.length);
|
|
209
|
+
}
|
|
210
|
+
/* handle the key tokens of a key press (like [ "x" ], [ "C-x" ], [ "M-f" ], or [ "<up>" ]) */
|
|
211
|
+
keys(tokens) {
|
|
212
|
+
for (const t of tokens) {
|
|
213
|
+
if (this.macroRec !== null)
|
|
214
|
+
this.macroRec.push(t);
|
|
215
|
+
this.dispatch(t);
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
/* handle a bracketed paste */
|
|
220
|
+
paste(text) {
|
|
221
|
+
if (this.mini !== null)
|
|
222
|
+
this.mini.text += text.replace(/\r?\n/g, " ");
|
|
223
|
+
else if (this.is !== null)
|
|
224
|
+
for (const c of text.replace(/\r?\n/g, " "))
|
|
225
|
+
this.isearchKey(c);
|
|
226
|
+
else if (this.reader === null && this.qr === null) {
|
|
227
|
+
this.recorded = false;
|
|
228
|
+
this.insert(text);
|
|
229
|
+
this.active = false;
|
|
230
|
+
this.lastCmd = "";
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/* take over a text change of the front end */
|
|
234
|
+
onText(value) {
|
|
235
|
+
this.recorded = false;
|
|
236
|
+
this.record();
|
|
237
|
+
this.value = value;
|
|
238
|
+
this.off = Math.min(this.off, value.length);
|
|
239
|
+
if (this.mark !== null)
|
|
240
|
+
this.mark = Math.min(this.mark, value.length);
|
|
241
|
+
this.emit(value);
|
|
242
|
+
}
|
|
243
|
+
/* take over a cursor change of the front end (a mouse click) */
|
|
244
|
+
onCursor(cursor) {
|
|
245
|
+
if (this.prompt() === null) {
|
|
246
|
+
this.off = toOffset(this.value, cursor);
|
|
247
|
+
this.lastCmd = "";
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/* take over a mouse selection as the active region */
|
|
251
|
+
onSelect(sel) {
|
|
252
|
+
if (this.prompt() !== null)
|
|
253
|
+
return false;
|
|
254
|
+
if (!sel || sel[0] === sel[1]) {
|
|
255
|
+
if (!this.active)
|
|
256
|
+
return false;
|
|
257
|
+
this.active = false;
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
this.mark = sel[0];
|
|
261
|
+
this.off = sel[1];
|
|
262
|
+
this.active = true;
|
|
263
|
+
this.lastCmd = "";
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
/* the region to be drawn: the current search or replace match, or the active region */
|
|
267
|
+
selection() {
|
|
268
|
+
if (this.is !== null)
|
|
269
|
+
return this.is.match;
|
|
270
|
+
if (this.qr !== null)
|
|
271
|
+
return this.qr.match;
|
|
272
|
+
if (this.active && this.mark !== null && this.mark !== this.off)
|
|
273
|
+
return this.mark < this.off ? [this.mark, this.off] : [this.off, this.mark];
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
/* the prompt of the minibuffer (with its input), or null without one */
|
|
277
|
+
prompt() {
|
|
278
|
+
if (this.reader !== null)
|
|
279
|
+
return this.reader.prompt;
|
|
280
|
+
if (this.mini !== null)
|
|
281
|
+
return this.mini.prompt + this.mini.text;
|
|
282
|
+
if (this.is !== null) {
|
|
283
|
+
const s = this.is;
|
|
284
|
+
const what = [s.failing ? "failing" : "", s.wrapped ? "wrapped" : "", "I-search"]
|
|
285
|
+
.filter((w) => w !== "").join(" ");
|
|
286
|
+
return `${what[0].toUpperCase()}${what.slice(1)}${s.forward ? "" : " backward"}: ${s.text}`;
|
|
287
|
+
}
|
|
288
|
+
if (this.qr !== null)
|
|
289
|
+
return `Query replacing ${this.qr.from} with ${this.qr.to}: (y, n, !, ., q) `;
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
/* the echo of the pending keys (like "C-x-"), or "" */
|
|
293
|
+
echo() {
|
|
294
|
+
const keys = [];
|
|
295
|
+
if (this.prefix !== null)
|
|
296
|
+
keys.push(["C-u", this.prefix.neg ? "-" : "", this.prefix.digits].filter((k) => k !== "").join(" "));
|
|
297
|
+
if (this.esc)
|
|
298
|
+
keys.push("ESC");
|
|
299
|
+
keys.push(...this.pend);
|
|
300
|
+
return keys.length > 0 ? keys.join(" ") + "-" : "";
|
|
301
|
+
}
|
|
302
|
+
/* the text being searched resp. replaced, to be highlighted */
|
|
303
|
+
searched() {
|
|
304
|
+
if (this.is !== null && this.is.text !== "")
|
|
305
|
+
return this.is.text;
|
|
306
|
+
if (this.qr !== null)
|
|
307
|
+
return this.qr.from;
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
/* whether a keyboard macro is being defined */
|
|
311
|
+
defining() {
|
|
312
|
+
return this.macroRec !== null;
|
|
313
|
+
}
|
|
314
|
+
/* ==== KEY DISPATCHING ==== */
|
|
315
|
+
/* dispatch a key token by the current input state */
|
|
316
|
+
dispatch(token) {
|
|
317
|
+
/* an ESC turns the next key into a Meta key, and a second one quits */
|
|
318
|
+
let t = token;
|
|
319
|
+
if (this.esc) {
|
|
320
|
+
this.esc = false;
|
|
321
|
+
if (t === "ESC")
|
|
322
|
+
t = "C-g";
|
|
323
|
+
else if (t.startsWith("C-"))
|
|
324
|
+
t = "C-M-" + t.slice(2);
|
|
325
|
+
else
|
|
326
|
+
t = "M-" + (t === " " ? "SPC" : t);
|
|
327
|
+
}
|
|
328
|
+
else if (t === "ESC") {
|
|
329
|
+
this.esc = true;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
this.recorded = false;
|
|
333
|
+
if (this.reader !== null) {
|
|
334
|
+
const r = this.reader;
|
|
335
|
+
this.reader = null;
|
|
336
|
+
r.take(t);
|
|
337
|
+
}
|
|
338
|
+
else if (this.mini !== null)
|
|
339
|
+
this.miniKey(t);
|
|
340
|
+
else if (this.is !== null)
|
|
341
|
+
this.isearchKey(t);
|
|
342
|
+
else if (this.qr !== null)
|
|
343
|
+
this.queryKey(t);
|
|
344
|
+
else
|
|
345
|
+
this.bufferKey(t);
|
|
346
|
+
}
|
|
347
|
+
/* handle a key of the buffer: prefix arguments, prefix keys, and commands */
|
|
348
|
+
bufferKey(t) {
|
|
349
|
+
this.message = "";
|
|
350
|
+
this.error = false;
|
|
351
|
+
if (this.pend.length === 0 && this.prefix === null && this.macroRec !== null)
|
|
352
|
+
this.macroStart = this.macroRec.length - 1;
|
|
353
|
+
if (this.pend.length === 0 && this.argument(t))
|
|
354
|
+
return;
|
|
355
|
+
const seq = [...this.pend, t].join(" ");
|
|
356
|
+
if (PREFIXES.has(seq)) {
|
|
357
|
+
this.pend.push(t);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
this.pend = [];
|
|
361
|
+
let cmd = BINDINGS.get(seq);
|
|
362
|
+
if (seq === "e" && this.lastCmd === "kmacro-end-and-call-macro" && this.macro.length > 0)
|
|
363
|
+
cmd = "kmacro-end-and-call-macro";
|
|
364
|
+
else if (cmd === undefined && isChar(seq))
|
|
365
|
+
cmd = "self-insert-command";
|
|
366
|
+
const p = this.prefix;
|
|
367
|
+
this.prefix = null;
|
|
368
|
+
if (cmd === undefined) {
|
|
369
|
+
this.fail(`${seq} is undefined`);
|
|
370
|
+
this.lastCmd = "";
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const arg = p === null ? 1 : p.digits !== "" ? parseInt(p.digits, 10) * (p.neg ? -1 : 1) : p.neg ? -1 : p.n;
|
|
374
|
+
const n = Math.max(-MAX_ARG, Math.min(MAX_ARG, arg));
|
|
375
|
+
this.bare = p !== null && p.digits === "" && !p.neg;
|
|
376
|
+
this.run(cmd, n, p !== null, t);
|
|
377
|
+
}
|
|
378
|
+
/* handle the keys of the prefix argument ("C-u", "C-u 5", "M-5", "M--") */
|
|
379
|
+
argument(t) {
|
|
380
|
+
const p = this.prefix;
|
|
381
|
+
if (t === "C-u") {
|
|
382
|
+
if (p === null)
|
|
383
|
+
this.prefix = { n: 4, digits: "", neg: false, closed: false };
|
|
384
|
+
else if (p.digits === "" && !p.neg && !p.closed)
|
|
385
|
+
p.n *= 4;
|
|
386
|
+
else
|
|
387
|
+
p.closed = true;
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
const m = /^M-([0-9-])$/.exec(t);
|
|
391
|
+
if (m) {
|
|
392
|
+
const q = this.prefix ?? (this.prefix = { n: 1, digits: "", neg: false, closed: false });
|
|
393
|
+
if (m[1] === "-")
|
|
394
|
+
q.neg = !q.neg;
|
|
395
|
+
else
|
|
396
|
+
q.digits += m[1];
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
if (p !== null && !p.closed && /^[0-9]$/.test(t)) {
|
|
400
|
+
p.digits += t;
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
if (p !== null && !p.closed && t === "-" && p.digits === "" && !p.neg) {
|
|
404
|
+
p.neg = true;
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
/* run a command (deactivating the mark on a text change, like in transient mark mode) */
|
|
410
|
+
run(cmd, n, raw, t) {
|
|
411
|
+
const value = this.value;
|
|
412
|
+
this.cmd = cmd;
|
|
413
|
+
this.exec(cmd, n, raw, t);
|
|
414
|
+
this.cmd = "";
|
|
415
|
+
if (this.value !== value)
|
|
416
|
+
this.active = false;
|
|
417
|
+
this.lastCmd = cmd;
|
|
418
|
+
}
|
|
419
|
+
/* ==== COMMANDS ==== */
|
|
420
|
+
/* execute a command with its numeric argument, whether the argument was given, and its key */
|
|
421
|
+
exec(cmd, n, raw, t) {
|
|
422
|
+
const v = this.value;
|
|
423
|
+
const o = this.off;
|
|
424
|
+
switch (cmd) {
|
|
425
|
+
/* ---- motion ---- */
|
|
426
|
+
case "forward-char":
|
|
427
|
+
case "backward-char": {
|
|
428
|
+
const k = cmd === "forward-char" ? n : -n;
|
|
429
|
+
const p = this.chars(o, k);
|
|
430
|
+
if (p === o && k !== 0)
|
|
431
|
+
this.fail(k > 0 ? "End of buffer" : "Beginning of buffer");
|
|
432
|
+
this.off = p;
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
case "next-line":
|
|
436
|
+
this.vertical(n);
|
|
437
|
+
break;
|
|
438
|
+
case "previous-line":
|
|
439
|
+
this.vertical(-n);
|
|
440
|
+
break;
|
|
441
|
+
case "move-beginning-of-line":
|
|
442
|
+
case "move-end-of-line": {
|
|
443
|
+
const p = n === 1 ? o : lineAt(v, Math.max(0, lineNo(v, o) + n - 1));
|
|
444
|
+
this.off = cmd === "move-beginning-of-line" ? lineStart(v, p) : lineEnd(v, p);
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case "forward-word":
|
|
448
|
+
case "backward-word":
|
|
449
|
+
this.off = this.words(o, cmd === "forward-word" ? n : -n);
|
|
450
|
+
break;
|
|
451
|
+
case "forward-paragraph":
|
|
452
|
+
case "backward-paragraph": {
|
|
453
|
+
let p = o;
|
|
454
|
+
for (let i = 0; i < Math.abs(n); i++)
|
|
455
|
+
p = (cmd === "forward-paragraph") === (n > 0) ? paragraphEnd(v, p) : paragraphBegin(v, p);
|
|
456
|
+
this.off = p;
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
case "back-to-indentation": {
|
|
460
|
+
let p = lineStart(v, o);
|
|
461
|
+
while (p < lineEnd(v, o) && isSpace(v[p]))
|
|
462
|
+
p++;
|
|
463
|
+
this.off = p;
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
case "beginning-of-buffer":
|
|
467
|
+
case "end-of-buffer":
|
|
468
|
+
if (!this.active) {
|
|
469
|
+
this.pushMark(o, false);
|
|
470
|
+
this.say("Mark set");
|
|
471
|
+
}
|
|
472
|
+
this.off = cmd === "beginning-of-buffer" ? 0 : v.length;
|
|
473
|
+
break;
|
|
474
|
+
case "scroll-up-command":
|
|
475
|
+
case "scroll-down-command": {
|
|
476
|
+
const rows = raw ? n : Math.max(1, this.height - 2);
|
|
477
|
+
this.vertical(cmd === "scroll-up-command" ? rows : -rows);
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
case "recenter-top-bottom":
|
|
481
|
+
break;
|
|
482
|
+
case "goto-line":
|
|
483
|
+
if (raw)
|
|
484
|
+
this.gotoLine(n);
|
|
485
|
+
else
|
|
486
|
+
this.ask("goto-line", "Goto line: ", "", (text) => {
|
|
487
|
+
const l = parseInt(text, 10);
|
|
488
|
+
if (!Number.isNaN(l))
|
|
489
|
+
this.gotoLine(l);
|
|
490
|
+
});
|
|
491
|
+
break;
|
|
492
|
+
case "goto-char":
|
|
493
|
+
this.ask("goto-char", "Goto char: ", "", (text) => {
|
|
494
|
+
const c = parseInt(text, 10);
|
|
495
|
+
if (!Number.isNaN(c)) {
|
|
496
|
+
this.pushMark(this.off, false);
|
|
497
|
+
this.off = Math.max(0, Math.min(c - 1, this.value.length));
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
break;
|
|
501
|
+
/* ---- inserting ---- */
|
|
502
|
+
case "self-insert-command":
|
|
503
|
+
this.insert(t.repeat(Math.max(0, n)));
|
|
504
|
+
break;
|
|
505
|
+
case "newline":
|
|
506
|
+
this.insert("\n".repeat(Math.max(0, n)));
|
|
507
|
+
break;
|
|
508
|
+
case "open-line":
|
|
509
|
+
this.insert("\n".repeat(Math.max(0, n)));
|
|
510
|
+
this.off = o;
|
|
511
|
+
break;
|
|
512
|
+
case "indent-for-tab-command":
|
|
513
|
+
case "tab-to-tab-stop":
|
|
514
|
+
this.insert("\t".repeat(Math.max(0, n)));
|
|
515
|
+
break;
|
|
516
|
+
case "quoted-insert":
|
|
517
|
+
this.reader = { prompt: "C-q-", take: (k) => {
|
|
518
|
+
const text = isChar(k) ? k : k === "TAB" ? "\t" : k === "C-j" || k === "RET" ? "\n" : "";
|
|
519
|
+
this.insert(text.repeat(Math.max(0, n)));
|
|
520
|
+
} };
|
|
521
|
+
break;
|
|
522
|
+
/* ---- deleting ---- */
|
|
523
|
+
case "delete-char":
|
|
524
|
+
case "delete-forward-char": {
|
|
525
|
+
if (cmd === "delete-forward-char" && this.deleteRegion())
|
|
526
|
+
break;
|
|
527
|
+
const p = this.chars(o, n);
|
|
528
|
+
if (p === o) {
|
|
529
|
+
this.fail(n >= 0 ? "End of buffer" : "Beginning of buffer");
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
if (raw)
|
|
533
|
+
this.kill(Math.min(o, p), Math.max(o, p), p < o);
|
|
534
|
+
else {
|
|
535
|
+
this.replace(Math.min(o, p), Math.max(o, p), "");
|
|
536
|
+
this.off = Math.min(o, p);
|
|
537
|
+
}
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
case "delete-backward-char": {
|
|
541
|
+
if (this.deleteRegion())
|
|
542
|
+
break;
|
|
543
|
+
const p = this.chars(o, -n);
|
|
544
|
+
if (p === o) {
|
|
545
|
+
this.fail(n < 0 ? "End of buffer" : "Beginning of buffer");
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
if (raw)
|
|
549
|
+
this.kill(Math.min(o, p), Math.max(o, p), p < o);
|
|
550
|
+
else {
|
|
551
|
+
this.replace(Math.min(o, p), Math.max(o, p), "");
|
|
552
|
+
this.off = Math.min(o, p);
|
|
553
|
+
}
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
case "delete-horizontal-space":
|
|
557
|
+
case "just-one-space": {
|
|
558
|
+
let s = o;
|
|
559
|
+
let e = o;
|
|
560
|
+
while (s > lineStart(v, o) && isSpace(v[s - 1]))
|
|
561
|
+
s--;
|
|
562
|
+
while (!(raw && cmd === "delete-horizontal-space") && e < lineEnd(v, o) && isSpace(v[e]))
|
|
563
|
+
e++;
|
|
564
|
+
const text = cmd === "just-one-space" ? " ".repeat(Math.max(0, n)) : "";
|
|
565
|
+
this.replace(s, e, text);
|
|
566
|
+
this.off = s + text.length;
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
case "delete-indentation": {
|
|
570
|
+
const cs = raw ? (lineEnd(v, o) < v.length ? lineEnd(v, o) + 1 : -1) : lineStart(v, o);
|
|
571
|
+
if (cs <= 0)
|
|
572
|
+
break;
|
|
573
|
+
let s = cs - 1;
|
|
574
|
+
while (s > lineStart(v, cs - 1) && isSpace(v[s - 1]))
|
|
575
|
+
s--;
|
|
576
|
+
let e = cs;
|
|
577
|
+
while (e < lineEnd(v, cs) && isSpace(v[e]))
|
|
578
|
+
e++;
|
|
579
|
+
const join = s === lineStart(v, cs - 1) || e === lineEnd(v, cs) || v[e] === ")" || v[s - 1] === "(" ? "" : " ";
|
|
580
|
+
this.replace(s, e, join);
|
|
581
|
+
this.off = s;
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
case "delete-blank-lines": {
|
|
585
|
+
const l = lineNo(v, o);
|
|
586
|
+
const last = lineNo(v, v.length);
|
|
587
|
+
const blank = (i) => isBlank(v, lineAt(v, i));
|
|
588
|
+
if (blank(l)) {
|
|
589
|
+
let a = l;
|
|
590
|
+
let b = l;
|
|
591
|
+
while (a > 0 && blank(a - 1))
|
|
592
|
+
a--;
|
|
593
|
+
while (b < last && blank(b + 1))
|
|
594
|
+
b++;
|
|
595
|
+
if (a === b) {
|
|
596
|
+
let s = lineAt(v, a);
|
|
597
|
+
const e = b < last ? lineAt(v, b + 1) : v.length;
|
|
598
|
+
if (e === v.length && s > 0)
|
|
599
|
+
s--;
|
|
600
|
+
this.replace(s, e, "");
|
|
601
|
+
this.off = Math.min(s, this.value.length);
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
this.replace(lineAt(v, a), lineEnd(v, lineAt(v, b)), "");
|
|
605
|
+
this.off = lineAt(this.value, a);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
let b = l + 1;
|
|
610
|
+
while (b <= last && blank(b))
|
|
611
|
+
b++;
|
|
612
|
+
if (b > l + 1)
|
|
613
|
+
this.replace(lineEnd(v, o), lineEnd(v, lineAt(v, b - 1)), "");
|
|
614
|
+
}
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
/* ---- changing ---- */
|
|
618
|
+
case "transpose-chars": {
|
|
619
|
+
let p = o;
|
|
620
|
+
for (let i = 0; i < Math.max(1, n); i++) {
|
|
621
|
+
const w = this.value;
|
|
622
|
+
if (p === lineEnd(w, p) && p > lineStart(w, p))
|
|
623
|
+
p = this.ops.previous(w, p);
|
|
624
|
+
const a = this.ops.previous(w, p);
|
|
625
|
+
const b = this.ops.next(w, p);
|
|
626
|
+
if (a === p || b === p) {
|
|
627
|
+
this.fail(a === p ? "Beginning of buffer" : "End of buffer");
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
this.replace(a, b, w.slice(p, b) + w.slice(a, p));
|
|
631
|
+
p = b;
|
|
632
|
+
}
|
|
633
|
+
this.off = p;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
case "transpose-words": {
|
|
637
|
+
const s1 = wordBegin(v, o);
|
|
638
|
+
const e1 = wordEnd(v, s1);
|
|
639
|
+
const e2 = wordEnd(v, e1);
|
|
640
|
+
const s2 = wordBegin(v, e2);
|
|
641
|
+
if (s1 >= s2 || e1 > s2 || s2 >= e2) {
|
|
642
|
+
this.fail("Don't have two things to transpose");
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
this.replace(s1, e2, v.slice(s2, e2) + v.slice(e1, s2) + v.slice(s1, e1));
|
|
646
|
+
this.off = e2;
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
case "transpose-lines": {
|
|
650
|
+
const cs = lineStart(v, o);
|
|
651
|
+
if (cs === 0) {
|
|
652
|
+
this.fail("Don't have two things to transpose");
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
const ps = lineStart(v, cs - 1);
|
|
656
|
+
const ce = lineEnd(v, cs);
|
|
657
|
+
this.replace(ps, ce, v.slice(cs, ce) + "\n" + v.slice(ps, cs - 1));
|
|
658
|
+
this.off = ce < v.length ? ce + 1 : ce;
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
case "upcase-word":
|
|
662
|
+
case "downcase-word":
|
|
663
|
+
case "capitalize-word": {
|
|
664
|
+
const s = n >= 0 ? o : this.words(o, n);
|
|
665
|
+
const e = n >= 0 ? this.words(o, n) : o;
|
|
666
|
+
const text = this.convert(cmd, v.slice(s, e));
|
|
667
|
+
this.replace(s, e, text);
|
|
668
|
+
if (n >= 0)
|
|
669
|
+
this.off = s + text.length;
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
case "upcase-region":
|
|
673
|
+
case "downcase-region": {
|
|
674
|
+
const r = this.region();
|
|
675
|
+
if (r === null)
|
|
676
|
+
break;
|
|
677
|
+
this.replace(r[0], r[1], this.convert(cmd, v.slice(r[0], r[1])));
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
case "fill-paragraph":
|
|
681
|
+
this.fill();
|
|
682
|
+
break;
|
|
683
|
+
/* ---- killing and yanking ---- */
|
|
684
|
+
case "kill-line": {
|
|
685
|
+
if (raw && n <= 0) {
|
|
686
|
+
let p = lineStart(v, o);
|
|
687
|
+
for (let i = 0; i < -n; i++)
|
|
688
|
+
p = p > 0 ? lineStart(v, p - 1) : 0;
|
|
689
|
+
this.kill(p, o, true);
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
let e = lineEnd(v, o);
|
|
693
|
+
if (raw) {
|
|
694
|
+
e = o;
|
|
695
|
+
for (let i = 0; i < n; i++)
|
|
696
|
+
e = lineEnd(v, e) < v.length ? lineEnd(v, e) + 1 : v.length;
|
|
697
|
+
}
|
|
698
|
+
else if (/^[ \t]*$/.test(v.slice(o, e)) && e < v.length)
|
|
699
|
+
e++;
|
|
700
|
+
if (e === o) {
|
|
701
|
+
this.fail("End of buffer");
|
|
702
|
+
break;
|
|
703
|
+
}
|
|
704
|
+
this.kill(o, e, false);
|
|
705
|
+
break;
|
|
706
|
+
}
|
|
707
|
+
case "kill-word":
|
|
708
|
+
if (n >= 0)
|
|
709
|
+
this.kill(o, this.words(o, n), false);
|
|
710
|
+
else
|
|
711
|
+
this.kill(this.words(o, n), o, true);
|
|
712
|
+
break;
|
|
713
|
+
case "backward-kill-word":
|
|
714
|
+
if (n >= 0)
|
|
715
|
+
this.kill(this.words(o, -n), o, true);
|
|
716
|
+
else
|
|
717
|
+
this.kill(o, this.words(o, -n), false);
|
|
718
|
+
break;
|
|
719
|
+
case "kill-region": {
|
|
720
|
+
const r = this.region();
|
|
721
|
+
if (r !== null)
|
|
722
|
+
this.kill(r[0], r[1], this.off < (this.mark ?? 0));
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
case "kill-ring-save": {
|
|
726
|
+
const r = this.region();
|
|
727
|
+
if (r !== null)
|
|
728
|
+
this.pushKill(v.slice(r[0], r[1]), this.off < (this.mark ?? 0), KILLS.has(this.lastCmd));
|
|
729
|
+
this.active = false;
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
case "yank": {
|
|
733
|
+
if (this.kills.length === 0) {
|
|
734
|
+
this.fail("Kill ring is empty");
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
const len = this.kills.length;
|
|
738
|
+
this.yankIdx = raw && !this.bare ? ((n - 1) % len + len) % len : 0;
|
|
739
|
+
this.insert(this.kills[this.yankIdx]);
|
|
740
|
+
this.yankRange = [o, this.off];
|
|
741
|
+
/* a bare "C-u" puts the point at the beginning and the mark at the end */
|
|
742
|
+
if (raw && this.bare) {
|
|
743
|
+
this.pushMark(this.off, false);
|
|
744
|
+
this.off = o;
|
|
745
|
+
}
|
|
746
|
+
else
|
|
747
|
+
this.pushMark(o, false);
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
case "yank-pop": {
|
|
751
|
+
if (this.kills.length === 0) {
|
|
752
|
+
this.fail("Kill ring is empty");
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
if (this.lastCmd !== "yank" && this.lastCmd !== "yank-pop") {
|
|
756
|
+
this.fail("Previous command was not a yank");
|
|
757
|
+
break;
|
|
758
|
+
}
|
|
759
|
+
const len = this.kills.length;
|
|
760
|
+
this.yankIdx = ((this.yankIdx + n) % len + len) % len;
|
|
761
|
+
const text = this.kills[this.yankIdx];
|
|
762
|
+
const [s, e] = this.yankRange;
|
|
763
|
+
this.replace(s, e, text);
|
|
764
|
+
this.off = s + text.length;
|
|
765
|
+
this.yankRange = [s, this.off];
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
case "zap-to-char": {
|
|
769
|
+
const append = KILLS.has(this.lastCmd);
|
|
770
|
+
this.reader = { prompt: "Zap to char: ", take: (k) => {
|
|
771
|
+
if (!isChar(k)) {
|
|
772
|
+
if (k === "C-g")
|
|
773
|
+
this.say("Quit");
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
let e = this.off;
|
|
777
|
+
for (let i = 0; i < Math.max(1, n); i++) {
|
|
778
|
+
const p = this.value.indexOf(k, e);
|
|
779
|
+
if (p < 0) {
|
|
780
|
+
this.fail(`Search failed: "${k}"`);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
e = p + k.length;
|
|
784
|
+
}
|
|
785
|
+
this.kill(this.off, e, false, append);
|
|
786
|
+
this.lastCmd = "zap-to-char";
|
|
787
|
+
} };
|
|
788
|
+
break;
|
|
789
|
+
}
|
|
790
|
+
/* ---- the mark and the region ---- */
|
|
791
|
+
case "set-mark-command":
|
|
792
|
+
if (raw) {
|
|
793
|
+
if (this.mark === null) {
|
|
794
|
+
this.fail("No mark set in this buffer");
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
this.off = this.mark;
|
|
798
|
+
this.active = false;
|
|
799
|
+
/* rotate the mark ring, so repeated pops cycle through it */
|
|
800
|
+
const m = this.markRing.pop();
|
|
801
|
+
if (m !== undefined) {
|
|
802
|
+
this.markRing.unshift(this.mark);
|
|
803
|
+
this.mark = m;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
else if (this.lastCmd === "set-mark-command" && this.active) {
|
|
807
|
+
this.active = false;
|
|
808
|
+
this.say("Mark deactivated");
|
|
809
|
+
}
|
|
810
|
+
else {
|
|
811
|
+
this.pushMark(o, true);
|
|
812
|
+
this.say("Mark set");
|
|
813
|
+
}
|
|
814
|
+
break;
|
|
815
|
+
case "exchange-point-and-mark":
|
|
816
|
+
if (this.mark === null) {
|
|
817
|
+
this.fail("No mark set in this buffer");
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
this.off = this.mark;
|
|
821
|
+
this.mark = o;
|
|
822
|
+
this.active = !raw;
|
|
823
|
+
break;
|
|
824
|
+
case "mark-whole-buffer":
|
|
825
|
+
this.pushMark(v.length, true);
|
|
826
|
+
this.off = 0;
|
|
827
|
+
this.say("Mark set");
|
|
828
|
+
break;
|
|
829
|
+
case "mark-paragraph": {
|
|
830
|
+
const e = paragraphEnd(v, o);
|
|
831
|
+
this.pushMark(e, true);
|
|
832
|
+
this.off = paragraphBegin(v, e);
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
case "mark-word": {
|
|
836
|
+
const from = this.active && this.mark !== null ? this.mark : o;
|
|
837
|
+
if (!this.active)
|
|
838
|
+
this.pushMark(o, true);
|
|
839
|
+
this.mark = this.words(from, n);
|
|
840
|
+
this.active = true;
|
|
841
|
+
break;
|
|
842
|
+
}
|
|
843
|
+
/* ---- undoing and quitting ---- */
|
|
844
|
+
case "undo":
|
|
845
|
+
this.undo(Math.max(1, n));
|
|
846
|
+
break;
|
|
847
|
+
case "keyboard-quit":
|
|
848
|
+
case "keyboard-escape-quit":
|
|
849
|
+
this.active = false;
|
|
850
|
+
if (this.macroRec !== null && cmd === "keyboard-quit") {
|
|
851
|
+
this.macroRec = null;
|
|
852
|
+
this.say("Keyboard macro definition cancelled");
|
|
853
|
+
}
|
|
854
|
+
else if (cmd === "keyboard-quit")
|
|
855
|
+
this.say("Quit");
|
|
856
|
+
break;
|
|
857
|
+
/* ---- searching and replacing ---- */
|
|
858
|
+
case "isearch-forward":
|
|
859
|
+
case "isearch-backward":
|
|
860
|
+
this.is = { forward: cmd === "isearch-forward", text: "", origin: o, failing: false,
|
|
861
|
+
wrapped: false, match: null, off: o, stack: [] };
|
|
862
|
+
break;
|
|
863
|
+
case "query-replace": {
|
|
864
|
+
const last = this.lastReplace;
|
|
865
|
+
const prompt = last ? `Query replace (default ${last[0]} → ${last[1]}): ` : "Query replace: ";
|
|
866
|
+
this.ask("replace", prompt, "", (from) => {
|
|
867
|
+
if (from === "") {
|
|
868
|
+
if (last)
|
|
869
|
+
this.queryStart(last[0], last[1]);
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
this.ask("replace", `Query replace ${from} with: `, "", (to) => this.queryStart(from, to));
|
|
873
|
+
});
|
|
874
|
+
break;
|
|
875
|
+
}
|
|
876
|
+
case "replace-string":
|
|
877
|
+
case "replace-regexp": {
|
|
878
|
+
const what = cmd === "replace-string" ? "Replace string" : "Replace regexp";
|
|
879
|
+
this.ask("replace", `${what}: `, "", (from) => {
|
|
880
|
+
if (from !== "")
|
|
881
|
+
this.ask("replace", `${what} ${from} with: `, "", (to) => this.replaceAll(from, to, cmd === "replace-regexp"));
|
|
882
|
+
});
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
/* ---- commands ---- */
|
|
886
|
+
case "execute-extended-command":
|
|
887
|
+
this.ask("command", raw ? "C-u M-x " : "M-x ", "", (name) => {
|
|
888
|
+
if (!COMMANDS.includes(name))
|
|
889
|
+
this.fail("[No match]");
|
|
890
|
+
else
|
|
891
|
+
this.run(name, n, raw, "");
|
|
892
|
+
}, COMMANDS);
|
|
893
|
+
break;
|
|
894
|
+
/* ---- files and quitting ---- */
|
|
895
|
+
case "save-buffer": {
|
|
896
|
+
const f = this.need();
|
|
897
|
+
if (f === null)
|
|
898
|
+
break;
|
|
899
|
+
if (f.name() === "")
|
|
900
|
+
this.ask("file", "File to save in: ", "", (file) => this.writeFile(file));
|
|
901
|
+
else if (!f.modified())
|
|
902
|
+
this.say("(No changes need to be saved)");
|
|
903
|
+
else
|
|
904
|
+
this.outcome(f.write("", false, false));
|
|
905
|
+
break;
|
|
906
|
+
}
|
|
907
|
+
case "write-file":
|
|
908
|
+
if (this.need() !== null)
|
|
909
|
+
this.ask("file", "Write file: ", "", (file) => this.writeFile(file));
|
|
910
|
+
break;
|
|
911
|
+
case "save-some-buffers": {
|
|
912
|
+
const f = this.need();
|
|
913
|
+
if (f === null)
|
|
914
|
+
break;
|
|
915
|
+
if (!f.modified())
|
|
916
|
+
this.say("(No files need saving)");
|
|
917
|
+
else
|
|
918
|
+
this.askYN(`Save file ${f.name() || "*scratch*"}? `, () => this.exec("save-buffer", 1, false, ""));
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
case "save-buffers-kill-terminal": {
|
|
922
|
+
const f = this.need();
|
|
923
|
+
if (f === null)
|
|
924
|
+
break;
|
|
925
|
+
if (!f.modified()) {
|
|
926
|
+
f.quit();
|
|
927
|
+
break;
|
|
928
|
+
}
|
|
929
|
+
this.askYN(`Save file ${f.name() || "*scratch*"}? `, () => {
|
|
930
|
+
if (f.name() === "")
|
|
931
|
+
this.ask("file", "File to save in: ", "", (file) => this.writeFile(file, () => f.quit()));
|
|
932
|
+
else {
|
|
933
|
+
const res = f.write("", false, false);
|
|
934
|
+
if (res.error)
|
|
935
|
+
this.outcome(res);
|
|
936
|
+
else
|
|
937
|
+
f.quit();
|
|
938
|
+
}
|
|
939
|
+
}, () => this.askYesNo("Modified buffers exist; exit anyway? ", () => f.quit()));
|
|
940
|
+
break;
|
|
941
|
+
}
|
|
942
|
+
case "kill-emacs":
|
|
943
|
+
this.need()?.quit();
|
|
944
|
+
break;
|
|
945
|
+
case "find-file": {
|
|
946
|
+
const f = this.need();
|
|
947
|
+
if (f !== null)
|
|
948
|
+
this.ask("file", "Find file: ", f.name(), (file) => {
|
|
949
|
+
if (file !== "" && file !== f.name())
|
|
950
|
+
this.fail("Visiting another file is not supported");
|
|
951
|
+
});
|
|
952
|
+
break;
|
|
953
|
+
}
|
|
954
|
+
case "revert-buffer": {
|
|
955
|
+
const f = this.need();
|
|
956
|
+
if (f === null)
|
|
957
|
+
break;
|
|
958
|
+
if (f.name() === "") {
|
|
959
|
+
this.fail("Buffer does not seem to be associated with any file");
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
this.askYesNo(`Revert buffer from file ${f.name()}? `, () => this.outcome(f.reload()));
|
|
963
|
+
break;
|
|
964
|
+
}
|
|
965
|
+
case "suspend-frame":
|
|
966
|
+
this.fail("Suspending is not supported");
|
|
967
|
+
break;
|
|
968
|
+
/* ---- information ---- */
|
|
969
|
+
case "what-cursor-position": {
|
|
970
|
+
const col = o - lineStart(v, o);
|
|
971
|
+
const pct = Math.round(100 * o / Math.max(1, v.length));
|
|
972
|
+
if (o >= v.length)
|
|
973
|
+
this.say(`point=${o + 1} of ${v.length + 1} (${pct}%) column=${col}`);
|
|
974
|
+
else {
|
|
975
|
+
const cp = v.codePointAt(o) ?? 0;
|
|
976
|
+
const ch = cp === 10 ? "C-j" : String.fromCodePoint(cp);
|
|
977
|
+
this.say(`Char: ${ch} (${cp}, #o${cp.toString(8)}, #x${cp.toString(16)}) ` +
|
|
978
|
+
`point=${o + 1} of ${v.length + 1} (${pct}%) column=${col}`);
|
|
979
|
+
}
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
case "count-words-region":
|
|
983
|
+
case "count-words": {
|
|
984
|
+
const mark = cmd === "count-words-region" ? this.mark : null;
|
|
985
|
+
const text = mark === null ? v : v.slice(Math.min(mark, o), Math.max(mark, o));
|
|
986
|
+
const lines = text === "" ? 0 : text.split("\n").length - (text.endsWith("\n") ? 1 : 0);
|
|
987
|
+
const words = (text.match(/[\p{L}\p{N}]+/gu) ?? []).length;
|
|
988
|
+
const what = mark === null ? "Buffer" : "Region";
|
|
989
|
+
this.say(`${what} has ${plural(lines, "line")}, ${plural(words, "word")}, ` +
|
|
990
|
+
`and ${plural([...text].length, "character")}.`);
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
case "what-line":
|
|
994
|
+
this.say(`Line ${lineNo(v, o) + 1}`);
|
|
995
|
+
break;
|
|
996
|
+
case "delete-trailing-whitespace": {
|
|
997
|
+
const text = v.replace(/[ \t]+$/gm, "");
|
|
998
|
+
if (text !== v) {
|
|
999
|
+
const cur = toCursor(v, o);
|
|
1000
|
+
this.replace(0, v.length, text);
|
|
1001
|
+
this.off = toOffset(text, cur);
|
|
1002
|
+
}
|
|
1003
|
+
break;
|
|
1004
|
+
}
|
|
1005
|
+
/* ---- keyboard macros ---- */
|
|
1006
|
+
case "kmacro-start-macro":
|
|
1007
|
+
if (this.macroRec !== null) {
|
|
1008
|
+
this.fail("Already defining keyboard macro");
|
|
1009
|
+
break;
|
|
1010
|
+
}
|
|
1011
|
+
this.macroRec = [];
|
|
1012
|
+
this.say("Defining kbd macro...");
|
|
1013
|
+
break;
|
|
1014
|
+
case "kmacro-end-macro":
|
|
1015
|
+
if (this.macroRec === null) {
|
|
1016
|
+
this.fail("Not defining kbd macro");
|
|
1017
|
+
break;
|
|
1018
|
+
}
|
|
1019
|
+
this.macro = this.macroRec.slice(0, this.macroStart);
|
|
1020
|
+
this.macroRec = null;
|
|
1021
|
+
this.say("Keyboard macro defined");
|
|
1022
|
+
break;
|
|
1023
|
+
case "kmacro-end-and-call-macro": {
|
|
1024
|
+
if (this.macroRec !== null) {
|
|
1025
|
+
this.macro = this.macroRec.slice(0, this.macroStart);
|
|
1026
|
+
this.macroRec = null;
|
|
1027
|
+
}
|
|
1028
|
+
if (this.macro.length === 0) {
|
|
1029
|
+
this.fail("No kbd macro has been defined");
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
if (this.depth > 0)
|
|
1033
|
+
break;
|
|
1034
|
+
this.depth++;
|
|
1035
|
+
for (let i = 0; i < Math.max(1, n) && !this.error; i++)
|
|
1036
|
+
for (const k of this.macro)
|
|
1037
|
+
if (!this.error)
|
|
1038
|
+
this.dispatch(k);
|
|
1039
|
+
this.depth--;
|
|
1040
|
+
this.lastCmd = cmd;
|
|
1041
|
+
if (!this.error)
|
|
1042
|
+
this.say("(Type e to repeat macro)");
|
|
1043
|
+
break;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
/* ==== MOTION HELPERS ==== */
|
|
1048
|
+
/* step over graphemes */
|
|
1049
|
+
chars(o, n) {
|
|
1050
|
+
let p = o;
|
|
1051
|
+
for (let i = 0; i < Math.abs(n); i++) {
|
|
1052
|
+
const q = n > 0 ? this.ops.next(this.value, p) : this.ops.previous(this.value, p);
|
|
1053
|
+
if (q === p)
|
|
1054
|
+
break;
|
|
1055
|
+
p = q;
|
|
1056
|
+
}
|
|
1057
|
+
return p;
|
|
1058
|
+
}
|
|
1059
|
+
/* step over words */
|
|
1060
|
+
words(o, n) {
|
|
1061
|
+
let p = o;
|
|
1062
|
+
for (let i = 0; i < Math.abs(n); i++)
|
|
1063
|
+
p = n > 0 ? wordEnd(this.value, p) : wordBegin(this.value, p);
|
|
1064
|
+
return p;
|
|
1065
|
+
}
|
|
1066
|
+
/* move by rows, keeping the goal column of the first move of a sequence */
|
|
1067
|
+
vertical(n) {
|
|
1068
|
+
if (this.lastCmd !== "next-line" && this.lastCmd !== "previous-line")
|
|
1069
|
+
this.goal = goalColumn(this.ops, this.value, this.off);
|
|
1070
|
+
const dir = n < 0 ? -1 : 1;
|
|
1071
|
+
for (let i = 0; i < Math.abs(n); i++) {
|
|
1072
|
+
const t = rowMove(this.ops, this.value, this.off, dir, this.goal);
|
|
1073
|
+
if (t === this.off) {
|
|
1074
|
+
this.fail(dir < 0 ? "Beginning of buffer" : "End of buffer");
|
|
1075
|
+
break;
|
|
1076
|
+
}
|
|
1077
|
+
this.off = t;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
/* go to a line, saving the mark */
|
|
1081
|
+
gotoLine(l) {
|
|
1082
|
+
this.pushMark(this.off, false);
|
|
1083
|
+
this.off = lineAt(this.value, Math.max(0, l - 1));
|
|
1084
|
+
}
|
|
1085
|
+
/* ==== EDITING HELPERS ==== */
|
|
1086
|
+
/* record the text for undoing, once per key and amalgamating up to 20 inserted characters */
|
|
1087
|
+
record() {
|
|
1088
|
+
if (this.recorded)
|
|
1089
|
+
return;
|
|
1090
|
+
this.recorded = true;
|
|
1091
|
+
if (this.cmd === "self-insert-command" && this.lastCmd === "self-insert-command" && this.amalgam < 20) {
|
|
1092
|
+
this.amalgam++;
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
this.amalgam = this.cmd === "self-insert-command" ? 1 : 0;
|
|
1096
|
+
this.undos.push({ value: this.value, off: this.off });
|
|
1097
|
+
if (this.undos.length > 2000)
|
|
1098
|
+
this.undos.splice(0, this.undos.length - 2000);
|
|
1099
|
+
}
|
|
1100
|
+
/* replace a range of the text, keeping the mark and the mark ring on the surrounding text */
|
|
1101
|
+
replace(s, e, text) {
|
|
1102
|
+
this.record();
|
|
1103
|
+
const d = text.length - (e - s);
|
|
1104
|
+
const adjust = (p) => p > e || (p === e && e > s) ? p + d : p > s ? s : p;
|
|
1105
|
+
this.value = this.value.slice(0, s) + text + this.value.slice(e);
|
|
1106
|
+
if (this.mark !== null)
|
|
1107
|
+
this.mark = adjust(this.mark);
|
|
1108
|
+
this.markRing = this.markRing.map(adjust);
|
|
1109
|
+
this.emit(this.value);
|
|
1110
|
+
}
|
|
1111
|
+
/* insert a text at the point */
|
|
1112
|
+
insert(text) {
|
|
1113
|
+
if (text === "")
|
|
1114
|
+
return;
|
|
1115
|
+
this.replace(this.off, this.off, text);
|
|
1116
|
+
this.off += text.length;
|
|
1117
|
+
}
|
|
1118
|
+
/* convert the case of a text */
|
|
1119
|
+
convert(cmd, text) {
|
|
1120
|
+
if (cmd.startsWith("upcase"))
|
|
1121
|
+
return text.toUpperCase();
|
|
1122
|
+
if (cmd.startsWith("downcase"))
|
|
1123
|
+
return text.toLowerCase();
|
|
1124
|
+
return text.replace(/[\p{L}\p{N}]+/gu, (w) => {
|
|
1125
|
+
const [c, ...rest] = [...w];
|
|
1126
|
+
return c.toUpperCase() + rest.join("").toLowerCase();
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
/* fill the paragraph of the point */
|
|
1130
|
+
fill() {
|
|
1131
|
+
const f = fillParagraph(this.value, this.off, FILL_COLUMN);
|
|
1132
|
+
if (f === null)
|
|
1133
|
+
return;
|
|
1134
|
+
if (f.text !== this.value.slice(f.start, f.end))
|
|
1135
|
+
this.replace(f.start, f.end, f.text);
|
|
1136
|
+
this.off = f.off;
|
|
1137
|
+
}
|
|
1138
|
+
/* ==== MARK AND KILL HELPERS ==== */
|
|
1139
|
+
/* set the mark, saving the previous one on the mark ring */
|
|
1140
|
+
pushMark(o, activate) {
|
|
1141
|
+
if (this.mark !== null) {
|
|
1142
|
+
this.markRing.push(this.mark);
|
|
1143
|
+
if (this.markRing.length > 16)
|
|
1144
|
+
this.markRing.shift();
|
|
1145
|
+
}
|
|
1146
|
+
this.mark = o;
|
|
1147
|
+
this.active = activate;
|
|
1148
|
+
}
|
|
1149
|
+
/* the region between the point and the mark */
|
|
1150
|
+
region() {
|
|
1151
|
+
if (this.mark === null) {
|
|
1152
|
+
this.fail("The mark is not set now, so there is no region");
|
|
1153
|
+
return null;
|
|
1154
|
+
}
|
|
1155
|
+
return this.mark < this.off ? [this.mark, this.off] : [this.off, this.mark];
|
|
1156
|
+
}
|
|
1157
|
+
/* delete the active region (of DEL and <deletechar>) */
|
|
1158
|
+
deleteRegion() {
|
|
1159
|
+
if (!this.active || this.mark === null || this.mark === this.off)
|
|
1160
|
+
return false;
|
|
1161
|
+
const s = Math.min(this.mark, this.off);
|
|
1162
|
+
this.replace(s, Math.max(this.mark, this.off), "");
|
|
1163
|
+
this.off = s;
|
|
1164
|
+
return true;
|
|
1165
|
+
}
|
|
1166
|
+
/* kill a range of the text into the kill ring */
|
|
1167
|
+
kill(s, e, prepend, append = KILLS.has(this.lastCmd)) {
|
|
1168
|
+
this.pushKill(this.value.slice(s, e), prepend, append);
|
|
1169
|
+
this.replace(s, e, "");
|
|
1170
|
+
this.off = s;
|
|
1171
|
+
}
|
|
1172
|
+
/* put a text onto the kill ring (and the clipboard), appending it to the last kill
|
|
1173
|
+
resp. prepending it for a backward kill */
|
|
1174
|
+
pushKill(text, prepend, append) {
|
|
1175
|
+
if (append && this.kills.length > 0)
|
|
1176
|
+
this.kills[0] = prepend ? text + this.kills[0] : this.kills[0] + text;
|
|
1177
|
+
else {
|
|
1178
|
+
this.kills.unshift(text);
|
|
1179
|
+
if (this.kills.length > 120)
|
|
1180
|
+
this.kills.pop();
|
|
1181
|
+
}
|
|
1182
|
+
this.clip(this.kills[0]);
|
|
1183
|
+
}
|
|
1184
|
+
/* undo changes, where an interrupted sequence of undos starts over and hence redoes */
|
|
1185
|
+
undo(n) {
|
|
1186
|
+
if (this.lastCmd !== "undo")
|
|
1187
|
+
this.undoPtr = this.undos.length;
|
|
1188
|
+
for (let i = 0; i < n; i++) {
|
|
1189
|
+
if (this.undoPtr === 0) {
|
|
1190
|
+
this.fail("No further undo information");
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
const snap = this.undos[--this.undoPtr];
|
|
1194
|
+
this.undos.push({ value: this.value, off: this.off });
|
|
1195
|
+
this.value = snap.value;
|
|
1196
|
+
this.off = Math.min(snap.off, snap.value.length);
|
|
1197
|
+
if (this.mark !== null)
|
|
1198
|
+
this.mark = Math.min(this.mark, this.value.length);
|
|
1199
|
+
this.emit(this.value);
|
|
1200
|
+
}
|
|
1201
|
+
this.say("Undo");
|
|
1202
|
+
}
|
|
1203
|
+
/* ==== MINIBUFFER ==== */
|
|
1204
|
+
/* read a text in the minibuffer */
|
|
1205
|
+
ask(kind, prompt, init, done, names) {
|
|
1206
|
+
this.mini = { kind, prompt, text: init, done, names, hist: -1 };
|
|
1207
|
+
}
|
|
1208
|
+
/* read a "y" or "n" answer */
|
|
1209
|
+
askYN(question, yes, no = () => { }) {
|
|
1210
|
+
this.reader = { prompt: `${question}(y or n) `, take: (t) => {
|
|
1211
|
+
if (t === "y" || t === "Y" || t === " ")
|
|
1212
|
+
yes();
|
|
1213
|
+
else if (t === "n" || t === "N" || t === "DEL")
|
|
1214
|
+
no();
|
|
1215
|
+
else if (t === "C-g")
|
|
1216
|
+
this.say("Quit");
|
|
1217
|
+
else
|
|
1218
|
+
this.askYN(question.startsWith("Please answer") ? question : `Please answer y or n. ${question}`, yes, no);
|
|
1219
|
+
} };
|
|
1220
|
+
}
|
|
1221
|
+
/* read a "yes" or "no" answer */
|
|
1222
|
+
askYesNo(question, yes) {
|
|
1223
|
+
this.ask("yes-or-no", `${question}(yes or no) `, "", (answer) => {
|
|
1224
|
+
if (answer === "yes")
|
|
1225
|
+
yes();
|
|
1226
|
+
else if (answer !== "no")
|
|
1227
|
+
this.askYesNo(question.startsWith("Please answer") ? question : `Please answer yes or no. ${question}`, yes);
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
/* handle a key of the minibuffer */
|
|
1231
|
+
miniKey(t) {
|
|
1232
|
+
const m = this.mini;
|
|
1233
|
+
if (m === null)
|
|
1234
|
+
return;
|
|
1235
|
+
const hist = this.history.get(m.kind) ?? [];
|
|
1236
|
+
if (isChar(t))
|
|
1237
|
+
m.text += t;
|
|
1238
|
+
else if (t === "DEL")
|
|
1239
|
+
m.text = m.text.slice(0, this.ops.previous(m.text, m.text.length));
|
|
1240
|
+
else if (t === "M-DEL" || t === "C-DEL")
|
|
1241
|
+
m.text = m.text.slice(0, wordBegin(m.text, m.text.length));
|
|
1242
|
+
else if (t === "C-y")
|
|
1243
|
+
m.text += this.kills[0] ?? "";
|
|
1244
|
+
else if (t === "TAB" && m.names) {
|
|
1245
|
+
const hits = m.names.filter((name) => name.startsWith(m.text));
|
|
1246
|
+
if (hits.length > 0)
|
|
1247
|
+
m.text = commonPrefix(hits);
|
|
1248
|
+
}
|
|
1249
|
+
else if ((t === "M-p" || t === "<up>") && m.hist + 1 < hist.length)
|
|
1250
|
+
m.text = hist[++m.hist];
|
|
1251
|
+
else if ((t === "M-n" || t === "<down>") && m.hist >= 0) {
|
|
1252
|
+
m.hist--;
|
|
1253
|
+
m.text = m.hist >= 0 ? hist[m.hist] : "";
|
|
1254
|
+
}
|
|
1255
|
+
else if (t === "C-g") {
|
|
1256
|
+
this.mini = null;
|
|
1257
|
+
this.say("Quit");
|
|
1258
|
+
}
|
|
1259
|
+
else if (t === "RET") {
|
|
1260
|
+
this.mini = null;
|
|
1261
|
+
if (m.text !== "")
|
|
1262
|
+
this.history.set(m.kind, [m.text, ...hist.filter((h) => h !== m.text)].slice(0, 50));
|
|
1263
|
+
m.done(m.text);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
/* ==== FILE HELPERS ==== */
|
|
1267
|
+
/* the file operations, or an error without them */
|
|
1268
|
+
need() {
|
|
1269
|
+
if (this.files === null)
|
|
1270
|
+
this.fail("No file operations available");
|
|
1271
|
+
return this.files;
|
|
1272
|
+
}
|
|
1273
|
+
/* show the outcome of a file operation */
|
|
1274
|
+
outcome(res) {
|
|
1275
|
+
if (res.error)
|
|
1276
|
+
this.fail(res.message);
|
|
1277
|
+
else
|
|
1278
|
+
this.say(res.message);
|
|
1279
|
+
}
|
|
1280
|
+
/* write the text to a file, which becomes the visited one, asking before overwriting,
|
|
1281
|
+
and continue after a successful write */
|
|
1282
|
+
writeFile(file, then = () => { }) {
|
|
1283
|
+
const f = this.files;
|
|
1284
|
+
if (f === null)
|
|
1285
|
+
return;
|
|
1286
|
+
if (file === "") {
|
|
1287
|
+
this.fail("No file name");
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
const done = (r) => {
|
|
1291
|
+
this.outcome(r);
|
|
1292
|
+
if (!r.error)
|
|
1293
|
+
then();
|
|
1294
|
+
};
|
|
1295
|
+
const res = f.write(file, false, true);
|
|
1296
|
+
if (res.exists)
|
|
1297
|
+
this.askYN(`File ${file} exists; overwrite? `, () => done(f.write(file, true, true)));
|
|
1298
|
+
else
|
|
1299
|
+
done(res);
|
|
1300
|
+
}
|
|
1301
|
+
/* ==== INCREMENTAL SEARCH ==== */
|
|
1302
|
+
/* find a text forward from an offset, resp. backward with the match starting at most there */
|
|
1303
|
+
find(text, from, forward) {
|
|
1304
|
+
if (text === "")
|
|
1305
|
+
return null;
|
|
1306
|
+
const re = literalRegExp(text);
|
|
1307
|
+
if (forward) {
|
|
1308
|
+
re.lastIndex = Math.max(0, from);
|
|
1309
|
+
const m = re.exec(this.value);
|
|
1310
|
+
return m ? [m.index, m.index + m[0].length] : null;
|
|
1311
|
+
}
|
|
1312
|
+
let found = null;
|
|
1313
|
+
for (const m of this.value.matchAll(re)) {
|
|
1314
|
+
if (m.index > from)
|
|
1315
|
+
break;
|
|
1316
|
+
found = [m.index, m.index + m[0].length];
|
|
1317
|
+
}
|
|
1318
|
+
return found;
|
|
1319
|
+
}
|
|
1320
|
+
/* handle a key of the incremental search */
|
|
1321
|
+
isearchKey(t) {
|
|
1322
|
+
const s = this.is;
|
|
1323
|
+
if (s === null)
|
|
1324
|
+
return;
|
|
1325
|
+
const save = () => s.stack.push({ forward: s.forward, text: s.text, failing: s.failing,
|
|
1326
|
+
wrapped: s.wrapped, match: s.match, off: this.off });
|
|
1327
|
+
const restore = (p) => {
|
|
1328
|
+
Object.assign(s, { forward: p.forward, text: p.text, failing: p.failing, wrapped: p.wrapped, match: p.match });
|
|
1329
|
+
this.off = p.off;
|
|
1330
|
+
};
|
|
1331
|
+
const apply = (m) => {
|
|
1332
|
+
if (m === null)
|
|
1333
|
+
s.failing = true;
|
|
1334
|
+
else {
|
|
1335
|
+
s.failing = false;
|
|
1336
|
+
s.match = m;
|
|
1337
|
+
this.off = s.forward ? m[1] : m[0];
|
|
1338
|
+
}
|
|
1339
|
+
};
|
|
1340
|
+
if (t === "C-s" || t === "C-r") {
|
|
1341
|
+
const forward = t === "C-s";
|
|
1342
|
+
save();
|
|
1343
|
+
if (s.text === "") {
|
|
1344
|
+
s.forward = forward;
|
|
1345
|
+
if (this.lastSearch !== "") {
|
|
1346
|
+
s.text = this.lastSearch;
|
|
1347
|
+
apply(this.find(s.text, forward ? this.off : this.off - s.text.length, forward));
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
else if (forward !== s.forward) {
|
|
1351
|
+
s.forward = forward;
|
|
1352
|
+
if (s.match !== null)
|
|
1353
|
+
this.off = forward ? s.match[1] : s.match[0];
|
|
1354
|
+
}
|
|
1355
|
+
else if (s.failing) {
|
|
1356
|
+
s.wrapped = true;
|
|
1357
|
+
apply(this.find(s.text, forward ? 0 : this.value.length, forward));
|
|
1358
|
+
}
|
|
1359
|
+
else
|
|
1360
|
+
apply(this.find(s.text, forward ? (s.match?.[1] ?? this.off) : (s.match?.[0] ?? this.off) - 1, forward));
|
|
1361
|
+
}
|
|
1362
|
+
else if (isChar(t) || t === "C-w" || t === "C-y") {
|
|
1363
|
+
save();
|
|
1364
|
+
if (t === "C-w")
|
|
1365
|
+
s.text += this.value.slice(this.off, wordEnd(this.value, this.off));
|
|
1366
|
+
else if (t === "C-y")
|
|
1367
|
+
s.text += this.kills[0] ?? "";
|
|
1368
|
+
else
|
|
1369
|
+
s.text += t;
|
|
1370
|
+
const base = s.match !== null ? s.match[0] : s.forward ? s.origin : s.origin - s.text.length;
|
|
1371
|
+
apply(this.find(s.text, base, s.forward));
|
|
1372
|
+
}
|
|
1373
|
+
else if (t === "DEL") {
|
|
1374
|
+
const p = s.stack.pop();
|
|
1375
|
+
if (p !== undefined)
|
|
1376
|
+
restore(p);
|
|
1377
|
+
}
|
|
1378
|
+
else if (t === "C-g") {
|
|
1379
|
+
if (s.failing) {
|
|
1380
|
+
while (s.failing && s.stack.length > 0)
|
|
1381
|
+
restore(s.stack.pop());
|
|
1382
|
+
s.failing = false;
|
|
1383
|
+
}
|
|
1384
|
+
else {
|
|
1385
|
+
this.off = s.origin;
|
|
1386
|
+
this.is = null;
|
|
1387
|
+
this.say("Quit");
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
else if (t === "RET")
|
|
1391
|
+
this.isearchEnd();
|
|
1392
|
+
else {
|
|
1393
|
+
this.isearchEnd();
|
|
1394
|
+
this.dispatch(t);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
/* end the incremental search, saving the mark where it started */
|
|
1398
|
+
isearchEnd() {
|
|
1399
|
+
const s = this.is;
|
|
1400
|
+
if (s === null)
|
|
1401
|
+
return;
|
|
1402
|
+
this.is = null;
|
|
1403
|
+
if (s.text !== "")
|
|
1404
|
+
this.lastSearch = s.text;
|
|
1405
|
+
if (this.off !== s.origin) {
|
|
1406
|
+
this.pushMark(s.origin, false);
|
|
1407
|
+
this.say("Mark saved where search started");
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
/* ==== REPLACING ==== */
|
|
1411
|
+
/* start a query replace from the point resp. in the active region */
|
|
1412
|
+
queryStart(from, to) {
|
|
1413
|
+
this.lastReplace = [from, to];
|
|
1414
|
+
const mark = this.mark;
|
|
1415
|
+
const region = this.active && mark !== null;
|
|
1416
|
+
const s = region ? Math.min(mark, this.off) : this.off;
|
|
1417
|
+
const e = region ? Math.max(mark, this.off) : this.value.length;
|
|
1418
|
+
this.active = false;
|
|
1419
|
+
this.qr = { from, to, pos: s, limit: e, match: [s, s], count: 0 };
|
|
1420
|
+
this.queryNext();
|
|
1421
|
+
}
|
|
1422
|
+
/* find the next match of the query replace, or end it */
|
|
1423
|
+
queryNext() {
|
|
1424
|
+
const q = this.qr;
|
|
1425
|
+
if (q === null)
|
|
1426
|
+
return;
|
|
1427
|
+
const m = this.find(q.from, q.pos, true);
|
|
1428
|
+
if (m === null || m[1] > q.limit) {
|
|
1429
|
+
this.queryEnd();
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
q.match = m;
|
|
1433
|
+
this.off = m[1];
|
|
1434
|
+
}
|
|
1435
|
+
/* replace the current match of the query replace */
|
|
1436
|
+
queryReplace() {
|
|
1437
|
+
const q = this.qr;
|
|
1438
|
+
if (q === null)
|
|
1439
|
+
return;
|
|
1440
|
+
const [s, e] = q.match;
|
|
1441
|
+
this.replace(s, e, q.to);
|
|
1442
|
+
q.pos = s + q.to.length;
|
|
1443
|
+
q.limit += q.to.length - (e - s);
|
|
1444
|
+
q.count++;
|
|
1445
|
+
this.off = q.pos;
|
|
1446
|
+
}
|
|
1447
|
+
/* end the query replace */
|
|
1448
|
+
queryEnd() {
|
|
1449
|
+
const q = this.qr;
|
|
1450
|
+
if (q === null)
|
|
1451
|
+
return;
|
|
1452
|
+
this.qr = null;
|
|
1453
|
+
this.say(`Replaced ${plural(q.count, "occurrence")}`);
|
|
1454
|
+
}
|
|
1455
|
+
/* handle a key of the query replace */
|
|
1456
|
+
queryKey(t) {
|
|
1457
|
+
const q = this.qr;
|
|
1458
|
+
if (q === null)
|
|
1459
|
+
return;
|
|
1460
|
+
switch (t) {
|
|
1461
|
+
case "y":
|
|
1462
|
+
case " ":
|
|
1463
|
+
this.queryReplace();
|
|
1464
|
+
this.queryNext();
|
|
1465
|
+
break;
|
|
1466
|
+
case "n":
|
|
1467
|
+
case "DEL":
|
|
1468
|
+
q.pos = q.match[1];
|
|
1469
|
+
this.queryNext();
|
|
1470
|
+
break;
|
|
1471
|
+
case "!":
|
|
1472
|
+
while (this.qr !== null) {
|
|
1473
|
+
this.queryReplace();
|
|
1474
|
+
this.queryNext();
|
|
1475
|
+
}
|
|
1476
|
+
break;
|
|
1477
|
+
case ".":
|
|
1478
|
+
this.queryReplace();
|
|
1479
|
+
this.queryEnd();
|
|
1480
|
+
break;
|
|
1481
|
+
case "q":
|
|
1482
|
+
case "RET":
|
|
1483
|
+
this.queryEnd();
|
|
1484
|
+
break;
|
|
1485
|
+
case "C-g":
|
|
1486
|
+
this.qr = null;
|
|
1487
|
+
this.say("Quit");
|
|
1488
|
+
break;
|
|
1489
|
+
default:
|
|
1490
|
+
this.queryEnd();
|
|
1491
|
+
this.dispatch(t);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
/* replace all matches from the point resp. in the active region */
|
|
1495
|
+
replaceAll(from, to, regexp) {
|
|
1496
|
+
let re;
|
|
1497
|
+
try {
|
|
1498
|
+
re = regexp ? new RegExp(from, from === from.toLowerCase() ? "gim" : "gm") : literalRegExp(from);
|
|
1499
|
+
}
|
|
1500
|
+
catch (err) {
|
|
1501
|
+
this.fail(`Invalid regexp: ${err instanceof Error ? err.message : String(err)}`);
|
|
1502
|
+
return;
|
|
1503
|
+
}
|
|
1504
|
+
const mark = this.mark;
|
|
1505
|
+
const region = this.active && mark !== null;
|
|
1506
|
+
const s = region ? Math.min(mark, this.off) : this.off;
|
|
1507
|
+
const e = region ? Math.max(mark, this.off) : this.value.length;
|
|
1508
|
+
const segment = this.value.slice(s, e);
|
|
1509
|
+
const count = [...segment.matchAll(re)].length;
|
|
1510
|
+
const result = regexp
|
|
1511
|
+
? segment.replace(re, to.replace(/\$/g, "$$$$").replace(/\\(\d|&)/g, (_, d) => d === "&" ? "$&" : "$" + d))
|
|
1512
|
+
: segment.replace(re, () => to);
|
|
1513
|
+
if (result !== segment)
|
|
1514
|
+
this.replace(s, e, result);
|
|
1515
|
+
this.off = s + result.length;
|
|
1516
|
+
this.active = false;
|
|
1517
|
+
this.say(`Replaced ${plural(count, "occurrence")}`);
|
|
1518
|
+
}
|
|
1519
|
+
/* ==== MESSAGES ==== */
|
|
1520
|
+
/* show an informational message */
|
|
1521
|
+
say(text) {
|
|
1522
|
+
this.message = text;
|
|
1523
|
+
this.error = false;
|
|
1524
|
+
}
|
|
1525
|
+
/* show an error message */
|
|
1526
|
+
fail(text) {
|
|
1527
|
+
this.message = text;
|
|
1528
|
+
this.error = true;
|
|
1529
|
+
}
|
|
1530
|
+
}
|