@jadelclemens/pi-vim 0.0.0-stage → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +94 -2
- package/config.ts +33 -0
- package/index.ts +43 -0
- package/keys.ts +26 -0
- package/modes/insert.ts +86 -0
- package/modes/normal.ts +1141 -0
- package/modes/replace.ts +136 -0
- package/modes/visual.ts +651 -0
- package/motions.ts +653 -0
- package/operators.ts +305 -0
- package/package.json +49 -4
- package/registers.ts +151 -0
- package/repeat.ts +113 -0
- package/search.ts +295 -0
- package/state.ts +85 -0
- package/text-objects.ts +365 -0
- package/vim-editor.ts +662 -0
package/modes/visual.ts
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visual mode handler.
|
|
3
|
+
* Supports character-wise (v) and line-wise (V) visual selection.
|
|
4
|
+
* Motions extend the selection, operators act on it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { VimState } from "../state.js";
|
|
8
|
+
import { resetOperatorState } from "../state.js";
|
|
9
|
+
import { isDigit, ESCAPE_SEQS } from "../keys.js";
|
|
10
|
+
import { matchesKey } from "@mariozechner/pi-tui";
|
|
11
|
+
import {
|
|
12
|
+
wordForward,
|
|
13
|
+
wordBackward,
|
|
14
|
+
wordEnd,
|
|
15
|
+
WORDForward,
|
|
16
|
+
WORDBackward,
|
|
17
|
+
WORDEnd,
|
|
18
|
+
goToFirstLine,
|
|
19
|
+
goToLastLine,
|
|
20
|
+
firstNonBlank,
|
|
21
|
+
lineStart,
|
|
22
|
+
lineEnd,
|
|
23
|
+
findCharForward,
|
|
24
|
+
findCharBackward,
|
|
25
|
+
tillCharForward,
|
|
26
|
+
tillCharBackward,
|
|
27
|
+
repeatCharSearch,
|
|
28
|
+
reverseCharSearch,
|
|
29
|
+
paragraphBackward,
|
|
30
|
+
paragraphForward,
|
|
31
|
+
matchingBracket,
|
|
32
|
+
type MotionFn,
|
|
33
|
+
} from "../motions.js";
|
|
34
|
+
import {
|
|
35
|
+
applyOperator,
|
|
36
|
+
deleteRange,
|
|
37
|
+
extractText,
|
|
38
|
+
type OperatorRange,
|
|
39
|
+
} from "../operators.js";
|
|
40
|
+
import { resolveTextObject } from "../text-objects.js";
|
|
41
|
+
import { isValidRegister, getRegister, deleteToRegister } from "../registers.js";
|
|
42
|
+
import type { Position } from "../motions.js";
|
|
43
|
+
import {
|
|
44
|
+
beginSearch,
|
|
45
|
+
searchNext,
|
|
46
|
+
searchPrev,
|
|
47
|
+
searchWordUnderCursor,
|
|
48
|
+
} from "../search.js";
|
|
49
|
+
|
|
50
|
+
export interface VisualModeContext {
|
|
51
|
+
state: VimState;
|
|
52
|
+
superHandleInput: (data: string) => void;
|
|
53
|
+
getText: () => string;
|
|
54
|
+
getCursor: () => { line: number; col: number };
|
|
55
|
+
setText: (text: string) => void;
|
|
56
|
+
moveCursorTo: (line: number, col: number) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Compute the operator range for the current visual selection.
|
|
61
|
+
*/
|
|
62
|
+
export function getVisualRange(
|
|
63
|
+
state: VimState,
|
|
64
|
+
cursor: Position,
|
|
65
|
+
lines: string[],
|
|
66
|
+
): OperatorRange {
|
|
67
|
+
const anchor = state.visualAnchor!;
|
|
68
|
+
const isLinewise = state.mode === "visual-line";
|
|
69
|
+
|
|
70
|
+
let start: Position;
|
|
71
|
+
let end: Position;
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
anchor.line < cursor.line ||
|
|
75
|
+
(anchor.line === cursor.line && anchor.col <= cursor.col)
|
|
76
|
+
) {
|
|
77
|
+
start = anchor;
|
|
78
|
+
end = cursor;
|
|
79
|
+
} else {
|
|
80
|
+
start = cursor;
|
|
81
|
+
end = anchor;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (isLinewise) {
|
|
85
|
+
return {
|
|
86
|
+
start: { line: start.line, col: 0 },
|
|
87
|
+
end: { line: end.line, col: (lines[end.line] || "").length },
|
|
88
|
+
linewise: true,
|
|
89
|
+
inclusive: true,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
start,
|
|
95
|
+
end,
|
|
96
|
+
linewise: false,
|
|
97
|
+
inclusive: true,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Execute a motion in visual mode - moves cursor, extending selection.
|
|
103
|
+
*/
|
|
104
|
+
function executeVisualMotion(
|
|
105
|
+
motion: MotionFn,
|
|
106
|
+
ctx: VisualModeContext,
|
|
107
|
+
count: number,
|
|
108
|
+
): void {
|
|
109
|
+
const lines = ctx.getText().split("\n");
|
|
110
|
+
const cursor = ctx.getCursor();
|
|
111
|
+
const result = motion(lines, cursor, count);
|
|
112
|
+
ctx.moveCursorTo(result.position.line, result.position.col);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Apply an operator to the visual selection and return to normal mode.
|
|
117
|
+
*/
|
|
118
|
+
function applyVisualOperator(
|
|
119
|
+
ctx: VisualModeContext,
|
|
120
|
+
operator: string,
|
|
121
|
+
): void {
|
|
122
|
+
const lines = ctx.getText().split("\n");
|
|
123
|
+
const cursor = ctx.getCursor();
|
|
124
|
+
const range = getVisualRange(ctx.state, cursor, lines);
|
|
125
|
+
const register = ctx.state.register;
|
|
126
|
+
|
|
127
|
+
const result = applyOperator(operator, lines, range, register);
|
|
128
|
+
|
|
129
|
+
ctx.setText(result.newLines.join("\n"));
|
|
130
|
+
ctx.moveCursorTo(result.cursor.line, result.cursor.col);
|
|
131
|
+
|
|
132
|
+
ctx.state.visualAnchor = null;
|
|
133
|
+
ctx.state.mode = result.enterInsert ? "insert" : "normal";
|
|
134
|
+
resetOperatorState(ctx.state);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Handle input in visual mode (both character-wise and line-wise).
|
|
139
|
+
*/
|
|
140
|
+
export function handleVisualMode(
|
|
141
|
+
data: string,
|
|
142
|
+
ctx: VisualModeContext,
|
|
143
|
+
): boolean {
|
|
144
|
+
const { state } = ctx;
|
|
145
|
+
|
|
146
|
+
// --- Escape / Ctrl+[ → return to normal ---
|
|
147
|
+
if (matchesKey(data, "escape") || data === "\x1b" || data === "\x03") {
|
|
148
|
+
state.visualAnchor = null;
|
|
149
|
+
state.mode = "normal";
|
|
150
|
+
resetOperatorState(state);
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// --- Pending register selection (after `"`) ---
|
|
155
|
+
if (state.pendingRegister) {
|
|
156
|
+
state.pendingRegister = false;
|
|
157
|
+
if (data.length === 1 && isValidRegister(data)) {
|
|
158
|
+
state.register = data;
|
|
159
|
+
} else {
|
|
160
|
+
state.register = '"';
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// --- Pending text object key (after 'i' or 'a') ---
|
|
166
|
+
if (state.pendingTextObjectPrefix) {
|
|
167
|
+
const prefix = state.pendingTextObjectPrefix;
|
|
168
|
+
state.pendingTextObjectPrefix = null;
|
|
169
|
+
|
|
170
|
+
const textObjFn = resolveTextObject(prefix, data);
|
|
171
|
+
if (textObjFn) {
|
|
172
|
+
const lines = ctx.getText().split("\n");
|
|
173
|
+
const cursor = ctx.getCursor();
|
|
174
|
+
const range = textObjFn(lines, cursor);
|
|
175
|
+
|
|
176
|
+
if (range) {
|
|
177
|
+
// In visual mode, text objects set the selection to the text object range
|
|
178
|
+
state.visualAnchor = { line: range.start.line, col: range.start.col };
|
|
179
|
+
ctx.moveCursorTo(range.end.line, range.end.col);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
resetOperatorState(state);
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// --- Pending character input for f/F/t/T ---
|
|
187
|
+
if (state.pendingCharMotion) {
|
|
188
|
+
const pending = state.pendingCharMotion;
|
|
189
|
+
state.pendingCharMotion = null;
|
|
190
|
+
|
|
191
|
+
if (data.length === 1 && data.charCodeAt(0) >= 32) {
|
|
192
|
+
const count = state.count || 1;
|
|
193
|
+
let motionFn: MotionFn;
|
|
194
|
+
|
|
195
|
+
switch (pending) {
|
|
196
|
+
case "f":
|
|
197
|
+
motionFn = findCharForward(data);
|
|
198
|
+
break;
|
|
199
|
+
case "F":
|
|
200
|
+
motionFn = findCharBackward(data);
|
|
201
|
+
break;
|
|
202
|
+
case "t":
|
|
203
|
+
motionFn = tillCharForward(data);
|
|
204
|
+
break;
|
|
205
|
+
case "T":
|
|
206
|
+
motionFn = tillCharBackward(data);
|
|
207
|
+
break;
|
|
208
|
+
default:
|
|
209
|
+
resetOperatorState(state);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
executeVisualMotion(motionFn, ctx, count);
|
|
214
|
+
resetOperatorState(state);
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
resetOperatorState(state);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// --- Pending `g` prefix ---
|
|
223
|
+
if (state.pendingG) {
|
|
224
|
+
state.pendingG = false;
|
|
225
|
+
|
|
226
|
+
if (data === "g") {
|
|
227
|
+
const count = state.count || 1;
|
|
228
|
+
const countExplicit = state.countStarted;
|
|
229
|
+
executeVisualMotion(goToFirstLine, ctx, countExplicit ? count : 1);
|
|
230
|
+
resetOperatorState(state);
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
resetOperatorState(state);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// --- Count prefix ---
|
|
239
|
+
if (isDigit(data) && (data !== "0" || state.countStarted)) {
|
|
240
|
+
state.count = Math.min(state.count * 10 + parseInt(data, 10), 99999);
|
|
241
|
+
state.countStarted = true;
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const count = state.count || 1;
|
|
246
|
+
const countExplicit = state.countStarted;
|
|
247
|
+
|
|
248
|
+
// --- Operators apply to visual selection ---
|
|
249
|
+
if (data === "d" || data === "x") {
|
|
250
|
+
applyVisualOperator(ctx, "d");
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (data === "c" || data === "s") {
|
|
255
|
+
applyVisualOperator(ctx, "c");
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (data === "y") {
|
|
260
|
+
applyVisualOperator(ctx, "y");
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (data === ">" ) {
|
|
265
|
+
applyVisualOperator(ctx, ">");
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (data === "<") {
|
|
270
|
+
applyVisualOperator(ctx, "<");
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (data === "D") {
|
|
275
|
+
// In visual mode, D deletes the selection (same as d)
|
|
276
|
+
applyVisualOperator(ctx, "d");
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (data === "C") {
|
|
281
|
+
applyVisualOperator(ctx, "c");
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (data === "Y") {
|
|
286
|
+
// In visual mode, Y yanks the selection
|
|
287
|
+
applyVisualOperator(ctx, "y");
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// --- Paste in visual mode (replaces selection with register contents) ---
|
|
292
|
+
if (data === "p" || data === "P") {
|
|
293
|
+
const reg = getRegister(state.register);
|
|
294
|
+
if (reg) {
|
|
295
|
+
const lines = ctx.getText().split("\n");
|
|
296
|
+
const cursor = ctx.getCursor();
|
|
297
|
+
const range = getVisualRange(state, cursor, lines);
|
|
298
|
+
|
|
299
|
+
// Delete selection
|
|
300
|
+
const deletedText = extractText(lines, range);
|
|
301
|
+
const deleteResult = deleteRange(lines, range);
|
|
302
|
+
|
|
303
|
+
// Insert the register content at the delete position
|
|
304
|
+
const newLines = deleteResult.newLines;
|
|
305
|
+
const pos = deleteResult.cursor;
|
|
306
|
+
|
|
307
|
+
if (reg.linewise) {
|
|
308
|
+
const pasteLines = reg.text.split("\n");
|
|
309
|
+
if (range.linewise) {
|
|
310
|
+
newLines.splice(pos.line, 0, ...pasteLines);
|
|
311
|
+
} else {
|
|
312
|
+
newLines.splice(pos.line + 1, 0, ...pasteLines);
|
|
313
|
+
}
|
|
314
|
+
ctx.setText(newLines.join("\n"));
|
|
315
|
+
const targetLine = range.linewise ? pos.line : pos.line + 1;
|
|
316
|
+
const match = (pasteLines[0] || "").match(/^\s*/);
|
|
317
|
+
ctx.moveCursorTo(targetLine, match ? match[0].length : 0);
|
|
318
|
+
} else {
|
|
319
|
+
const line = newLines[pos.line] || "";
|
|
320
|
+
const pasteLines = reg.text.split("\n");
|
|
321
|
+
|
|
322
|
+
if (pasteLines.length === 1) {
|
|
323
|
+
newLines[pos.line] = line.substring(0, pos.col) + reg.text + line.substring(pos.col);
|
|
324
|
+
ctx.setText(newLines.join("\n"));
|
|
325
|
+
ctx.moveCursorTo(pos.line, pos.col + reg.text.length - 1);
|
|
326
|
+
} else {
|
|
327
|
+
const before = line.substring(0, pos.col);
|
|
328
|
+
const after = line.substring(pos.col);
|
|
329
|
+
const merged = [
|
|
330
|
+
before + pasteLines[0],
|
|
331
|
+
...pasteLines.slice(1, -1),
|
|
332
|
+
pasteLines[pasteLines.length - 1] + after,
|
|
333
|
+
];
|
|
334
|
+
newLines.splice(pos.line, 1, ...merged);
|
|
335
|
+
ctx.setText(newLines.join("\n"));
|
|
336
|
+
const lastIdx = pos.line + pasteLines.length - 1;
|
|
337
|
+
const lastCol = (pasteLines[pasteLines.length - 1] || "").length - 1;
|
|
338
|
+
ctx.moveCursorTo(lastIdx, Math.max(0, lastCol));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Store deleted text in unnamed register (vim behavior: deleted text goes to "")
|
|
343
|
+
deleteToRegister('"', deletedText, range.linewise);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
state.visualAnchor = null;
|
|
347
|
+
state.mode = "normal";
|
|
348
|
+
resetOperatorState(state);
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// --- Register selection prefix ---
|
|
353
|
+
if (data === '"') {
|
|
354
|
+
state.pendingRegister = true;
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// --- Text object prefixes ---
|
|
359
|
+
if (data === "i" || data === "a") {
|
|
360
|
+
state.pendingTextObjectPrefix = data;
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// --- Mode switching ---
|
|
365
|
+
if (data === "v") {
|
|
366
|
+
if (state.mode === "visual") {
|
|
367
|
+
// v in visual → back to normal
|
|
368
|
+
state.visualAnchor = null;
|
|
369
|
+
state.mode = "normal";
|
|
370
|
+
} else {
|
|
371
|
+
// V (visual-line) → switch to visual (character-wise)
|
|
372
|
+
state.mode = "visual";
|
|
373
|
+
}
|
|
374
|
+
resetOperatorState(state);
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (data === "V") {
|
|
379
|
+
if (state.mode === "visual-line") {
|
|
380
|
+
// V in visual-line → back to normal
|
|
381
|
+
state.visualAnchor = null;
|
|
382
|
+
state.mode = "normal";
|
|
383
|
+
} else {
|
|
384
|
+
// visual → switch to visual-line
|
|
385
|
+
state.mode = "visual-line";
|
|
386
|
+
}
|
|
387
|
+
resetOperatorState(state);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// --- `o` - swap cursor and anchor ---
|
|
392
|
+
if (data === "o" || data === "O") {
|
|
393
|
+
if (state.visualAnchor) {
|
|
394
|
+
const cursor = ctx.getCursor();
|
|
395
|
+
const anchor = { ...state.visualAnchor };
|
|
396
|
+
state.visualAnchor = { line: cursor.line, col: cursor.col };
|
|
397
|
+
ctx.moveCursorTo(anchor.line, anchor.col);
|
|
398
|
+
}
|
|
399
|
+
resetOperatorState(state);
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// --- Join lines (J) ---
|
|
404
|
+
if (data === "J") {
|
|
405
|
+
const lines = ctx.getText().split("\n");
|
|
406
|
+
const cursor = ctx.getCursor();
|
|
407
|
+
const range = getVisualRange(state, cursor, lines);
|
|
408
|
+
const startLine = range.start.line;
|
|
409
|
+
const endLine = range.end.line;
|
|
410
|
+
|
|
411
|
+
if (endLine > startLine) {
|
|
412
|
+
const newLines = [...lines];
|
|
413
|
+
let joinLine = startLine;
|
|
414
|
+
for (let i = startLine; i < endLine; i++) {
|
|
415
|
+
const currentLine = newLines[joinLine]!;
|
|
416
|
+
const nextLine = (newLines[joinLine + 1] || "").replace(/^\s+/, "");
|
|
417
|
+
const joinCol = currentLine.length;
|
|
418
|
+
newLines[joinLine] = currentLine + (nextLine.length > 0 ? " " + nextLine : "");
|
|
419
|
+
newLines.splice(joinLine + 1, 1);
|
|
420
|
+
}
|
|
421
|
+
ctx.setText(newLines.join("\n"));
|
|
422
|
+
ctx.moveCursorTo(startLine, 0);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
state.visualAnchor = null;
|
|
426
|
+
state.mode = "normal";
|
|
427
|
+
resetOperatorState(state);
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// --- Toggle case (~) ---
|
|
432
|
+
if (data === "~") {
|
|
433
|
+
const lines = ctx.getText().split("\n");
|
|
434
|
+
const cursor = ctx.getCursor();
|
|
435
|
+
const range = getVisualRange(state, cursor, lines);
|
|
436
|
+
const newLines = [...lines];
|
|
437
|
+
|
|
438
|
+
if (range.linewise) {
|
|
439
|
+
for (let ln = range.start.line; ln <= range.end.line; ln++) {
|
|
440
|
+
newLines[ln] = toggleCaseLine(newLines[ln] || "");
|
|
441
|
+
}
|
|
442
|
+
} else if (range.start.line === range.end.line) {
|
|
443
|
+
const line = newLines[range.start.line] || "";
|
|
444
|
+
newLines[range.start.line] =
|
|
445
|
+
line.substring(0, range.start.col) +
|
|
446
|
+
toggleCaseLine(line.substring(range.start.col, range.end.col + 1)) +
|
|
447
|
+
line.substring(range.end.col + 1);
|
|
448
|
+
} else {
|
|
449
|
+
// Multi-line character-wise
|
|
450
|
+
const firstLine = newLines[range.start.line] || "";
|
|
451
|
+
newLines[range.start.line] =
|
|
452
|
+
firstLine.substring(0, range.start.col) +
|
|
453
|
+
toggleCaseLine(firstLine.substring(range.start.col));
|
|
454
|
+
for (let ln = range.start.line + 1; ln < range.end.line; ln++) {
|
|
455
|
+
newLines[ln] = toggleCaseLine(newLines[ln] || "");
|
|
456
|
+
}
|
|
457
|
+
const lastLine = newLines[range.end.line] || "";
|
|
458
|
+
newLines[range.end.line] =
|
|
459
|
+
toggleCaseLine(lastLine.substring(0, range.end.col + 1)) +
|
|
460
|
+
lastLine.substring(range.end.col + 1);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
ctx.setText(newLines.join("\n"));
|
|
464
|
+
ctx.moveCursorTo(range.start.line, range.start.col);
|
|
465
|
+
|
|
466
|
+
state.visualAnchor = null;
|
|
467
|
+
state.mode = "normal";
|
|
468
|
+
resetOperatorState(state);
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// --- Motions ---
|
|
473
|
+
switch (data) {
|
|
474
|
+
case "h":
|
|
475
|
+
for (let i = 0; i < count; i++) ctx.superHandleInput(ESCAPE_SEQS.left);
|
|
476
|
+
resetOperatorState(state);
|
|
477
|
+
return true;
|
|
478
|
+
|
|
479
|
+
case "j":
|
|
480
|
+
for (let i = 0; i < count; i++) ctx.superHandleInput(ESCAPE_SEQS.down);
|
|
481
|
+
resetOperatorState(state);
|
|
482
|
+
return true;
|
|
483
|
+
|
|
484
|
+
case "k":
|
|
485
|
+
for (let i = 0; i < count; i++) ctx.superHandleInput(ESCAPE_SEQS.up);
|
|
486
|
+
resetOperatorState(state);
|
|
487
|
+
return true;
|
|
488
|
+
|
|
489
|
+
case "l":
|
|
490
|
+
for (let i = 0; i < count; i++) ctx.superHandleInput(ESCAPE_SEQS.right);
|
|
491
|
+
resetOperatorState(state);
|
|
492
|
+
return true;
|
|
493
|
+
|
|
494
|
+
case "0":
|
|
495
|
+
executeVisualMotion(lineStart, ctx, 1);
|
|
496
|
+
resetOperatorState(state);
|
|
497
|
+
return true;
|
|
498
|
+
|
|
499
|
+
case "$":
|
|
500
|
+
executeVisualMotion(lineEnd, ctx, count);
|
|
501
|
+
resetOperatorState(state);
|
|
502
|
+
return true;
|
|
503
|
+
|
|
504
|
+
case "^":
|
|
505
|
+
executeVisualMotion(firstNonBlank, ctx, 1);
|
|
506
|
+
resetOperatorState(state);
|
|
507
|
+
return true;
|
|
508
|
+
|
|
509
|
+
case "w":
|
|
510
|
+
executeVisualMotion(wordForward, ctx, count);
|
|
511
|
+
resetOperatorState(state);
|
|
512
|
+
return true;
|
|
513
|
+
|
|
514
|
+
case "b":
|
|
515
|
+
executeVisualMotion(wordBackward, ctx, count);
|
|
516
|
+
resetOperatorState(state);
|
|
517
|
+
return true;
|
|
518
|
+
|
|
519
|
+
case "e":
|
|
520
|
+
executeVisualMotion(wordEnd, ctx, count);
|
|
521
|
+
resetOperatorState(state);
|
|
522
|
+
return true;
|
|
523
|
+
|
|
524
|
+
case "W":
|
|
525
|
+
executeVisualMotion(WORDForward, ctx, count);
|
|
526
|
+
resetOperatorState(state);
|
|
527
|
+
return true;
|
|
528
|
+
|
|
529
|
+
case "B":
|
|
530
|
+
executeVisualMotion(WORDBackward, ctx, count);
|
|
531
|
+
resetOperatorState(state);
|
|
532
|
+
return true;
|
|
533
|
+
|
|
534
|
+
case "E":
|
|
535
|
+
executeVisualMotion(WORDEnd, ctx, count);
|
|
536
|
+
resetOperatorState(state);
|
|
537
|
+
return true;
|
|
538
|
+
|
|
539
|
+
case "f":
|
|
540
|
+
case "F":
|
|
541
|
+
case "t":
|
|
542
|
+
case "T":
|
|
543
|
+
state.pendingCharMotion = data;
|
|
544
|
+
return true;
|
|
545
|
+
|
|
546
|
+
case ";":
|
|
547
|
+
executeVisualMotion(repeatCharSearch, ctx, count);
|
|
548
|
+
resetOperatorState(state);
|
|
549
|
+
return true;
|
|
550
|
+
|
|
551
|
+
case ",":
|
|
552
|
+
executeVisualMotion(reverseCharSearch, ctx, count);
|
|
553
|
+
resetOperatorState(state);
|
|
554
|
+
return true;
|
|
555
|
+
|
|
556
|
+
case "g":
|
|
557
|
+
state.pendingG = true;
|
|
558
|
+
return true;
|
|
559
|
+
|
|
560
|
+
case "G":
|
|
561
|
+
if (countExplicit) {
|
|
562
|
+
executeVisualMotion(goToLastLine, ctx, count);
|
|
563
|
+
} else {
|
|
564
|
+
const lines = ctx.getText().split("\n");
|
|
565
|
+
executeVisualMotion(goToLastLine, ctx, lines.length);
|
|
566
|
+
}
|
|
567
|
+
resetOperatorState(state);
|
|
568
|
+
return true;
|
|
569
|
+
|
|
570
|
+
case "{":
|
|
571
|
+
executeVisualMotion(paragraphBackward, ctx, count);
|
|
572
|
+
resetOperatorState(state);
|
|
573
|
+
return true;
|
|
574
|
+
|
|
575
|
+
case "}":
|
|
576
|
+
executeVisualMotion(paragraphForward, ctx, count);
|
|
577
|
+
resetOperatorState(state);
|
|
578
|
+
return true;
|
|
579
|
+
|
|
580
|
+
case "%":
|
|
581
|
+
executeVisualMotion(matchingBracket, ctx, 1);
|
|
582
|
+
resetOperatorState(state);
|
|
583
|
+
return true;
|
|
584
|
+
|
|
585
|
+
// === Search motions ===
|
|
586
|
+
case "n":
|
|
587
|
+
executeVisualMotion(searchNext, ctx, count);
|
|
588
|
+
resetOperatorState(state);
|
|
589
|
+
return true;
|
|
590
|
+
|
|
591
|
+
case "N":
|
|
592
|
+
executeVisualMotion(searchPrev, ctx, count);
|
|
593
|
+
resetOperatorState(state);
|
|
594
|
+
return true;
|
|
595
|
+
|
|
596
|
+
case "*": {
|
|
597
|
+
const sLines = ctx.getText().split("\n");
|
|
598
|
+
const sCursor = ctx.getCursor();
|
|
599
|
+
const sResult = searchWordUnderCursor(sLines, sCursor, "forward");
|
|
600
|
+
ctx.moveCursorTo(sResult.position.line, sResult.position.col);
|
|
601
|
+
resetOperatorState(state);
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
case "#": {
|
|
606
|
+
const sLines = ctx.getText().split("\n");
|
|
607
|
+
const sCursor = ctx.getCursor();
|
|
608
|
+
const sResult = searchWordUnderCursor(sLines, sCursor, "backward");
|
|
609
|
+
ctx.moveCursorTo(sResult.position.line, sResult.position.col);
|
|
610
|
+
resetOperatorState(state);
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// === Search command-line entry ===
|
|
615
|
+
case "/":
|
|
616
|
+
beginSearch("forward", state.mode as "visual" | "visual-line");
|
|
617
|
+
state.mode = "command-line";
|
|
618
|
+
resetOperatorState(state);
|
|
619
|
+
return true;
|
|
620
|
+
|
|
621
|
+
case "?":
|
|
622
|
+
beginSearch("backward", state.mode as "visual" | "visual-line");
|
|
623
|
+
state.mode = "command-line";
|
|
624
|
+
resetOperatorState(state);
|
|
625
|
+
return true;
|
|
626
|
+
|
|
627
|
+
default:
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Pass control sequences through
|
|
632
|
+
resetOperatorState(state);
|
|
633
|
+
if (data.length === 1 && data.charCodeAt(0) < 32) {
|
|
634
|
+
ctx.superHandleInput(data);
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
if (data.length > 1 && data.startsWith("\x1b")) {
|
|
638
|
+
ctx.superHandleInput(data);
|
|
639
|
+
return true;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function toggleCaseLine(s: string): string {
|
|
646
|
+
let result = "";
|
|
647
|
+
for (const ch of s) {
|
|
648
|
+
result += ch === ch.toLowerCase() ? ch.toUpperCase() : ch.toLowerCase();
|
|
649
|
+
}
|
|
650
|
+
return result;
|
|
651
|
+
}
|