-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlangScriptingForMC.patch
2715 lines (2644 loc) · 86.4 KB
/
SlangScriptingForMC.patch
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
From 2cf267d8a0cfda61fdc94696bbc997c8369a43d1 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Mon, 25 Jan 2021 13:54:14 -0600
Subject: Slang Scripting Support.
---
lib/fileloc.h | 2 +
lib/keybind.c | 140 +++-
lib/keybind.h | 31 +-
lib/mcconfig/paths.c | 3 +
lib/widget/dialog.c | 4 +
lib/widget/wtools.c | 64 ++
lib/widget/wtools.h | 11 +
misc/grow_shrink_integer.plugin.sl | 102 +++
misc/init.sl | 49 ++
src/Makefile.am | 5 +-
src/editor/edit.c | 26 +
src/editor/editbuffer.c | 122 ++++
src/editor/editbuffer.h | 4 +
src/editor/editcmd.c | 58 +-
src/editor/editwidget.c | 1 +
src/main.c | 11 +
src/setup.c | 18 +-
src/slang_api_functions.c | 423 ++++++++++++
src/slang_api_functions.h | 52 ++
src/slang_api_functions_glue.c | 871 ++++++++++++++++++++++++
src/slang_engine.c | 275 ++++++++
src/{vfs/fish/fish.h => slang_engine.h} | 19 +-
22 files changed, 2212 insertions(+), 79 deletions(-)
create mode 100644 misc/grow_shrink_integer.plugin.sl
create mode 100644 misc/init.sl
create mode 100644 src/slang_api_functions.c
create mode 100644 src/slang_api_functions.h
create mode 100644 src/slang_api_functions_glue.c
create mode 100644 src/slang_engine.c
copy src/{vfs/fish/fish.h => slang_engine.h} (72%)
diff --git a/lib/fileloc.h b/lib/fileloc.h
index c7d3bc625..44c3b5efd 100644
--- a/lib/fileloc.h
+++ b/lib/fileloc.h
@@ -30,6 +30,8 @@
#define CHARSETS_LIST "mc.charsets"
#define MC_LIB_EXT "mc.ext"
#define MC_MACRO_FILE "mc.macros"
+#define MC_SLANG_INIT_FILE "init.sl"
+#define MC_PLUGIN_DIR "plugin"
#define FISH_PREFIX "fish"
diff --git a/lib/keybind.c b/lib/keybind.c
index abd44d3e2..4fd8aaed6 100644
--- a/lib/keybind.c
+++ b/lib/keybind.c
@@ -38,6 +38,8 @@
/*** global variables ****************************************************************************/
+int new_dynamic_command_id = 100000;
+
/*** file scope macro definitions ****************************************************************/
#define ADD_KEYMAP_NAME(name) \
@@ -47,7 +49,7 @@
/*** file scope variables ************************************************************************/
-static name_keymap_t command_names[] = {
+static name_keymap_t command_names_start[] = {
/* common */
ADD_KEYMAP_NAME (InsertChar),
ADD_KEYMAP_NAME (Enter),
@@ -375,13 +377,24 @@ static name_keymap_t command_names[] = {
{NULL, CK_IgnoreKey}
};
-/* *INDENT-OFF* */
-static const size_t num_command_names = G_N_ELEMENTS (command_names) - 1;
-/* *INDENT-ON* */
+static name_keymap_t *command_names = NULL;
+
+static size_t num_command_names = 0;
+
+static gboolean has_been_sorted = FALSE;
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
+/* Initializes the dynamic command names array. */
+static void
+init_command_names (void)
+{
+ keybind_add_new_action (NULL, -1);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
static int
name_keymap_comparator (const void *p1, const void *p2)
{
@@ -396,12 +409,14 @@ name_keymap_comparator (const void *p1, const void *p2)
static inline void
sort_command_names (void)
{
- static gboolean has_been_sorted = FALSE;
+ if (!command_names)
+ init_command_names ();
if (!has_been_sorted)
{
qsort (command_names, num_command_names,
sizeof (command_names[0]), &name_keymap_comparator);
+
has_been_sorted = TRUE;
}
}
@@ -409,7 +424,7 @@ sort_command_names (void)
/* --------------------------------------------------------------------------------------------- */
static void
-keymap_add (GArray * keymap, long key, long cmd, const char *caption)
+keymap_add (GArray * keymap, long key, long cmd, const char *caption, key_origin_t origin)
{
if (key != 0 && cmd != CK_IgnoreKey)
{
@@ -417,6 +432,7 @@ keymap_add (GArray * keymap, long key, long cmd, const char *caption)
new_bind.key = key;
new_bind.command = cmd;
+ new_bind.origin = origin;
g_snprintf (new_bind.caption, sizeof (new_bind.caption), "%s", caption);
g_array_append_val (keymap, new_bind);
}
@@ -426,14 +442,96 @@ keymap_add (GArray * keymap, long key, long cmd, const char *caption)
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
+/* Adds a dynamic command name. Can be used to initialize the dynamic
+ command names array by passing NULL as the first parameter. */
+int
+keybind_add_new_action (const char *new_command_name, int new_ck_id)
+{
+ name_keymap_t search_key = { 0 };
+ name_keymap_t *old_command_names = command_names, *res = NULL, *src_commands;
+ int ret = 0, /* Default return code → no command name added */
+ size = 0;
+
+ /* Make a private copy of the command name. */
+ search_key.name = new_command_name = g_strdup (new_command_name);
+
+ /* Count the size of the existing command names array. */
+
+ src_commands = command_names ? command_names : command_names_start;
+ for (; src_commands[size].name; size++);
+
+ /* Initialize the size variable of the command names array. */
+ num_command_names = size;
+
+ /* Check if the key already exists, i.e.: if the command is already registered. */
+ if (search_key.name != NULL && strlen (search_key.name))
+ {
+ res = bsearch (&search_key, src_commands,
+ num_command_names, sizeof (command_names_start[0]), name_keymap_comparator);
+ }
+
+ /*
+ * a) Such command already exists or, b) no command given and the registry is already
+ * initialized (the first bootstrap invocation has been already run) ? If so, then exit doing
+ * no action. Note that the CK ID of the existing command is not updated and remains the same.
+ */
+
+ if (res != NULL || (!new_command_name && old_command_names))
+ {
+ return ret;
+ }
+
+ /* Allocate new space and copy the old keymap names. */
+ command_names = (name_keymap_t *) calloc (size + (new_command_name ? 2 : 1),
+ sizeof (name_keymap_t));
+
+ /* Extending an existing array? */
+ if (old_command_names != NULL)
+ {
+ /* ...yes. Copy from `old_command_names` dynamic array. */
+ memcpy (command_names, old_command_names, (size + 1) * sizeof (name_keymap_t));
+
+ /* Release the previous command names after copying them. */
+ free (old_command_names);
+ }
+ else
+ {
+ /* ...no. Copy the initial, compiled in command names into the dynamic array. */
+ memcpy (command_names, command_names_start, sizeof (command_names_start));
+ }
+
+ /* Add a new keymap if requested (i.e.: if given any non-NULL name). */
+ if (new_command_name != NULL)
+ {
+ /* Insert the new command name at the end, then follow an empty sentinel element. */
+ command_names[size].name = new_command_name;
+ command_names[size].val = new_ck_id;
+ command_names[size + 1].name = NULL;
+ command_names[size + 1].val = CK_IgnoreKey;
+
+ /* Increase the command name count. */
+ num_command_names++;
+
+ /* Sort the new array. */
+ has_been_sorted = FALSE;
+ sort_command_names ();
+
+ /* Return that a new command name has been registered. */
+ ret = 1;
+ }
+ return ret;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
void
-keybind_cmd_bind (GArray * keymap, const char *keybind, long action)
+keybind_cmd_bind (GArray * keymap, const char *keybind, long action, key_origin_t origin)
{
char *caption = NULL;
long key;
key = lookup_key (keybind, &caption);
- keymap_add (keymap, key, action, caption);
+ keymap_add (keymap, key, action, caption, origin);
g_free (caption);
}
@@ -459,12 +557,34 @@ const char *
keybind_lookup_actionname (long action)
{
size_t i;
+ const char *name = NULL;
for (i = 0; command_names[i].name != NULL; i++)
if (command_names[i].val == action)
- return command_names[i].name;
+ {
+ name = command_names[i].name;
+ break;
+ }
- return NULL;
+
+ return name;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* Finds and returns the ·origin· field of the key bound to the given CK action. */
+key_origin_t
+keybind_lookup_keymap_origin (const global_keymap_t * keymap, long action)
+{
+ if (keymap != NULL)
+ {
+ size_t i;
+
+ for (i = 0; keymap[i].key != 0; i++)
+ if (keymap[i].command == action)
+ return keymap[i].origin;
+ }
+ return ORIGIN_UNKNOWN;
}
/* --------------------------------------------------------------------------------------------- */
diff --git a/lib/keybind.h b/lib/keybind.h
index af019df09..a3f1cd268 100644
--- a/lib/keybind.h
+++ b/lib/keybind.h
@@ -346,6 +346,30 @@ enum
CK_MergeOther
};
+/*
+ * The values of the key binding «origin» indicator field in the global_keymap_t entry for the
+ * binding. It informs about from where the key binding comes from (the binary, a config file,
+ * a slang script / plugin).
+ */
+
+typedef enum
+{
+ /*
+ * A special, error value used when the keymap entry for the given action cannot be found,
+ * when looking up origin field by the CK action code.
+ */
+ ORIGIN_UNKNOWN = -1,
+
+ /*
+ * Assign 0, so that omision of the field in the global_keymap_t initializer will lead to this.
+ * Meaning: the `mc` binary is the origin of the keybinding.
+ */
+ ORIGIN_COMPILE_TIME = 0,
+
+ ORIGIN_FILE, /* The binding comes from a file. */
+ ORIGIN_SLANG_SCRIPT /* The binding comes from S-Lang script execution (e.g.: a plugin) */
+} key_origin_t;
+
/*** structures declarations (and typedefs of structures)*****************************************/
typedef struct name_keymap_t
@@ -368,15 +392,20 @@ typedef struct global_keymap_t
long key;
long command;
char caption[KEYMAP_SHORTCUT_LENGTH];
+ key_origin_t origin;
} global_keymap_t;
/*** global variables defined in .c file *********************************************************/
+extern int new_dynamic_command_id;
+
/*** declarations of public functions ************************************************************/
-void keybind_cmd_bind (GArray * keymap, const char *keybind, long action);
+int keybind_add_new_action (const char *new_command_name, int new_ck_id);
+void keybind_cmd_bind (GArray * keymap, const char *keybind, long action, key_origin_t origin);
long keybind_lookup_action (const char *name);
const char *keybind_lookup_actionname (long action);
+key_origin_t keybind_lookup_keymap_origin (const global_keymap_t * keymap, long action);
const char *keybind_lookup_keymap_shortcut (const global_keymap_t * keymap, long action);
long keybind_lookup_keymap_command (const global_keymap_t * keymap, long key);
diff --git a/lib/mcconfig/paths.c b/lib/mcconfig/paths.c
index e111dc7a2..33ca890b4 100644
--- a/lib/mcconfig/paths.c
+++ b/lib/mcconfig/paths.c
@@ -72,6 +72,9 @@ static const struct
{ "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU },
{ "panels.ini", &mc_config_str, MC_PANELS_FILE },
+ /* Plugins (S-Lang scripting). */
+ { "plugin", &mc_config_str, MC_PLUGIN_DIR },
+
/* User should move this file with applying some changes in file */
{ "", &mc_config_str, MC_FILEBIND_FILE },
diff --git a/lib/widget/dialog.c b/lib/widget/dialog.c
index b8a08f029..2aca8f8a0 100644
--- a/lib/widget/dialog.c
+++ b/lib/widget/dialog.c
@@ -313,6 +313,10 @@ frontend_dlg_run (WDialog * h)
widget_update_cursor (wh);
+ /* Emit any postponed message boxes. */
+ if (are_postponed_messages ())
+ display_postponed_messages ();
+
/* Clear interrupt flag */
tty_got_interrupt ();
d_key = tty_get_event (&event, GROUP (h)->mouse_status == MOU_REPEAT, TRUE);
diff --git a/lib/widget/wtools.c b/lib/widget/wtools.c
index 5c5dc4487..d1104d8bc 100644
--- a/lib/widget/wtools.c
+++ b/lib/widget/wtools.c
@@ -50,8 +50,17 @@
/*** file scope type declarations ****************************************************************/
+typedef struct postponed_msg_s
+{
+ int flags;
+ char *title;
+ char *text;
+} postponed_msg_t;
+
/*** file scope variables ************************************************************************/
+static GSList *postponed_msgs = NULL;
+
static WDialog *last_query_dlg;
static int sel_pos = 0;
@@ -268,6 +277,61 @@ wtools_parent_call_string (void *routine, int argc, ...)
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
+/* Return truth if there are some queued postponed messages. */
+gboolean
+are_postponed_messages (void)
+{
+ return postponed_msgs != NULL;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+void
+postponed_message (int flags_, const char *title, const char *text, ...)
+{
+ va_list args;
+
+ /* Initialize a heap allocated, message container struct. */
+ postponed_msg_t *msg_heap;
+ msg_heap = g_new (postponed_msg_t, 1);
+
+ va_start (args, text);
+
+ *msg_heap = (postponed_msg_t)
+ {
+ flags_, g_strdup (title), g_strdup_vprintf (text, args)};
+
+ va_end (args);
+
+ /* Append the heap pointer to the GSList. */
+ postponed_msgs = g_slist_append (postponed_msgs, msg_heap);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+void
+display_postponed_messages ()
+{
+ GSList *it;
+
+ for (it = postponed_msgs; it != NULL; it = it->next)
+ {
+ postponed_msg_t *msg = (postponed_msg_t *) it->data;
+
+ /* Show the dialog with the saved message body and flags. */
+ message (msg->flags, msg->title, msg->text);
+
+ /* Release the fields of the struct. */
+ g_free (msg->title);
+ g_free (msg->text);
+ }
+
+ /* Release whole list and also call g_free on each element. */
+ g_slist_free_full (g_steal_pointer (&postponed_msgs), g_free);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
/** Used to ask questions to the user */
int
query_dialog (const char *header, const char *text, int flags, int count, ...)
diff --git a/lib/widget/wtools.h b/lib/widget/wtools.h
index cd0bc3253..b1f7a9b47 100644
--- a/lib/widget/wtools.h
+++ b/lib/widget/wtools.h
@@ -61,6 +61,17 @@ struct simple_status_msg_t
/*** declarations of public functions ************************************************************/
+/*
+ * A simple message queue that postpones the messages until MC enters the main loop. It allows to
+ * use dialog messages early during the initialization, e.g.: to signal problems during it. Message
+ * will appear when UI is fully initialized and drawn.
+ */
+
+gboolean are_postponed_messages (void);
+void postponed_message (int flags, const char *title, const char *text, ...)
+ __attribute__ ((format (printf, 3, 4)));
+void display_postponed_messages (void);
+
/* The input dialogs */
char *input_dialog (const char *header, const char *text,
const char *history_name, const char *def_text,
diff --git a/misc/grow_shrink_integer.plugin.sl b/misc/grow_shrink_integer.plugin.sl
new file mode 100644
index 000000000..ad91db04e
--- /dev/null
+++ b/misc/grow_shrink_integer.plugin.sl
@@ -0,0 +1,102 @@
+% Copy this file to ~/.config/mc/plugin/, it'll be automatically loaded at startup.
+
+define grow_shrink_int__get_current_or_next_number() {
+ variable pos = -1, bol_offset, eol_offset, start_digit_offset,
+ t = "", found, start_digit_seen, c1, c2;
+
+ bol_offset = mc->cure_get_bol ();
+ eol_offset = mc->cure_get_eol ();
+
+ % Find the start of the number (a word, actually)
+ for (pos = mc->cure_cursor_offset(); pos >= bol_offset; pos--)
+ {
+ c1 = mc->cure_get_byte (pos);
+ c2 = mc->cure_get_byte (pos - 1);
+
+ if (not isdigit(c1))
+ break;
+ if (not isspace (c1) && isspace (c2))
+ break;
+ }
+
+ % Find the end of the number (a word, actually)
+ found=0;
+ start_digit_offset=0;
+ start_digit_seen=0;
+ for (; pos <= eol_offset; pos++)
+ {
+ c1 = mc->cure_get_byte (pos);
+ c2 = mc->cure_get_byte (pos + 1);
+
+ % Append the byte to the string
+ if (isdigit (c1)) {
+ if (not start_digit_seen) {
+ start_digit_seen=1;
+ start_digit_offset=pos;
+ }
+ found = 1; t += char(c1);
+ }
+
+ if (isdigit (c1) && not isdigit (c2)) {
+ found += 1;
+ break;
+ }
+ }
+
+ % Any number found?
+ % If not, return an empty string and minus one position
+ if (found == 2) {
+ % Include any minus sign
+ c1 = mc->cure_get_byte (start_digit_offset-1);
+ if (c1 == '-')
+ t = char(c1) + t;
+ }
+ return t, pos;
+}
+
+% A backend function for the two next public functions
+define grow_shrink_int__increment(direction) {
+ variable pos, cpos, number = 0;
+ variable number_str, new_number_buf;
+ variable number_len = 0, new_number_len = 0, idx;
+
+ % Get the number
+ (number_str, pos) = grow_shrink_int__get_current_or_next_number();
+ % Has been a number found?
+ if (strlen(number_str) > 0) {
+ number_len = strlen(number_str);
+ % Convert the string into integer to increment it
+ number = atoll(number_str);
+ number += (direction > 0) ? 1 : -1;
+ new_number_buf = string(number);
+ new_number_len = strlen(new_number_buf);
+
+ % Move the cursor to the found number
+ cpos = mc->cure_cursor_offset();
+ mc->cure_cursor_move (pos-cpos);
+ % Delete the existing number
+ mc->cure_delete();
+ for (idx = 0; idx < number_len-1; idx ++)
+ mc->cure_backspace();
+ % Insert updated number
+ for (idx = 0; idx < new_number_len; idx ++)
+ mc->cure_insert_ahead(new_number_buf[new_number_len-idx-1]);
+ }
+
+ % Return the updated number. Just for fun :) Maybe it'll find some use one day
+ return number;
+}
+
+% The end user function for incrementing an integer
+define grow_shrink_int__grow_int() {
+ return grow_shrink_int__increment(1);
+}
+
+% The end user function for taking 1 from an integer
+define grow_shrink_int__shrink_int() {
+ return grow_shrink_int__increment(-1);
+}
+
+% Register the default key bindings – Alt-a for grow, Alt-x for shrink.
+mc->editor_map_key_to_func("GrowInteger", "alt-a", "grow_shrink_int__grow_int");
+mc->editor_map_key_to_func("ShrinkInteger", "alt-x", "grow_shrink_int__shrink_int");
diff --git a/misc/init.sl b/misc/init.sl
new file mode 100644
index 000000000..9f77dcfa5
--- /dev/null
+++ b/misc/init.sl
@@ -0,0 +1,49 @@
+% MCEdit Startup Script in S-Lang Scripting Language
+%
+% See:
+% – https://www.jedsoft.org/slang/doc/html/slang.html,
+% for a reference on the language.
+
+% Increase to 2 to have confirmation messages after loading this script and other plugins.
+mc_loglevel=1;
+
+% A function showing a listbox:
+define listbox_display_function() {
+ variable items = ["This is a listbox", "It's displayed from…",
+ "…`init.sl` S-Lang startup script"];
+
+ variable sel = mc->listbox(5, 35, "Welcome",items);
+
+ if (sel >= 0)
+ mc->message("You have selected:", "item #" + string(sel+1) + ": " + items[sel]);
+ else
+ mc->message("Info", "No selection have been made");
+
+ return 1;
+}
+
+% A function causing runtime error:
+define divide_by_zero() {
+ variable string = "Divided by 0 on purpose";
+ variable mc = 1 / 0;
+}
+
+% A function to present backtrace functionality:
+define b_function() {
+ variable tmp_variable = 5;
+ divide_by_zero();
+ return 0;
+}
+
+% A function to present backtrace functionality:
+define a_function() {
+ b_function();
+ return 0;
+}
+
+
+% Show an error message on Ctrl-t:
+mc->editor_map_key_to_func("DivBy0Action", "ctrl-t", "a_function");
+
+% Show a listbox on Alt-y:
+mc->editor_map_key_to_func("ListboxAction", "alt-y", "listbox_display_function");
diff --git a/src/Makefile.am b/src/Makefile.am
index e883ab4bd..3f8100352 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -80,7 +80,10 @@ libinternal_la_SOURCES = \
setup.c setup.h \
textconf.c textconf.h \
usermenu.c usermenu.h \
- util.c util.h
+ util.c util.h \
+ slang_engine.c slang_engine.h \
+ slang_api_functions.c \
+ slang_api_functions_glue.c
if CHARSET
libinternal_la_SOURCES += selcodepage.c selcodepage.h
diff --git a/src/editor/edit.c b/src/editor/edit.c
index edda1f832..53b9c059c 100644
--- a/src/editor/edit.c
+++ b/src/editor/edit.c
@@ -42,6 +42,7 @@
#include <sys/stat.h>
#include <stdint.h> /* UINTMAX_MAX */
#include <stdlib.h>
+#include <slang.h>
#include "lib/global.h"
@@ -65,6 +66,7 @@
#include "src/setup.h" /* option_tab_spacing */
#include "src/keybind-defaults.h"
+#include "src/slang_engine.h"
#include "edit-impl.h"
#include "editwidget.h"
@@ -3248,7 +3250,31 @@ void
edit_execute_cmd (WEdit * edit, long command, int char_for_insertion)
{
Widget *w = WIDGET (edit);
+ GSList *slang_code = NULL;
+ /* Check if the command is a S-Lang script registered command */
+ if ((slang_code = get_command_callback (command)) != NULL)
+ {
+ int ret_api, ret_api2 = -1, ret_fun = 0;
+ ret_api = SLang_execute_function (slang_code->data);
+ if (ret_api == 0)
+ message (D_ERROR, "S-Lang plugin error", "Function doesn't exist.");
+ else if (ret_api < 0 || -1 == (ret_api2 = SLang_pop_int (&ret_fun)))
+ {
+ /*
+ * If the function returned 1, then edit_execute_cmd() will continue, otherwise it will
+ * exit. This feature can be used to tap into build in commands and moderate/replace
+ * their execution.
+ */
+ if (SLang_get_error ())
+ {
+ SLang_restart (1);
+ SLang_set_error (0);
+ }
+ }
+ if (ret_api <= 0 || ret_api2 <= 0 || !ret_fun)
+ return;
+ }
if (command == CK_WindowFullscreen)
{
edit_toggle_fullscreen (edit);
diff --git a/src/editor/editbuffer.c b/src/editor/editbuffer.c
index 101918bef..d3f78271f 100644
--- a/src/editor/editbuffer.c
+++ b/src/editor/editbuffer.c
@@ -33,6 +33,7 @@
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
#include <sys/types.h>
#include "lib/global.h"
@@ -384,6 +385,127 @@ edit_buffer_get_eol (const edit_buffer_t * buf, off_t current)
return current;
}
+/* --------------------------------------------------------------------------------------------- */
+/**
+ * Find first character of current word. If jump_spaces is TRUE, then any whitespace gap on the
+ left is being ignored (i.e.: jumped across for the preceding word). */
+
+gboolean
+edit_buffer_find_word_start (const edit_buffer_t * buf, gboolean jump_spaces, off_t * word_start,
+ gsize * word_len)
+{
+ int c, spc_count = 0;
+ off_t i = 1;
+ gboolean have_ending_char = FALSE;
+
+ /* STARTUP: Initialize the output variables to a reasonable initial values. */
+ *word_start = buf->curs1;
+ *word_len = (gsize) 0;
+
+ /* Return nothing if at begin of file. */
+ if (buf->curs1 <= 0)
+ return FALSE;
+
+ /* A first peek at the first preceding char. */
+ c = edit_buffer_get_previous_byte (buf);
+ /* Return if the word is empty (i.e.: the char is a space and we're not jumping over them). */
+ if ((!jump_spaces && isspace (c)) || c == '\n')
+ return FALSE;
+
+ /* Skip any whitespace. */
+ if (jump_spaces && isspace (c))
+ {
+ spc_count++;
+ /* Jump over all whitespace on the left. Initial i++ means: a skip of the first space (move
+ * on to processing next char before it). */
+ for (i++; buf->curs1 - i >= 0; i++)
+ {
+ c = edit_buffer_get_byte (buf, buf->curs1 - i);
+ if (!isspace (c))
+ {
+ break;
+ }
+ else if (c == '\n')
+ /* Preliminary new line → return nothing. This function works in current line. */
+ return FALSE;
+ spc_count++;
+ }
+ }
+
+ /* Whitespace till the beginning of the buffer? */
+ if (isspace (c))
+ return FALSE;
+
+ /* Accept this (either initial or one found after skipping spaces) char and move on, if
+ * there are any left chars in buffer. */
+ i++;
+
+ /*
+ * Word boundary char right after optional spaces → accept only it.
+ * This means that a single word boundary char constitutes a word of length 1, either
+ * after spaces (skipped above ↑) or immediately before cursor.
+ */
+
+ if (is_break_char (c))
+ /* i variable points to the single char, skip iterating more. */
+ have_ending_char = TRUE;
+
+ /* Search start of word to be completed. */
+ for (; !have_ending_char && buf->curs1 - i >= 0; i++)
+ {
+ c = edit_buffer_get_byte (buf, buf->curs1 - i);
+
+ if (is_break_char (c))
+ /*
+ * The isdigit() condition here was too specific for this general function (it was
+ * excluding any words starting with a digit).
+ */
+ break;
+ }
+
+ /* Success – a word has been properly found and delimited. */
+ *word_start = buf->curs1 - i + 1; /* Save start found to result variable */
+ *word_len = (gsize) i - 1 - spc_count; /* …and word length */
+
+ return TRUE;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* Gets the word on the left of the cursor.
+ *
+ * @param buf The edit buffer.
+ * @param jump_spaces Should any whitespace gap be jumped over? If not, it'll cause empty result.
+ * @param initial Initial contents of the result.
+ * @param release_on_empty Should the initial g_string be released when returning NULL.
+ * @return g_string with the word or NULL if the word is empty.
+ */
+GString *
+edit_buffer_get_left_whole_word (const edit_buffer_t * buf, gboolean jump_spaces, GString * initial,
+ gboolean release_on_empty)
+{
+ GString *ret = initial;
+ gsize i, word_len = 0;
+ off_t word_start = 0;
+
+ /* Search start of word left of cursor. */
+ if (!edit_buffer_find_word_start (buf, jump_spaces, &word_start, &word_len))
+ {
+ if (initial && release_on_empty)
+ g_string_free (initial, TRUE);
+ return NULL;
+ }
+
+ /* ret = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
+ if (!ret)
+ ret = g_string_sized_new (32);
+
+ for (i = 0; i < word_len; i++)
+ g_string_append_c (ret, edit_buffer_get_byte (buf, word_start + i));
+
+ return ret;
+}
+
/* --------------------------------------------------------------------------------------------- */
/**
* Get word from specified offset.
diff --git a/src/editor/editbuffer.h b/src/editor/editbuffer.h
index 9d65e71eb..33dd8f548 100644
--- a/src/editor/editbuffer.h
+++ b/src/editor/editbuffer.h
@@ -46,6 +46,10 @@ int edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int *
long edit_buffer_count_lines (const edit_buffer_t * buf, off_t first, off_t last);
off_t edit_buffer_get_bol (const edit_buffer_t * buf, off_t current);
off_t edit_buffer_get_eol (const edit_buffer_t * buf, off_t current);
+gboolean edit_buffer_find_word_start (const edit_buffer_t * buf, gboolean jump_spaces,
+ off_t * word_start, gsize * word_len);
+GString *edit_buffer_get_left_whole_word (const edit_buffer_t * buf, gboolean jump_spaces,
+ GString * initial, gboolean release_on_empty);
GString *edit_buffer_get_word_from_pos (const edit_buffer_t * buf, off_t start_pos, off_t * start,
gsize * cut);
diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c
index 0d2caa923..6ec993e4d 100644
--- a/src/editor/editcmd.c
+++ b/src/editor/editcmd.c
@@ -1127,49 +1127,6 @@ pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
}
}
-/* --------------------------------------------------------------------------------------------- */
-/** find first character of current word */
-
-static gboolean
-edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
-{
- int c;
- off_t i;
-
- /* return if at begin of file */
- if (buf->curs1 <= 0)
- return FALSE;
-
- c = edit_buffer_get_previous_byte (buf);
- /* return if not at end or in word */
- if (is_break_char (c))
- return FALSE;
-
- /* search start of word to be completed */
- for (i = 1;; i++)
- {
- int last;
-
- last = c;
- c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
-
- if (is_break_char (c))
- {
- /* return if word starts with digit */
- if (isdigit (last))
- return FALSE;
-
- break;
- }
- }
-
- /* success */
- *word_start = buf->curs1 - i; /* start found */
- *word_len = (gsize) i;
-
- return TRUE;
-}
-
/* --------------------------------------------------------------------------------------------- */
/**
* Get current word under cursor
@@ -3340,7 +3297,7 @@ edit_complete_word_cmd (WEdit * edit)
GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
/* search start of word to be completed */
- if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
+ if (!edit_buffer_find_word_start (&edit->buffer, FALSE, &word_start, &word_len))
return;
/* prepare match expression */
@@ -3508,7 +3465,6 @@ edit_get_match_keyword_cmd (WEdit * edit)
gsize word_len = 0, max_len = 0;
int num_def = 0;
gsize i;
- off_t word_start = 0;
GString *match_expr;
char *path = NULL;
char *ptr = NULL;
@@ -3519,15 +3475,10 @@ edit_get_match_keyword_cmd (WEdit * edit)
for (i = 0; i < MAX_DEFINITIONS; i++)
def_hash[i].filename = NULL;
- /* search start of word to be completed */
- if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
- return;
-
/* prepare match expression */
- match_expr = g_string_sized_new (word_len);
- for (i = 0; i < word_len; i++)
- g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
-
+ match_expr = edit_buffer_get_left_whole_word (&edit->buffer, TRUE, NULL, FALSE);
+ if (match_expr == NULL)
+ return;
ptr = g_get_current_dir ();
path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
g_free (ptr);
@@ -3554,7 +3505,6 @@ edit_get_match_keyword_cmd (WEdit * edit)
g_free (path);
max_len = MAX_WIDTH_DEF_DIALOG;
- word_len = 0;
if (num_def > 0)
editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
(etags_hash_t *) & def_hash, num_def);
diff --git a/src/editor/editwidget.c b/src/editor/editwidget.c
index 18ac00e66..5f553a8cb 100644
--- a/src/editor/editwidget.c
+++ b/src/editor/editwidget.c
@@ -61,6 +61,7 @@
#include "src/filemanager/cmd.h" /* save_setup_cmd() */
#include "src/learn.h" /* learn_keys() */
#include "src/args.h" /* mcedit_arg_t */
+#include "src/slang_engine.h"
#include "edit-impl.h"
#include "editwidget.h"
diff --git a/src/main.c b/src/main.c
index d691bcb2a..dd8b0b7ea 100644
--- a/src/main.c
+++ b/src/main.c
@@ -62,6 +62,7 @@
#include "filemanager/ext.h" /* flush_extension_file() */
#include "filemanager/command.h" /* cmdline */
#include "filemanager/panel.h" /* panalized_panel */
+#include "editor/editwidget.h"
#include "vfs/plugins_init.h"
@@ -77,6 +78,7 @@
#include "selcodepage.h"
#endif /* HAVE_CHARSET */
+#include "slang_engine.h"
#include "consaver/cons.saver.h" /* cons_saver_pid */
/*** global variables ****************************************************************************/
@@ -456,6 +458,15 @@ main (int argc, char *argv[])
}
}
+ /* Initialize the S-Lang interpreter and load `init.sl` file. */
+ slang_init_engine ();
+
+ /*
+ * Load the plugins before the setup so that the commands that are being
+ * created in them can be bound with a key.
+ */
+ slang_plugins_init ();
+
/* Program main loop */
if (mc_global.midnight_shutdown)
exit_code = EXIT_SUCCESS;
diff --git a/src/setup.c b/src/setup.c
index 77c07649d..fce4f18a9 100644
--- a/src/setup.c
+++ b/src/setup.c
@@ -784,12 +784,28 @@ load_keymap_from_section (const char *section_name, GArray * keymap, mc_config_t
long action;