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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
#include "core/lua_bridge.h"
#include <lauxlib.h>
#include <lualib.h>
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *EDITOR_REG_KEY = "lemacs.editor";
static Editor *get_editor(lua_State *L) {
lua_getfield(L, LUA_REGISTRYINDEX, EDITOR_REG_KEY);
Editor *editor = (Editor *)lua_touserdata(L, -1);
lua_pop(L, 1);
return editor;
}
static int l_insert(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
if (!buffer) {
lua_pushboolean(L, 0);
return 1;
}
size_t len = 0;
const char *text = luaL_checklstring(L, 1, &len);
for (size_t i = 0; i < len; ++i) {
if (!buffer_insert_char(buffer, text[i])) {
lua_pushboolean(L, 0);
return 1;
}
}
lua_pushboolean(L, 1);
return 1;
}
static int l_backspace(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
lua_pushboolean(L, buffer ? buffer_backspace(buffer) : false);
return 1;
}
static int l_delete_forward(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
lua_pushboolean(L, buffer ? buffer_delete_forward(buffer) : false);
return 1;
}
static int l_move_left(lua_State *L) {
Editor *editor = get_editor(L);
editor_move_left(editor);
return 0;
}
static int l_move_right(lua_State *L) {
Editor *editor = get_editor(L);
editor_move_right(editor);
return 0;
}
static int l_move_up(lua_State *L) {
Editor *editor = get_editor(L);
editor_move_up(editor);
return 0;
}
static int l_move_down(lua_State *L) {
Editor *editor = get_editor(L);
editor_move_down(editor);
return 0;
}
static int l_move_beginning_of_line(lua_State *L) {
Editor *editor = get_editor(L);
editor_move_beginning_of_line(editor);
return 0;
}
static int l_move_end_of_line(lua_State *L) {
Editor *editor = get_editor(L);
editor_move_end_of_line(editor);
return 0;
}
static int l_open_file(lua_State *L) {
Editor *editor = get_editor(L);
const char *path = luaL_checkstring(L, 1);
lua_pushboolean(L, editor_open_file(editor, path));
return 1;
}
static int l_save_file(lua_State *L) {
Editor *editor = get_editor(L);
const char *path = NULL;
if (!lua_isnoneornil(L, 1)) {
path = luaL_checkstring(L, 1);
}
lua_pushboolean(L, editor_save_file(editor, path));
return 1;
}
static int l_get_point(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
lua_pushinteger(L, (lua_Integer)(buffer ? buffer_point(buffer) : 0));
return 1;
}
static int l_set_point(lua_State *L) {
Editor *editor = get_editor(L);
lua_Integer point = luaL_checkinteger(L, 1);
if (point < 0) {
point = 0;
}
editor_set_point(editor, (size_t)point);
return 0;
}
static int l_char_at(lua_State *L) {
Editor *editor = get_editor(L);
lua_Integer idx = luaL_checkinteger(L, 1);
if (idx < 0) {
lua_pushstring(L, "");
return 1;
}
char ch = editor_char_at(editor, (size_t)idx);
if (ch == '\0') {
lua_pushstring(L, "");
return 1;
}
char text[2] = {ch, '\0'};
lua_pushstring(L, text);
return 1;
}
static int l_set_font(lua_State *L) {
Editor *editor = get_editor(L);
const char *query = luaL_checkstring(L, 1);
editor_set_font_query(editor, query);
return 0;
}
static int l_set_theme_color(lua_State *L) {
Editor *editor = get_editor(L);
const char *name = luaL_checkstring(L, 1);
int r = (int)luaL_checkinteger(L, 2);
int g = (int)luaL_checkinteger(L, 3);
int b = (int)luaL_checkinteger(L, 4);
int a = 255;
if (!lua_isnoneornil(L, 5)) {
a = (int)luaL_checkinteger(L, 5);
}
lua_pushboolean(L, editor_set_theme_color(editor, name, r, g, b, a));
return 1;
}
static int l_set_cursor_shape(lua_State *L) {
Editor *editor = get_editor(L);
const char *shape = luaL_checkstring(L, 1);
int cursor_shape = -1;
if (strcmp(shape, "bar") == 0) {
cursor_shape = LEMACS_CURSOR_BAR;
} else if (strcmp(shape, "block") == 0) {
cursor_shape = LEMACS_CURSOR_BLOCK;
} else if (strcmp(shape, "underline") == 0) {
cursor_shape = LEMACS_CURSOR_UNDERLINE;
} else {
lua_pushboolean(L, 0);
return 1;
}
lua_pushboolean(L, editor_set_cursor_shape(editor, cursor_shape));
return 1;
}
static int l_set_cursor_thickness(lua_State *L) {
Editor *editor = get_editor(L);
int thickness = (int)luaL_checkinteger(L, 1);
editor_set_cursor_thickness(editor, thickness);
return 0;
}
static int l_aux_buffer_new(lua_State *L) {
Editor *editor = get_editor(L);
int index = editor_aux_buffer_new(editor);
if (index < 0) {
lua_pushnil(L);
return 1;
}
lua_pushinteger(L, index);
return 1;
}
static int l_aux_buffer_set_text(lua_State *L) {
Editor *editor = get_editor(L);
int aux_index = (int)luaL_checkinteger(L, 1);
const char *text = luaL_checkstring(L, 2);
lua_pushboolean(L, editor_aux_buffer_set_text(editor, aux_index, text));
return 1;
}
static int l_layout_reset(lua_State *L) {
Editor *editor = get_editor(L);
editor_layout_reset(editor);
return 0;
}
static int l_layout_add_view(lua_State *L) {
Editor *editor = get_editor(L);
int source_type = (int)luaL_checkinteger(L, 1);
int source_index = (int)luaL_checkinteger(L, 2);
int x = (int)luaL_checkinteger(L, 3);
int y = (int)luaL_checkinteger(L, 4);
int width = (int)luaL_checkinteger(L, 5);
int height = (int)luaL_checkinteger(L, 6);
bool show_cursor = lua_toboolean(L, 7) != 0;
int bg_r = (int)luaL_checkinteger(L, 8);
int bg_g = (int)luaL_checkinteger(L, 9);
int bg_b = (int)luaL_checkinteger(L, 10);
int bg_a = (int)luaL_optinteger(L, 11, 255);
int fg_r = (int)luaL_checkinteger(L, 12);
int fg_g = (int)luaL_checkinteger(L, 13);
int fg_b = (int)luaL_checkinteger(L, 14);
int fg_a = (int)luaL_optinteger(L, 15, 255);
EditorColor bg = {(uint8_t)bg_r, (uint8_t)bg_g, (uint8_t)bg_b, (uint8_t)bg_a};
EditorColor fg = {(uint8_t)fg_r, (uint8_t)fg_g, (uint8_t)fg_b, (uint8_t)fg_a};
lua_pushboolean(L, editor_layout_add_view(editor, source_type, source_index, x, y, width, height, show_cursor, bg, fg));
return 1;
}
static int l_screen_width(lua_State *L) {
lua_pushinteger(L, GetScreenWidth());
return 1;
}
static int l_screen_height(lua_State *L) {
lua_pushinteger(L, GetScreenHeight());
return 1;
}
static int l_buffer_length(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
lua_pushinteger(L, (lua_Integer)(buffer ? buffer_length(buffer) : 0));
return 1;
}
static int l_request_quit(lua_State *L) {
Editor *editor = get_editor(L);
editor->should_quit = true;
return 0;
}
static int l_buffer_path(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
const char *path = buffer ? buffer_path(buffer) : NULL;
if (path && path[0] != '\0') {
lua_pushstring(L, path);
} else {
lua_pushnil(L);
}
return 1;
}
static int l_buffer_modified(lua_State *L) {
Editor *editor = get_editor(L);
Buffer *buffer = editor_current_buffer(editor);
lua_pushboolean(L, buffer ? buffer_modified(buffer) : false);
return 1;
}
static int l_buffer_count(lua_State *L) {
Editor *editor = get_editor(L);
lua_pushinteger(L, (lua_Integer)editor_buffer_count(editor));
return 1;
}
static int l_current_buffer_index(lua_State *L) {
Editor *editor = get_editor(L);
lua_pushinteger(L, (lua_Integer)editor_current_buffer_index(editor));
return 1;
}
static int l_switch_buffer(lua_State *L) {
Editor *editor = get_editor(L);
int index = (int)luaL_checkinteger(L, 1);
lua_pushboolean(L, editor_switch_buffer(editor, index));
return 1;
}
static int l_new_buffer(lua_State *L) {
Editor *editor = get_editor(L);
int index = editor_new_buffer(editor);
if (index < 0) {
lua_pushnil(L);
} else {
lua_pushinteger(L, index);
}
return 1;
}
static int l_buffer_path_at(lua_State *L) {
Editor *editor = get_editor(L);
int index = (int)luaL_checkinteger(L, 1);
const char *path = editor_buffer_path_at(editor, index);
if (!path) {
lua_pushnil(L);
} else {
lua_pushstring(L, path);
}
return 1;
}
static int l_buffer_modified_at(lua_State *L) {
Editor *editor = get_editor(L);
int index = (int)luaL_checkinteger(L, 1);
lua_pushboolean(L, editor_buffer_modified_at(editor, index));
return 1;
}
static void register_editor_api(lua_State *L) {
lua_newtable(L);
lua_pushcfunction(L, l_insert);
lua_setfield(L, -2, "insert");
lua_pushcfunction(L, l_backspace);
lua_setfield(L, -2, "backspace");
lua_pushcfunction(L, l_delete_forward);
lua_setfield(L, -2, "delete_forward");
lua_pushcfunction(L, l_move_left);
lua_setfield(L, -2, "move_left");
lua_pushcfunction(L, l_move_right);
lua_setfield(L, -2, "move_right");
lua_pushcfunction(L, l_move_up);
lua_setfield(L, -2, "move_up");
lua_pushcfunction(L, l_move_down);
lua_setfield(L, -2, "move_down");
lua_pushcfunction(L, l_move_beginning_of_line);
lua_setfield(L, -2, "move_beginning_of_line");
lua_pushcfunction(L, l_move_end_of_line);
lua_setfield(L, -2, "move_end_of_line");
lua_pushcfunction(L, l_open_file);
lua_setfield(L, -2, "open_file");
lua_pushcfunction(L, l_save_file);
lua_setfield(L, -2, "save_file");
lua_pushcfunction(L, l_aux_buffer_new);
lua_setfield(L, -2, "aux_buffer_new");
lua_pushcfunction(L, l_aux_buffer_set_text);
lua_setfield(L, -2, "aux_buffer_set_text");
lua_pushcfunction(L, l_layout_reset);
lua_setfield(L, -2, "layout_reset");
lua_pushcfunction(L, l_layout_add_view);
lua_setfield(L, -2, "layout_add_view");
lua_pushcfunction(L, l_screen_width);
lua_setfield(L, -2, "screen_width");
lua_pushcfunction(L, l_screen_height);
lua_setfield(L, -2, "screen_height");
lua_pushcfunction(L, l_get_point);
lua_setfield(L, -2, "point");
lua_pushcfunction(L, l_set_point);
lua_setfield(L, -2, "set_point");
lua_pushcfunction(L, l_char_at);
lua_setfield(L, -2, "char_at");
lua_pushcfunction(L, l_set_font);
lua_setfield(L, -2, "set_font");
lua_pushcfunction(L, l_set_theme_color);
lua_setfield(L, -2, "set_theme_color");
lua_pushcfunction(L, l_set_cursor_shape);
lua_setfield(L, -2, "set_cursor_shape");
lua_pushcfunction(L, l_set_cursor_thickness);
lua_setfield(L, -2, "set_cursor_thickness");
lua_pushcfunction(L, l_buffer_length);
lua_setfield(L, -2, "buffer_length");
lua_pushcfunction(L, l_buffer_path);
lua_setfield(L, -2, "buffer_path");
lua_pushcfunction(L, l_buffer_modified);
lua_setfield(L, -2, "buffer_modified");
lua_pushcfunction(L, l_buffer_count);
lua_setfield(L, -2, "buffer_count");
lua_pushcfunction(L, l_current_buffer_index);
lua_setfield(L, -2, "current_buffer_index");
lua_pushcfunction(L, l_switch_buffer);
lua_setfield(L, -2, "switch_buffer");
lua_pushcfunction(L, l_new_buffer);
lua_setfield(L, -2, "new_buffer");
lua_pushcfunction(L, l_buffer_path_at);
lua_setfield(L, -2, "buffer_path_at");
lua_pushcfunction(L, l_buffer_modified_at);
lua_setfield(L, -2, "buffer_modified_at");
lua_pushcfunction(L, l_request_quit);
lua_setfield(L, -2, "request_quit");
lua_setglobal(L, "editor");
}
static bool call_global_with_string(lua_State *L, const char *name, const char *arg) {
lua_getglobal(L, name);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return false;
}
lua_pushstring(L, arg);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
fprintf(stderr, "lua error in %s: %s\n", name, lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
bool handled = lua_toboolean(L, -1) != 0;
lua_pop(L, 1);
return handled;
}
static bool call_global_noargs(lua_State *L, const char *name) {
lua_getglobal(L, name);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return false;
}
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
fprintf(stderr, "lua error in %s: %s\n", name, lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
return true;
}
static void configure_lua_package_path(lua_State *L, const char *init_script_path) {
char runtime_lua_path[2048] = {0};
snprintf(runtime_lua_path, sizeof(runtime_lua_path), "%s", init_script_path ? init_script_path : "runtime/init.lua");
char *last_slash = strrchr(runtime_lua_path, '/');
if (!last_slash) {
return;
}
*last_slash = '\0';
char add_path[2300] = {0};
snprintf(add_path, sizeof(add_path), "%s/lua/?.lua;%s/lua/?/init.lua", runtime_lua_path, runtime_lua_path);
lua_getglobal(L, "package");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return;
}
lua_getfield(L, -1, "path");
const char *current = lua_tostring(L, -1);
if (!current) {
current = "";
}
lua_pop(L, 1);
char merged[4096] = {0};
if (current[0] != '\0') {
snprintf(merged, sizeof(merged), "%s;%s", add_path, current);
} else {
snprintf(merged, sizeof(merged), "%s", add_path);
}
lua_pushstring(L, merged);
lua_setfield(L, -2, "path");
lua_pop(L, 1);
}
bool lua_bridge_init(LuaBridge *bridge, Editor *editor) {
bridge->editor = editor;
bridge->runtime_init_path[0] = '\0';
bridge->L = luaL_newstate();
if (!bridge->L) {
return false;
}
luaL_openlibs(bridge->L);
lua_pushlightuserdata(bridge->L, editor);
lua_setfield(bridge->L, LUA_REGISTRYINDEX, EDITOR_REG_KEY);
register_editor_api(bridge->L);
return true;
}
void lua_bridge_free(LuaBridge *bridge) {
if (bridge->L) {
lua_close(bridge->L);
bridge->L = NULL;
}
}
bool lua_bridge_load_runtime(LuaBridge *bridge, const char *init_script_path) {
configure_lua_package_path(bridge->L, init_script_path);
snprintf(bridge->runtime_init_path, sizeof(bridge->runtime_init_path), "%s", init_script_path ? init_script_path : "");
if (luaL_dofile(bridge->L, init_script_path) != LUA_OK) {
const char *err = lua_tostring(bridge->L, -1);
fprintf(stderr, "failed to load %s: %s\n", init_script_path, err ? err : "unknown");
lua_pop(bridge->L, 1);
return false;
}
return true;
}
bool lua_bridge_load_optional_init(LuaBridge *bridge, const char *script_path) {
if (!script_path || script_path[0] == '\0') {
return false;
}
lua_getglobal(bridge->L, "dofile");
if (!lua_isfunction(bridge->L, -1)) {
lua_pop(bridge->L, 1);
return false;
}
lua_pushstring(bridge->L, script_path);
if (lua_pcall(bridge->L, 1, 0, 0) != LUA_OK) {
fprintf(stderr, "optional init load error (%s): %s\n", script_path, lua_tostring(bridge->L, -1));
lua_pop(bridge->L, 1);
return false;
}
return true;
}
static bool call_global_with_optional_string(lua_State *L, const char *name, const char *arg) {
lua_getglobal(L, name);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return false;
}
if (arg && arg[0] != '\0') {
lua_pushstring(L, arg);
} else {
lua_pushnil(L);
}
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
fprintf(stderr, "lua error in %s: %s\n", name, lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
bool ok = lua_toboolean(L, -1) != 0;
lua_pop(L, 1);
return ok;
}
bool lua_bridge_call_open_file(LuaBridge *bridge, const char *path) {
return call_global_with_optional_string(bridge->L, "on_open_file", path);
}
bool lua_bridge_call_text(LuaBridge *bridge, const char *text) {
return call_global_with_string(bridge->L, "on_text", text ? text : "");
}
bool lua_bridge_call_key(LuaBridge *bridge, const char *key_name) {
return call_global_with_string(bridge->L, "on_key", key_name);
}
bool lua_bridge_call_tick(LuaBridge *bridge) {
return call_global_noargs(bridge->L, "on_tick");
}
|