-
Notifications
You must be signed in to change notification settings - Fork 3
/
vi_command.h
112 lines (100 loc) · 2.54 KB
/
vi_command.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
#ifndef VI_COMMAND_H
#define VI_COMMAND_H
#include <ctype.h>
#include <string.h>
constexpr char kSimpleActions[] = "iIaAJrsSoOuUpPxXCDY/\n";
constexpr char kCompoundActions[] = "cdy";
constexpr char kSimpleMotions[] = "hjklwbe${}\b G[]";
constexpr char kCompoundMotions[] = "gfFtT";
struct ViMotion {
ViMotion() { Reset(); }
ViMotion(int count, char move) : count(count), move(move), go(0) {}
void Reset() {
count = 0;
move = 0;
go = 0;
}
bool IsForward() const { return strchr("jlwe G", move); }
bool IsByLine() const { return strchr("jkgG", move); }
int count = 0;
char move = 0;
char go = 0;
};
struct ViCommand {
ViCommand& Add(const char* keys) {
while (*keys) Add(*keys++);
return *this;
}
ViCommand& Add(int key) {
if (completed) Reset();
if (action == 'r') {
character = key;
Complete();
} else if (motion.move && strchr(kCompoundMotions, motion.move)) {
motion.go = key;
Complete();
} else if (key == '0' && !count && !motion.count) {
motion.move = key;
Complete();
} else if (isdigit(key)) {
if (!action) {
auto new_count = count * 10 + key - '0';
if (new_count <= kMaxCount) count = new_count;
} else {
auto new_count = motion.count * 10 + key - '0';
if (new_count <= kMaxCount) motion.count = new_count;
}
} else if (strchr(kSimpleActions, key)) {
if (!action) {
action = key;
if (action != 'r') Complete();
} else {
Reset();
}
} else if (strchr(kCompoundActions, key)) {
if (!action) {
action = key;
} else if (action == key) {
doubled = true;
Complete();
} else {
Reset();
}
} else if (strchr(kSimpleMotions, key) && !motion.move) {
if (!action) {
motion.count = count;
count = 0;
}
motion.move = key;
Complete();
} else if (strchr(kCompoundMotions, key) && !motion.move) {
if (!action) {
motion.count = count;
count = 0;
}
motion.move = key;
} else
Reset();
return *this;
}
void Complete() {
if (count == 0 && action) count = 1;
if (motion.count == 0 && motion.move) motion.count = 1;
completed = true;
}
void Reset() {
count = 0;
action = 0;
doubled = false;
completed = false;
motion.Reset();
}
const int kMaxCount = 99;
int count = 0;
char action = 0;
bool doubled = false;
char character = 0; // for 'r' command
ViMotion motion;
bool completed = false;
};
#endif