-
Notifications
You must be signed in to change notification settings - Fork 263
/
vis-lua.c
3707 lines (3428 loc) · 99 KB
/
vis-lua.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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
* Lua Extension API for the [Vis Editor](https://github.com/martanne/vis).
*
* *WARNING:* there is no stability guarantee at this time, the API might
* change without notice!
*
* This document might be out of date, run `make luadoc` to regenerate it.
*
* @module vis
* @author Marc André Tanner
* @license ISC
* @release RELEASE
*/
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/types.h>
#include <pwd.h>
#include "vis-lua.h"
#include "vis-core.h"
#include "text-motions.h"
#include "util.h"
#ifndef VIS_PATH
#define VIS_PATH "/usr/local/share/vis"
#endif
#define VIS_LUA_TYPE_VIS "vis"
#define VIS_LUA_TYPE_WIN_OPTS "winoptions"
#define VIS_LUA_TYPE_VIS_OPTS "visoptions"
#define VIS_LUA_TYPE_FILE "file"
#define VIS_LUA_TYPE_TEXT "text"
#define VIS_LUA_TYPE_MARK "mark"
#define VIS_LUA_TYPE_MARKS "marks"
#define VIS_LUA_TYPE_WINDOW "window"
#define VIS_LUA_TYPE_SELECTION "selection"
#define VIS_LUA_TYPE_SELECTIONS "selections"
#define VIS_LUA_TYPE_UI "ui"
#define VIS_LUA_TYPE_REGISTERS "registers"
#define VIS_LUA_TYPE_KEYACTION "keyaction"
#ifndef DEBUG_LUA
#define DEBUG_LUA 0
#endif
#if DEBUG_LUA
#define debug(...) do { printf(__VA_ARGS__); fflush(stdout); } while (0)
#else
#define debug(...) do { } while (0)
#endif
#if !CONFIG_LUA
bool vis_lua_path_add(Vis *vis, const char *path) { return true; }
bool vis_lua_paths_get(Vis *vis, char **lpath, char **cpath) { return false; }
void vis_lua_process_response(Vis *vis, const char *name,
char *buffer, size_t len, ResponseType rtype) { }
#else
#if DEBUG_LUA
static void stack_dump_entry(lua_State *L, int i) {
int t = lua_type(L, i);
switch (t) {
case LUA_TNIL:
printf("nil");
break;
case LUA_TBOOLEAN:
printf(lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TLIGHTUSERDATA:
printf("lightuserdata(%p)", lua_touserdata(L, i));
break;
case LUA_TNUMBER:
printf("%g", lua_tonumber(L, i));
break;
case LUA_TSTRING:
printf("`%s'", lua_tostring(L, i));
break;
case LUA_TTABLE:
printf("table[");
lua_pushnil(L); /* first key */
while (lua_next(L, i > 0 ? i : i - 1)) {
stack_dump_entry(L, -2);
printf("=");
stack_dump_entry(L, -1);
printf(",");
lua_pop(L, 1); /* remove value, keep key */
}
printf("]");
break;
case LUA_TUSERDATA:
printf("userdata(%p)", lua_touserdata(L, i));
break;
default: /* other values */
printf("%s", lua_typename(L, t));
break;
}
}
static void stack_dump(lua_State *L, const char *format, ...) {
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
int top = lua_gettop(L);
for (int i = 1; i <= top; i++) {
printf("%d: ", i);
stack_dump_entry(L, i);
printf("\n");
}
printf("\n\n");
fflush(stdout);
}
#endif
static int panic_handler(lua_State *L) {
void *ud = NULL;
lua_getallocf(L, &ud);
if (ud) {
Vis *vis = ud;
vis->lua = NULL;
const char *msg = NULL;
if (lua_type(L, -1) == LUA_TSTRING)
msg = lua_tostring(L, -1);
vis_info_show(vis, "Fatal Lua error: %s", msg ? msg : "unknown reason");
lua_close(L);
if (vis->running)
siglongjmp(vis->sigbus_jmpbuf, 1);
}
return 0;
}
static int error_handler(lua_State *L) {
Vis *vis = lua_touserdata(L, lua_upvalueindex(1));
if (vis->errorhandler)
return 1;
vis->errorhandler = true;
size_t len;
const char *msg = lua_tostring(L, 1);
if (msg)
luaL_traceback(L, L, msg, 1);
msg = lua_tolstring(L, 1, &len);
vis_message_show(vis, msg);
vis->errorhandler = false;
return 1;
}
static int pcall(Vis *vis, lua_State *L, int nargs, int nresults) {
/* insert a custom error function below all arguments */
int msgh = lua_gettop(L) - nargs;
lua_pushlightuserdata(L, vis);
lua_pushcclosure(L, error_handler, 1);
lua_insert(L, msgh);
int ret = lua_pcall(L, nargs, nresults, msgh);
lua_remove(L, msgh);
return ret;
}
/* expects a lua function at stack position `narg` and stores a
* reference to it in the registry. The return value can be used
* to look it up.
*
* registry["vis.functions"][(void*)(function)] = function
*/
static const void *func_ref_new(lua_State *L, int narg) {
const void *addr = lua_topointer(L, narg);
if (!lua_isfunction(L, narg) || !addr)
luaL_argerror(L, narg, "function expected");
lua_getfield(L, LUA_REGISTRYINDEX, "vis.functions");
lua_pushlightuserdata(L, (void*)addr);
lua_pushvalue(L, narg);
lua_settable(L, -3);
lua_pop(L, 1);
return addr;
}
/* retrieve function from registry and place it at the top of the stack */
static bool func_ref_get(lua_State *L, const void *addr) {
if (!addr)
return false;
lua_getfield(L, LUA_REGISTRYINDEX, "vis.functions");
lua_pushlightuserdata(L, (void*)addr);
lua_gettable(L, -2);
lua_remove(L, -2);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return false;
}
return true;
}
/* creates a new metatable for a given type and stores a mapping:
*
* registry["vis.types"][metatable] = type
*
* leaves the metatable at the top of the stack.
*/
static void obj_type_new(lua_State *L, const char *type) {
luaL_newmetatable(L, type);
lua_getglobal(L, "vis");
if (!lua_isnil(L, -1)) {
lua_getfield(L, -1, "types");
lua_pushvalue(L, -3);
lua_setfield(L, -2, type);
lua_pop(L, 1);
}
lua_pop(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, "vis.types");
lua_pushvalue(L, -2);
lua_pushstring(L, type);
lua_settable(L, -3);
lua_pop(L, 1);
}
/* get type of userdatum at the top of the stack:
*
* return registry["vis.types"][getmetatable(userdata)]
*/
const char *obj_type_get(lua_State *L) {
if (lua_isnil(L, -1))
return "nil";
lua_getfield(L, LUA_REGISTRYINDEX, "vis.types");
lua_getmetatable(L, -2);
lua_gettable(L, -2);
// XXX: in theory string might become invalid when popped from stack
const char *type = lua_tostring(L, -1);
lua_pop(L, 2);
return type;
}
static void *obj_new(lua_State *L, size_t size, const char *type) {
void *obj = lua_newuserdata(L, size);
luaL_getmetatable(L, type);
lua_setmetatable(L, -2);
lua_newtable(L);
lua_setuservalue(L, -2);
return obj;
}
/* returns registry["vis.objects"][addr] if it is of correct type */
static void *obj_ref_get(lua_State *L, void *addr, const char *type) {
lua_getfield(L, LUA_REGISTRYINDEX, "vis.objects");
lua_pushlightuserdata(L, addr);
lua_gettable(L, -2);
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
debug("get: vis.objects[%p] = nil\n", addr);
lua_pop(L, 1);
return NULL;
}
if (DEBUG_LUA) {
const char *actual_type = obj_type_get(L);
if (strcmp(type, actual_type) != 0)
debug("get: vis.objects[%p] = %s (BUG: expected %s)\n", addr, actual_type, type);
void **handle = luaL_checkudata(L, -1, type);
if (!handle)
debug("get: vis.objects[%p] = %s (BUG: invalid handle)\n", addr, type);
else if (*handle != addr)
debug("get: vis.objects[%p] = %s (BUG: handle mismatch %p)\n", addr, type, *handle);
}
/* verify that obj is correct type then unmodify the stack */
luaL_checkudata(L, -1, type);
lua_pop(L, 1);
return addr;
}
/* expects a userdatum at the top of the stack and sets
*
* registry["vis.objects"][addr] = userdata
*/
static void obj_ref_set(lua_State *L, void *addr) {
//debug("set: vis.objects[%p] = %s\n", addr, obj_type_get(L));
lua_getfield(L, LUA_REGISTRYINDEX, "vis.objects");
lua_pushlightuserdata(L, addr);
lua_pushvalue(L, -3);
lua_settable(L, -3);
lua_pop(L, 1);
}
/* invalidates an object reference
*
* registry["vis.objects"][addr] = nil
*/
static void obj_ref_free(lua_State *L, void *addr) {
if (DEBUG_LUA) {
lua_getfield(L, LUA_REGISTRYINDEX, "vis.objects");
lua_pushlightuserdata(L, addr);
lua_gettable(L, -2);
lua_remove(L, -2);
if (lua_isnil(L, -1))
debug("free-unused: %p\n", addr);
else
debug("free: vis.objects[%p] = %s\n", addr, obj_type_get(L));
lua_pop(L, 1);
}
lua_pushnil(L);
obj_ref_set(L, addr);
}
/* creates a new object reference of given type if it does not already exist in the registry:
*
* if (registry["vis.types"][metatable(registry["vis.objects"][addr])] != type) {
* // XXX: should not happen
* registry["vis.objects"][addr] = new_obj(addr, type)
* }
* return registry["vis.objects"][addr];
*/
static void *obj_ref_new(lua_State *L, void *addr, const char *type) {
if (!addr) {
lua_pushnil(L);
return NULL;
}
lua_getfield(L, LUA_REGISTRYINDEX, "vis.objects");
lua_pushlightuserdata(L, addr);
lua_gettable(L, -2);
lua_remove(L, -2);
const char *old_type = obj_type_get(L);
if (strcmp(type, old_type) == 0) {
debug("new: vis.objects[%p] = %s (returning existing object)\n", addr, old_type);
void **handle = luaL_checkudata(L, -1, type);
if (!handle)
debug("new: vis.objects[%p] = %s (BUG: invalid handle)\n", addr, old_type);
else if (*handle != addr)
debug("new: vis.objects[%p] = %s (BUG: handle mismatch %p)\n", addr, old_type, *handle);
return addr;
}
if (!lua_isnil(L, -1))
debug("new: vis.objects[%p] = %s (WARNING: changing object type from %s)\n", addr, type, old_type);
else
debug("new: vis.objects[%p] = %s (creating new object)\n", addr, type);
lua_pop(L, 1);
void **handle = obj_new(L, sizeof(addr), type);
obj_ref_set(L, addr);
*handle = addr;
return addr;
}
/* (type) check validity of object reference at stack location `idx' and retrieve it */
static void *obj_ref_check(lua_State *L, int idx, const char *type) {
void **addr = luaL_checkudata(L, idx, type);
if (!obj_ref_get(L, *addr, type))
luaL_argerror(L, idx, "invalid object reference");
return *addr;
}
static void *obj_ref_check_containerof(lua_State *L, int idx, const char *type, size_t offset) {
void *obj = obj_ref_check(L, idx, type);
return obj ? ((char*)obj-offset) : obj;
}
static void *obj_lightref_new(lua_State *L, void *addr, const char *type) {
if (!addr)
return NULL;
void **handle = obj_new(L, sizeof(addr), type);
*handle = addr;
return addr;
}
static void *obj_lightref_check(lua_State *L, int idx, const char *type) {
void **addr = luaL_checkudata(L, idx, type);
return *addr;
}
static int index_common(lua_State *L) {
lua_getmetatable(L, 1);
lua_pushvalue(L, 2);
lua_gettable(L, -2);
if (lua_isnil(L, -1)) {
lua_getuservalue(L, 1);
lua_pushvalue(L, 2);
lua_gettable(L, -2);
}
return 1;
}
static int newindex_common(lua_State *L) {
lua_getuservalue(L, 1);
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_settable(L, -3);
return 0;
}
static size_t getpos(lua_State *L, int narg) {
return lua_tounsigned(L, narg);
}
static size_t checkpos(lua_State *L, int narg) {
lua_Number n = luaL_checknumber(L, narg);
/* on most systems SIZE_MAX can't be represented in lua_Number.
* using < avoids undefined behaviour when n == SIZE_MAX+1
* which can be represented in lua_Number
*/
if (n >= 0 && n < (lua_Number)SIZE_MAX && n == (size_t)n)
return n;
return luaL_argerror(L, narg, "expected position, got number");
}
static void pushpos(lua_State *L, size_t pos) {
if (pos == EPOS)
lua_pushnil(L);
else
lua_pushunsigned(L, pos);
}
static void pushrange(lua_State *L, Filerange *r) {
if (!r || !text_range_valid(r)) {
lua_pushnil(L);
return;
}
lua_createtable(L, 0, 2);
lua_pushstring(L, "start");
lua_pushunsigned(L, r->start);
lua_settable(L, -3);
lua_pushstring(L, "finish");
lua_pushunsigned(L, r->end);
lua_settable(L, -3);
}
static Filerange getrange(lua_State *L, int index) {
Filerange range = text_range_empty();
if (lua_istable(L, index)) {
lua_getfield(L, index, "start");
range.start = checkpos(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "finish");
range.end = checkpos(L, -1);
lua_pop(L, 1);
} else {
range.start = checkpos(L, index);
range.end = range.start + checkpos(L, index+1);
}
return range;
}
static const char *keymapping(Vis *vis, const char *keys, const Arg *arg) {
lua_State *L = vis->lua;
if (!func_ref_get(L, arg->v))
return keys;
lua_pushstring(L, keys);
if (pcall(vis, L, 1, 1) != 0)
return keys;
if (lua_type(L, -1) != LUA_TNUMBER)
return keys; /* invalid or no return value, assume zero */
lua_Number number = lua_tonumber(L, -1);
lua_Integer integer = lua_tointeger(L, -1);
if (number != integer)
return keys;
if (integer < 0)
return NULL; /* need more input */
size_t len = integer;
size_t max = strlen(keys);
return (len <= max) ? keys+len : keys;
}
/***
* The main editor object.
* @type Vis
*/
/***
* Version information.
* @tfield string VERSION
* version information in `git describe` format, same as reported by `vis -v`.
*/
/***
* Lua API object types
* @field types meta tables of userdata objects used for type checking
* @local
*/
/***
* User interface.
* @tfield Ui ui the user interface being used
*/
/***
* Mode constants.
* @tfield modes modes
*/
/***
* Events.
* @tfield events events
*/
/***
* Registers.
* @field registers array to access the register by single letter name
*/
/***
* Scintillua lexer module.
* @field lexers might be `nil` if module is not found
*/
/***
* LPeg lexer module.
* @field lpeg might be `nil` if module is not found
*/
/***
* Current count.
* @tfield int count the specified count for the current command or `nil` if none was given
*/
/***
* Create an iterator over all windows.
* @function windows
* @return the new iterator
* @see win
* @usage
* for win in vis:windows() do
* -- do something with win
* end
*/
static int windows_iter(lua_State *L);
static int windows(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
Win **handle = lua_newuserdata(L, sizeof *handle), *next;
for (next = vis->windows; next && next->file->internal; next = next->next);
*handle = next;
lua_pushcclosure(L, windows_iter, 1);
return 1;
}
static int windows_iter(lua_State *L) {
Win **handle = lua_touserdata(L, lua_upvalueindex(1));
if (!*handle)
return 0;
Win *win = obj_ref_new(L, *handle, VIS_LUA_TYPE_WINDOW), *next;
if (win) {
for (next = win->next; next && next->file->internal; next = next->next);
*handle = next;
}
return 1;
}
/***
* Create an iterator over all files.
* @function files
* @return the new iterator
* @usage
* for file in vis:files() do
* -- do something with file
* end
*/
static int files_iter(lua_State *L);
static int files(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
File **handle = lua_newuserdata(L, sizeof *handle);
*handle = vis->files;
lua_pushcclosure(L, files_iter, 1);
return 1;
}
static int files_iter(lua_State *L) {
File **handle = lua_touserdata(L, lua_upvalueindex(1));
if (!*handle)
return 0;
File *file = obj_ref_new(L, *handle, VIS_LUA_TYPE_FILE);
if (file)
*handle = file->next;
return 1;
}
/***
* Create an iterator over all mark names.
* @function mark_names
* @return the new iterator
* @usage
* local marks = vis.win.marks
* for name in vis:mark_names() do
* local mark = marks[name]
* for i = 1, #mark do
* -- do something with: name, mark[i].start, mark[i].finish
* end
* end
*/
static int mark_names_iter(lua_State *L);
static int mark_names(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
lua_pushlightuserdata(L, vis);
enum VisMark *handle = lua_newuserdata(L, sizeof *handle);
*handle = 0;
lua_pushcclosure(L, mark_names_iter, 2);
return 1;
}
static int mark_names_iter(lua_State *L) {
Vis *vis = lua_touserdata(L, lua_upvalueindex(1));
enum VisMark *handle = lua_touserdata(L, lua_upvalueindex(2));
char mark = vis_mark_to(vis, *handle);
if (mark) {
lua_pushlstring(L, &mark, 1);
(*handle)++;
return 1;
}
return 0;
}
/***
* Create an iterator over all register names.
* @function register_names
* @return the new iterator
* @usage
* for name in vis:register_names() do
* local reg = vis.registers[name]
* for i = 1, #reg do
* -- do something with register value reg[i]
* end
* end
*/
static int register_names_iter(lua_State *L);
static int register_names(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
lua_pushlightuserdata(L, vis);
enum VisRegister *handle = lua_newuserdata(L, sizeof *handle);
*handle = 0;
lua_pushcclosure(L, register_names_iter, 2);
return 1;
}
static int register_names_iter(lua_State *L) {
Vis *vis = lua_touserdata(L, lua_upvalueindex(1));
enum VisRegister *handle = lua_touserdata(L, lua_upvalueindex(2));
char reg = vis_register_to(vis, *handle);
if (reg) {
lua_pushlstring(L, ®, 1);
(*handle)++;
return 1;
}
return 0;
}
/***
* Execute a `:`-command.
* @function command
* @tparam string command the command to execute
* @treturn bool whether the command succeeded
* @usage
* vis:command("set number")
*/
static int command(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const char *cmd = luaL_checkstring(L, 2);
bool ret = vis_cmd(vis, cmd);
lua_pushboolean(L, ret);
return 1;
}
/***
* Display a short message.
*
* The single line message will be displayed at the bottom of
* the screen and automatically hidden once a key is pressed.
*
* @function info
* @tparam string message the message to display
*/
static int info(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const char *msg = luaL_checkstring(L, 2);
vis_info_show(vis, "%s", msg);
return 0;
}
/***
* Display a multi line message.
*
* Opens a new window and displays an arbitrarily long message.
*
* @function message
* @tparam string message the message to display
*/
static int message(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const char *msg = luaL_checkstring(L, 2);
vis_message_show(vis, msg);
return 0;
}
/***
* Register a Lua function as key action.
* @function action_register
* @tparam string name the name of the action, can be referred to in key bindings as `<name>` pseudo key
* @tparam Function func the lua function implementing the key action (see @{keyhandler})
* @tparam[opt] string help the single line help text as displayed in `:help`
* @treturn KeyAction action the registered key action
* @see Vis:map
* @see Window:map
*/
static int action_register(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const char *name = luaL_checkstring(L, 2);
const void *func = func_ref_new(L, 3);
const char *help = luaL_optstring(L, 4, NULL);
KeyAction *action = vis_action_new(vis, name, help, keymapping, (Arg){ .v = func });
if (!action)
goto err;
if (!vis_action_register(vis, action))
goto err;
obj_ref_new(L, action, VIS_LUA_TYPE_KEYACTION);
return 1;
err:
vis_action_free(vis, action);
lua_pushnil(L);
return 1;
}
static int keymap(lua_State *L, Vis *vis, Win *win) {
int mode = luaL_checkint(L, 2);
const char *key = luaL_checkstring(L, 3);
const char *help = luaL_optstring(L, 5, NULL);
KeyBinding *binding = vis_binding_new(vis);
if (!binding)
goto err;
if (lua_isstring(L, 4)) {
const char *alias = luaL_checkstring(L, 4);
if (!(binding->alias = strdup(alias)))
goto err;
} else if (lua_isfunction(L, 4)) {
const void *func = func_ref_new(L, 4);
if (!(binding->action = vis_action_new(vis, NULL, help, keymapping, (Arg){ .v = func })))
goto err;
} else if (lua_isuserdata(L, 4)) {
binding->action = obj_ref_check(L, 4, VIS_LUA_TYPE_KEYACTION);
} else {
goto err;
}
if (win) {
if (!vis_window_mode_map(win, mode, true, key, binding))
goto err;
} else {
if (!vis_mode_map(vis, mode, true, key, binding))
goto err;
}
lua_pushboolean(L, true);
return 1;
err:
vis_binding_free(vis, binding);
lua_pushboolean(L, false);
return 1;
}
/***
* Map a key to a Lua function.
*
* Creates a new key mapping in a given mode.
*
* @function map
* @tparam int mode the mode to which the mapping should be added
* @tparam string key the key to map
* @tparam function func the Lua function to handle the key mapping (see @{keyhandler})
* @tparam[opt] string help the single line help text as displayed in `:help`
* @treturn bool whether the mapping was successfully established
* @see Window:map
* @usage
* vis:map(vis.modes.INSERT, "<C-k>", function(keys)
* if #keys < 2 then
* return -1 -- need more input
* end
* local digraph = keys:sub(1, 2)
* if digraph == "l*" then
* vis:feedkeys('λ')
* return 2 -- consume 2 bytes of input
* end
* end, "Insert digraph")
*/
/***
* Setup a key alias.
*
* This is equivalent to `vis:command('map! mode key alias')`.
*
* Mappings are always recursive!
* @function map
* @tparam int mode the mode to which the mapping should be added
* @tparam string key the key to map
* @tparam string alias the key to map to
* @treturn bool whether the mapping was successfully established
* @see Window:map
* @usage
* vis:map(vis.modes.NORMAL, "j", "k")
*/
/***
* Map a key to a key action.
*
* @function map
* @tparam int mode the mode to which the mapping should be added
* @tparam string key the key to map
* @param action the action to map
* @treturn bool whether the mapping was successfully established
* @see Window:map
* @usage
* local action = vis:action_register("info", function()
* vis:info("Mapping works!")
* end, "Info message help text")
* vis:map(vis.modes.NORMAL, "gh", action)
* vis:map(vis.modes.NORMAL, "gl", action)
*/
static int map(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
return keymap(L, vis, NULL);
}
/***
* Unmap a global key binding.
*
* @function unmap
* @tparam int mode the mode from which the mapping should be removed
* @tparam string key the mapping to remove
* @treturn bool whether the mapping was successfully removed
* @see Window:unmap
*/
static int keyunmap(lua_State *L, Vis *vis, Win *win) {
enum VisMode mode = luaL_checkint(L, 2);
const char *key = luaL_checkstring(L, 3);
bool ret;
if (!win)
ret = vis_mode_unmap(vis, mode, key);
else
ret = vis_window_mode_unmap(win, mode, key);
lua_pushboolean(L, ret);
return 1;
}
static int unmap(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
return keyunmap(L, vis, NULL);
}
/***
* Get all currently active mappings of a mode.
*
* @function mappings
* @tparam int mode the mode to query
* @treturn table the active mappings and their associated help texts
* @usage
* local bindings = vis:mappings(vis.modes.NORMAL)
* for key, help in pairs(bindings) do
* -- do something
* end
* @see Vis:map
*/
static bool binding_collect(const char *key, void *value, void *ctx) {
lua_State *L = ctx;
KeyBinding *binding = value;
lua_getfield(L, -1, key);
bool new = lua_isnil(L, -1);
lua_pop(L, 1);
if (new) {
const char *help = binding->alias ? binding->alias : VIS_HELP_USE(binding->action->help);
lua_pushstring(L, help ? help : "");
lua_setfield(L, -2, key);
}
return true;
}
static int mappings(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
lua_newtable(L);
for (Mode *mode = mode_get(vis, luaL_checkint(L, 2)); mode; mode = mode->parent) {
if (!mode->bindings)
continue;
map_iterate(mode->bindings, binding_collect, vis->lua);
}
return 1;
}
/***
* Execute a motion.
*
* @function motion
* @tparam int id the id of the motion to execute
* @treturn bool whether the id was valid
* @local
*/
static int motion(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
enum VisMotion id = luaL_checkunsigned(L, 2);
// TODO handle var args?
lua_pushboolean(L, vis && vis_motion(vis, id));
return 1;
}
static size_t motion_lua(Vis *vis, Win *win, void *data, size_t pos) {
lua_State *L = vis->lua;
if (!L || !func_ref_get(L, data) || !obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW))
return EPOS;
lua_pushunsigned(L, pos);
if (pcall(vis, L, 2, 1) != 0)
return EPOS;
return getpos(L, -1);
}
/***
* Register a custom motion.
*
* @function motion_register
* @tparam function motion the Lua function implementing the motion
* @treturn int the associated motion id, or `-1` on failure
* @see motion, motion_new
* @local
* @usage
* -- custom motion advancing to the next byte
* local id = vis:motion_register(function(win, pos)
* return pos+1
* end)
*/
static int motion_register(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const void *func = func_ref_new(L, 2);
int id = vis_motion_register(vis, (void*)func, motion_lua);
lua_pushinteger(L, id);
return 1;
}
/***
* Execute an operator.
*
* @function operator
* @tparam int id the id of the operator to execute
* @treturn bool whether the id was valid
* @local
*/
static int operator(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
enum VisOperator id = luaL_checkunsigned(L, 2);
// TODO handle var args?
lua_pushboolean(L, vis && vis_operator(vis, id));
return 1;
}
static size_t operator_lua(Vis *vis, Text *text, OperatorContext *c) {
lua_State *L = vis->lua;
if (!L || !func_ref_get(L, c->context))
return EPOS;
File *file = vis->files;
while (file && (file->internal || file->text != text))
file = file->next;
if (!file || !obj_ref_new(L, file, VIS_LUA_TYPE_FILE))
return EPOS;
pushrange(L, &c->range);
pushpos(L, c->pos);
if (pcall(vis, L, 3, 1) != 0)
return EPOS;
return getpos(L, -1);
}
/***
* Register a custom operator.
*
* @function operator_register
* @tparam function operator the Lua function implementing the operator
* @treturn int the associated operator id, or `-1` on failure
* @see operator, operator_new
* @local
* @usage
* -- custom operator replacing every 'a' with 'b'
* local id = vis:operator_register(function(file, range, pos)
* local data = file:content(range)
* data = data:gsub("a", "b")
* file:delete(range)
* file:insert(range.start, data)
* return range.start -- new cursor location
* end)
*/
static int operator_register(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const void *func = func_ref_new(L, 2);
int id = vis_operator_register(vis, operator_lua, (void*)func);
lua_pushinteger(L, id);
return 1;
}
/***
* Execute a text object.
*
* @function textobject
* @tparam int id the id of the text object to execute
* @treturn bool whether the id was valid
* @see textobject_register, textobject_new
* @local
*/
static int textobject(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
enum VisTextObject id = luaL_checkunsigned(L, 2);
lua_pushboolean(L, vis_textobject(vis, id));
return 1;
}
static Filerange textobject_lua(Vis *vis, Win *win, void *data, size_t pos) {
lua_State *L = vis->lua;
if (!L || !func_ref_get(L, data) || !obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW))
return text_range_empty();
lua_pushunsigned(L, pos);