-
Notifications
You must be signed in to change notification settings - Fork 53
/
editor.c
2705 lines (2485 loc) · 104 KB
/
editor.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
#include <unistd.h>
#include <signal.h>
#include <uthash.h>
#include <utlist.h>
#include <inttypes.h>
#include <fnmatch.h>
#define TB_IMPL
#include "termbox2.h"
#undef TB_IMPL
#include "mle.h"
#include "mlbuf.h"
static int _editor_set_macro_toggle_key(editor_t *editor, char *key);
static int _editor_bview_exists(editor_t *editor, bview_t *bview);
static int _editor_register_cmd_fn(editor_t *editor, char *name, int (*func)(cmd_context_t *ctx));
static int _editor_should_skip_rc(int argc, char **argv);
static int _editor_close_bview_inner(editor_t *editor, bview_t *bview, int *optret_num_closed);
static int _editor_destroy_cmd(editor_t *editor, cmd_t *cmd);
static int _editor_prompt_input_submit(cmd_context_t *ctx);
static int _editor_prompt_input_complete(cmd_context_t *ctx);
static prompt_history_t *_editor_prompt_find_or_add_history(cmd_context_t *ctx, prompt_hnode_t **optret_prompt_hnode);
static int _editor_prompt_history_up(cmd_context_t *ctx);
static int _editor_prompt_history_down(cmd_context_t *ctx);
static int _editor_prompt_history_append(cmd_context_t *ctx, char *data);
static int _editor_prompt_yna_all(cmd_context_t *ctx);
static int _editor_prompt_yn_yes(cmd_context_t *ctx);
static int _editor_prompt_yn_no(cmd_context_t *ctx);
static int _editor_prompt_cancel(cmd_context_t *ctx);
static int _editor_menu_submit(cmd_context_t *ctx);
static int _editor_menu_cancel(cmd_context_t *ctx);
static int _editor_prompt_isearch_next(cmd_context_t *ctx);
static int _editor_prompt_isearch_prev(cmd_context_t *ctx);
static int _editor_prompt_isearch_viewport_up(cmd_context_t *ctx);
static int _editor_prompt_isearch_viewport_down(cmd_context_t *ctx);
static int _editor_prompt_isearch_drop_cursors(cmd_context_t *ctx);
static void _editor_loop(editor_t *editor, loop_context_t *loop_ctx);
static int _editor_debug_key_input(void);
static void _editor_refresh_cmd_context(editor_t *editor, cmd_context_t *cmd_ctx);
static void _editor_notify_cmd_observers(cmd_context_t *ctx, int is_before);
static int _editor_maybe_toggle_macro(editor_t *editor, kinput_t *input);
static void _editor_resize(editor_t *editor, int w, int h);
static void _editor_maybe_lift_temp_anchors(cmd_context_t *ctx);
static void _editor_draw_cursors(editor_t *editor, bview_t *bview);
static void _editor_get_user_input(editor_t *editor, cmd_context_t *ctx);
static void _editor_ingest_paste(editor_t *editor, cmd_context_t *ctx);
static void _editor_handle_mouse(editor_t *editor, tb_event_t *ev);
static void _editor_append_pastebuf(editor_t *editor, cmd_context_t *ctx, kinput_t *input);
static void _editor_record_macro_input(kmacro_t *macro, kinput_t *input);
static cmd_t *_editor_get_command(editor_t *editor, cmd_context_t *ctx, kinput_t *opt_paste_input);
static kbinding_t *_editor_get_kbinding_node(kbinding_t *node, kinput_t *input, cmd_context_t *ctx, int is_paste, int *ret_is_numeric);
static cmd_t *_editor_resolve_cmd(editor_t *editor, cmd_t **rcmd, char *cmd_name);
static int _editor_key_to_input(char *key, kinput_t *ret_input);
static int _editor_event_to_key(struct tb_event *ev, char *ret_key);
static void _editor_init_signal_handlers(editor_t *editor);
static void _editor_continue(int signum);
static void _editor_graceful_exit(int signum);
static void _editor_register_cmds(editor_t *editor);
static void _editor_init_kmaps(editor_t *editor);
static void _editor_init_kmap(editor_t *editor, kmap_t **ret_kmap, char *name, char *default_cmd_name, int allow_fallthru, kbinding_def_t *defs);
static void _editor_init_kmap_add_binding(editor_t *editor, kmap_t *kmap, kbinding_def_t *binding_def);
static int _editor_init_kmap_add_binding_to_trie(kbinding_t **trie, char *cmd_name, char *cur_key_patt, char *full_key_patt, char *static_param);
static int _editor_init_kmap_by_str(editor_t *editor, kmap_t **ret_kmap, char *str);
static int _editor_init_kmap_add_binding_by_str(editor_t *editor, kmap_t *kmap, char *str);
static void _editor_destroy_kmap(kmap_t *kmap, kbinding_t *trie);
static int _editor_add_macro_by_str(editor_t *editor, char *str);
static void _editor_init_syntaxes(editor_t *editor);
static void _editor_init_syntax(editor_t *editor, syntax_t **optret_syntax, char *name, char *path_pattern, int tab_width, int tab_to_space, srule_def_t *defs);
static int _editor_init_syntax_by_str(editor_t *editor, syntax_t **ret_syntax, char *str);
static int _editor_init_syntax_add_rule(syntax_t *syntax, srule_def_t *def);
static int _editor_init_syntax_add_rule_by_str(syntax_t *syntax, char *str);
static void _editor_destroy_syntax_map(syntax_t *map);
static int _editor_init_from_rc_read(editor_t *editor, FILE *rc, char **ret_rc_data, size_t *ret_rc_data_len);
static int _editor_init_from_rc_exec(editor_t *editor, char *rc_path, char **ret_rc_data, size_t *ret_rc_data_len);
static int _editor_init_from_rc(editor_t *editor, FILE *rc, char *rc_path);
static int _editor_init_from_args(editor_t *editor, int argc, char **argv);
static void _editor_init_status(editor_t *editor);
static void _editor_init_bviews(editor_t *editor, int argc, char **argv);
static int _editor_init_headless_mode(editor_t *editor);
static int _editor_init_startup_macro(editor_t *editor);
static int _editor_init_term(editor_t *editor);
static void _editor_display_keys(editor_t *editor);
static void _editor_remember_keys(editor_t *editor, kinput_t *input);
// Init editor from args
int editor_init(editor_t *editor, int argc, char **argv) {
int rv;
FILE *rc;
char *home_rc;
rv = MLE_OK;
do {
// Create a resuable PCRE2 match data block. This is hacky / lazy. PCRE
// is used all over the place, even in places where there's no easy way
// to get at a shared state, e.g., mark.c. Also feels wasteful to keep
// reallocating this thing, so let's just create one with 10 match
// slots which is the most we ever use. Free in editor_deinit.
pcre2_md = pcre2_match_data_create(10, NULL);
// Set editor defaults
editor->is_in_init = 1;
editor->tab_width = MLE_DEFAULT_TAB_WIDTH;
editor->tab_to_space = MLE_DEFAULT_TAB_TO_SPACE;
editor->trim_paste = MLE_DEFAULT_TRIM_PASTE;
editor->auto_indent = MLE_DEFAULT_AUTO_INDENT;
editor->highlight_bracket_pairs = MLE_DEFAULT_HILI_BRACKET_PAIRS;
editor->read_rc_file = MLE_DEFAULT_READ_RC_FILE;
editor->soft_wrap = MLE_DEFAULT_SOFT_WRAP;
editor->coarse_undo = MLE_DEFAULT_COARSE_UNDO;
editor->viewport_scope_x = -4;
editor->viewport_scope_y = -1;
editor->color_col = -1;
editor->exit_code = EXIT_SUCCESS;
editor->headless_mode = isatty(STDIN_FILENO) == 0 ? 1 : 0;
_editor_set_macro_toggle_key(editor, MLE_DEFAULT_MACRO_TOGGLE_KEY);
// Init signal handlers
_editor_init_signal_handlers(editor);
// Register commands
_editor_register_cmds(editor);
// Init kmaps
_editor_init_kmaps(editor);
// Init syntaxes
_editor_init_syntaxes(editor);
// Parse rc files
if (!_editor_should_skip_rc(argc, argv)) {
home_rc = NULL;
if (getenv("HOME")) {
asprintf(&home_rc, "%s/%s", getenv("HOME"), ".mlerc");
if (util_is_file(home_rc, "rb", &rc)) {
rv = _editor_init_from_rc(editor, rc, home_rc);
fclose(rc);
}
free(home_rc);
}
if (rv != MLE_OK) break;
if (util_is_file("/etc/mlerc", "rb", &rc)) {
rv = _editor_init_from_rc(editor, rc, "/etc/mlerc");
fclose(rc);
}
if (rv != MLE_OK) break;
}
// Parse cli args
rv = _editor_init_from_args(editor, argc, argv);
if (rv != MLE_OK) break;
// Init status bar
_editor_init_status(editor);
// Init bviews
_editor_init_bviews(editor, argc, argv);
// Init startup macro
_editor_init_headless_mode(editor);
// Init headless mode
_editor_init_startup_macro(editor);
// Init terminal
rv = _editor_init_term(editor);
if (rv != MLE_OK) break;
} while(0);
editor->is_in_init = 0;
return rv;
}
// Run editor
int editor_run(editor_t *editor) {
loop_context_t loop_ctx;
memset(&loop_ctx, 0, sizeof(loop_context_t));
_editor_resize(editor, -1, -1);
if (editor->debug_key_input) {
return _editor_debug_key_input();
}
_editor_loop(editor, &loop_ctx);
if (editor->headless_mode && editor->active_edit) {
buffer_write_to_fd(editor->active_edit->buffer, STDOUT_FILENO, NULL);
}
if (editor->debug_dump_state_on_exit) {
editor_debug_dump(editor, stderr);
}
return MLE_OK;
}
// Deinit editor
int editor_deinit(editor_t *editor) {
bview_t *bview;
bview_t *bview_tmp1;
bview_t *bview_tmp2;
kmap_t *kmap;
kmap_t *kmap_tmp;
kmacro_t *macro;
kmacro_t *macro_tmp;
cmd_t *cmd;
cmd_t *cmd_tmp;
prompt_history_t *prompt_history;
prompt_history_t *prompt_history_tmp;
prompt_hnode_t *prompt_hnode;
prompt_hnode_t *prompt_hnode_tmp1;
prompt_hnode_t *prompt_hnode_tmp2;
observer_t *observer;
observer_t *observer_tmp;
if (editor->status) bview_destroy(editor->status);
CDL_FOREACH_SAFE2(editor->all_bviews, bview, bview_tmp1, bview_tmp2, all_prev, all_next) {
CDL_DELETE2(editor->all_bviews, bview, all_prev, all_next);
bview_destroy(bview);
}
HASH_ITER(hh, editor->kmap_map, kmap, kmap_tmp) {
HASH_DEL(editor->kmap_map, kmap);
_editor_destroy_kmap(kmap, kmap->bindings->children);
if (kmap->default_cmd_name) free(kmap->default_cmd_name);
free(kmap->bindings);
free(kmap->name);
free(kmap);
}
HASH_ITER(hh, editor->macro_map, macro, macro_tmp) {
HASH_DEL(editor->macro_map, macro);
if (macro->inputs) free(macro->inputs);
if (macro->name) free(macro->name);
free(macro);
}
HASH_ITER(hh, editor->cmd_map, cmd, cmd_tmp) {
HASH_DEL(editor->cmd_map, cmd);
_editor_destroy_cmd(editor, cmd);
}
HASH_ITER(hh, editor->prompt_history, prompt_history, prompt_history_tmp) {
HASH_DEL(editor->prompt_history, prompt_history);
free(prompt_history->prompt_str);
CDL_FOREACH_SAFE(prompt_history->prompt_hlist, prompt_hnode, prompt_hnode_tmp1, prompt_hnode_tmp2) {
CDL_DELETE(prompt_history->prompt_hlist, prompt_hnode);
free(prompt_hnode->data);
free(prompt_hnode);
}
free(prompt_history);
}
DL_FOREACH_SAFE(editor->observers, observer, observer_tmp) {
editor_destroy_observer(editor, observer);
}
if (editor->macro_record) {
if (editor->macro_record->inputs) free(editor->macro_record->inputs);
free(editor->macro_record);
}
_editor_destroy_syntax_map(editor->syntax_map);
if (editor->kmap_init_name) free(editor->kmap_init_name);
if (editor->insertbuf) free(editor->insertbuf);
if (editor->cut_buffer) free(editor->cut_buffer);
if (editor->ttyfd) close(editor->ttyfd);
if (editor->startup_macro_name) free(editor->startup_macro_name);
if (editor->macro_last) free(editor->macro_last);
pcre2_match_data_free(pcre2_md);
if (!editor->headless_mode) {
tb_shutdown();
}
return MLE_OK;
}
// Prompt user for input
int editor_prompt(editor_t *editor, char *prompt, editor_prompt_params_t *params, char **optret_answer) {
bview_t *bview_tmp;
loop_context_t loop_ctx;
memset(&loop_ctx, 0, sizeof(loop_context_t));
// Disallow nested prompts
if (editor->prompt) {
if (optret_answer) *optret_answer = NULL;
return MLE_ERR;
}
// Init loop_ctx
loop_ctx.invoker = editor->active;
loop_ctx.should_exit = 0;
loop_ctx.prompt_answer = NULL;
// Init prompt
editor_open_bview(editor, NULL, MLE_BVIEW_TYPE_PROMPT, NULL, 0, 1, 0, 0, NULL, &editor->prompt);
if (params && params->prompt_cb) bview_add_listener(editor->prompt, params->prompt_cb, params->prompt_cb_udata);
editor->prompt->prompt_str = prompt;
bview_push_kmap(editor->prompt, params && params->kmap ? params->kmap : editor->kmap_prompt_input);
// Insert data if present
if (params && params->data && params->data_len > 0) {
buffer_insert(editor->prompt->buffer, 0, params->data, params->data_len, NULL);
mark_move_eol(editor->prompt->active_cursor->mark);
}
// Loop inside prompt
_editor_loop(editor, &loop_ctx);
// Set answer
if (optret_answer) {
*optret_answer = loop_ctx.prompt_answer;
} else if (loop_ctx.prompt_answer) {
free(loop_ctx.prompt_answer);
loop_ctx.prompt_answer = NULL;
}
// Restore previous focus
bview_tmp = editor->prompt;
editor->prompt = NULL;
editor_close_bview(editor, bview_tmp, NULL);
editor_set_active(editor, loop_ctx.invoker);
return MLE_OK;
}
// A printf version of editor_prompt
int editor_prompt_fmt(editor_t *editor, editor_prompt_params_t *params, char **optret_answer, char *prompt_fmt, ...) {
char prompt[512];
va_list vl;
int rv;
va_start(vl, prompt_fmt);
rv = vsnprintf(prompt, sizeof(prompt), prompt_fmt, vl);
va_end(vl);
if (rv < 0 || rv >= (int)sizeof(prompt)) return MLE_ERR;
return editor_prompt(editor, prompt, params, optret_answer);
}
// Open dialog menu
int editor_menu(editor_t *editor, cmd_func_t callback, char *opt_buf_data, int opt_buf_data_len, aproc_t *opt_aproc, bview_t **optret_menu) {
bview_t *menu;
editor_open_bview(editor, NULL, MLE_BVIEW_TYPE_EDIT, NULL, 0, 1, 0, 0, NULL, &menu);
menu->is_menu = 1;
menu->soft_wrap = 0;
menu->menu_callback = callback;
bview_push_kmap(menu, editor->kmap_menu);
if (opt_aproc) {
aproc_set_owner(opt_aproc, menu, &(menu->aproc));
}
if (opt_buf_data) {
mark_insert_before(menu->active_cursor->mark, opt_buf_data, opt_buf_data_len);
}
if (optret_menu) *optret_menu = menu;
return MLE_OK;
}
// Open a bview
int editor_open_bview(editor_t *editor, bview_t *opt_parent, int type, char *opt_path, int opt_path_len, int make_active, bint_t linenum, int skip_resize, buffer_t *opt_buffer, bview_t **optret_bview) {
bview_t *bview;
bview_rect_t *rect;
int found;
found = 0;
// Check if already open and not dirty
if (opt_path) {
CDL_FOREACH2(editor->all_bviews, bview, all_next) {
if (bview->buffer
&& !bview->buffer->is_unsaved
&& bview->buffer->path
&& strlen(bview->buffer->path) == (size_t)opt_path_len
&& strncmp(opt_path, bview->buffer->path, opt_path_len) == 0
) {
found = 1;
break;
}
}
}
// Make new bview if not already open
if (!found) {
bview = bview_new(editor, type, opt_path, opt_path_len, opt_buffer);
CDL_APPEND2(editor->all_bviews, bview, all_prev, all_next);
if (!opt_parent) {
DL_APPEND2(editor->top_bviews, bview, top_prev, top_next);
} else {
opt_parent->split_child = bview;
}
}
if (make_active) {
editor_set_active(editor, bview);
}
if (!found && !editor->is_in_init && !skip_resize) {
switch (type) {
case MLE_BVIEW_TYPE_STATUS: rect = &editor->rect_status; break;
case MLE_BVIEW_TYPE_PROMPT: rect = &editor->rect_prompt; break;
default:
case MLE_BVIEW_TYPE_EDIT: rect = &editor->rect_edit; break;
}
bview_resize(bview, rect->x, rect->y, rect->w, rect->h);
}
if (linenum > 0) {
mark_move_to(bview->active_cursor->mark, linenum - 1, 0);
bview_center_viewport_y(bview);
}
if (optret_bview) {
*optret_bview = bview;
}
if (!found && opt_path && util_is_dir(opt_path)) {
// TODO This is hacky
cmd_context_t ctx;
memset(&ctx, 0, sizeof(cmd_context_t));
ctx.editor = editor;
ctx.static_param = opt_path;
ctx.bview = bview;
cmd_browse(&ctx);
editor_close_bview(editor, bview, NULL);
}
return MLE_OK;
}
// Close a bview
int editor_close_bview(editor_t *editor, bview_t *bview, int *optret_num_closed) {
int rc;
if (optret_num_closed) *optret_num_closed = 0;
if ((rc = _editor_close_bview_inner(editor, bview, optret_num_closed)) == MLE_OK) {
_editor_resize(editor, editor->w, editor->h);
}
return rc;
}
// Set the active bview
int editor_set_active(editor_t *editor, bview_t *bview) {
if (!_editor_bview_exists(editor, bview)) {
MLE_RETURN_ERR(editor, "No bview %p in editor->all_bviews", (void*)bview);
} else if (editor->prompt && editor->prompt != bview) {
MLE_RETURN_ERR(editor, "Cannot abandon prompt for bview %p", (void*)bview);
}
editor->active = bview;
if (MLE_BVIEW_IS_EDIT(bview)) {
editor->active_edit_last = editor->active_edit;
editor->active_edit = bview;
editor->active_edit_root = bview_get_split_root(bview);
}
bview_rectify_viewport(bview);
return MLE_OK;
}
// Print debug info
int editor_debug_dump(editor_t *editor, FILE *fp) {
bview_t *bview;
cursor_t *cursor;
buffer_t *buffer;
bline_t *bline;
bint_t c;
int bview_index;
int cursor_index;
bview_index = 0;
CDL_FOREACH2(editor->all_bviews, bview, all_next) {
if (!MLE_BVIEW_IS_EDIT(bview)) continue;
cursor_index = 0;
DL_FOREACH(bview->cursors, cursor) {
fprintf(fp, "bview.%d.cursor.%d.mark.line_index=%" PRIdMAX "\n", bview_index, cursor_index, cursor->mark->bline->line_index);
fprintf(fp, "bview.%d.cursor.%d.mark.col=%" PRIdMAX "\n", bview_index, cursor_index, cursor->mark->col);
if (cursor->is_anchored) {
fprintf(fp, "bview.%d.cursor.%d.anchor.line_index=%" PRIdMAX "\n", bview_index, cursor_index, cursor->anchor->bline->line_index);
fprintf(fp, "bview.%d.cursor.%d.anchor.col=%" PRIdMAX "\n", bview_index, cursor_index, cursor->anchor->col);
}
fprintf(fp, "bview.%d.cursor.%d.sel_rule=%c\n", bview_index, cursor_index, cursor->sel_rule ? 'y' : 'n');
cursor_index += 1;
}
fprintf(fp, "bview.%d.cursor_count=%d\n", bview_index, cursor_index);
buffer = bview->buffer;
fprintf(fp, "bview.%d.buffer.byte_count=%" PRIdMAX "\n", bview_index, buffer->byte_count);
fprintf(fp, "bview.%d.buffer.line_count=%" PRIdMAX "\n", bview_index, buffer->line_count);
fprintf(fp, "bview.%d.buffer.path=%s\n", bview_index, buffer->path ? buffer->path : "");
for (bline = buffer->first_line; bline != NULL; bline = bline->next) {
MLBUF_BLINE_ENSURE_CHARS(bline);
fprintf(fp, "bview.%d.buffer.blines.%" PRIdMAX ".chars=", bview_index, bline->line_index);
for (c = 0; c < bline->char_count; ++c) {
fprintf(fp, "<ch=%" PRIu32 " fg=%" PRIu16 " bg=%" PRIu16 ">",
bline->chars[c].ch,
bline->chars[c].style.fg,
bline->chars[c].style.bg
);
}
fprintf(fp, "\n");
}
fprintf(fp, "bview.%d.buffer.blines_end\n", bview_index);
fprintf(fp, "bview.%d.buffer.data_begin\n", bview_index);
buffer_write_to_file(buffer, fp, NULL);
fprintf(fp, "\nbview.%d.buffer.data_end\n", bview_index);
bview_index += 1;
}
fprintf(fp, "bview_count=%d\n", bview_index);
return MLE_OK;
}
// Invoke tb_set_input_mode appropriately
int editor_set_input_mode(editor_t *editor) {
int input_mode;
if (editor->headless_mode) {
return MLE_OK;
}
input_mode = TB_INPUT_ALT;
if (editor->mouse_support) {
input_mode |= TB_INPUT_MOUSE;
}
tb_set_input_mode(input_mode);
return MLE_OK;
}
// Set macro toggle key
static int _editor_set_macro_toggle_key(editor_t *editor, char *key) {
return _editor_key_to_input(key, &editor->macro_toggle_key);
}
// Return 1 if bview exists in editor, else return 0
static int _editor_bview_exists(editor_t *editor, bview_t *bview) {
bview_t *tmp;
CDL_FOREACH2(editor->all_bviews, tmp, all_next) {
if (tmp == bview) return 1;
}
return 0;
}
// Return number of EDIT bviews open
int editor_bview_edit_count(editor_t *editor) {
int count;
bview_t *bview;
count = 0;
CDL_FOREACH2(editor->all_bviews, bview, all_next) {
if (MLE_BVIEW_IS_EDIT(bview)) count += 1;
}
return count;
}
// Register a command
static int _editor_register_cmd_fn(editor_t *editor, char *name, int (*func)(cmd_context_t *ctx)) {
cmd_t cmd = {0};
cmd.name = name;
cmd.func = func;
return editor_register_cmd(editor, &cmd);
}
// Register a command (extended)
int editor_register_cmd(editor_t *editor, cmd_t *cmd) {
cmd_t *existing_cmd;
cmd_t *new_cmd;
HASH_FIND_STR(editor->cmd_map, cmd->name, existing_cmd);
if (existing_cmd) return MLE_ERR;
new_cmd = calloc(1, sizeof(cmd_t));
*new_cmd = *cmd;
new_cmd->name = strdup(new_cmd->name);
HASH_ADD_KEYPTR(hh, editor->cmd_map, new_cmd->name, strlen(new_cmd->name), new_cmd);
return MLE_OK;
}
// Get input from either macro or user
int editor_get_input(editor_t *editor, loop_context_t *loop_ctx, cmd_context_t *ctx) {
ctx->is_user_input = 0;
if (editor->macro_apply
&& editor->macro_apply_input_index < editor->macro_apply->inputs_len
) {
// Get input from macro
MLE_KINPUT_COPY(ctx->input, editor->macro_apply->inputs[editor->macro_apply_input_index]);
editor->macro_apply_input_index += 1;
} else {
// Get input from user
if (editor->macro_apply) {
// Clear macro if present
editor->macro_apply = NULL;
editor->macro_apply_input_index = 0;
}
if (editor->headless_mode) {
// Bail if in headless mode
loop_ctx->should_exit = 1;
return MLE_ERR;
} else {
// Get input from user
_editor_get_user_input(editor, ctx);
ctx->is_user_input = 1;
}
}
if (editor->is_recording_macro && editor->macro_record) {
// Record macro input
_editor_record_macro_input(editor->macro_record, &ctx->input);
}
// Remember keys for display
if (editor->debug_display_keys) _editor_remember_keys(editor, &ctx->input);
return MLE_OK;
}
// Display the editor
int editor_display(editor_t *editor) {
bview_t *bview;
if (editor->headless_mode) return MLE_OK;
tb_clear();
bview_draw(editor->active_edit_root);
bview_draw(editor->status);
if (editor->prompt) bview_draw(editor->prompt);
DL_FOREACH2(editor->top_bviews, bview, top_next) {
_editor_draw_cursors(editor, bview);
}
if (editor->debug_display_keys) _editor_display_keys(editor);
tb_present();
return MLE_OK;
}
// Register a cmd observer
int editor_register_observer(editor_t *editor, char *event_patt, void *udata, observer_func_t fn_callback, observer_t **optret_observer) {
observer_t *observer;
observer = calloc(1, sizeof(observer_t));
observer->event_patt = strdup(event_patt);
observer->callback = fn_callback;
observer->udata = udata;
DL_APPEND(editor->observers, observer);
if (optret_observer) *optret_observer = observer;
return MLE_OK;
}
// Register a cmd observer
int editor_destroy_observer(editor_t *editor, observer_t *observer) {
DL_DELETE(editor->observers, observer);
free(observer->event_patt);
free(observer);
return MLE_OK;
}
// Close a bview
static int _editor_close_bview_inner(editor_t *editor, bview_t *bview, int *optret_num_closed) {
if (!_editor_bview_exists(editor, bview)) {
MLE_RETURN_ERR(editor, "No bview %p in editor->all_bviews", (void*)bview);
}
if (bview->split_child) {
_editor_close_bview_inner(editor, bview->split_child, optret_num_closed);
}
if (bview->split_parent) {
bview->split_parent->split_child = NULL;
editor_set_active(editor, bview->split_parent);
} else if (bview == editor->active_edit) {
if (bview->all_prev && bview->all_prev != bview && MLE_BVIEW_IS_EDIT(bview->all_prev)) {
editor_set_active(editor, bview->all_prev);
} else if (bview->all_next && bview->all_next != bview && MLE_BVIEW_IS_EDIT(bview->all_next)) {
editor_set_active(editor, bview->all_next);
} else {
editor_open_bview(editor, NULL, MLE_BVIEW_TYPE_EDIT, NULL, 0, 1, 0, 0, NULL, NULL);
}
}
if (!bview->split_parent) {
DL_DELETE2(editor->top_bviews, bview, top_prev, top_next);
}
if (bview->all_next && bview->all_prev) {
CDL_DELETE2(editor->all_bviews, bview, all_prev, all_next);
}
bview_destroy(bview);
if (optret_num_closed) *optret_num_closed += 1;
return MLE_OK;
}
// Destroy a command
static int _editor_destroy_cmd(editor_t *editor, cmd_t *cmd) {
(void)editor;
free(cmd->name);
free(cmd);
return MLE_OK;
}
// Invoked when user hits enter in a prompt_input
static int _editor_prompt_input_submit(cmd_context_t *ctx) {
bint_t answer_len;
char *answer;
buffer_get(ctx->bview->buffer, &answer, &answer_len);
ctx->loop_ctx->prompt_answer = strndup(answer, answer_len);
_editor_prompt_history_append(ctx, ctx->loop_ctx->prompt_answer);
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Invoke when user hits tab in a prompt_input
static int _editor_prompt_input_complete(cmd_context_t *ctx) {
loop_context_t *loop_ctx;
char *cmd;
char *cmd_arg;
char *terms;
size_t terms_len;
int num_terms;
char *term;
int term_index;
loop_ctx = ctx->loop_ctx;
// Update tab_complete_term and tab_complete_index
if (loop_ctx->last_cmd_ctx.cmd && loop_ctx->last_cmd_ctx.cmd->func == _editor_prompt_input_complete) {
loop_ctx->tab_complete_index += 1;
} else if (ctx->bview->buffer->first_line->data_len < MLE_LOOP_CTX_MAX_COMPLETE_TERM_SIZE) {
snprintf(
loop_ctx->tab_complete_term,
MLE_LOOP_CTX_MAX_COMPLETE_TERM_SIZE,
"%.*s",
(int)ctx->bview->buffer->first_line->data_len,
ctx->bview->buffer->first_line->data
);
loop_ctx->tab_complete_index = 0;
} else {
return MLE_OK;
}
// Assemble compgen command
cmd_arg = util_escape_shell_arg(
loop_ctx->tab_complete_term,
strlen(loop_ctx->tab_complete_term)
);
asprintf(&cmd, "compgen -f %s 2>/dev/null | sort", cmd_arg);
// Run compgen command
terms = NULL;
terms_len = 0;
util_shell_exec(ctx->editor, cmd, 1, NULL, 0, 0, "bash", &terms, &terms_len, NULL);
free(cmd);
free(cmd_arg);
// Get number of terms
// TODO valgrind thinks there's an error here
num_terms = 0;
term = strchr(terms, '\n');
while (term) {
num_terms += 1;
term = strchr(term + 1, '\n');
}
// Bail if no terms
if (num_terms < 1) {
free(terms);
return MLE_OK;
}
// Determine term index
term_index = loop_ctx->tab_complete_index % num_terms;
// Set prompt input to term
term = strtok(terms, "\n");
while (term != NULL) {
if (term_index == 0) {
buffer_set(ctx->bview->buffer, term, strlen(term));
mark_move_eol(ctx->cursor->mark);
break;
} else {
term_index -= 1;
}
term = strtok(NULL, "\n");
}
free(terms);
return MLE_OK;
}
// Find or add a prompt history entry for the current prompt
static prompt_history_t *_editor_prompt_find_or_add_history(cmd_context_t *ctx, prompt_hnode_t **optret_prompt_hnode) {
prompt_history_t *prompt_history;
HASH_FIND_STR(ctx->editor->prompt_history, ctx->bview->prompt_str, prompt_history);
if (!prompt_history) {
prompt_history = calloc(1, sizeof(prompt_history_t));
prompt_history->prompt_str = strdup(ctx->bview->prompt_str);
HASH_ADD_KEYPTR(hh, ctx->editor->prompt_history, prompt_history->prompt_str, strlen(prompt_history->prompt_str), prompt_history);
}
if (!ctx->loop_ctx->prompt_hnode) {
ctx->loop_ctx->prompt_hnode = prompt_history->prompt_hlist
? prompt_history->prompt_hlist->prev
: NULL;
}
if (optret_prompt_hnode) {
*optret_prompt_hnode = ctx->loop_ctx->prompt_hnode;
}
return prompt_history;
}
// Prompt history up
static int _editor_prompt_history_up(cmd_context_t *ctx) {
prompt_hnode_t *prompt_hnode;
_editor_prompt_find_or_add_history(ctx, &prompt_hnode);
if (prompt_hnode) {
ctx->loop_ctx->prompt_hnode = prompt_hnode->prev;
buffer_set(ctx->buffer, prompt_hnode->data, prompt_hnode->data_len);
}
return MLE_OK;
}
// Prompt history down
static int _editor_prompt_history_down(cmd_context_t *ctx) {
prompt_hnode_t *prompt_hnode;
_editor_prompt_find_or_add_history(ctx, &prompt_hnode);
if (prompt_hnode) {
ctx->loop_ctx->prompt_hnode = prompt_hnode->next;
buffer_set(ctx->buffer, prompt_hnode->data, prompt_hnode->data_len);
}
return MLE_OK;
}
// Prompt history append
static int _editor_prompt_history_append(cmd_context_t *ctx, char *data) {
prompt_history_t *prompt_history;
prompt_hnode_t *prompt_hnode;
prompt_history = _editor_prompt_find_or_add_history(ctx, NULL);
prompt_hnode = calloc(1, sizeof(prompt_hnode_t));
prompt_hnode->data = strdup(data);
prompt_hnode->data_len = (bint_t)strlen(data);
CDL_APPEND(prompt_history->prompt_hlist, prompt_hnode);
return MLE_OK;
}
// Invoked when user hits a in a prompt_yna
static int _editor_prompt_yna_all(cmd_context_t *ctx) {
ctx->loop_ctx->prompt_answer = MLE_PROMPT_ALL;
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Invoked when user hits y in a prompt_yn(a)
static int _editor_prompt_yn_yes(cmd_context_t *ctx) {
ctx->loop_ctx->prompt_answer = MLE_PROMPT_YES;
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Invoked when user hits n in a prompt_yn(a)
static int _editor_prompt_yn_no(cmd_context_t *ctx) {
ctx->loop_ctx->prompt_answer = MLE_PROMPT_NO;
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Invoked when user cancels (Ctrl-C) a prompt_(input|yn), or hits any key in a prompt_ok
static int _editor_prompt_cancel(cmd_context_t *ctx) {
ctx->loop_ctx->prompt_answer = NULL;
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Invoked when user hits enter in a menu
static int _editor_menu_submit(cmd_context_t *ctx) {
if (ctx->bview->menu_callback) return ctx->bview->menu_callback(ctx);
return MLE_OK;
}
// Invoked when user hits C-c in a menu
static int _editor_menu_cancel(cmd_context_t *ctx) {
if (ctx->bview->aproc) aproc_destroy(ctx->bview->aproc, 1);
return MLE_OK;
}
// Invoked when user hits down in a prompt_isearch
static int _editor_prompt_isearch_next(cmd_context_t *ctx) {
if (ctx->editor->active_edit->isearch_rule) {
mark_move_next_cre_nudge(ctx->editor->active_edit->active_cursor->mark, ctx->editor->active_edit->isearch_rule->cre);
bview_center_viewport_y(ctx->editor->active_edit);
}
return MLE_OK;
}
// Invoked when user hits up in a prompt_isearch
static int _editor_prompt_isearch_prev(cmd_context_t *ctx) {
if (ctx->editor->active_edit->isearch_rule) {
mark_move_prev_cre(ctx->editor->active_edit->active_cursor->mark, ctx->editor->active_edit->isearch_rule->cre);
bview_center_viewport_y(ctx->editor->active_edit);
}
return MLE_OK;
}
// Invoked when user hits up in a prompt_isearch
static int _editor_prompt_isearch_viewport_up(cmd_context_t *ctx) {
return bview_set_viewport_y(ctx->editor->active_edit, ctx->editor->active_edit->viewport_mark->bline->line_index - 5, 0);
}
// Invoked when user hits up in a prompt_isearch
static int _editor_prompt_isearch_viewport_down(cmd_context_t *ctx) {
return bview_set_viewport_y(ctx->editor->active_edit, ctx->editor->active_edit->viewport_mark->bline->line_index + 5, 0);
}
// Drops a cursor on each isearch match
static int _editor_prompt_isearch_drop_cursors(cmd_context_t *ctx) {
bview_t *bview;
mark_t *mark;
pcre2_code *cre;
cursor_t *orig_cursor;
cursor_t *last_cursor;
bint_t nchars;
bview = ctx->editor->active_edit;
if (!bview->isearch_rule) return MLE_OK;
orig_cursor = bview->active_cursor;
mark = bview->active_cursor->mark;
cre = bview->isearch_rule->cre;
mark_move_beginning(mark);
last_cursor = NULL;
while (mark_move_next_cre_ex(mark, cre, NULL, NULL, &nchars) == MLBUF_OK) {
bview_add_cursor(bview, mark->bline, mark->col, &last_cursor);
mark_move_by(mark, MLE_MAX(1, nchars));
}
if (last_cursor) {
mark_join(orig_cursor->mark, last_cursor->mark);
bview_remove_cursor(bview, last_cursor);
}
bview->active_cursor = orig_cursor;
bview_center_viewport_y(bview);
ctx->loop_ctx->prompt_answer = NULL;
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Run editor loop
static void _editor_loop(editor_t *editor, loop_context_t *loop_ctx) {
cmd_t *cmd;
cmd_context_t cmd_ctx;
// Increment loop_depth
editor->loop_depth += 1;
// Init cmd_context
memset(&cmd_ctx, 0, sizeof(cmd_context_t));
cmd_ctx.editor = editor;
cmd_ctx.loop_ctx = loop_ctx;
// Loop until editor should exit
while (!loop_ctx->should_exit) {
// Set loop_ctx
editor->loop_ctx = loop_ctx;
// Display editor
if (!editor->is_display_disabled) {
editor_display(editor);
}
// Bail if debug_exit_after_startup set
if (editor->debug_exit_after_startup) {
break;
}
// Check for async io
// aproc_drain_all will bail and return 0 if there's any tty data
if (editor->aprocs && aproc_drain_all(editor->aprocs, &editor->ttyfd)) {
continue;
}
// Get input
if (editor_get_input(editor, loop_ctx, &cmd_ctx) == MLE_ERR) {
break;
}
// Toggle macro?
if (_editor_maybe_toggle_macro(editor, &cmd_ctx.input)) {
continue;
}
if ((cmd = _editor_get_command(editor, &cmd_ctx, NULL)) != NULL) {
// Found cmd in kmap trie, now execute
cmd_ctx.cmd = cmd;
if (cmd_ctx.is_user_input && cmd->func == cmd_insert_data) {
// Ingest user paste
_editor_ingest_paste(editor, &cmd_ctx);
} else if (cmd->func == cmd_repeat && loop_ctx->last_cmd_ctx.cmd) {
// Repeat last command
memcpy(&cmd_ctx, &loop_ctx->last_cmd_ctx, sizeof(cmd_ctx));
}
// Notify cmd:*:before observers
_editor_notify_cmd_observers(&cmd_ctx, 1);
// Refresh cmd ctx if observers changed anything
_editor_refresh_cmd_context(editor, &cmd_ctx);
// Execute cmd
cmd_ctx.cmd->func(&cmd_ctx);
// Lift any temp anchors
_editor_maybe_lift_temp_anchors(&cmd_ctx);
// Notify cmd:*:after observers
_editor_notify_cmd_observers(&cmd_ctx, 0);
// Copy cmd_ctx to last_cmd_ctx
memcpy(&loop_ctx->last_cmd_ctx, &cmd_ctx, sizeof(cmd_ctx));
loop_ctx->binding_node = NULL;
cmd_ctx.wildcard_params_len = 0;
cmd_ctx.numeric_params_len = 0;
cmd_ctx.pastebuf_len = 0;
} else if (loop_ctx->need_more_input) {
// Need more input to find
} else {
// Not found, bad command
loop_ctx->binding_node = NULL;
}
}
// Free stuff
if (cmd_ctx.pastebuf) free(cmd_ctx.pastebuf);
str_free(&loop_ctx->last_insert);
if (loop_ctx->input_trail) free(loop_ctx->input_trail);
loop_ctx->input_trail_len = 0;
loop_ctx->input_trail_cap = 0;
// Decrement loop_depth
editor->loop_depth -= 1;
}
// Run debug routine to show key names as they input
static int _editor_debug_key_input(void) {
char key[MLE_MAX_KEYNAME_LEN + 1];
struct tb_event ev;
int y, h;
y = 0;
h = tb_height();
tb_print(0, y++, 0, 0, "_editor_debug_key_input: Press q to quit.");