-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13completion.c
153 lines (120 loc) · 3.41 KB
/
13completion.c
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
#include "common.h"
wchar_t*? ViWin*::selector2(ViWin* self, list<wstring>* lines)
{
wchar_t*? result = null;
bool end_of_select = false;
bool canceled = false;
int maxx = getmaxx(self.win);
int maxy = getmaxy(self.win);
int scrolltop = 0;
int cursor = 0;
while(!end_of_select) {
clear();
int maxy2 = lines.length() - scrolltop;
/// view ///
for(int y=0; y<maxy && y < maxy2; y++) {
auto it = lines.item(scrolltop+y, null);
auto line = it.substring(0, maxx-1);
if(cursor == y) {
attron(A_REVERSE);
mvprintw(y, 0, "%ls", line);
attroff(A_REVERSE);
}
else {
mvprintw(y, 0, "%ls", line);
}
}
refresh();
/// input ///
auto key = getch();
switch(key) {
case KEY_UP:
case 'k':
case 'P'-'A'+1:
cursor--;
break;
case KEY_DOWN:
case 'j':
case 'N'-'A'+1:
case (('I'-'A')+1):
cursor++;
break;
case 'D'-'A'+1:
cursor+=10;
break;
case (('U'-'A')+1):
cursor-=10;
break;
case ('C'-'A')+1:
case 'q':
case ('['-'A')+1:
canceled = true;
end_of_select = true;
break;
case KEY_ENTER:
case ('J'-'A')+1:
end_of_select = true;
break;
}
/// modification ///
if(cursor < 0) {
int scroll_size = -cursor +1;
cursor = 0;
scrolltop-=scroll_size;
if(scrolltop < 0) {
scrolltop = 0;
cursor = 0;
}
}
if(maxy2 < maxy) {
if(cursor >= maxy2) {
cursor = maxy2 - 1;
}
}
else {
if(cursor >= maxy) {
int scroll_size = cursor - maxy + 1;
scrolltop += scroll_size;
cursor -= scroll_size;
}
}
}
if(!canceled) {
result = lines.item(scrolltop+cursor, null);
}
return result;
}
void ViWin*::completion(ViWin* self, Vi* nvi) version 13
{
auto line = self.texts.item(self.scroll+self.cursorY, null);
wchar_t* p = line + self.cursorX;
p--;
while(p >= line) {
if((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_')
{
p--;
}
else {
break;
}
}
p++;
int len = ((wchar_t*)(line + self.cursorX) - p);
auto word = line.substring(self.cursorX-len, self.cursorX);
auto candidates = new list<wstring>.initialize();
foreach(it, self.texts) {
auto li = it.to_string().scan(/[a-zA-Z0-9_]+/);
foreach(it2, li) {
if(it2.index(word.to_string(), -1) == 0 && strcmp(it2, word.to_string()) != 0)
{
candidates.push_back(it2.to_wstring());
}
}
}
auto candidates2 = candidates.sort().uniq()
auto candidate = self.selector2(candidates2);
if(candidate) {
auto append = candidate.substring(len, -1);
self.insertText(append);
}
}