-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvim.js
More file actions
395 lines (365 loc) · 11.4 KB
/
Copy pathvim.js
File metadata and controls
395 lines (365 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// vim.js - Vim mode handler for bukowski.
//
// `cursor.col` is a cell column (display cells from line start); h/l step by
// grapheme so the cursor never lands inside a wide char or surrogate pair.
// Producers here pair with search.js (cell-col matches) and single.js's
// highlighters (walk getLine cell-by-cell). See src/utils/cellCoord.js.
const {
cellColFromCharIdx,
charIdxFromCellCol,
lastGraphemeCellCol,
stepGraphemeLeft,
stepGraphemeRight,
} = require('./src/utils/cellCoord');
class VimHandler {
constructor(viewport) {
this.vp = viewport;
this.count = 0;
this.pending = ''; // Multi-char command buffer (e.g., 'g' for gg)
}
// Handle a key in normal/visual mode. Returns true if handled.
handleKey(key) {
const vp = this.vp;
// Count accumulation (1-9 start count, 0 only continues)
if (key >= '1' && key <= '9') {
this.count = this.count * 10 + parseInt(key);
return true;
}
if (key === '0' && this.count > 0) {
this.count = this.count * 10;
return true;
}
const count = this.count || 1;
this.count = 0;
// Multi-char: gg
if (this.pending === 'g') {
this.pending = '';
if (key === 'g') {
if (vp.mode === 'visual' || vp.mode === 'vline') {
vp.visualCursor.line = 0;
vp.visualCursor.col = 0;
this.ensureLineVisible(0);
} else if (vp.mode === 'normal') {
vp.normalCursor.line = 0;
vp.normalCursor.col = 0;
this.ensureLineVisible(0);
} else {
vp.offset = 0;
vp.followTail = false;
}
vp.draw();
}
return true;
}
// Visual modes
if (vp.mode === 'visual') {
return this.handleVisualCharKey(key, count);
}
if (vp.mode === 'vline') {
return this.handleVisualLineKey(key, count);
}
// Normal mode
return this.handleNormalKey(key, count);
}
handleNormalKey(key, count) {
const vp = this.vp;
const cursor = vp.normalCursor;
switch (key) {
case 'j':
cursor.line = Math.min(vp.contentHeight - 1, cursor.line + count);
this.clampNormalCol();
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case 'k':
cursor.line = Math.max(0, cursor.line - count);
this.clampNormalCol();
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case 'h': {
const lineText = vp.getLineText(cursor.line);
let charIdx = charIdxFromCellCol(lineText, cursor.col);
for (let i = 0; i < count && charIdx > 0; i++) {
charIdx = stepGraphemeLeft(lineText, charIdx);
}
cursor.col = cellColFromCharIdx(lineText, charIdx);
vp.draw();
return true;
}
case 'l': {
const lineText = vp.getLineText(cursor.line);
const lastCol = lastGraphemeCellCol(lineText);
let charIdx = charIdxFromCellCol(lineText, cursor.col);
for (let i = 0; i < count; i++) {
const next = stepGraphemeRight(lineText, charIdx);
if (cellColFromCharIdx(lineText, next) > lastCol) break;
charIdx = next;
}
cursor.col = cellColFromCharIdx(lineText, charIdx);
vp.draw();
return true;
}
case '0':
cursor.col = 0;
vp.draw();
return true;
case '$':
cursor.col = lastGraphemeCellCol(vp.getLineText(cursor.line));
vp.draw();
return true;
case '\x04': // Ctrl+d
cursor.line = Math.min(vp.contentHeight - 1, cursor.line + Math.floor(vp.height / 2));
this.clampNormalCol();
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case '\x15': // Ctrl+u
cursor.line = Math.max(0, cursor.line - Math.floor(vp.height / 2));
this.clampNormalCol();
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case '\x06': // Ctrl+f
cursor.line = Math.min(vp.contentHeight - 1, cursor.line + vp.height);
this.clampNormalCol();
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case '\x02': // Ctrl+b
cursor.line = Math.max(0, cursor.line - vp.height);
this.clampNormalCol();
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case 'g':
this.pending = 'g';
return true;
case 'G':
cursor.line = vp.contentHeight - 1;
cursor.col = 0;
this.ensureLineVisible(cursor.line);
vp.draw();
return true;
case 'v':
this.enterVisual('char');
return true;
case 'V':
this.enterVisual('line');
return true;
case '/':
vp.enterSearch();
return true;
case 'n':
vp.nextMatch();
return true;
case 'N':
vp.prevMatch();
return true;
default:
return false;
}
}
clampNormalCol() {
const vp = this.vp;
const lineText = vp.getLineText(vp.normalCursor.line);
vp.normalCursor.col = Math.min(vp.normalCursor.col, lastGraphemeCellCol(lineText));
}
// Character-level visual mode (v)
handleVisualCharKey(key, count) {
const vp = this.vp;
switch (key) {
case 'j':
vp.visualCursor.line = Math.min(vp.contentHeight - 1, vp.visualCursor.line + count);
this.clampCursorCol();
this.ensureLineVisible(vp.visualCursor.line);
vp.draw();
return true;
case 'k':
vp.visualCursor.line = Math.max(0, vp.visualCursor.line - count);
this.clampCursorCol();
this.ensureLineVisible(vp.visualCursor.line);
vp.draw();
return true;
case 'h': {
const lineText = vp.getLineText(vp.visualCursor.line);
let charIdx = charIdxFromCellCol(lineText, vp.visualCursor.col);
for (let i = 0; i < count && charIdx > 0; i++) {
charIdx = stepGraphemeLeft(lineText, charIdx);
}
vp.visualCursor.col = cellColFromCharIdx(lineText, charIdx);
vp.draw();
return true;
}
case 'l': {
const lineText = vp.getLineText(vp.visualCursor.line);
const lastCol = lastGraphemeCellCol(lineText);
let charIdx = charIdxFromCellCol(lineText, vp.visualCursor.col);
for (let i = 0; i < count; i++) {
const next = stepGraphemeRight(lineText, charIdx);
if (cellColFromCharIdx(lineText, next) > lastCol) break;
charIdx = next;
}
vp.visualCursor.col = cellColFromCharIdx(lineText, charIdx);
vp.draw();
return true;
}
case '0':
vp.visualCursor.col = 0;
vp.draw();
return true;
case '$':
vp.visualCursor.col = lastGraphemeCellCol(vp.getLineText(vp.visualCursor.line));
vp.draw();
return true;
case 'G':
vp.visualCursor.line = vp.contentHeight - 1;
this.clampCursorCol();
this.ensureLineVisible(vp.visualCursor.line);
vp.draw();
return true;
case 'g':
this.pending = 'g';
return true;
case 'y':
this.yankSelection();
return true;
case '\x1b': // Escape - exit visual mode
vp.mode = 'normal';
vp.draw();
return true;
default:
return false;
}
}
// Line-level visual mode (V)
handleVisualLineKey(key, count) {
const vp = this.vp;
switch (key) {
case 'j':
vp.visualCursor.line = Math.min(vp.contentHeight - 1, vp.visualCursor.line + count);
this.ensureLineVisible(vp.visualCursor.line);
vp.draw();
return true;
case 'k':
vp.visualCursor.line = Math.max(0, vp.visualCursor.line - count);
this.ensureLineVisible(vp.visualCursor.line);
vp.draw();
return true;
case 'h':
case 'l':
// No-op in line mode
return true;
case 'G':
vp.visualCursor.line = vp.contentHeight - 1;
this.ensureLineVisible(vp.visualCursor.line);
vp.draw();
return true;
case 'g':
this.pending = 'g';
return true;
case 'y':
this.yankSelection();
return true;
case '\x1b': // Escape - exit visual mode
vp.mode = 'normal';
vp.draw();
return true;
default:
return false;
}
}
enterVisual(type, fromMode) {
const vp = this.vp;
const prevMode = fromMode || vp.mode;
vp.mode = type === 'line' ? 'vline' : 'visual';
let line, col;
if (prevMode === 'normal') {
// From normal mode: use virtual cursor
line = vp.normalCursor.line;
col = vp.normalCursor.col;
} else {
// From insert mode: use Claude's actual cursor position
const buffer = vp.term.buffer.active;
line = buffer.baseY + buffer.cursorY;
col = buffer.cursorX;
}
vp.visualAnchor = { line, col };
vp.visualCursor = { line, col };
// Ensure cursor is visible
this.ensureLineVisible(line);
vp.draw();
}
clampCursorCol() {
const vp = this.vp;
const lineText = vp.getLineText(vp.visualCursor.line);
vp.visualCursor.col = Math.min(vp.visualCursor.col, lastGraphemeCellCol(lineText));
}
ensureLineVisible(line) {
const vp = this.vp;
if (line < vp.offset) {
vp.offset = line;
vp.followTail = false;
} else if (line >= vp.offset + vp.height) {
vp.offset = line - vp.height + 1;
vp.followTail = false;
}
}
yankSelection() {
const vp = this.vp;
let text = '';
if (vp.mode === 'vline') {
// Line mode: yank full lines
const startLine = Math.min(vp.visualAnchor.line, vp.visualCursor.line);
const endLine = Math.max(vp.visualAnchor.line, vp.visualCursor.line);
const lines = [];
for (let i = startLine; i <= endLine; i++) {
lines.push(vp.getLineText(i));
}
text = lines.join('\n');
} else {
// Character mode: yank from anchor to cursor
const anchor = vp.visualAnchor;
const cursor = vp.visualCursor;
// Determine start and end positions
let start, end;
if (anchor.line < cursor.line || (anchor.line === cursor.line && anchor.col <= cursor.col)) {
start = anchor;
end = cursor;
} else {
start = cursor;
end = anchor;
}
// Cell-col → JS-char idx for slicing. End col is inclusive in vim
// semantics: step one cell past to capture the whole grapheme there.
if (start.line === end.line) {
const line = vp.getLineText(start.line);
const a = charIdxFromCellCol(line, start.col);
const b = charIdxFromCellCol(line, end.col + 1);
text = line.substring(a, b);
} else {
const lines = [];
const firstLine = vp.getLineText(start.line);
lines.push(firstLine.substring(charIdxFromCellCol(firstLine, start.col)));
for (let i = start.line + 1; i < end.line; i++) {
lines.push(vp.getLineText(i));
}
const lastLine = vp.getLineText(end.line);
lines.push(lastLine.substring(0, charIdxFromCellCol(lastLine, end.col + 1)));
text = lines.join('\n');
}
}
// OSC 52 clipboard
const b64 = Buffer.from(text).toString('base64');
process.stdout.write(`\x1b]52;c;${b64}\x07`);
// Return to insert mode for quick paste workflow
vp.mode = 'insert';
vp.draw();
}
// Reset state (e.g., on mode change)
reset() {
this.count = 0;
this.pending = '';
}
}
module.exports = { VimHandler };