-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvim.js
More file actions
353 lines (324 loc) · 9.64 KB
/
vim.js
File metadata and controls
353 lines (324 loc) · 9.64 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
// vim.js - Vim mode handler for bukowski
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':
cursor.col = Math.max(0, cursor.col - count);
vp.draw();
return true;
case 'l':
const lineLen = vp.getLineText(cursor.line).length;
cursor.col = Math.min(Math.max(0, lineLen - 1), cursor.col + count);
vp.draw();
return true;
case '0':
cursor.col = 0;
vp.draw();
return true;
case '$':
cursor.col = Math.max(0, vp.getLineText(cursor.line).length - 1);
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 lineLen = vp.getLineText(vp.normalCursor.line).length;
vp.normalCursor.col = Math.min(vp.normalCursor.col, Math.max(0, lineLen - 1));
}
// 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':
vp.visualCursor.col = Math.max(0, vp.visualCursor.col - count);
vp.draw();
return true;
case 'l':
const lineLen = vp.getLineText(vp.visualCursor.line).length;
vp.visualCursor.col = Math.min(lineLen - 1, vp.visualCursor.col + count);
vp.draw();
return true;
case '0':
vp.visualCursor.col = 0;
vp.draw();
return true;
case '$':
vp.visualCursor.col = Math.max(0, vp.getLineText(vp.visualCursor.line).length - 1);
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 lineLen = vp.getLineText(vp.visualCursor.line).length;
vp.visualCursor.col = Math.min(vp.visualCursor.col, Math.max(0, lineLen - 1));
}
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;
}
if (start.line === end.line) {
// Single line
const line = vp.getLineText(start.line);
text = line.substring(start.col, end.col + 1);
} else {
// Multi-line
const lines = [];
// First line: from start.col to end
lines.push(vp.getLineText(start.line).substring(start.col));
// Middle lines: full
for (let i = start.line + 1; i < end.line; i++) {
lines.push(vp.getLineText(i));
}
// Last line: from start to end.col
lines.push(vp.getLineText(end.line).substring(0, 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 };