This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
1790 lines (1608 loc) · 54.6 KB
/
cmd.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 <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include "mle.h"
#define MLE_MULTI_CURSOR_MARK_FN(pcursor, pfn, ...) do {\
cursor_t *cursor; \
DL_FOREACH((pcursor)->bview->cursors, cursor) { \
if (cursor->is_asleep) continue; \
pfn(cursor->mark, ##__VA_ARGS__); \
} \
} while(0)
#define MLE_MULTI_CURSOR_MARK_FN_NO_ARGS(pcursor, pfn) do {\
cursor_t *cursor; \
DL_FOREACH((pcursor)->bview->cursors, cursor) { \
if (cursor->is_asleep) continue; \
pfn(cursor->mark); \
} \
} while(0)
#define MLE_MULTI_CURSOR_CODE(pcursor, pcode) do { \
cursor_t *cursor; \
DL_FOREACH((pcursor)->bview->cursors, cursor) { \
if (cursor->is_asleep) continue; \
pcode \
} \
} while(0)
static void _cmd_force_redraw(cmd_context_t *ctx);
static int _cmd_pre_close(editor_t *editor, bview_t *bview);
static int _cmd_quit_inner(editor_t *editor, bview_t *bview);
static int _cmd_save(editor_t *editor, bview_t *bview, int save_as);
static int _cmd_search_next(bview_t *bview, cursor_t *cursor, mark_t *search_mark, char *regex, int regex_len);
static void _cmd_aproc_bview_passthru_cb(aproc_t *self, char *buf, size_t buf_len);
static void _cmd_isearch_prompt_cb(bview_t *bview, baction_t *action, void *udata);
static int _cmd_menu_browse_cb(cmd_context_t *ctx);
static int _cmd_menu_blist_cb(cmd_context_t *ctx);
static int _cmd_menu_grep_cb(cmd_context_t *ctx);
static int _cmd_menu_ctag_cb(cmd_context_t *ctx);
static int _cmd_indent(cmd_context_t *ctx, int outdent);
static int _cmd_indent_line(bline_t *bline, int use_tabs, int outdent);
static void _cmd_help_inner(char *buf, kbinding_t *trie, str_t *h);
static void _cmd_insert_auto_indent_newline(cmd_context_t *ctx);
static void _cmd_insert_auto_indent_closing_bracket(cmd_context_t *ctx);
static void _cmd_shell_apply_cmd(cmd_context_t *ctx, char *cmd);
static void _cmd_get_input(cmd_context_t *ctx, kinput_t *ret_input);
static int _cmd_fsearch_inner(cmd_context_t *ctx, char *shell_cmd);
// Insert data
int cmd_insert_data(cmd_context_t *ctx) {
bint_t insertbuf_len;
char *insertbuf_cur;
bint_t len;
size_t insert_size;
kinput_t *input;
int i;
// Ensure space in insertbuf
insert_size = MLE_MAX(6, ctx->bview->buffer->tab_width) * (ctx->pastebuf_len + 1);
if (ctx->editor->insertbuf_size < insert_size) {
ctx->editor->insertbuf = realloc(ctx->editor->insertbuf, insert_size);
memset(ctx->editor->insertbuf, 0, insert_size);
ctx->editor->insertbuf_size = insert_size;
}
// Fill insertbuf... i=-1: ctx->input, i>=0: ctx->pastebuf
insertbuf_len = 0;
insertbuf_cur = ctx->editor->insertbuf;
for (i = -1; i == -1 || (size_t)i < ctx->pastebuf_len; i++) {
input = i == -1 ? &ctx->input : &ctx->pastebuf[i];
if (input->ch) {
len = utf8_unicode_to_char(insertbuf_cur, input->ch);
} else if (input->key == TB_KEY_ENTER || input->key == TB_KEY_CTRL_J || input->key == TB_KEY_CTRL_M) {
len = sprintf(insertbuf_cur, "\n");
} else if (input->key >= 0x20 && input->key <= 0x7e) {
len = sprintf(insertbuf_cur, "%c", input->key);
} else if (input->key == 0x09) {
if (ctx->bview->tab_to_space) {
len = ctx->bview->buffer->tab_width - (ctx->cursor->mark->col % ctx->bview->buffer->tab_width);
len = sprintf(insertbuf_cur, "%*c", (int)len, ' ');
} else {
len = sprintf(insertbuf_cur, "\t");
}
} else {
len = 0; // TODO verbatim input
}
insertbuf_cur += len;
insertbuf_len += len;
}
// Write insertbuf to buffer
if (insertbuf_len < 1) {
return MLE_ERR;
}
ctx->editor->insertbuf[insertbuf_len] = '\0';
// Insert
if (insertbuf_len > 1
&& ctx->editor->trim_paste
&& memchr(ctx->editor->insertbuf, '\n', insertbuf_len) != NULL
) {
// Insert with trim
char *trimmed = NULL;
int trimmed_len = 0;
util_pcre_replace("(?m) +$", ctx->editor->insertbuf, "", &trimmed, &trimmed_len);
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_insert_before, trimmed, trimmed_len);
free(trimmed);
} else if (ctx->editor->auto_indent && !ctx->cursor->next && insertbuf_len == 1 && ctx->editor->insertbuf[0] == '\n') {
_cmd_insert_auto_indent_newline(ctx);
} else if (ctx->editor->auto_indent && !ctx->cursor->next && insertbuf_len == 1 && ctx->editor->insertbuf[0] == '}') {
_cmd_insert_auto_indent_closing_bracket(ctx);
} else {
// Insert without trim
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_insert_before, ctx->editor->insertbuf, insertbuf_len);
}
// Remember last insert data
str_set_len(&ctx->loop_ctx->last_insert, ctx->editor->insertbuf, insertbuf_len);
return MLE_OK;
}
// Insert newline above current line
int cmd_insert_newline_above(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
mark_move_bol(cursor->mark);
mark_insert_after(cursor->mark, "\n", 1);
);
return MLE_OK;
}
// Delete char before cursor mark
int cmd_delete_before(cmd_context_t *ctx) {
bint_t offset;
mark_get_offset(ctx->cursor->mark, &offset);
if (offset < 1) return MLE_OK;
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_delete_before, 1);
return MLE_OK;
}
// Delete char after cursor mark
int cmd_delete_after(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_delete_after, 1);
return MLE_OK;
}
// Move cursor to beginning of line
int cmd_move_bol(cmd_context_t *ctx) {
uint32_t ch;
mark_t *mark;
MLE_MULTI_CURSOR_CODE(ctx->cursor,
mark_clone(cursor->mark, &mark);
mark_move_bol(mark);
mark_get_char_after(mark, &ch);
if (isspace((char)ch)) {
mark_move_next_re(mark, "\\S", 2);
}
if (mark->col < cursor->mark->col) {
mark_join(cursor->mark, mark);
} else {
mark_move_bol(cursor->mark);
}
mark_destroy(mark);
);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor to end of line
int cmd_move_eol(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN_NO_ARGS(ctx->cursor, mark_move_eol);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor to beginning of buffer
int cmd_move_beginning(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN_NO_ARGS(ctx->cursor, mark_move_beginning);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor to end of buffer
int cmd_move_end(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN_NO_ARGS(ctx->cursor, mark_move_end);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor left one char
int cmd_move_left(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_by, -1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor right one char
int cmd_move_right(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_by, 1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor up one line
int cmd_move_up(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_vert, -1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor down one line
int cmd_move_down(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_vert, 1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move cursor one page up
int cmd_move_page_up(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_vert, -1 * ctx->bview->rect_buffer.h);
bview_zero_viewport_y(ctx->bview);
return MLE_OK;
}
// Move cursor one page down
int cmd_move_page_down(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_vert, ctx->bview->rect_buffer.h);
bview_zero_viewport_y(ctx->bview);
return MLE_OK;
}
// Move to specific line
int cmd_move_to_line(cmd_context_t *ctx) {
char *linestr;
bint_t line;
editor_prompt(ctx->editor, "move_to_line: Line num?", NULL, &linestr);
if (!linestr) return MLE_OK;
line = strtoll(linestr, NULL, 10);
free(linestr);
if (line < 1) line = 1;
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_to, line - 1, 0);
bview_center_viewport_y(ctx->bview);
return MLE_OK;
}
// Move vertically relative to current line
int cmd_move_relative(cmd_context_t *ctx) {
int delta;
if (ctx->loop_ctx->numeric_params_len < 1) {
return MLE_ERR;
}
if (strcmp(ctx->static_param, "up") == 0) {
delta = -1;
} else if (strcmp(ctx->static_param, "down") == 0) {
delta = 1;
} else {
return MLE_ERR;
}
delta *= ctx->loop_ctx->numeric_params[0];
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_vert, delta);
return MLE_OK;
}
// Move one word forward
int cmd_move_word_forward(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_next_re_nudge, MLE_RE_WORD_FORWARD, sizeof(MLE_RE_WORD_FORWARD)-1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move one word back
int cmd_move_word_back(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_prev_re, MLE_RE_WORD_BACK, sizeof(MLE_RE_WORD_BACK)-1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move to next open bracket
int cmd_move_bracket_forward(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_next_str_nudge, "{", 1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move to prev open bracket
int cmd_move_bracket_back(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_prev_str, "{", 1);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move to matching bracket, or prev bracket if not on a bracket
int cmd_move_bracket_toggle(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
if (mark_move_bracket_pair(cursor->mark, ctx->buffer->byte_count) == MLBUF_ERR) {
mark_move_prev_re(cursor->mark, "[\\[\\(\\{]", strlen("[\\[\\(\\{]"));
}
);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Delete word back
int cmd_delete_word_before(cmd_context_t *ctx) {
mark_t *tmark;
MLE_MULTI_CURSOR_CODE(ctx->cursor,
mark_clone(cursor->mark, &tmark);
mark_move_prev_re(tmark, MLE_RE_WORD_BACK, sizeof(MLE_RE_WORD_BACK)-1);
mark_delete_between_mark(cursor->mark, tmark);
mark_destroy(tmark);
);
return MLE_OK;
}
// Delete word ahead
int cmd_delete_word_after(cmd_context_t *ctx) {
mark_t *tmark;
MLE_MULTI_CURSOR_CODE(ctx->cursor,
mark_clone(cursor->mark, &tmark);
mark_move_next_re(tmark, MLE_RE_WORD_FORWARD, sizeof(MLE_RE_WORD_FORWARD)-1);
mark_delete_between_mark(cursor->mark, tmark);
mark_destroy(tmark);
);
return MLE_OK;
}
// Toggle sel bound on cursors
int cmd_toggle_anchor(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
cursor_toggle_anchor(cursor, 1);
);
return MLE_OK;
}
// Drop an is_asleep=1 cursor
int cmd_drop_sleeping_cursor(cmd_context_t *ctx) {
return bview_add_cursor_asleep(ctx->bview, ctx->cursor->mark->bline, ctx->cursor->mark->col, NULL);
}
// Awake all is_asleep=1 cursors
int cmd_wake_sleeping_cursors(cmd_context_t *ctx) {
return bview_wake_sleeping_cursors(ctx->bview);
}
// Remove all cursors except the active one
int cmd_remove_extra_cursors(cmd_context_t *ctx) {
return bview_remove_cursors_except(ctx->bview, ctx->cursor);
}
// Drop column of cursors in selection
int cmd_drop_cursor_column(cmd_context_t *ctx) {
bline_t *bline;
bint_t col;
mark_t *lo;
mark_t *hi;
MLE_MULTI_CURSOR_CODE(ctx->cursor,
if (!cursor->is_anchored) continue;
col = cursor->mark->col;
cursor_get_lo_hi(cursor, &lo, &hi);
for (bline = lo->bline; bline != hi->bline->next; bline = bline->next) {
MLBUF_BLINE_ENSURE_CHARS(bline);
if ((bline == lo->bline && col < lo->col)
|| (bline == hi->bline && col > hi->col)
|| col > bline->char_count
) {
continue;
}
if (bline != cursor->mark->bline || col != cursor->mark->col) {
bview_add_cursor(ctx->bview, bline, col, NULL);
}
}
cursor_toggle_anchor(cursor, 1);
);
return MLE_OK;
}
// Search for a regex
int cmd_search(cmd_context_t *ctx) {
char *regex;
int regex_len;
mark_t *search_mark;
editor_prompt(ctx->editor, "search: Regex?", NULL, ®ex);
if (!regex) return MLE_OK;
regex_len = strlen(regex);
search_mark = buffer_add_mark(ctx->bview->buffer, NULL, 0);
MLE_MULTI_CURSOR_CODE(ctx->cursor,
_cmd_search_next(ctx->bview, cursor, search_mark, regex, regex_len);
);
mark_destroy(search_mark);
if (ctx->bview->last_search) free(ctx->bview->last_search);
ctx->bview->last_search = regex;
return MLE_OK;
}
// Search for next instance of last search regex
int cmd_search_next(cmd_context_t *ctx) {
int regex_len;
mark_t *search_mark;
if (!ctx->bview->last_search) return MLE_OK;
regex_len = strlen(ctx->bview->last_search);
search_mark = buffer_add_mark(ctx->bview->buffer, NULL, 0);
MLE_MULTI_CURSOR_CODE(ctx->cursor,
_cmd_search_next(ctx->bview, cursor, search_mark, ctx->bview->last_search, regex_len);
);
mark_destroy(search_mark);
return MLE_OK;
}
// Interactive search and replace
int cmd_replace(cmd_context_t *ctx) {
return cursor_replace(ctx->cursor, 1, NULL, NULL);
}
// Redraw screen
int cmd_redraw(cmd_context_t *ctx) {
bview_center_viewport_y(ctx->bview);
_cmd_force_redraw(ctx);
return MLE_OK;
}
// Zero viewport y
int cmd_viewport_top(cmd_context_t *ctx) {
bview_zero_viewport_y(ctx->bview);
return MLE_OK;
}
// Center viewport y
int cmd_viewport_mid(cmd_context_t *ctx) {
bview_center_viewport_y(ctx->bview);
return MLE_OK;
}
// Max viewport y
int cmd_viewport_bot(cmd_context_t *ctx) {
bview_max_viewport_y(ctx->bview);
return MLE_OK;
}
// Toggle between top and mid viewport y
int cmd_viewport_toggle(cmd_context_t *ctx) {
bline_t *orig;
bline_t *mid;
bline_t *top;
orig = ctx->bview->viewport_bline;
cmd_viewport_mid(ctx); mid = ctx->bview->viewport_bline;
cmd_viewport_top(ctx); top = ctx->bview->viewport_bline;
if (mid == orig) {
cmd_viewport_top(ctx);
} else if (top == orig) {
cmd_viewport_bot(ctx);
} else {
cmd_viewport_mid(ctx);
}
return MLE_OK;
}
// Find next occurence of word under cursor
int cmd_find_word(cmd_context_t *ctx) {
char *re;
char *word;
bint_t re_len;
bint_t word_len;
MLE_MULTI_CURSOR_CODE(ctx->cursor,
if (cursor_select_by(cursor, "word", 0) == MLE_OK) {
mark_get_between_mark(cursor->mark, cursor->anchor, &word, &word_len);
re_len = asprintf(&re, "\\b%s\\b", word);
free(word);
cursor_toggle_anchor(cursor, 0);
if (mark_move_next_re(cursor->mark, re, re_len) == MLBUF_ERR) {
mark_move_beginning(cursor->mark);
mark_move_next_re(cursor->mark, re, re_len);
}
free(re);
}
);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Incremental search
int cmd_isearch(cmd_context_t *ctx) {
editor_prompt(ctx->editor, "isearch: Regex?", &(editor_prompt_params_t) {
.kmap = ctx->editor->kmap_prompt_isearch,
.prompt_cb = _cmd_isearch_prompt_cb
}, NULL);
if (ctx->bview->isearch_rule) {
buffer_remove_srule(ctx->bview->buffer, ctx->bview->isearch_rule);
srule_destroy(ctx->bview->isearch_rule);
ctx->bview->isearch_rule = NULL;
}
return MLE_OK;
}
// Cut text
int cmd_cut(cmd_context_t *ctx) {
int append;
append = ctx->loop_ctx->last_cmd && ctx->loop_ctx->last_cmd->func == cmd_cut ? 1 : 0;
MLE_MULTI_CURSOR_CODE(ctx->cursor,
cursor_cut_copy(cursor, 1, 1, append);
);
return MLE_OK;
}
// Copy text
int cmd_copy(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
cursor_cut_copy(cursor, 0, 1, 0);
);
return MLE_OK;
}
// Paste text
int cmd_uncut(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
cursor_uncut(cursor);
);
return MLE_OK;
}
// Copy in between chars
int cmd_copy_by(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
if (cursor_select_by(cursor, ctx->static_param, 0) == MLE_OK) {
cursor_cut_copy(cursor, 0, 0, 0);
}
);
return MLE_OK;
}
// Cut in between chars
int cmd_cut_by(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
if (cursor_select_by(cursor, ctx->static_param, 0) == MLE_OK) {
cursor_cut_copy(cursor, 1, 0, 0);
}
);
return MLE_OK;
}
// Anchor between chars
int cmd_anchor_by(cmd_context_t *ctx) {
MLE_MULTI_CURSOR_CODE(ctx->cursor,
cursor_select_by(cursor, ctx->static_param, 1);
);
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Go to next bview
int cmd_next(cmd_context_t *ctx) {
editor_set_active(ctx->editor, ctx->bview->all_next);
return MLE_OK;
}
// Go to prev bview
int cmd_prev(cmd_context_t *ctx) {
editor_set_active(ctx->editor, ctx->bview->all_prev);
return MLE_OK;
}
// Split a bview vertically
int cmd_split_vertical(cmd_context_t *ctx) {
bview_t *child;
if (bview_split(ctx->bview, 1, 0.5, &child) == MLE_OK) {
editor_set_active(ctx->editor, child);
}
return MLE_OK;
}
// Split a bview horizontally
int cmd_split_horizontal(cmd_context_t *ctx) {
bview_t *child;
if (bview_split(ctx->bview, 0, 0.5, &child) == MLE_OK) {
editor_set_active(ctx->editor, child);
}
return MLE_OK;
}
// Fuzzy path search via fzf
int cmd_fsearch(cmd_context_t *ctx) {
return _cmd_fsearch_inner(ctx, "fzf");
}
// Fuzzy path search via fzy
int cmd_fsearch_fzy(cmd_context_t *ctx) {
char shell_cmd[32];
int nlines;
nlines = tb_height();
nlines = MLE_MAX(MLE_MIN(nlines, 9999), 3);
sprintf(shell_cmd, "find . -type f | fzy -l %d", nlines);
return _cmd_fsearch_inner(ctx, shell_cmd);
}
// Grep for pattern in cwd
int cmd_grep(cmd_context_t *ctx) {
aproc_t *aproc;
char *path;
char *path_arg;
char *cmd;
char *grep_fmt;
editor_prompt(ctx->editor, "grep: Pattern?", NULL, &path);
if (!path) return MLE_OK;
if (ctx->static_param) {
grep_fmt = ctx->static_param;
} else {
#ifdef __APPLE__
grep_fmt = "grep --color=never -E -i -I -n -r %s . 2>/dev/null";
#else
grep_fmt = "grep --color=never -P -i -I -n -r %s . 2>/dev/null";
#endif
}
path_arg = util_escape_shell_arg(path, strlen(path));
free(path);
asprintf(&cmd, grep_fmt, path_arg);
free(path_arg);
if (!cmd) {
MLE_RETURN_ERR(ctx->editor, "Failed to format grep cmd: %s", grep_fmt);
}
aproc = aproc_new(ctx->editor, ctx->bview, &(ctx->bview->aproc), cmd, 0, _cmd_aproc_bview_passthru_cb);
free(cmd);
if (!aproc) return MLE_ERR;
editor_menu(ctx->editor, _cmd_menu_grep_cb, NULL, 0, aproc, NULL);
return MLE_OK;
}
// Invoke ctag search
int cmd_ctag(cmd_context_t *ctx) {
aproc_t *aproc;
char *word;
char *word_arg;
char *cmd;
bint_t word_len;
if (cursor_select_by(ctx->cursor, "word", 0) != MLE_OK) {
MLE_RETURN_ERR(ctx->editor, "%s", "Failed to select word under cursor");
}
mark_get_between_mark(ctx->cursor->mark, ctx->cursor->anchor, &word, &word_len);
cursor_toggle_anchor(ctx->cursor, 0);
word_arg = util_escape_shell_arg(word, word_len);
free(word);
asprintf(&cmd, "readtags -e - %s 2>/dev/null", word_arg);
free(word_arg);
if (!cmd) {
MLE_RETURN_ERR(ctx->editor, "%s", "Failed to format readtags cmd");
}
aproc = aproc_new(ctx->editor, ctx->bview, &(ctx->bview->aproc), cmd, 0, _cmd_aproc_bview_passthru_cb);
free(cmd);
if (!aproc) return MLE_ERR;
editor_menu(ctx->editor, _cmd_menu_ctag_cb, NULL, 0, aproc, NULL);
return MLE_OK;
}
// Browse directory via tree
int cmd_browse(cmd_context_t *ctx) {
bview_t *menu;
aproc_t *aproc;
char *cmd;
asprintf(&cmd, "tree --charset C -n -f -L 2 %s 2>/dev/null", ctx->static_param ? ctx->static_param : "");
aproc = aproc_new(ctx->editor, ctx->bview, &(ctx->bview->aproc), cmd, 0, _cmd_aproc_bview_passthru_cb);
free(cmd);
if (!aproc) return MLE_ERR;
editor_menu(ctx->editor, _cmd_menu_browse_cb, "..\n", 3, aproc, &menu);
mark_move_beginning(menu->active_cursor->mark);
bview_zero_viewport_y(menu);
return MLE_OK;
}
// Buffer list
int cmd_blist(cmd_context_t *ctx) {
bview_t *bview;
bview_t *menu;
int path_max;
char *path;
int bview_count;
str_t blist = {0};
path_max = 1;
CDL_FOREACH2(ctx->editor->all_bviews, bview, all_next) {
if (!MLE_BVIEW_IS_EDIT(bview)) continue;
if (bview->buffer->path && (int)strlen(bview->buffer->path) > path_max) {
path_max = (int)strlen(bview->buffer->path);
}
}
bview_count = 0;
CDL_FOREACH2(ctx->editor->all_bviews, bview, all_next) {
if (!MLE_BVIEW_IS_EDIT(bview)) continue;
bview_count += 1;
path = bview->buffer->path ? bview->buffer->path : bview->path;
if (!path || strlen(path) < 1) path = "-";
str_sprintf(
&blist,
" #%-4d %-*s (unsaved=%s len=%" PRIdMAX " buffer=%p bview=%p id=%d)\n",
bview_count,
path_max,
path,
bview->buffer ? (bview->buffer->is_unsaved ? "y" : "n") : "y",
bview->buffer->byte_count,
bview->buffer,
bview,
bview->id
);
}
editor_menu(ctx->editor, _cmd_menu_blist_cb, blist.data, blist.len, NULL, &menu);
mark_move_beginning(menu->active_cursor->mark);
bview_zero_viewport_y(menu);
str_free(&blist);
return MLE_OK;
}
// Save-as file
int cmd_save_as(cmd_context_t *ctx) {
_cmd_save(ctx->editor, ctx->bview, 1);
return MLE_OK;
}
// Save file
int cmd_save(cmd_context_t *ctx) {
_cmd_save(ctx->editor, ctx->bview, 0);
return MLE_OK;
}
// Open file in a new bview
int cmd_open_file(cmd_context_t *ctx) {
char *path;
editor_prompt(ctx->editor, "new_open: File?", NULL, &path);
if (!path) return MLE_OK;
editor_open_bview(ctx->editor, NULL, MLE_BVIEW_TYPE_EDIT, path, strlen(path), 1, 0, 0, NULL, NULL);
free(path);
return MLE_OK;
}
// Open empty buffer in a new bview
int cmd_open_new(cmd_context_t *ctx) {
editor_open_bview(ctx->editor, NULL, MLE_BVIEW_TYPE_EDIT, NULL, 0, 1, 0, 0, NULL, NULL);
return MLE_OK;
}
// Open file into current bview
int cmd_open_replace_file(cmd_context_t *ctx) {
char *path;
if (_cmd_pre_close(ctx->editor, ctx->bview) == MLE_ERR) return MLE_OK;
path = NULL;
editor_prompt(ctx->editor, "replace_open: Path?", NULL, &path);
if (!path) return MLE_OK;
bview_open(ctx->bview, path, strlen(path));
free(path);
return MLE_OK;
}
// Open empty buffer into current bview
int cmd_open_replace_new(cmd_context_t *ctx) {
if (_cmd_pre_close(ctx->editor, ctx->bview) == MLE_ERR) return MLE_OK;
bview_open(ctx->bview, NULL, 0);
return MLE_OK;
}
// Close bview
int cmd_close(cmd_context_t *ctx) {
int num_open;
int num_closed;
if (_cmd_pre_close(ctx->editor, ctx->bview) == MLE_ERR) return MLE_OK;
num_open = editor_bview_edit_count(ctx->editor);
editor_close_bview(ctx->editor, ctx->bview, &num_closed);
ctx->loop_ctx->should_exit = num_closed == num_open ? 1 : 0;
return MLE_OK;
}
// Quit editor
int cmd_quit(cmd_context_t *ctx) {
bview_t *bview;
bview_t *tmp;
if (ctx->editor->loop_depth > 1) return MLE_OK;
DL_FOREACH_SAFE2(ctx->editor->top_bviews, bview, tmp, top_next) {
if (!MLE_BVIEW_IS_EDIT(bview)) {
continue;
} else if (_cmd_quit_inner(ctx->editor, bview) == MLE_ERR) {
return MLE_OK;
}
}
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Quit editor without saving
int cmd_quit_without_saving(cmd_context_t *ctx) {
ctx->loop_ctx->should_exit = 1;
return MLE_OK;
}
// Apply a macro with single-char name
int cmd_apply_macro_by(cmd_context_t *ctx) {
kmacro_t *macro;
uint32_t ch;
char name[6] = { 0 };
if (ctx->editor->macro_apply) MLE_RETURN_ERR(ctx->editor, "Cannot nest macros%s", "");
ch = MLE_PARAM_WILDCARD(ctx, 0);
if (!ch) return MLE_OK;
utf8_unicode_to_char(name, ch);
HASH_FIND_STR(ctx->editor->macro_map, name, macro);
if (!macro) MLE_RETURN_ERR(ctx->editor, "Macro not found with name '%s'", name);
ctx->editor->macro_apply = macro;
ctx->editor->macro_apply_input_index = 0;
return MLE_OK;
}
// Apply a macro
int cmd_apply_macro(cmd_context_t *ctx) {
char *name;
kmacro_t *macro;
if (ctx->editor->macro_apply) MLE_RETURN_ERR(ctx->editor, "Cannot nest macros%s", "");
editor_prompt(ctx->editor, "apply_macro: Name?", NULL, &name);
if (!name) return MLE_OK;
HASH_FIND_STR(ctx->editor->macro_map, name, macro);
free(name);
if (!macro) MLE_RETURN_ERR(ctx->editor, "Macro not found%s", "");
ctx->editor->macro_apply = macro;
ctx->editor->macro_apply_input_index = 0;
return MLE_OK;
}
// No-op
int cmd_noop(cmd_context_t *ctx) {
(void)ctx;
return MLE_OK;
}
// Move forward til a certain char
int cmd_move_until_forward(cmd_context_t *ctx) {
uint32_t ch;
char str[6] = { 0 };
ch = MLE_PARAM_WILDCARD(ctx, 0);
if (!ch) return MLE_OK;
utf8_unicode_to_char(str, ch);
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_next_str_nudge, str, strlen(str));
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Move back til a certain char
int cmd_move_until_back(cmd_context_t *ctx) {
uint32_t ch;
char str[6] = { 0 };
ch = MLE_PARAM_WILDCARD(ctx, 0);
if (!ch) return MLE_OK;
utf8_unicode_to_char(str, ch);
MLE_MULTI_CURSOR_MARK_FN(ctx->cursor, mark_move_prev_str, str, strlen(str));
bview_rectify_viewport(ctx->bview);
return MLE_OK;
}
// Undo
int cmd_undo(cmd_context_t *ctx) {
buffer_undo(ctx->bview->buffer);
return MLE_OK;
}
// Redo
int cmd_redo(cmd_context_t *ctx) {
buffer_redo(ctx->bview->buffer);
return MLE_OK;
}
// Indent line(s)
int cmd_indent(cmd_context_t *ctx) {
return _cmd_indent(ctx, 0);
}
// Outdent line(s)
int cmd_outdent(cmd_context_t *ctx) {
return _cmd_indent(ctx, 1);
}
// Set option
int cmd_set_opt(cmd_context_t *ctx) {
char *prompt;
char *val;
int vali;
if (!ctx->static_param) return MLE_ERR;
asprintf(&prompt, "set_opt: %s?", ctx->static_param);
editor_prompt(ctx->editor, prompt, NULL, &val);
free(prompt);
if (!val) return MLE_OK;
vali = atoi(val);
if (strcmp(ctx->static_param, "tab_to_space") == 0) {
ctx->bview->tab_to_space = vali ? 1 : 0;
} else if (strcmp(ctx->static_param, "tab_width") == 0) {
ctx->bview->tab_width = MLE_MAX(vali, 1);
buffer_set_tab_width(ctx->bview->buffer, ctx->bview->tab_width);
} else if (strcmp(ctx->static_param, "syntax") == 0) {
bview_set_syntax(ctx->bview, val);
buffer_apply_styles(ctx->bview->buffer, ctx->bview->buffer->first_line, ctx->bview->buffer->line_count);
} else if (strcmp(ctx->static_param, "soft_wrap") == 0) {
ctx->bview->soft_wrap = vali ? 1 : 0;
}
return MLE_OK;
}
// Shell
int cmd_shell(cmd_context_t *ctx) {
char *cmd;
// Get shell cmd
if (ctx->static_param) {
cmd = strdup(ctx->static_param);
} else {
editor_prompt(ctx->editor, "shell: Cmd?", NULL, &cmd);
if (!cmd) return MLE_OK;
}
// Apply shell cmd
_cmd_shell_apply_cmd(ctx, cmd);
free(cmd);
return MLE_OK;
}
// Perl
int cmd_perl(cmd_context_t *ctx) {
char *code;
char *code_escaped;
char *cmd;
// Get perl code
if (ctx->static_param) {
code = strdup(ctx->static_param);
} else {
editor_prompt(ctx->editor, "perl: Code?", NULL, &code);
if (!code) return MLE_OK;
}
// Shell escape code
code_escaped = util_escape_shell_arg(code, strlen(code));
free(code);
// Format cmd
asprintf(&cmd, "perl -lp -E 'BEGIN{$i=0}' -E %s 2>/dev/null", code_escaped);
free(code_escaped);
// Apply perl cmd
_cmd_shell_apply_cmd(ctx, cmd);
free(cmd);
return MLE_OK;
}
// Jump to a `\S{2,}` on screen via `[a-z][a-z]`
int cmd_jump(cmd_context_t *ctx) {
bline_t *bline;
bint_t col;
bint_t nchars;
bint_t stop_line_index;
mark_t *mark;
mark_t *jumps;
int jumpi, jumpt, screen_x, screen_y;
char jumpa[3];
kinput_t ev[2];
int headless;
// Check if headless
headless = ctx->editor->headless_mode ? 1 : 0;
// Set boundaries
mark_clone(ctx->cursor->mark, &mark);
if (headless) {
mark_move_bol(mark);
stop_line_index = mark->bline->line_index + 1;
} else {
mark_move_to_w_bline(mark, ctx->bview->viewport_bline, 0);
stop_line_index = ctx->bview->viewport_bline->line_index + ctx->bview->rect_buffer.h;
}
// Make jump map
jumps = calloc(26*26, sizeof(mark_t));
jumpi = 0;
do {
// Loop for words
while (jumpi < 26*26 && mark_move_next_re_ex(mark, "\\S{2,}", strlen("\\S{2,}"), &bline, &col, &nchars) == MLBUF_OK) {
if (bline->line_index >= stop_line_index) break;
jumps[jumpi].bline = bline;
jumps[jumpi].col = col;
mark_move_by(mark, MLE_MAX(0, nchars - 2));
if (!headless) {
bview_get_screen_coords(ctx->bview, mark, &screen_x, &screen_y, NULL);
sprintf(jumpa, "%c%c", 'a' + (jumpi / 26), 'a' + (jumpi % 26));
tb_print(screen_x, screen_y, TB_WHITE | TB_BOLD, TB_MAGENTA, jumpa);
}
jumpi += 1;
mark_move_by(mark, 2);
}
if (jumpi < 1) break;
if (!headless) tb_present();