-
Notifications
You must be signed in to change notification settings - Fork 3
/
vi_translator.h
470 lines (424 loc) · 14.5 KB
/
vi_translator.h
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#ifndef VI_TRANSLATOR_H
#define VI_TRANSLATOR_H
// For Firefox and Chrome
#include <limits.h>
#include <stdio.h>
#include "clock.h"
#include "key_map.h"
#include "translator.h"
#include "vi_command.h"
class ViTranslator : public Translator {
public:
ViTranslator(Clock* clock) : clock_(clock) {}
const char* name() const { return "vi"; }
void Input(Events events, Sender* sender) override {
// TODO: Send button events directly.
sender_ = sender;
// If in insert mode, send events directly unless Esc is there.
if (insert_mode_ && !IsEscaped(events)) {
sender->Send(events);
return;
}
bool shifted = IsShifted(events);
unsigned now = clock_->Milliseconds();
for (int i = 0; i < kMaxEvents; ++i) {
// Skip if key is not pressed.
auto& event = events.keys[i];
if (event.key == 0) continue;
auto& old_event = in_events_.keys[i];
if (old_event.key == 0) {
event.repeated = false;
event.send_time = now;
SendKey(event.key, shifted);
} else {
// Keep the old state and let AutoRepeat do its job.
event.send_time = old_event.send_time;
event.repeated = old_event.repeated;
}
}
if (insert_mode_) in_events_.Reset();
else in_events_ = events;
}
void AutoRepeat() override {
if (insert_mode_) return;
bool shifted = IsShifted(in_events_);
unsigned now = clock_->Milliseconds();
for (int i = 0; i < kMaxEvents; ++i) {
auto& event = in_events_.keys[i];
if (event.key == 0) continue;
unsigned wait_until = event.send_time + (event.repeated ? 30 : 500);
if (now >= wait_until || now - wait_until < INT_MAX) {
event.repeated = true;
event.send_time = now;
SendKey(event.key, shifted);
}
}
}
private:
bool IsEscaped(const Events& events) const {
if (events.modifiers) return false;
for (int i = 0; i < kMaxEvents; ++i)
if (events.keys[i].key == KEY_ESC) return true;
return false;
}
bool IsShifted(const Events& events) const {
auto modifiers = events.modifiers & ~0xE000;
return (modifiers & MODIFIERKEY_SHIFT) ||
(modifiers & MODIFIERKEY_LEFT_SHIFT) ||
(modifiers & MODIFIERKEY_RIGHT_SHIFT);
}
bool IsControlled(const Events& events) const {
auto modifiers = events.modifiers & ~0xE000;
return (modifiers & MODIFIERKEY_CTRL) ||
(modifiers & MODIFIERKEY_LEFT_CTRL) ||
(modifiers & MODIFIERKEY_RIGHT_CTRL);
}
bool IsAlted(const Events& events) const {
auto modifiers = events.modifiers & ~0xE000;
return (modifiers & MODIFIERKEY_ALT) ||
(modifiers & MODIFIERKEY_LEFT_ALT) ||
(modifiers & MODIFIERKEY_RIGHT_ALT);
}
void SendKey(int key_code, bool shifted) {
// Shift-Escape sends a real escape, e.g. for closing a dialog.
if (key_code == KEY_ESC && shifted) {
Escape();
return;
}
// Escape resets the command.
if (key_code == KEY_ESC) {
command_.Reset();
insert_mode_ = false;
return;
}
// Read recognized keys.
auto key = ConvertKeyCode(key_code, shifted);
if (key == 0) return;
// Form the command sequence and execute it if complete.
command_.Add(key);
if (command_.completed) {
Execute(command_);
}
}
int ConvertKeyCode(int key_code, bool shifted) {
switch (key_code) {
case KEY_SPACE: return ' ';
case KEY_BACKSPACE: return '\b';
case KEY_ENTER: return '\n';
case KEY_QUOTE: return shifted ? '"' : '\'';
case KEY_COMMA: return shifted ? '<' : ',';
case KEY_MINUS: return shifted ? '_' : '-';
case KEY_PERIOD: return shifted ? '>' : '.';
case KEY_SLASH: return shifted ? '?' : '/';
case KEY_0: return shifted ? ')' : '0';
case KEY_1: return shifted ? '!' : '1';
case KEY_2: return shifted ? '@' : '2';
case KEY_3: return shifted ? '#' : '3';
case KEY_4: return shifted ? '$' : '4';
case KEY_5: return shifted ? '%' : '5';
case KEY_6: return shifted ? '^' : '6';
case KEY_7: return shifted ? '&' : '7';
case KEY_8: return shifted ? '*' : '8';
case KEY_9: return shifted ? '(' : '9';
case KEY_SEMICOLON: return shifted ? ':' : ';';
case KEY_EQUAL: return shifted ? '+' : '=';
case KEY_A: return shifted ? 'A' : 'a';
case KEY_B: return shifted ? 'B' : 'b';
case KEY_C: return shifted ? 'C' : 'c';
case KEY_D: return shifted ? 'D' : 'd';
case KEY_E: return shifted ? 'E' : 'e';
case KEY_F: return shifted ? 'F' : 'f';
case KEY_G: return shifted ? 'G' : 'g';
case KEY_H: return shifted ? 'H' : 'h';
case KEY_I: return shifted ? 'I' : 'i';
case KEY_J: return shifted ? 'J' : 'j';
case KEY_K: return shifted ? 'K' : 'k';
case KEY_L: return shifted ? 'L' : 'l';
case KEY_M: return shifted ? 'M' : 'm';
case KEY_N: return shifted ? 'N' : 'n';
case KEY_O: return shifted ? 'O' : 'o';
case KEY_P: return shifted ? 'P' : 'p';
case KEY_Q: return shifted ? 'Q' : 'q';
case KEY_R: return shifted ? 'R' : 'r';
case KEY_S: return shifted ? 'S' : 's';
case KEY_T: return shifted ? 'T' : 't';
case KEY_U: return shifted ? 'U' : 'u';
case KEY_V: return shifted ? 'V' : 'v';
case KEY_W: return shifted ? 'W' : 'w';
case KEY_X: return shifted ? 'X' : 'x';
case KEY_Y: return shifted ? 'Y' : 'y';
case KEY_Z: return shifted ? 'Z' : 'z';
case KEY_LEFT_BRACE: return shifted ? '{' : '[';
case KEY_BACKSLASH: return shifted ? '|' : '\\';
case KEY_RIGHT_BRACE: return shifted ? '}' : ']';
case KEY_TILDE: return shifted ? '~' : '`';
default: return 0; // to be ignored
}
}
bool Execute(const ViCommand& command) {
switch (command.action) {
case 0: Move(command.motion); break;
case 'i': Insert(); break;
case 'I': StartOfLine(); Insert(); break;
case 'a': Append(); break;
case 'A': EndOfLine(); Insert(); break;
case 'J': JoinLines(command.count); break;
case 'r': ReplaceByCharacter(command.count, command.character); break;
case 's': Substitute(command.count); break;
case 'S': Change(command.count, true, command.motion); break;
case 'o': OpenLine(); break;
case 'O': OpenLineAbove(); break;
case 'u': Undo(command.count); break;
case 'U': Redo(command.count); break; // different from vi
case 'p': PasteAfter(command.count); break;
case 'P': PasteBefore(command.count); break;
case 'x': Delete(1, false, ViMotion(command.count, 'l')); break;
case 'X': Delete(1, false, ViMotion(command.count, 'h')); break;
case 'c': Change(command.count, command.doubled, command.motion); break;
case 'C': ChangeToEndOfLine(); break;
case 'd': Delete(command.count, command.doubled, command.motion); break;
case 'D': DeleteToEndOfLine(); break;
case 'y': Yank(command.count, command.doubled, command.motion); break;
case 'Y': Yank(command.count, true, command.motion); break;
case '/': Find(); Insert(); break;
case '\n': Insert(); NewLine(); break;
default: return false;
}
return true;
}
bool Move(const ViMotion& motion) {
for (int i = 0; i < motion.count; ++i) {
switch (motion.move) {
case 'h': Left(); break;
case 'j': Down(); break;
case 'k': Up(); break;
case 'l': Right(); break;
case 'b': PrevStartOfWord(); break;
case 'w': NextStartOfWord(); break;
case 'e': NextEndOfWord(); break;
case '0': StartOfLine(); break;
case '$': EndOfLine(); break;
case '{': PrevParagraph(); break;
case '}': NextParagraph(); break;
case '\b': PageUp(); break;
case ' ': PageDown(); break;
case 'f': FindCharacter(motion.go, true); break;
case 'F': FindCharacter(motion.go, false); break;
case 't': TillCharacter(motion.go, true); break;
case 'T': TillCharacter(motion.go, false); break;
case 'g':
switch (motion.go) {
case 'g': StartOfDoc(); break;
case 't': NextTab(); break;
case 'T': PrevTab(); break;
default: return false;
}
break;
case 'G': EndOfDoc(); break;
case '[': PrevPage(); break;
case ']': NextPage(); break;
default: return false;
}
}
return true;
}
void Insert() {
insert_mode_ = true;
}
void Append() {
Right();
Insert();
}
void JoinLines(int count) {
count -= (count > 1);
for (int i = 0; i < count; ++i) {
EndOfLine();
Delete();
Emit(KEY_SPACE);
}
}
void ReplaceByCharacter(int count, char character) {
Select(1, ViMotion{count,'l'});
for (int i = 0; i < count; ++i) Print(character);
}
void Substitute(int count) {
Select(1, ViMotion{count,'l'});
Copy();
Insert();
}
void OpenLine() {
EndOfLine();
NewLine();
Insert();
}
void OpenLineAbove() {
StartOfLine();
NewLine();
Up();
Insert();
}
void PasteAfter(int count) {
if (copy_by_line_) EndOfLine();
else Right();
for (int i = 0; i < count; ++i) {
if (copy_by_line_) NewLine();
Paste();
}
}
void PasteBefore(int count) {
for (int i = 0; i < count; ++i) {
if (copy_by_line_) {
StartOfLine();
NewLine();
Up();
}
Paste();
}
}
void Undo(int count) {
for (int i = 0; i < count; ++i) Undo();
}
void Redo(int count) {
for (int i = 0; i < count; ++i) Redo();
}
void Change(int count, bool doubled, const ViMotion& motion) {
if (doubled) SelectLines(count);
else Select(count, motion);
Copy();
Insert();
}
void ChangeToEndOfLine() {
Select(1, ViMotion(1, '$'));
Copy();
Insert();
}
void Delete(int count, bool doubled, const ViMotion& motion) {
if (doubled) SelectLines(count);
else Select(count, motion);
Copy();
Emit(KEY_DELETE);
if (copy_by_line_) Emit(KEY_DELETE);
}
void DeleteToEndOfLine() {
Select(1, ViMotion(1, '$'));
Copy();
Emit(KEY_DELETE);
}
void Yank(int count, bool doubled, const ViMotion& motion) {
if (doubled) SelectLines(count);
else Select(count, motion);
Copy();
if (doubled || motion.IsForward()) Right();
else Left();
}
void SelectLines(int count) {
Select(count-1, ViMotion(1,'j'));
}
void Select(int count, const ViMotion& motion) {
copy_by_line_ = motion.IsByLine();
if (motion.IsByLine()) {
if (motion.IsForward()) StartOfLine();
else EndOfLine();
}
PressModifier(MODIFIERKEY_SHIFT);
if (motion.IsByLine()) {
if (motion.IsForward()) EndOfLine();
else StartOfLine();
}
for (int i = 0; i < count; ++i) {
Move(motion);
}
if (motion.IsByLine()) {
if (motion.IsForward()) EndOfLine();
else StartOfLine();
}
ReleaseModifier(MODIFIERKEY_SHIFT);
}
void FindCharacter(char character, bool forward) {
Find();
Print(character);
if (forward) Emit(KEY_ENTER);
else Emit(MODIFIERKEY_SHIFT, KEY_ENTER);
Emit(KEY_ESC);
Right();
Left();
}
void TillCharacter(char character, bool forward) {
Find();
Print(character);
if (forward) Emit(KEY_ENTER);
else Emit(MODIFIERKEY_SHIFT, KEY_ENTER);
Emit(KEY_ESC);
if (forward) Left();
else Right();
}
// Cursor-moving
void Left() { Emit(KEY_LEFT); }
void Right() { Emit(KEY_RIGHT); }
void Up() { Emit(KEY_UP); }
void Down() { Emit(KEY_DOWN); }
void Delete() { Emit(KEY_DELETE); }
void StartOfLine() { Emit(KEY_HOME); }
void EndOfLine() { Emit(KEY_END); }
void PageUp() { Emit(KEY_PAGE_UP); }
void PageDown() { Emit(KEY_PAGE_DOWN); }
// TODO: word movements are browser-dependent
void PrevStartOfWord() { Emit(MODIFIERKEY_CTRL, KEY_LEFT); }
void NextStartOfWord() { Emit(MODIFIERKEY_CTRL, KEY_RIGHT); }
void NextEndOfWord() { Emit(MODIFIERKEY_CTRL, KEY_RIGHT); }
void StartOfDoc() { Emit(MODIFIERKEY_CTRL, KEY_HOME); }
void EndOfDoc() { Emit(MODIFIERKEY_CTRL, KEY_END); }
// Special
void NewLine() { Emit(KEY_ENTER); }
void Escape() { Emit(KEY_ESC); }
void Find() { Emit(MODIFIERKEY_CTRL, KEY_F); }
void Copy() { Emit(MODIFIERKEY_CTRL, KEY_C); }
void Paste() { Emit(MODIFIERKEY_CTRL, KEY_V); }
void Undo() { Emit(MODIFIERKEY_CTRL, KEY_Z); }
void Redo() { Emit(MODIFIERKEY_CTRL, KEY_Y); }
void PrevParagraph() { Emit(MODIFIERKEY_CTRL, KEY_UP); }
void NextParagraph() { Emit(MODIFIERKEY_CTRL, KEY_DOWN); }
void PrevPage() { Emit(MODIFIERKEY_ALT, KEY_LEFT); }
void NextPage() { Emit(MODIFIERKEY_ALT, KEY_RIGHT); }
void PrevTab() { Emit(MODIFIERKEY_CTRL, KEY_PAGE_UP); }
void NextTab() { Emit(MODIFIERKEY_CTRL, KEY_PAGE_DOWN); }
// Send keys
void Emit(int key) {
// Only one key at a time.
out_events_.keys[0].key = key;
sender_->Delay(kDelayMs);
sender_->Send(out_events_);
out_events_.keys[0].key = 0;
sender_->Delay(kDelayMs);
sender_->Send(out_events_);
}
void Emit(int modifier, int key) {
PressModifier(modifier);
Emit(key);
ReleaseModifier(modifier);
}
void PressModifier(int modifier) {
out_events_.modifiers |= modifier;
sender_->Delay(kDelayMs);
sender_->Send(out_events_);
}
void ReleaseModifier(int modifier) {
out_events_.modifiers &= ~modifier;
sender_->Delay(kDelayMs);
sender_->Send(out_events_);
}
void Print(char character) {
char message[2] = {character, 0};
sender_->Delay(kDelayMs);
sender_->Send(message);
}
static const int kDelayMs = 10;
ViCommand command_;
bool copy_by_line_ = false;
Clock* const clock_;
Sender* sender_ = nullptr;
Events in_events_;
Events out_events_;
bool insert_mode_ = false;
};
#endif