forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.def.h
1171 lines (1107 loc) · 52.3 KB
/
config.def.h
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
/** start by reading from the top of vis.c up until config.h is included */
#define DEFAULT_TERM "xterm" /* default term to use if $TERM isn't set */
/* macros used to specify keys for key bindings */
#define ESC 0x1B
#define NONE(k) { .str = { k }, .code = 0 }
#define KEY(k) { .str = { '\0' }, .code = KEY_##k }
#define CONTROL(k) NONE((k)&0x1F)
#define META(k) { .str = { ESC, (k) }, .code = 0 }
#define BACKSPACE(func, name, arg) \
{ { KEY(BACKSPACE) }, (func), { .name = (arg) } }, \
{ { CONTROL('H') }, (func), { .name = (arg) } }, \
{ { NONE(127) }, (func), { .name = (arg) } }, \
{ { CONTROL('B') }, (func), { .name = (arg) } }
/* a mode contains a set of key bindings which are currently valid.
*
* each mode can specify one parent mode which is consultated if a given key
* is not found in the current mode. hence the modes form a tree which is
* searched from the current mode up towards the root mode until a valid binding
* is found.
*
* if no binding is found, mode->input(...) is called and the user entered
* keys are passed as argument. this is used to change the document content.
*/
static Mode vis_modes[];
enum {
VIS_MODE_BASIC,
VIS_MODE_MARK,
VIS_MODE_MARK_LINE,
VIS_MODE_MARK_SET,
VIS_MODE_MOVE,
VIS_MODE_TEXTOBJ,
VIS_MODE_INNER_TEXTOBJ,
VIS_MODE_OPERATOR,
VIS_MODE_OPERATOR_OPTION,
VIS_MODE_REGISTER,
VIS_MODE_NORMAL,
VIS_MODE_VISUAL,
VIS_MODE_VISUAL_LINE,
VIS_MODE_READLINE,
VIS_MODE_PROMPT,
VIS_MODE_INSERT_REGISTER,
VIS_MODE_INSERT,
VIS_MODE_REPLACE,
};
/* command recognized at the ':'-prompt. tested top to bottom, first match wins. */
static Command cmds[] = {
{ "^[0-9]+$", cmd_gotoline, false },
{ "^bd(elete)?!?$", cmd_bdelete, false },
{ "^e(dit)?!?$", cmd_edit, false },
{ "^new$", cmd_new, false },
{ "^o(pen)?$", cmd_open, false },
{ "^qa(ll)?!?$", cmd_qall, false },
{ "^q(quit)?!?$", cmd_quit, false },
{ "^r(ead)?$", cmd_read, false },
{ "^sav(as)?$", cmd_saveas, false },
{ "^set?$", cmd_set, true },
{ "^sp(lit)?$", cmd_split, false },
{ "^s(ubstitute)?$", cmd_substitute, false },
{ "^vnew?$", cmd_vnew, false },
{ "^v(split)?$", cmd_vsplit, false },
{ "^wq!?$", cmd_wq, false },
{ "^w(rite)?$", cmd_write, false },
{ /* array terminator */ },
};
/* draw a statubar, do whatever you want with win->statuswin curses window */
static void statusbar(EditorWin *win) {
size_t line, col;
bool focused = vis->win == win || vis->prompt->editor == win;
window_cursor_getxy(win->win, &line, &col);
wattrset(win->statuswin, focused ? A_REVERSE|A_BOLD : A_REVERSE);
mvwhline(win->statuswin, 0, 0, ' ', win->width);
mvwprintw(win->statuswin, 0, 0, "%s %s %s %s",
mode->name && mode->name[0] == '-' ? mode->name : "",
text_filename_get(win->text),
text_modified(win->text) ? "[+]" : "",
vis->recording ? "recording": "");
char buf[win->width + 1];
int len = snprintf(buf, win->width, "%d, %d", line, col);
if (len > 0) {
buf[len] = '\0';
mvwaddstr(win->statuswin, 0, win->width - len - 1, buf);
}
}
/* called before any other keybindings are checked, if the function returns false
* the key is completely ignored. */
static bool vis_keypress(Key *key) {
editor_info_hide(vis);
if (vis->recording)
macro_append(vis->recording, key, sizeof(*key));
return true;
}
static KeyBinding basic_movement[] = {
{ { CONTROL('Z') }, suspend, { NULL } },
{ { KEY(LEFT) }, movement, { .i = MOVE_CHAR_PREV } },
{ { KEY(SLEFT) }, movement, { .i = MOVE_LONGWORD_START_PREV } },
{ { KEY(RIGHT) }, movement, { .i = MOVE_CHAR_NEXT } },
{ { KEY(SRIGHT) }, movement, { .i = MOVE_LONGWORD_START_NEXT } },
{ { KEY(UP) }, movement, { .i = MOVE_SCREEN_LINE_UP } },
{ { KEY(DOWN) }, movement, { .i = MOVE_SCREEN_LINE_DOWN } },
{ { KEY(PPAGE) }, wscroll, { .i = -PAGE } },
{ { KEY(NPAGE) }, wscroll, { .i = +PAGE } },
{ { KEY(HOME) }, movement, { .i = MOVE_LINE_START } },
{ { KEY(END) }, movement, { .i = MOVE_LINE_FINISH } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_movements[] = {
BACKSPACE( movement, i, MOVE_CHAR_PREV ),
{ { NONE('h') }, movement, { .i = MOVE_CHAR_PREV } },
{ { NONE(' ') }, movement, { .i = MOVE_CHAR_NEXT } },
{ { NONE('l') }, movement, { .i = MOVE_CHAR_NEXT } },
{ { NONE('k') }, movement, { .i = MOVE_SCREEN_LINE_UP } },
{ { CONTROL('P') }, movement, { .i = MOVE_SCREEN_LINE_UP } },
{ { NONE('j') }, movement, { .i = MOVE_SCREEN_LINE_DOWN } },
{ { CONTROL('J') }, movement, { .i = MOVE_SCREEN_LINE_DOWN } },
{ { CONTROL('N') }, movement, { .i = MOVE_SCREEN_LINE_DOWN } },
{ { KEY(ENTER) }, movement, { .i = MOVE_SCREEN_LINE_DOWN } },
{ { NONE('^') }, movement, { .i = MOVE_LINE_START } },
{ { NONE('g'), NONE('_') }, movement, { .i = MOVE_LINE_FINISH } },
{ { NONE('$') }, movement, { .i = MOVE_LINE_LASTCHAR } },
{ { NONE('%') }, movement, { .i = MOVE_BRACKET_MATCH } },
{ { NONE('b') }, movement, { .i = MOVE_WORD_START_PREV } },
{ { NONE('B') }, movement, { .i = MOVE_LONGWORD_START_PREV } },
{ { NONE('w') }, movement, { .i = MOVE_WORD_START_NEXT } },
{ { NONE('W') }, movement, { .i = MOVE_LONGWORD_START_NEXT } },
{ { NONE('g'), NONE('e') }, movement, { .i = MOVE_WORD_END_PREV } },
{ { NONE('g'), NONE('E') }, movement, { .i = MOVE_LONGWORD_END_PREV } },
{ { NONE('e') }, movement, { .i = MOVE_WORD_END_NEXT } },
{ { NONE('E') }, movement, { .i = MOVE_LONGWORD_END_NEXT } },
{ { NONE('{') }, movement, { .i = MOVE_PARAGRAPH_PREV } },
{ { NONE('}') }, movement, { .i = MOVE_PARAGRAPH_NEXT } },
{ { NONE('(') }, movement, { .i = MOVE_SENTENCE_PREV } },
{ { NONE(')') }, movement, { .i = MOVE_SENTENCE_NEXT } },
{ { NONE('g'), NONE('g') }, gotoline, { .i = -1 } },
{ { NONE('g'), NONE('0') }, movement, { .i = MOVE_SCREEN_LINE_BEGIN } },
{ { NONE('g'), NONE('m') }, movement, { .i = MOVE_SCREEN_LINE_MIDDLE } },
{ { NONE('g'), NONE('$') }, movement, { .i = MOVE_SCREEN_LINE_END } },
{ { NONE('G') }, gotoline, { .i = +1 } },
{ { NONE('|') }, movement, { .i = MOVE_COLUMN } },
{ { NONE('n') }, movement, { .i = MOVE_SEARCH_FORWARD } },
{ { NONE('N') }, movement, { .i = MOVE_SEARCH_BACKWARD } },
{ { NONE('H') }, movement, { .i = MOVE_WINDOW_LINE_TOP } },
{ { NONE('M') }, movement, { .i = MOVE_WINDOW_LINE_MIDDLE } },
{ { NONE('L') }, movement, { .i = MOVE_WINDOW_LINE_BOTTOM } },
{ { NONE('*') }, movement, { .i = MOVE_SEARCH_WORD_FORWARD } },
{ { NONE('#') }, movement, { .i = MOVE_SEARCH_WORD_BACKWARD} },
{ { NONE('f') }, movement_key, { .i = MOVE_RIGHT_TO } },
{ { NONE('F') }, movement_key, { .i = MOVE_LEFT_TO } },
{ { NONE('t') }, movement_key, { .i = MOVE_RIGHT_TILL } },
{ { NONE('T') }, movement_key, { .i = MOVE_LEFT_TILL } },
{ { NONE('/') }, prompt_search,{ .s = "/" } },
{ { NONE('?') }, prompt_search,{ .s = "?" } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_textobjs[] = {
{ { NONE('a'), NONE('w') }, textobj, { .i = TEXT_OBJ_OUTER_WORD } },
{ { NONE('a'), NONE('W') }, textobj, { .i = TEXT_OBJ_OUTER_LONGWORD } },
{ { NONE('a'), NONE('s') }, textobj, { .i = TEXT_OBJ_SENTENCE } },
{ { NONE('a'), NONE('p') }, textobj, { .i = TEXT_OBJ_PARAGRAPH } },
{ { NONE('a'), NONE('[') }, textobj, { .i = TEXT_OBJ_OUTER_SQUARE_BRACKET } },
{ { NONE('a'), NONE(']') }, textobj, { .i = TEXT_OBJ_OUTER_SQUARE_BRACKET } },
{ { NONE('a'), NONE('(') }, textobj, { .i = TEXT_OBJ_OUTER_PARANTHESE } },
{ { NONE('a'), NONE(')') }, textobj, { .i = TEXT_OBJ_OUTER_PARANTHESE } },
{ { NONE('a'), NONE('b') }, textobj, { .i = TEXT_OBJ_OUTER_PARANTHESE } },
{ { NONE('a'), NONE('<') }, textobj, { .i = TEXT_OBJ_OUTER_ANGLE_BRACKET } },
{ { NONE('a'), NONE('>') }, textobj, { .i = TEXT_OBJ_OUTER_ANGLE_BRACKET } },
{ { NONE('a'), NONE('{') }, textobj, { .i = TEXT_OBJ_OUTER_CURLY_BRACKET } },
{ { NONE('a'), NONE('}') }, textobj, { .i = TEXT_OBJ_OUTER_CURLY_BRACKET } },
{ { NONE('a'), NONE('B') }, textobj, { .i = TEXT_OBJ_OUTER_CURLY_BRACKET } },
{ { NONE('a'), NONE('"') }, textobj, { .i = TEXT_OBJ_OUTER_QUOTE } },
{ { NONE('a'), NONE('\'') }, textobj, { .i = TEXT_OBJ_OUTER_SINGLE_QUOTE } },
{ { NONE('a'), NONE('`') }, textobj, { .i = TEXT_OBJ_OUTER_BACKTICK } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_inner_textobjs[] = {
{ { NONE('i'), NONE('w') }, textobj, { .i = TEXT_OBJ_INNER_WORD } },
{ { NONE('i'), NONE('W') }, textobj, { .i = TEXT_OBJ_INNER_LONGWORD } },
{ { NONE('i'), NONE('s') }, textobj, { .i = TEXT_OBJ_SENTENCE } },
{ { NONE('i'), NONE('p') }, textobj, { .i = TEXT_OBJ_PARAGRAPH } },
{ { NONE('i'), NONE('[') }, textobj, { .i = TEXT_OBJ_INNER_SQUARE_BRACKET } },
{ { NONE('i'), NONE(']') }, textobj, { .i = TEXT_OBJ_INNER_SQUARE_BRACKET } },
{ { NONE('i'), NONE('(') }, textobj, { .i = TEXT_OBJ_INNER_PARANTHESE } },
{ { NONE('i'), NONE(')') }, textobj, { .i = TEXT_OBJ_INNER_PARANTHESE } },
{ { NONE('i'), NONE('b') }, textobj, { .i = TEXT_OBJ_INNER_PARANTHESE } },
{ { NONE('i'), NONE('<') }, textobj, { .i = TEXT_OBJ_INNER_ANGLE_BRACKET } },
{ { NONE('i'), NONE('>') }, textobj, { .i = TEXT_OBJ_INNER_ANGLE_BRACKET } },
{ { NONE('i'), NONE('{') }, textobj, { .i = TEXT_OBJ_INNER_CURLY_BRACKET } },
{ { NONE('i'), NONE('}') }, textobj, { .i = TEXT_OBJ_INNER_CURLY_BRACKET } },
{ { NONE('i'), NONE('B') }, textobj, { .i = TEXT_OBJ_INNER_CURLY_BRACKET } },
{ { NONE('i'), NONE('"') }, textobj, { .i = TEXT_OBJ_INNER_QUOTE } },
{ { NONE('i'), NONE('\'') }, textobj, { .i = TEXT_OBJ_INNER_SINGLE_QUOTE } },
{ { NONE('i'), NONE('`') }, textobj, { .i = TEXT_OBJ_INNER_BACKTICK } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_operators[] = {
{ { NONE('0') }, zero, { NULL } },
{ { NONE('1') }, count, { .i = 1 } },
{ { NONE('2') }, count, { .i = 2 } },
{ { NONE('3') }, count, { .i = 3 } },
{ { NONE('4') }, count, { .i = 4 } },
{ { NONE('5') }, count, { .i = 5 } },
{ { NONE('6') }, count, { .i = 6 } },
{ { NONE('7') }, count, { .i = 7 } },
{ { NONE('8') }, count, { .i = 8 } },
{ { NONE('9') }, count, { .i = 9 } },
{ { NONE('d') }, operator, { .i = OP_DELETE } },
{ { NONE('c') }, operator, { .i = OP_CHANGE } },
{ { NONE('y') }, operator, { .i = OP_YANK } },
{ { NONE('p') }, put, { .i = +1 } },
{ { NONE('P') }, put, { .i = -1 } },
{ { NONE('>') }, operator, { .i = OP_SHIFT_RIGHT } },
{ { NONE('<') }, operator, { .i = OP_SHIFT_LEFT } },
{ { NONE('g'), NONE('U') }, changecase, { .i = +1 } },
{ { NONE('~') }, changecase, { .i = 0 } },
{ { NONE('g'), NONE('u') }, changecase, { .i = -1 } },
{ /* empty last element, array terminator */ },
};
static void vis_mode_operator_enter(Mode *old) {
vis_modes[VIS_MODE_OPERATOR].parent = &vis_modes[VIS_MODE_OPERATOR_OPTION];
}
static void vis_mode_operator_leave(Mode *new) {
vis_modes[VIS_MODE_OPERATOR].parent = &vis_modes[VIS_MODE_MOVE];
}
static void vis_mode_operator_input(const char *str, size_t len) {
/* invalid operator */
action_reset(&action);
switchmode_to(mode_prev);
}
static KeyBinding vis_operator_options[] = {
{ { NONE('v') }, linewise, { .b = false } },
{ { NONE('V') }, linewise, { .b = true } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_registers[] = { /* {a-zA-Z0-9.%#:-"} */
{ { NONE('"'), NONE('a') }, reg, { .i = REG_a } },
{ { NONE('"'), NONE('b') }, reg, { .i = REG_b } },
{ { NONE('"'), NONE('c') }, reg, { .i = REG_c } },
{ { NONE('"'), NONE('d') }, reg, { .i = REG_d } },
{ { NONE('"'), NONE('e') }, reg, { .i = REG_e } },
{ { NONE('"'), NONE('f') }, reg, { .i = REG_f } },
{ { NONE('"'), NONE('g') }, reg, { .i = REG_g } },
{ { NONE('"'), NONE('h') }, reg, { .i = REG_h } },
{ { NONE('"'), NONE('i') }, reg, { .i = REG_i } },
{ { NONE('"'), NONE('j') }, reg, { .i = REG_j } },
{ { NONE('"'), NONE('k') }, reg, { .i = REG_k } },
{ { NONE('"'), NONE('l') }, reg, { .i = REG_l } },
{ { NONE('"'), NONE('m') }, reg, { .i = REG_m } },
{ { NONE('"'), NONE('n') }, reg, { .i = REG_n } },
{ { NONE('"'), NONE('o') }, reg, { .i = REG_o } },
{ { NONE('"'), NONE('p') }, reg, { .i = REG_p } },
{ { NONE('"'), NONE('q') }, reg, { .i = REG_q } },
{ { NONE('"'), NONE('r') }, reg, { .i = REG_r } },
{ { NONE('"'), NONE('s') }, reg, { .i = REG_s } },
{ { NONE('"'), NONE('t') }, reg, { .i = REG_t } },
{ { NONE('"'), NONE('u') }, reg, { .i = REG_u } },
{ { NONE('"'), NONE('v') }, reg, { .i = REG_v } },
{ { NONE('"'), NONE('w') }, reg, { .i = REG_w } },
{ { NONE('"'), NONE('x') }, reg, { .i = REG_x } },
{ { NONE('"'), NONE('y') }, reg, { .i = REG_y } },
{ { NONE('"'), NONE('z') }, reg, { .i = REG_z } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_marks[] = {
{ { NONE('`'), NONE('a') }, mark, { .i = MARK_a } },
{ { NONE('`'), NONE('b') }, mark, { .i = MARK_b } },
{ { NONE('`'), NONE('c') }, mark, { .i = MARK_c } },
{ { NONE('`'), NONE('d') }, mark, { .i = MARK_d } },
{ { NONE('`'), NONE('e') }, mark, { .i = MARK_e } },
{ { NONE('`'), NONE('f') }, mark, { .i = MARK_f } },
{ { NONE('`'), NONE('g') }, mark, { .i = MARK_g } },
{ { NONE('`'), NONE('h') }, mark, { .i = MARK_h } },
{ { NONE('`'), NONE('i') }, mark, { .i = MARK_i } },
{ { NONE('`'), NONE('j') }, mark, { .i = MARK_j } },
{ { NONE('`'), NONE('k') }, mark, { .i = MARK_k } },
{ { NONE('`'), NONE('l') }, mark, { .i = MARK_l } },
{ { NONE('`'), NONE('m') }, mark, { .i = MARK_m } },
{ { NONE('`'), NONE('n') }, mark, { .i = MARK_n } },
{ { NONE('`'), NONE('o') }, mark, { .i = MARK_o } },
{ { NONE('`'), NONE('p') }, mark, { .i = MARK_p } },
{ { NONE('`'), NONE('q') }, mark, { .i = MARK_q } },
{ { NONE('`'), NONE('r') }, mark, { .i = MARK_r } },
{ { NONE('`'), NONE('s') }, mark, { .i = MARK_s } },
{ { NONE('`'), NONE('t') }, mark, { .i = MARK_t } },
{ { NONE('`'), NONE('u') }, mark, { .i = MARK_u } },
{ { NONE('`'), NONE('v') }, mark, { .i = MARK_v } },
{ { NONE('`'), NONE('w') }, mark, { .i = MARK_w } },
{ { NONE('`'), NONE('x') }, mark, { .i = MARK_x } },
{ { NONE('`'), NONE('y') }, mark, { .i = MARK_y } },
{ { NONE('`'), NONE('z') }, mark, { .i = MARK_z } },
{ { NONE('`'), NONE('<') }, mark, { .i = MARK_SELECTION_START } },
{ { NONE('`'), NONE('>') }, mark, { .i = MARK_SELECTION_END } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_marks_line[] = {
{ { NONE('\''), NONE('a') }, mark_line, { .i = MARK_a } },
{ { NONE('\''), NONE('b') }, mark_line, { .i = MARK_b } },
{ { NONE('\''), NONE('c') }, mark_line, { .i = MARK_c } },
{ { NONE('\''), NONE('d') }, mark_line, { .i = MARK_d } },
{ { NONE('\''), NONE('e') }, mark_line, { .i = MARK_e } },
{ { NONE('\''), NONE('f') }, mark_line, { .i = MARK_f } },
{ { NONE('\''), NONE('g') }, mark_line, { .i = MARK_g } },
{ { NONE('\''), NONE('h') }, mark_line, { .i = MARK_h } },
{ { NONE('\''), NONE('i') }, mark_line, { .i = MARK_i } },
{ { NONE('\''), NONE('j') }, mark_line, { .i = MARK_j } },
{ { NONE('\''), NONE('k') }, mark_line, { .i = MARK_k } },
{ { NONE('\''), NONE('l') }, mark_line, { .i = MARK_l } },
{ { NONE('\''), NONE('m') }, mark_line, { .i = MARK_m } },
{ { NONE('\''), NONE('n') }, mark_line, { .i = MARK_n } },
{ { NONE('\''), NONE('o') }, mark_line, { .i = MARK_o } },
{ { NONE('\''), NONE('p') }, mark_line, { .i = MARK_p } },
{ { NONE('\''), NONE('q') }, mark_line, { .i = MARK_q } },
{ { NONE('\''), NONE('r') }, mark_line, { .i = MARK_r } },
{ { NONE('\''), NONE('s') }, mark_line, { .i = MARK_s } },
{ { NONE('\''), NONE('t') }, mark_line, { .i = MARK_t } },
{ { NONE('\''), NONE('u') }, mark_line, { .i = MARK_u } },
{ { NONE('\''), NONE('v') }, mark_line, { .i = MARK_v } },
{ { NONE('\''), NONE('w') }, mark_line, { .i = MARK_w } },
{ { NONE('\''), NONE('x') }, mark_line, { .i = MARK_x } },
{ { NONE('\''), NONE('y') }, mark_line, { .i = MARK_y } },
{ { NONE('\''), NONE('z') }, mark_line, { .i = MARK_z } },
{ { NONE('\''), NONE('<') }, mark_line, { .i = MARK_SELECTION_START } },
{ { NONE('\''), NONE('>') }, mark_line, { .i = MARK_SELECTION_END } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_marks_set[] = {
{ { NONE('m'), NONE('a') }, mark_set, { .i = MARK_a } },
{ { NONE('m'), NONE('b') }, mark_set, { .i = MARK_b } },
{ { NONE('m'), NONE('c') }, mark_set, { .i = MARK_c } },
{ { NONE('m'), NONE('d') }, mark_set, { .i = MARK_d } },
{ { NONE('m'), NONE('e') }, mark_set, { .i = MARK_e } },
{ { NONE('m'), NONE('f') }, mark_set, { .i = MARK_f } },
{ { NONE('m'), NONE('g') }, mark_set, { .i = MARK_g } },
{ { NONE('m'), NONE('h') }, mark_set, { .i = MARK_h } },
{ { NONE('m'), NONE('i') }, mark_set, { .i = MARK_i } },
{ { NONE('m'), NONE('j') }, mark_set, { .i = MARK_j } },
{ { NONE('m'), NONE('k') }, mark_set, { .i = MARK_k } },
{ { NONE('m'), NONE('l') }, mark_set, { .i = MARK_l } },
{ { NONE('m'), NONE('m') }, mark_set, { .i = MARK_m } },
{ { NONE('m'), NONE('n') }, mark_set, { .i = MARK_n } },
{ { NONE('m'), NONE('o') }, mark_set, { .i = MARK_o } },
{ { NONE('m'), NONE('p') }, mark_set, { .i = MARK_p } },
{ { NONE('m'), NONE('q') }, mark_set, { .i = MARK_q } },
{ { NONE('m'), NONE('r') }, mark_set, { .i = MARK_r } },
{ { NONE('m'), NONE('s') }, mark_set, { .i = MARK_s } },
{ { NONE('m'), NONE('t') }, mark_set, { .i = MARK_t } },
{ { NONE('m'), NONE('u') }, mark_set, { .i = MARK_u } },
{ { NONE('m'), NONE('v') }, mark_set, { .i = MARK_v } },
{ { NONE('m'), NONE('w') }, mark_set, { .i = MARK_w } },
{ { NONE('m'), NONE('x') }, mark_set, { .i = MARK_x } },
{ { NONE('m'), NONE('y') }, mark_set, { .i = MARK_y } },
{ { NONE('m'), NONE('z') }, mark_set, { .i = MARK_z } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_mode_normal[] = {
{ { CONTROL('w'), NONE('n') }, winnew, { .s = NULL } },
{ { CONTROL('w'), NONE('c') }, cmd, { .s = "q" } },
{ { CONTROL('w'), NONE('s') }, cmd, { .s = "split" } },
{ { CONTROL('w'), NONE('v') }, cmd, { .s = "vsplit" } },
{ { CONTROL('w'), NONE('j') }, call, { .f = editor_window_next } },
{ { CONTROL('w'), NONE('k') }, call, { .f = editor_window_prev } },
{ { CONTROL('B') }, wscroll, { .i = -PAGE } },
{ { CONTROL('F') }, wscroll, { .i = +PAGE } },
{ { CONTROL('U') }, wscroll, { .i = -PAGE_HALF } },
{ { CONTROL('D') }, wscroll, { .i = +PAGE_HALF } },
{ { CONTROL('E') }, wslide, { .i = -1 } },
{ { CONTROL('Y') }, wslide, { .i = +1 } },
{ { CONTROL('O') }, jumplist, { .i = -1 } },
{ { CONTROL('I') }, jumplist, { .i = +1 } },
{ { NONE('a') }, insertmode, { .i = MOVE_CHAR_NEXT } },
{ { NONE('A') }, insertmode, { .i = MOVE_LINE_END } },
{ { NONE('C') }, change, { .i = MOVE_LINE_END } },
{ { NONE('D') }, delete, { .i = MOVE_LINE_END } },
{ { NONE('I') }, insertmode, { .i = MOVE_LINE_START } },
{ { NONE('.') }, repeat, { NULL } },
{ { NONE('o') }, openline, { .i = MOVE_LINE_NEXT } },
{ { NONE('O') }, openline, { .i = MOVE_LINE_PREV } },
{ { NONE('J') }, join, { .i = MOVE_SCREEN_LINE_DOWN} },
{ { NONE('x') }, delete, { .i = MOVE_CHAR_NEXT } },
{ { NONE('r') }, replace, { NULL } },
{ { NONE('i') }, switchmode, { .i = VIS_MODE_INSERT } },
{ { NONE('v') }, switchmode, { .i = VIS_MODE_VISUAL } },
{ { NONE('V') }, switchmode, { .i = VIS_MODE_VISUAL_LINE } },
{ { NONE('R') }, switchmode, { .i = VIS_MODE_REPLACE } },
{ { NONE('S') }, operator_twice, { .i = OP_CHANGE } },
{ { NONE('s') }, change, { .i = MOVE_CHAR_NEXT } },
{ { NONE('Y') }, operator_twice, { .i = OP_YANK } },
{ { NONE('X') }, delete, { .i = MOVE_CHAR_PREV } },
{ { NONE('u') }, undo, { NULL } },
{ { CONTROL('R') }, redo, { NULL } },
{ { CONTROL('L') }, call, { .f = editor_draw } },
{ { NONE(':') }, prompt_cmd, { .s = "" } },
{ { NONE('Z'), NONE('Z') }, cmd, { .s = "wq" } },
{ { NONE('Z'), NONE('Q') }, cmd, { .s = "q!" } },
{ { NONE('z'), NONE('t') }, window, { .w = window_redraw_top } },
{ { NONE('z'), NONE('z') }, window, { .w = window_redraw_center } },
{ { NONE('z'), NONE('b') }, window, { .w = window_redraw_bottom } },
{ { NONE('q') }, macro_record, { NULL } },
{ { NONE('@') }, macro_replay, { NULL } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_mode_visual[] = {
BACKSPACE( operator, i, OP_DELETE ),
{ { NONE(ESC) }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { CONTROL('c') }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { NONE('v') }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { NONE('V') }, switchmode, { .i = VIS_MODE_VISUAL_LINE } },
{ { NONE(':') }, prompt_cmd, { .s = "'<,'>" } },
{ { CONTROL('H') }, operator, { .i = OP_DELETE } },
{ { NONE('d') }, operator, { .i = OP_DELETE } },
{ { NONE('x') }, operator, { .i = OP_DELETE } },
{ { NONE('y') }, operator, { .i = OP_YANK } },
{ { NONE('c') }, operator, { .i = OP_CHANGE } },
{ { NONE('r') }, operator, { .i = OP_CHANGE } },
{ { NONE('s') }, operator, { .i = OP_CHANGE } },
{ { NONE('J') }, operator, { .i = OP_JOIN } },
{ /* empty last element, array terminator */ },
};
static void vis_mode_visual_enter(Mode *old) {
if (!old->visual) {
window_selection_start(vis->win->win);
vis_modes[VIS_MODE_OPERATOR].parent = &vis_modes[VIS_MODE_TEXTOBJ];
}
}
static void vis_mode_visual_leave(Mode *new) {
if (!new->visual) {
window_selection_clear(vis->win->win);
vis_modes[VIS_MODE_OPERATOR].parent = &vis_modes[VIS_MODE_MOVE];
}
}
static KeyBinding vis_mode_visual_line[] = {
{ { NONE('v') }, switchmode, { .i = VIS_MODE_VISUAL } },
{ { NONE('V') }, switchmode, { .i = VIS_MODE_NORMAL } },
{ /* empty last element, array terminator */ },
};
static void vis_mode_visual_line_enter(Mode *old) {
Win *win = vis->win->win;
window_cursor_to(win, text_line_begin(vis->win->text, window_cursor_get(win)));
if (!old->visual) {
window_selection_start(vis->win->win);
vis_modes[VIS_MODE_OPERATOR].parent = &vis_modes[VIS_MODE_TEXTOBJ];
}
movement(&(const Arg){ .i = MOVE_LINE_END });
}
static void vis_mode_visual_line_leave(Mode *new) {
if (!new->visual) {
window_selection_clear(vis->win->win);
vis_modes[VIS_MODE_OPERATOR].parent = &vis_modes[VIS_MODE_MOVE];
}
}
static KeyBinding vis_mode_readline[] = {
BACKSPACE( call, f, editor_backspace_key ),
{ { NONE(ESC) }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { CONTROL('c') }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { CONTROL('D') }, call, { .f = editor_delete_key } },
{ { CONTROL('W') }, delete, { .i = MOVE_LONGWORD_START_PREV } },
{ { CONTROL('U') }, delete, { .i = MOVE_LINE_BEGIN } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_mode_prompt[] = {
BACKSPACE( prompt_backspace, s, NULL ),
{ { KEY(ENTER) }, prompt_enter, { NULL } },
{ { CONTROL('J') }, prompt_enter, { NULL } },
{ { KEY(UP) }, prompt_up, { NULL } },
{ { KEY(DOWN) }, prompt_down, { NULL } },
{ { KEY(HOME) }, movement, { .i = MOVE_FILE_BEGIN } },
{ { CONTROL('B') }, movement, { .i = MOVE_FILE_BEGIN } },
{ { KEY(END) }, movement, { .i = MOVE_FILE_END } },
{ { CONTROL('E') }, movement, { .i = MOVE_FILE_END } },
{ { NONE('\t') }, NULL, { NULL } },
{ /* empty last element, array terminator */ },
};
static void vis_mode_prompt_input(const char *str, size_t len) {
editor_insert_key(vis, str, len);
}
static void vis_mode_prompt_enter(Mode *old) {
if (old->isuser && old != &vis_modes[VIS_MODE_PROMPT])
mode_before_prompt = old;
}
static void vis_mode_prompt_leave(Mode *new) {
if (new->isuser)
editor_prompt_hide(vis);
}
static KeyBinding vis_mode_insert_register[] = {
{ { CONTROL('R'), NONE('a') }, insert_register, { .i = REG_a } },
{ { CONTROL('R'), NONE('b') }, insert_register, { .i = REG_b } },
{ { CONTROL('R'), NONE('c') }, insert_register, { .i = REG_c } },
{ { CONTROL('R'), NONE('d') }, insert_register, { .i = REG_d } },
{ { CONTROL('R'), NONE('e') }, insert_register, { .i = REG_e } },
{ { CONTROL('R'), NONE('f') }, insert_register, { .i = REG_f } },
{ { CONTROL('R'), NONE('g') }, insert_register, { .i = REG_g } },
{ { CONTROL('R'), NONE('h') }, insert_register, { .i = REG_h } },
{ { CONTROL('R'), NONE('i') }, insert_register, { .i = REG_i } },
{ { CONTROL('R'), NONE('j') }, insert_register, { .i = REG_j } },
{ { CONTROL('R'), NONE('k') }, insert_register, { .i = REG_k } },
{ { CONTROL('R'), NONE('l') }, insert_register, { .i = REG_l } },
{ { CONTROL('R'), NONE('m') }, insert_register, { .i = REG_m } },
{ { CONTROL('R'), NONE('n') }, insert_register, { .i = REG_n } },
{ { CONTROL('R'), NONE('o') }, insert_register, { .i = REG_o } },
{ { CONTROL('R'), NONE('p') }, insert_register, { .i = REG_p } },
{ { CONTROL('R'), NONE('q') }, insert_register, { .i = REG_q } },
{ { CONTROL('R'), NONE('r') }, insert_register, { .i = REG_r } },
{ { CONTROL('R'), NONE('s') }, insert_register, { .i = REG_s } },
{ { CONTROL('R'), NONE('t') }, insert_register, { .i = REG_t } },
{ { CONTROL('R'), NONE('u') }, insert_register, { .i = REG_u } },
{ { CONTROL('R'), NONE('v') }, insert_register, { .i = REG_v } },
{ { CONTROL('R'), NONE('w') }, insert_register, { .i = REG_w } },
{ { CONTROL('R'), NONE('x') }, insert_register, { .i = REG_x } },
{ { CONTROL('R'), NONE('y') }, insert_register, { .i = REG_y } },
{ { CONTROL('R'), NONE('z') }, insert_register, { .i = REG_z } },
{ /* empty last element, array terminator */ },
};
static KeyBinding vis_mode_insert[] = {
{ { CONTROL('L') }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { CONTROL('[') }, switchmode, { .i = VIS_MODE_NORMAL } },
{ { CONTROL('I') }, insert_tab, { NULL } },
{ { CONTROL('J') }, insert_newline, { NULL } },
{ { CONTROL('M') }, insert_newline, { NULL } },
{ { CONTROL('O') }, switchmode, { .i = VIS_MODE_OPERATOR } },
{ { CONTROL('V') }, insert_verbatim, { NULL } },
{ { CONTROL('D') }, operator_twice, { .i = OP_SHIFT_LEFT } },
{ { CONTROL('T') }, operator_twice, { .i = OP_SHIFT_RIGHT } },
{ { CONTROL('X'), CONTROL('E') }, wslide, { .i = -1 } },
{ { CONTROL('X'), CONTROL('Y') }, wslide, { .i = +1 } },
{ { NONE('\t') }, insert_tab, { NULL } },
{ { KEY(END) }, movement, { .i = MOVE_LINE_END } },
{ /* empty last element, array terminator */ },
};
static void vis_mode_insert_leave(Mode *old) {
/* make sure we can recover the current state after an editing operation */
text_snapshot(vis->win->text);
}
static void vis_mode_insert_idle(void) {
text_snapshot(vis->win->text);
}
static void vis_mode_insert_input(const char *str, size_t len) {
static size_t oldpos = EPOS;
size_t pos = window_cursor_get(vis->win->win);
if (pos != oldpos)
buffer_truncate(&buffer_repeat);
buffer_append(&buffer_repeat, str, len);
oldpos = pos + len;
action_reset(&action_prev);
action_prev.op = &ops[OP_REPEAT_INSERT];
editor_insert_key(vis, str, len);
}
static KeyBinding vis_mode_replace[] = {
{ { NONE(ESC) }, switchmode, { .i = VIS_MODE_NORMAL } },
{ /* empty last element, array terminator */ },
};
static void vis_mode_replace_leave(Mode *old) {
/* make sure we can recover the current state after an editing operation */
text_snapshot(vis->win->text);
}
static void vis_mode_replace_input(const char *str, size_t len) {
static size_t oldpos = EPOS;
size_t pos = window_cursor_get(vis->win->win);
if (pos != oldpos)
buffer_truncate(&buffer_repeat);
buffer_append(&buffer_repeat, str, len);
oldpos = pos + len;
action_reset(&action_prev);
action_prev.op = &ops[OP_REPEAT_REPLACE];
editor_replace_key(vis, str, len);
}
/*
* the tree of modes currently looks like this. the double line between OPERATOR-OPTION
* and OPERATOR is only in effect once an operator is detected. that is when entering the
* OPERATOR mode its parent is set to OPERATOR-OPTION which makes {INNER-,}TEXTOBJ
* reachable. once the operator is processed (i.e. the OPERATOR mode is left) its parent
* mode is reset back to MOVE.
*
* Similarly the +-ed line between OPERATOR and TEXTOBJ is only active within the visual
* modes.
*
*
* BASIC
* (arrow keys etc.)
* / |
* /-------------------/ |
* READLINE MARK
* / \ (` [a-z])
* / \ |
* / \ |
* INSERT-REGISTER PROMPT MARK-LINE
* (Ctrl+R [a-z]) (history etc) (' [a-z])
* | |
* | |
* INSERT MOVE
* | (h,j,k,l ...)
* | | \-----------------\
* | | |
* REPLACE OPERATOR ++++ INNER-TEXTOBJ
* (d,c,y,p ..) + (i [wsp[]()b<>{}B"'`] )
* | \\ + |
* | \\ + |
* REGISTER \\ + TEXTOBJ
* (" [a-z]) \\ + (a [wsp[]()b<>{}B"'`] )
* /-----------/ | \\ + + |
* / | \\ + + |
* VISUAL MARK-SET \\ OPERATOR-OPTION
* | (m [a-z]) \\ (v,V)
* | | \\ //
* | | \\======//
* VISUAL-LINE NORMAL
*/
static Mode vis_modes[] = {
[VIS_MODE_BASIC] = {
.name = "BASIC",
.parent = NULL,
.bindings = basic_movement,
},
[VIS_MODE_MARK] = {
.name = "MARK",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_BASIC],
.bindings = vis_marks,
},
[VIS_MODE_MARK_LINE] = {
.name = "MARK-LINE",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_MARK],
.bindings = vis_marks_line,
},
[VIS_MODE_MOVE] = {
.name = "MOVE",
.parent = &vis_modes[VIS_MODE_MARK_LINE],
.bindings = vis_movements,
},
[VIS_MODE_INNER_TEXTOBJ] = {
.name = "INNER-TEXTOBJ",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_MOVE],
.bindings = vis_inner_textobjs,
},
[VIS_MODE_TEXTOBJ] = {
.name = "TEXTOBJ",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_INNER_TEXTOBJ],
.bindings = vis_textobjs,
},
[VIS_MODE_OPERATOR_OPTION] = {
.name = "OPERATOR-OPTION",
.parent = &vis_modes[VIS_MODE_TEXTOBJ],
.bindings = vis_operator_options,
},
[VIS_MODE_OPERATOR] = {
.name = "OPERATOR",
.parent = &vis_modes[VIS_MODE_MOVE],
.bindings = vis_operators,
.enter = vis_mode_operator_enter,
.leave = vis_mode_operator_leave,
.input = vis_mode_operator_input,
},
[VIS_MODE_REGISTER] = {
.name = "REGISTER",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_OPERATOR],
.bindings = vis_registers,
},
[VIS_MODE_MARK_SET] = {
.name = "MARK-SET",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_REGISTER],
.bindings = vis_marks_set,
},
[VIS_MODE_NORMAL] = {
.name = "NORMAL",
.isuser = true,
.parent = &vis_modes[VIS_MODE_MARK_SET],
.bindings = vis_mode_normal,
},
[VIS_MODE_VISUAL] = {
.name = "--VISUAL--",
.isuser = true,
.parent = &vis_modes[VIS_MODE_REGISTER],
.bindings = vis_mode_visual,
.enter = vis_mode_visual_enter,
.leave = vis_mode_visual_leave,
.visual = true,
},
[VIS_MODE_VISUAL_LINE] = {
.name = "--VISUAL LINE--",
.isuser = true,
.parent = &vis_modes[VIS_MODE_VISUAL],
.bindings = vis_mode_visual_line,
.enter = vis_mode_visual_line_enter,
.leave = vis_mode_visual_line_leave,
.visual = true,
},
[VIS_MODE_READLINE] = {
.name = "READLINE",
.parent = &vis_modes[VIS_MODE_BASIC],
.bindings = vis_mode_readline,
},
[VIS_MODE_PROMPT] = {
.name = "PROMPT",
.isuser = true,
.parent = &vis_modes[VIS_MODE_READLINE],
.bindings = vis_mode_prompt,
.input = vis_mode_prompt_input,
.enter = vis_mode_prompt_enter,
.leave = vis_mode_prompt_leave,
},
[VIS_MODE_INSERT_REGISTER] = {
.name = "INSERT-REGISTER",
.common_prefix = true,
.parent = &vis_modes[VIS_MODE_READLINE],
.bindings = vis_mode_insert_register,
},
[VIS_MODE_INSERT] = {
.name = "--INSERT--",
.isuser = true,
.parent = &vis_modes[VIS_MODE_INSERT_REGISTER],
.bindings = vis_mode_insert,
.leave = vis_mode_insert_leave,
.input = vis_mode_insert_input,
.idle = vis_mode_insert_idle,
.idle_timeout = 3,
},
[VIS_MODE_REPLACE] = {
.name = "--REPLACE--",
.isuser = true,
.parent = &vis_modes[VIS_MODE_INSERT],
.bindings = vis_mode_replace,
.leave = vis_mode_replace_leave,
.input = vis_mode_replace_input,
.idle = vis_mode_insert_idle,
.idle_timeout = 3,
},
};
/* incomplete list of useful but currently missing functionality from nano's help ^G:
^X (F2) Close the current file buffer / Exit from nano
^O (F3) Write the current file to disk
^J (F4) Justify the current paragraph
^R (F5) Insert another file into the current one
^W (F6) Search for a string or a regular expression
^K (F9) Cut the current line and store it in the cutbuffer
^U (F10) Uncut from the cutbuffer into the current line
^T (F12) Invoke the spell checker, if available
^_ (F13) (M-G) Go to line and column number
^\ (F14) (M-R) Replace a string or a regular expression
^^ (F15) (M-A) Mark text at the cursor position
M-W (F16) Repeat last search
M-^ (M-6) Copy the current line and store it in the cutbuffer
M-V Insert the next keystroke verbatim
XXX: CONTROL(' ') = 0, ^Space Go forward one word
*/
static KeyBinding nano_keys[] = {
{ { CONTROL('D') }, call, { .f = editor_delete_key } },
BACKSPACE( call, f, editor_backspace_key ),
{ { CONTROL('F') }, movement, { .i = MOVE_CHAR_NEXT } },
{ { CONTROL('P') }, movement, { .i = MOVE_SCREEN_LINE_UP } },
{ { CONTROL('N') }, movement, { .i = MOVE_SCREEN_LINE_DOWN } },
{ { CONTROL('Y') }, wscroll, { .i = -PAGE } },
{ { KEY(F(7)) }, wscroll, { .i = -PAGE } },
{ { CONTROL('V') }, wscroll, { .i = +PAGE } },
{ { KEY(F(8)) }, wscroll, { .i = +PAGE } },
#if 0
// CONTROL(' ') == 0 which signals the end of array
{ { CONTROL(' ') }, movement, { .i = MOVE_LONGWORD_START_NEXT } },
#endif
{ { META(' ') }, movement, { .i = MOVE_LONGWORD_START_PREV } },
{ { CONTROL('A') }, movement, { .i = MOVE_LINE_START } },
{ { CONTROL('E') }, movement, { .i = MOVE_LINE_END } },
{ { META(']') }, movement, { .i = MOVE_BRACKET_MATCH } },
{ { META(')') }, movement, { .i = MOVE_PARAGRAPH_PREV } },
{ { META('(') }, movement, { .i = MOVE_PARAGRAPH_NEXT } },
{ { META('\\') }, movement, { .i = MOVE_FILE_BEGIN } },
{ { META('|') }, movement, { .i = MOVE_FILE_BEGIN } },
{ { META('/') }, movement, { .i = MOVE_FILE_END } },
{ { META('?') }, movement, { .i = MOVE_FILE_END } },
{ { META('U') }, undo, { NULL } },
{ { META('E') }, redo, { NULL } },
#if 0
{ { CONTROL('I') }, insert, { .s = "\t" } },
/* TODO: handle this in vis to insert \n\r when appriopriate */
{ { CONTROL('M') }, insert, { .s = "\n" } },
#endif
{ { CONTROL('L') }, call, { .f = editor_draw } },
{ /* empty last element, array terminator */ },
};
static Mode nano[] = {
{ .parent = NULL, .bindings = basic_movement, },
{ .parent = &nano[0], .bindings = nano_keys, .input = vis_mode_insert_input },
};
/* list of vis configurations, first entry is default. name is matched with
* argv[0] i.e. program name upon execution
*/
static Config editors[] = {
{
.name = "vis",
.mode = &vis_modes[VIS_MODE_NORMAL],
.statusbar = statusbar,
.keypress = vis_keypress,
},
{
.name = "nano",
.mode = &nano[1],
.statusbar = statusbar,
.keypress = vis_keypress,
},
};
/* default editor configuration to use */
static Config *config = &editors[0];
/* Color definitions for use in the sytax highlighting rules below. A fore
* or background color of -1 specifies the default terminal color. */
enum {
COLOR_NOHILIT,
COLOR_SYNTAX0,
COLOR_SYNTAX1,
COLOR_SYNTAX2,
COLOR_SYNTAX3,
COLOR_SYNTAX4,
COLOR_SYNTAX5,
COLOR_SYNTAX6,
COLOR_SYNTAX7,
COLOR_KEYWORD = COLOR_SYNTAX1,
COLOR_CONSTANT = COLOR_SYNTAX4,
COLOR_DATATYPE = COLOR_SYNTAX2,
COLOR_OPERATOR = COLOR_SYNTAX2,
COLOR_CONTROL = COLOR_SYNTAX3,
COLOR_PREPROCESSOR = COLOR_SYNTAX4,
COLOR_PRAGMA = COLOR_SYNTAX4,
COLOR_KEYWORD2 = COLOR_SYNTAX4,
COLOR_BRACKETS = COLOR_SYNTAX5,
COLOR_STRING = COLOR_SYNTAX6,
COLOR_LITERAL = COLOR_SYNTAX6,
COLOR_VARIABLE = COLOR_SYNTAX6,
COLOR_TARGET = COLOR_SYNTAX5,
COLOR_COMMENT = COLOR_SYNTAX7,
};
static Color colors[] = {
[COLOR_NOHILIT] = { .fg = -1, .bg = -1, .attr = A_NORMAL },
[COLOR_SYNTAX0] = { .fg = COLOR_RED, .bg = -1, .attr = A_BOLD },
[COLOR_SYNTAX1] = { .fg = COLOR_GREEN, .bg = -1, .attr = A_BOLD },
[COLOR_SYNTAX2] = { .fg = COLOR_GREEN, .bg = -1, .attr = A_NORMAL },
[COLOR_SYNTAX3] = { .fg = COLOR_MAGENTA, .bg = -1, .attr = A_BOLD },
[COLOR_SYNTAX4] = { .fg = COLOR_MAGENTA, .bg = -1, .attr = A_NORMAL },
[COLOR_SYNTAX5] = { .fg = COLOR_BLUE, .bg = -1, .attr = A_BOLD },
[COLOR_SYNTAX6] = { .fg = COLOR_RED, .bg = -1, .attr = A_NORMAL },
[COLOR_SYNTAX7] = { .fg = COLOR_BLUE, .bg = -1, .attr = A_NORMAL },
{ /* empty last element, array terminator */ }
};
/* Syntax color definitions per file type. Each rule consists of a regular
* expression, a color to apply in case of a match and boolean flag inidcating
* whether it is a multiline rule.
*
* The syntax rules where initially imported from the sandy editor, written by
* Rafael Garcia <[email protected]>
*/
#define B "\\b"
/* Use this if \b is not in your libc's regex implementation */
// #define B "^| |\t|\\(|\\)|\\[|\\]|\\{|\\}|\\||$"
/* common rules, used by multiple languages */
#define SYNTAX_MULTILINE_COMMENT { \
"(/\\*([^*]|\\*+[^*/])*\\*+/|/\\*([^*]|\\*[^/])*$|^([^/]|/[^*])*\\*/)", \
&colors[COLOR_COMMENT], \
true, /* multiline */ \
}
#define SYNTAX_SINGLE_LINE_COMMENT { \
"(//.*)", \
&colors[COLOR_COMMENT], \
}
#define SYNTAX_LITERAL { \
"('(\\\\.|.)')|"B"(0x[0-9A-Fa-f]+|[0-9]+)"B, \
&colors[COLOR_LITERAL], \
}
#define SYNTAX_STRING { \
"(\"(\\\\.|[^\"])*\")", \
&colors[COLOR_STRING], \
false, /* multiline */ \
}
#define SYNTAX_CONSTANT { \
B"[A-Z_][0-9A-Z_]+"B, \
&colors[COLOR_CONSTANT], \
}
#define SYNTAX_BRACKET { \
"(\\(|\\)|\\{|\\}|\\[|\\])", \
&colors[COLOR_BRACKETS], \
}
#define SYNTAX_C_PREPROCESSOR { \
"(^#[\\t ]*(define|include(_next)?|(un|ifn?)def|endif|el(if|se)|if|warning|error|pragma))", \
&colors[COLOR_PREPROCESSOR], \
}
/* these rules are applied top to bottom, first match wins. Therefore more 'greedy'
* rules such as for comments should be the first entries.
*
* The array of syntax definition must be terminated with an empty element.
*/
static Syntax syntaxes[] = {{
.name = "c",
.file = "\\.(c(pp|xx)?|h(pp|xx)?|cc)$",
.rules = {
SYNTAX_MULTILINE_COMMENT,
SYNTAX_SINGLE_LINE_COMMENT,
SYNTAX_LITERAL,
SYNTAX_STRING,
SYNTAX_CONSTANT,
SYNTAX_BRACKET,
{
"<[a-zA-Z0-9\\.\\-_/]+\\.(c(pp|xx)?|h(pp|xx)?|cc)>",
&colors[COLOR_STRING],
},
SYNTAX_C_PREPROCESSOR,
{
B"(for|if|while|do|else|case|default|switch|try|throw|catch|operator|new|delete)"B,
&colors[COLOR_KEYWORD],
},{
B"(float|double|bool|char|int|short|long|sizeof|enum|void|static|const|struct|union|"
"typedef|extern|(un)?signed|inline|((s?size)|((u_?)?int(8|16|32|64|ptr)))_t|class|"
"namespace|template|public|protected|private|typename|this|friend|virtual|using|"
"mutable|volatile|register|explicit)"B,
&colors[COLOR_DATATYPE],
},{
B"(goto|continue|break|return)"B,
&colors[COLOR_CONTROL],
}}
},{
.name = "sh",
.file = "\\.sh$",
.rules = {{
"#.*$",
&colors[COLOR_COMMENT],
},
SYNTAX_STRING,
{
"^[0-9A-Z_]+\\(\\)",
&colors[COLOR_CONSTANT],
},{
"\\$\\{?[0-9A-Z_!@#$*?-]+\\}?",
&colors[COLOR_VARIABLE],
},{
B"(case|do|done|elif|else|esac|exit|fi|for|function|if|in|local|read|return|select|shift|then|time|until|while)"B,
&colors[COLOR_KEYWORD],
},{
"(\\{|\\}|\\(|\\)|\\;|\\]|\\[|`|\\\\|\\$|<|>|!|=|&|\\|)",
&colors[COLOR_BRACKETS],