-
Notifications
You must be signed in to change notification settings - Fork 7
/
module.cpp
370 lines (312 loc) · 9.53 KB
/
module.cpp
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
extern "C" {
#include <lua.h>
}
#define EXPORT \
extern "C" __attribute__((visibility("default"))) __attribute__((used))
#include "grammars.h"
#include "textmate.h"
#include "themes.h"
#include "util.h"
#include <map>
static std::map<int, doc_data_ptr> docs;
static bool has_running_threads = false;
/* paramaters
* nvim.lua buffer lines are 1-based
* every reference to a buffer line number passed to these function must be
* 1-based textmate parser lines are 0-based ... subtract here..
*/
int highlight_is_line_dirty(lua_State *L) {
int linenr = lua_tonumber(L, -2);
int docid = lua_tonumber(L, -1);
int block_line = linenr - 1;
if (docs.find(docid) == docs.end()) {
lua_pushnumber(L, 1);
return 1;
}
doc_data_ptr doc = docs[docid];
block_data_ptr block = doc->block_at(block_line);
// block_data_ptr prev = doc->previous_block(block_line);
lua_pushnumber(L, (!block || block->dirty) ? 1 : 0);
bool _threads = Textmate::has_running_threads();
if (_threads != has_running_threads) {
for (auto d : docs) {
d.second->make_dirty();
}
has_running_threads = _threads;
}
return 1;
}
int highlight_make_line_dirty(lua_State *L) {
int linenr = lua_tonumber(L, -2);
int docid = lua_tonumber(L, -1);
int block_line = linenr - 1;
if (docs.find(docid) == docs.end()) {
return 1;
}
doc_data_ptr doc = docs[docid];
block_data_ptr block = doc->block_at(block_line);
if (block) {
// log(">>%d", block_line);
block->make_dirty();
}
return 1;
}
// check and add at every highlight call
// int highlight_line_add_doc(lua_State *L)
// {
// int docid = lua_tonumber(L, -1);
// if (docs.find(docid) == docs.end()) {
// doc = std::make_shared<doc_data_t>();
// docs[docid] = doc;
// }
// }
int highlight_remove_doc(lua_State *L) {
int docid = lua_tonumber(L, -1);
auto it = docs.find(docid);
if (it != docs.end()) {
docs.erase(it);
}
return 1;
}
int highlight_line(lua_State *L) {
const char *p = lua_tostring(L, -4);
int linenr = lua_tonumber(L, -3);
int langid = lua_tonumber(L, -2);
int docid = lua_tonumber(L, -1);
doc_data_ptr doc;
if (docs.find(docid) == docs.end()) {
docs[docid] = std::make_shared<doc_data_t>();
// docs[docid] = doc;
}
// log(">>%d %d %d", linenr, langid, docid);
// lua_newtable(L);
// return 1;
doc = docs[docid];
std::string code = p;
std::vector<textstyle_t> res;
int block_line = linenr - 1;
block_data_ptr block = doc->block_at(block_line);
block_data_ptr prev_block = doc->previous_block(block_line);
block_data_ptr next_block = doc->next_block(block_line);
std::vector<span_info_t> spans;
res = Textmate::run_highlighter((char *)code.c_str(), Textmate::language(),
Textmate::theme(), block ? block.get() : NULL,
prev_block ? prev_block.get() : NULL,
next_block ? next_block.get() : NULL, &spans);
// log("highlight_line %d", block_line);
lua_newtable(L);
int row = 1;
for (auto r : spans) {
int col = 1;
lua_newtable(L);
lua_pushnumber(L, r.start);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, r.length);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, r.fg.r);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, r.fg.g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, r.fg.b);
lua_rawseti(L, -2, col++);
lua_pushstring(L, r.scope.c_str());
lua_rawseti(L, -2, col++);
lua_rawseti(L, -2, row++);
}
return 1;
}
int highlight_set_extensions_dir(lua_State *L) {
const char *p = lua_tostring(L, -1);
Textmate::initialize(p);
log("highlight_set_extensions_dir");
return 1;
}
int highlight_load_theme(lua_State *L) {
const char *p = lua_tostring(L, -1);
int theme_id = Textmate::load_theme(p);
log("highlight_load_theme %s", p);
lua_pushnumber(L, theme_id);
for (auto d : docs) {
d.second->make_dirty();
}
return 1;
}
int highlight_set_theme(lua_State *L) {
int id = lua_tonumber(L, -1);
Textmate::set_theme(id);
for (auto d : docs) {
d.second->make_dirty();
}
return 1;
}
int highlight_load_language(lua_State *L) {
const char *p = lua_tostring(L, -1);
int lang_id = Textmate::load_language(p);
log("highlight_load_language %s %d", p, lang_id);
lua_pushnumber(L, lang_id);
return 1;
}
int highlight_set_language(lua_State *L) {
int id = lua_tonumber(L, -1);
Textmate::set_language(id);
return 1;
}
int highlight_add_block(lua_State *L) {
int linenr = lua_tonumber(L, -2);
int docid = lua_tonumber(L, -1);
linenr -= 1;
if (linenr < 0) return 1;
if (docs.find(docid) == docs.end()) {
return 1;
}
// log("add %d %d", docid, linenr);
docs[docid]->add_block_at(linenr);
return 1;
}
int highlight_remove_block(lua_State *L) {
int linenr = lua_tonumber(L, -2);
int docid = lua_tonumber(L, -1);
linenr -= 1;
if (linenr < 0) return 1;
if (docs.find(docid) == docs.end()) {
return 1;
}
docs[docid]->remove_block_at(linenr);
// log("remove %d %d", docid, linenr);
return 1;
}
int highlight_themes(lua_State *L) {
std::vector<list_item_t> items = Textmate::theme_extensions();
lua_newtable(L);
int row = 1;
for (auto r : items) {
int col = 1;
lua_newtable(L);
lua_pushstring(L, r.name.c_str());
lua_rawseti(L, -2, col++);
lua_pushstring(L, r.description.c_str());
lua_rawseti(L, -2, col++);
lua_pushstring(L, r.value.c_str());
lua_rawseti(L, -2, col++);
lua_rawseti(L, -2, row++);
}
return 1;
}
int highlight_languages(lua_State *L) {
std::vector<list_item_t> items = Textmate::grammar_extensions();
lua_newtable(L);
int row = 1;
for (auto r : items) {
int col = 1;
lua_newtable(L);
lua_pushstring(L, r.name.c_str());
lua_rawseti(L, -2, col++);
lua_pushstring(L, r.description.c_str());
lua_rawseti(L, -2, col++);
lua_pushstring(L, r.value.c_str());
lua_rawseti(L, -2, col++);
lua_rawseti(L, -2, row++);
}
return 1;
}
int highlight_theme_info(lua_State *L) {
lua_newtable(L);
theme_info_t theme = Textmate::theme_info();
int col = 1;
lua_newtable(L);
lua_pushnumber(L, theme.fg_r); // 1
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.fg_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.fg_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.bg_r); // 4
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.bg_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.bg_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.sel_r); // 7
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.sel_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.sel_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.cmt_r); // 10
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.cmt_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.cmt_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.fn_r); // 13
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.fn_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.fn_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.kw_r); // 16
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.kw_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.kw_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.var_r); // 17
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.var_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.var_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.type_r); // 20
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.type_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.type_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.struct_r); // 23
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.struct_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.struct_b);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.ctrl_r); // 26
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.ctrl_g);
lua_rawseti(L, -2, col++);
lua_pushnumber(L, theme.ctrl_b);
lua_rawseti(L, -2, col++);
return 1;
}
EXPORT int luaopen_textmate(lua_State *L) {
// Textmate::load_theme_data(THEME_MONOKAI);
// Textmate::load_language_data(GRAMMAR_CPP);
lua_newtable(L);
lua_pushcfunction(L, highlight_line);
lua_setfield(L, -2, "highlight_line");
lua_pushcfunction(L, highlight_set_extensions_dir);
lua_setfield(L, -2, "highlight_set_extensions_dir");
lua_pushcfunction(L, highlight_load_theme);
lua_setfield(L, -2, "highlight_load_theme");
lua_pushcfunction(L, highlight_load_language);
lua_setfield(L, -2, "highlight_load_language");
lua_pushcfunction(L, highlight_set_theme);
lua_setfield(L, -2, "highlight_set_theme");
lua_pushcfunction(L, highlight_set_language);
lua_setfield(L, -2, "highlight_set_language");
lua_pushcfunction(L, highlight_is_line_dirty);
lua_setfield(L, -2, "highlight_is_line_dirty");
lua_pushcfunction(L, highlight_make_line_dirty);
lua_setfield(L, -2, "highlight_make_line_dirty");
lua_pushcfunction(L, highlight_remove_doc);
lua_setfield(L, -2, "highlight_remove_doc");
lua_pushcfunction(L, highlight_add_block);
lua_setfield(L, -2, "highlight_add_block");
lua_pushcfunction(L, highlight_remove_block);
lua_setfield(L, -2, "highlight_remove_block");
lua_pushcfunction(L, highlight_themes);
lua_setfield(L, -2, "highlight_themes");
lua_pushcfunction(L, highlight_theme_info);
lua_setfield(L, -2, "highlight_theme_info");
lua_pushcfunction(L, highlight_languages);
lua_setfield(L, -2, "highlight_languages");
return 1;
}