forked from miketang84/mp-5.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp_tags.mpsl
320 lines (246 loc) · 8.32 KB
/
mp_tags.mpsl
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
/*
Minimum Profit - A Text Editor
Tags.
Angel Ortega <angel@triptico.com> et al.
This software is released into the public domain.
NO WARRANTY. See file LICENSE for details.
*/
/* default ctags command */
mp.config.ctags_cmd = "ctags *";
/** editor actions **/
mp_doc.actions['find_tag'] = sub(d) {
local tag = d->get_word();
/* ask for it, taking the word under the cursor */
local r = mp.form(
[
{
label: L("Tag to seek") + ':',
type: 'text',
history: 'find_tag',
value: tag
}
]
);
if (r != NULL)
mp.open_tag(r[0]);
};
mp_doc.actions['complete'] = sub(d) {
local w = d->get_word();
if (w) {
local words = {};
d->busy(1);
/* create a unique list of words */
foreach (l, d.txt.lines)
words += mp.split_by_words(l)->hmap();
/* delete current word from the set because
the user wouldn't want to complete a complete word */
del(words, w);
d->busy(0);
d->complete(map(words, sub (v, i) { i; }));
}
};
mp_doc.actions['complete_symbol'] = sub(d) {
mp.long_op(mp.load_tags, 1);
if (!d->complete(map(mp.tags, sub (v, i) { i; }), L("Select symbol") + ':'))
mp.alert(L("No matching symbol found."));
};
/* completes the current word using clang */
mp_doc.actions['complete_with_clang'] = sub(d) {
local w = d->get_word(), clang_options = "";
if (size(w) == 0)
return NULL;
local ext = "", extension = "";
ext = lc(d.name->regex("/\.[^.]+$/"));
if (ext == ".cpp") extension = "c++";
if (ext == ".c++") extension = "c++";
if (ext == ".cc") extension = "c++";
if (ext == ".c") extension = "c";
/* For headers we can't deduce if it's c++ or c, so let's guess it's C++ */
if (ext == ".h") extension = "c++";
if (ext == ".hpp") extension = "c++";
if (ext == ".hh") extension = "c++";
/* Clang only works for C/C++ */
if (extension == "") return NULL;
d->busy(1);
/* the clang command to trigger is:
clang -fsyntax-only -Xclang -code-completion-macros -Xclang -code-completion-patterns -Xclang -code-completion-brief-comments -Xclang -code-completion-at=file:line:col <clang-build-options> <file>
we use stdin here to avoid creating a temporary file
*/
local target = sprintf("clang -x%s -fsyntax-only -Xclang -code-completion-macros -Xclang -code-completion-brief-comments -Xclang -code-completion-at=-:%d:%d %s - 2>/dev/null", extension, d.txt.y+1, d.txt.x - size(w)+1, clang_options);
local p, lines = [];
if ((p = popen2(target)) != NULL) {
/* write the document first (close is done in the function) */
mp.save_th(p[1], d);
pclose(p[1]);
foreach (l, p[0])
lines->push(mp.chomp(l));
pclose(p[0]);
/* we only keep completions that start with the current word */
lines = lines->grep(regcomp('/^COMPLETION: ' + w + '/'))->map(sub(v) { split(v, ': '); });
}
else {
d->busy(0);
mp.alert(sprintf(L("Error executing '%s'"), target));
return NULL;
}
if (size(lines) == 0) {
mp.message = {
timeout: time() + 2,
string: L("No completion found")
};
return NULL;
}
local list = [];
/* lines format is [[garbage, completion with arguments, <optional description>], ...] */
foreach(line, lines) {
if (line[1] != NULL) {
local completion = split(line[1], " ");
if (line[2] != NULL)
list->push(completion[0] + "\t" + line[2]);
else list->push(completion[0]);
}
}
d->busy(0);
d->complete(list);
};
/** default key bindings **/
mp_doc.keycodes['ctrl-t'] = "find_tag";
mp_doc.keycodes['ctrl-u'] = "complete";
mp_doc.keycodes['alt-u'] = "complete_with_clang";
/** action descriptions **/
mp.actdesc['find_tag'] = LL("Search tag...");
mp.actdesc['complete'] = LL("Complete...");
mp.actdesc['complete_symbol'] = LL("Symbol completion...");
mp.actdesc['complete_with_clang'] = LL("Completion with clang...");
/** code **/
sub mp.load_tags(force)
/* load a 'tags' file */
{
local f, a;
if ((f = open("tags", "r")) == NULL) {
/* if force is set, execute the ctags command */
if (!force || (f = popen(mp.config.ctags_cmd, "r")) == NULL)
return;
/* read (and discard) a line from it */
read(f);
pclose(f);
/* try again */
if ((f = open("tags", "r")) == NULL)
return;
}
/* deletes all currently stored tags from the word/color cache */
foreach (v, i, mp.tags)
del(mp.word_color, i);
/* reset current tags */
mp.tags = {};
/* get the attribute for tags */
a = mp.colors.tag.attr;
while ((l = read(f))) {
local t = split(l, "\t");
local l, r;
/* clean the regex 'markup' for the label */
l = sregex(t[2], '@^/\^@', '');
l = sregex(l, '@\$/;"$@', '') + ' [' + t[1] + ']';
/* clean the regex itself */
r = sregex(t[2], '@/;"$@');
r = sregex(r, '@^/@');
/* escape all troublesome characters */
r = sregex(r, "/([\(\)\*\?\[\{\}]|\])/g", sub (m) { "\\" + m; });
/* store the tag information */
mp.tags[t[0]] = {
tag: t[0],
file: t[1],
regex: r,
label: l
};
/* store the word as specially-colored */
mp.word_color[t[0]] = a;
}
close(f);
}
sub mp.open_tag(tag)
/* opens a tag (uses GUI) */
{
local doc, a, t;
/* force loading of the tags file */
mp.long_op(mp.load_tags, 1);
/* greps all tags containing the tag, and returns the
values from mp.tags */
t = mp.tags->map(sub (v, i) { i; })->grep(regcomp('/' + tag + '/'))->map(mp.tags);
/* no tags? */
if (size(t) == 0)
mp.alert(L("Tag not found."));
else {
/* only one? get the first one */
if (size(t) == 1)
a = 0;
else {
/* build a list to ask the user */
local l = map(t, sub(e) { e.label; });
local r = mp.form(
[
{
label: L("Select tag") + ':',
type: 'list',
list: l
}
]
);
if (r != NULL)
a = r[0];
}
if (a != NULL) {
tag = t[a];
/* open the file */
doc = mp.long_op(mp.open, tag.file);
/* move up and search */
doc->move_bof();
doc->long_op(mp_doc.search, tag.regex);
}
}
}
sub mp_doc.complete(d, list, label)
/* completes the current word given a list (uses GUI) */
{
local ret = 0;
local word = d->get_word();
if (word) {
local l, a;
/* takes all list elements starting with word */
l = list->grep(regcomp('/^' + word + '/'))->sort();
if (size(l)) {
if (size(l) == 1)
a = 0;
else {
/* more than one; ask user */
local r = mp.form(
[
{
label: label || L("Select") + ':',
type: 'list',
list: l
}
]
);
if (r != NULL)
a = r[0];
}
if (a != NULL) {
/* accepted; substitute current word */
d->store_undo();
/* split line by words to take the offset of current word */
local r = d->split_line_by_words();
local offset = r[1][r[2]];
local ll = l[a]->split("\t");
/* substitute current word with newly selected word */
local w = splice(d.txt.lines[d.txt.y], ll[0], offset, size(word));
/* change line and x cursor */
d.txt.lines[d.txt.y] = w;
d.txt.x = offset + size(ll[0]);
d.txt.mod += 1;
ret = 1;
}
}
}
return ret;
}