forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis.c
2122 lines (1901 loc) · 62.2 KB
/
vis.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
/*
* Copyright (c) 2014 Marc André Tanner <mat at brain-dump.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "editor.h"
#include "text-motions.h"
#include "text-objects.h"
#include "util.h"
#ifdef PDCURSES
int ESCDELAY;
#endif
#ifndef NCURSES_REENTRANT
# define set_escdelay(d) (ESCDELAY = (d))
#endif
typedef union {
bool b;
int i;
const char *s;
void (*w)(Win*); /* generic window commands */
void (*f)(Editor*); /* generic editor commands */
} Arg;
typedef struct {
char str[6]; /* UTF8 character or terminal escape code */
int code; /* curses KEY_* constant */
} Key;
#define MAX_KEYS 2
typedef Key KeyCombo[MAX_KEYS];
typedef struct {
KeyCombo key;
void (*func)(const Arg *arg);
const Arg arg;
} KeyBinding;
typedef struct Mode Mode;
struct Mode {
Mode *parent; /* if no match is found in this mode, search will continue there */
KeyBinding *bindings; /* NULL terminated array of keybindings for this mode */
const char *name; /* descriptive, user facing name of the mode */
bool isuser; /* whether this is a user or internal mode */
bool common_prefix; /* whether the first key in this mode is always the same */
void (*enter)(Mode *old); /* called right before the mode becomes active */
void (*leave)(Mode *new); /* called right before the mode becomes inactive */
bool (*unknown)(KeyCombo); /* called whenever a key combination is not found in this mode,
the return value determines whether parent modes will be searched */
void (*input)(const char*, size_t); /* called whenever a key is not found in this mode and all its parent modes */
void (*idle)(void); /* called whenever a certain idle time i.e. without any user input elapsed */
time_t idle_timeout; /* idle time in seconds after which the registered function will be called */
bool visual; /* whether text selection is possible in this mode */
};
typedef struct {
char *name; /* is used to match against argv[0] to enable this config */
Mode *mode; /* default mode in which the editor should start in */
void (*statusbar)(EditorWin*); /* routine which is called whenever the cursor is moved within a window */
bool (*keypress)(Key*); /* called before any other keybindings are checked,
* return value decides whether key should be ignored */
} Config;
typedef struct {
int count; /* how many times should the command be executed? */
Register *reg; /* always non-NULL, set to a default register */
Filerange range; /* which part of the file should be affected by the operator */
size_t pos; /* at which byte from the start of the file should the operation start? */
bool linewise; /* should the changes always affect whole lines? */
const Arg *arg; /* arbitrary arguments */
} OperatorContext;
typedef struct {
void (*func)(OperatorContext*); /* function implementing the operator logic */
} Operator;
typedef struct {
size_t (*cmd)(const Arg*); /* a custom movement based on user input from vis.c */
size_t (*win)(Win*); /* a movement based on current window content from window.h */
size_t (*txt)(Text*, size_t pos); /* a movement form text-motions.h */
enum {
LINEWISE = 1 << 0,
CHARWISE = 1 << 1,
INCLUSIVE = 1 << 2,
EXCLUSIVE = 1 << 3,
IDEMPOTENT = 1 << 4,
JUMP = 1 << 5,
} type;
int count;
} Movement;
typedef struct {
Filerange (*range)(Text*, size_t pos); /* a text object from text-objects.h */
enum {
INNER,
OUTER,
} type;
} TextObject;
typedef struct { /** collects all information until an operator is executed */
int count;
bool linewise;
Operator *op;
Movement *movement;
TextObject *textobj;
Register *reg;
MarkIntern mark;
Key key;
Arg arg;
} Action;
typedef struct { /* command definitions for the ':'-prompt */
const char *name; /* regular expression pattern to match command */
bool (*cmd)(Filerange*, const char *argv[]);
/* command logic called with a NULL terminated array
* of arguments. argv[0] will be the command name,
* as matched by the regex. */
bool args; /* whether argv should be populated with words
* separated by spaces. if false, argv[1] will
* contain the remaining command line unmodified */
regex_t regex; /* compiled form of the pattern in 'name' */
} Command;
/** global variables */
static volatile bool running = true; /* exit main loop once this becomes false */
static Editor *vis; /* global editor instance, keeps track of all windows etc. */
static Mode *mode; /* currently active mode, used to search for keybindings */
static Mode *mode_prev; /* previsouly active user mode */
static Mode *mode_before_prompt; /* user mode which was active before entering prompt */
static Action action; /* current action which is in progress */
static Action action_prev; /* last operator action used by the repeat '.' key */
static Buffer buffer_repeat;/* repeat last modification i.e. insertion/replacement */
/** operators */
static void op_change(OperatorContext *c);
static void op_yank(OperatorContext *c);
static void op_put(OperatorContext *c);
static void op_delete(OperatorContext *c);
static void op_shift_right(OperatorContext *c);
static void op_shift_left(OperatorContext *c);
static void op_case_change(OperatorContext *c);
static void op_join(OperatorContext *c);
static void op_repeat_insert(OperatorContext *c);
static void op_repeat_replace(OperatorContext *c);
/* these can be passed as int argument to operator(&(const Arg){ .i = OP_*}) */
enum {
OP_DELETE,
OP_CHANGE,
OP_YANK,
OP_PUT,
OP_SHIFT_RIGHT,
OP_SHIFT_LEFT,
OP_CASE_CHANGE,
OP_JOIN,
OP_REPEAT_INSERT,
OP_REPEAT_REPLACE,
};
static Operator ops[] = {
[OP_DELETE] = { op_delete },
[OP_CHANGE] = { op_change },
[OP_YANK] = { op_yank },
[OP_PUT] = { op_put },
[OP_SHIFT_RIGHT] = { op_shift_right },
[OP_SHIFT_LEFT] = { op_shift_left },
[OP_CASE_CHANGE] = { op_case_change },
[OP_JOIN] = { op_join },
[OP_REPEAT_INSERT] = { op_repeat_insert },
[OP_REPEAT_REPLACE] = { op_repeat_replace },
};
#define PAGE INT_MAX
#define PAGE_HALF (INT_MAX-1)
/* these can be passed as int argument to movement(&(const Arg){ .i = MOVE_* }) */
enum {
MOVE_LINE_DOWN,
MOVE_LINE_UP,
MOVE_SCREEN_LINE_UP,
MOVE_SCREEN_LINE_DOWN,
MOVE_SCREEN_LINE_BEGIN,
MOVE_SCREEN_LINE_MIDDLE,
MOVE_SCREEN_LINE_END,
MOVE_LINE_PREV,
MOVE_LINE_BEGIN,
MOVE_LINE_START,
MOVE_LINE_FINISH,
MOVE_LINE_LASTCHAR,
MOVE_LINE_END,
MOVE_LINE_NEXT,
MOVE_LINE,
MOVE_COLUMN,
MOVE_CHAR_PREV,
MOVE_CHAR_NEXT,
MOVE_WORD_START_NEXT,
MOVE_WORD_END_PREV,
MOVE_WORD_END_NEXT,
MOVE_WORD_START_PREV,
MOVE_LONGWORD_START_PREV,
MOVE_LONGWORD_START_NEXT,
MOVE_LONGWORD_END_PREV,
MOVE_LONGWORD_END_NEXT,
MOVE_SENTENCE_PREV,
MOVE_SENTENCE_NEXT,
MOVE_PARAGRAPH_PREV,
MOVE_PARAGRAPH_NEXT,
MOVE_BRACKET_MATCH,
MOVE_LEFT_TO,
MOVE_RIGHT_TO,
MOVE_LEFT_TILL,
MOVE_RIGHT_TILL,
MOVE_FILE_BEGIN,
MOVE_FILE_END,
MOVE_MARK,
MOVE_MARK_LINE,
MOVE_SEARCH_WORD_FORWARD,
MOVE_SEARCH_WORD_BACKWARD,
MOVE_SEARCH_FORWARD,
MOVE_SEARCH_BACKWARD,
MOVE_WINDOW_LINE_TOP,
MOVE_WINDOW_LINE_MIDDLE,
MOVE_WINDOW_LINE_BOTTOM,
};
/** movements which can be used besides the one in text-motions.h and window.h */
/* search in forward direction for the word under the cursor */
static size_t search_word_forward(const Arg *arg);
/* search in backward direction for the word under the cursor */
static size_t search_word_backward(const Arg *arg);
/* search again for the last used search pattern */
static size_t search_forward(const Arg *arg);
static size_t search_backward(const Arg *arg);
/* goto action.mark */
static size_t mark_goto(const Arg *arg);
/* goto first non-blank char on line pointed by action.mark */
static size_t mark_line_goto(const Arg *arg);
/* goto to next occurence of action.key to the right */
static size_t to(const Arg *arg);
/* goto to position before next occurence of action.key to the right */
static size_t till(const Arg *arg);
/* goto to next occurence of action.key to the left */
static size_t to_left(const Arg *arg);
/* goto to position after next occurence of action.key to the left */
static size_t till_left(const Arg *arg);
/* goto line number action.count */
static size_t line(const Arg *arg);
/* goto to byte action.count on current line */
static size_t column(const Arg *arg);
/* goto the action.count-th line from top of the focused window */
static size_t window_lines_top(const Arg *arg);
/* goto the start of middle line of the focused window */
static size_t window_lines_middle(const Arg *arg);
/* goto the action.count-th line from bottom of the focused window */
static size_t window_lines_bottom(const Arg *arg);
static Movement moves[] = {
[MOVE_LINE_UP] = { .win = window_line_up },
[MOVE_LINE_DOWN] = { .win = window_line_down },
[MOVE_SCREEN_LINE_UP] = { .win = window_screenline_up },
[MOVE_SCREEN_LINE_DOWN] = { .win = window_screenline_down },
[MOVE_SCREEN_LINE_BEGIN] = { .win = window_screenline_begin, .type = CHARWISE },
[MOVE_SCREEN_LINE_MIDDLE] = { .win = window_screenline_middle, .type = CHARWISE },
[MOVE_SCREEN_LINE_END] = { .win = window_screenline_end, .type = CHARWISE|INCLUSIVE },
[MOVE_LINE_PREV] = { .txt = text_line_prev, .type = LINEWISE },
[MOVE_LINE_BEGIN] = { .txt = text_line_begin, .type = LINEWISE },
[MOVE_LINE_START] = { .txt = text_line_start, .type = LINEWISE },
[MOVE_LINE_FINISH] = { .txt = text_line_finish, .type = LINEWISE|INCLUSIVE },
[MOVE_LINE_LASTCHAR] = { .txt = text_line_lastchar, .type = LINEWISE|INCLUSIVE },
[MOVE_LINE_END] = { .txt = text_line_end, .type = LINEWISE },
[MOVE_LINE_NEXT] = { .txt = text_line_next, .type = LINEWISE },
[MOVE_LINE] = { .cmd = line, .type = LINEWISE|IDEMPOTENT|JUMP},
[MOVE_COLUMN] = { .cmd = column, .type = CHARWISE|IDEMPOTENT},
[MOVE_CHAR_PREV] = { .win = window_char_prev },
[MOVE_CHAR_NEXT] = { .win = window_char_next },
[MOVE_WORD_START_PREV] = { .txt = text_word_start_prev, .type = CHARWISE },
[MOVE_WORD_START_NEXT] = { .txt = text_word_start_next, .type = CHARWISE },
[MOVE_WORD_END_PREV] = { .txt = text_word_end_prev, .type = CHARWISE|INCLUSIVE },
[MOVE_WORD_END_NEXT] = { .txt = text_word_end_next, .type = CHARWISE|INCLUSIVE },
[MOVE_LONGWORD_START_PREV] = { .txt = text_longword_start_prev, .type = CHARWISE },
[MOVE_LONGWORD_START_NEXT] = { .txt = text_longword_start_next, .type = CHARWISE },
[MOVE_LONGWORD_END_PREV] = { .txt = text_longword_end_prev, .type = CHARWISE|INCLUSIVE },
[MOVE_LONGWORD_END_NEXT] = { .txt = text_longword_end_next, .type = CHARWISE|INCLUSIVE },
[MOVE_SENTENCE_PREV] = { .txt = text_sentence_prev, .type = LINEWISE },
[MOVE_SENTENCE_NEXT] = { .txt = text_sentence_next, .type = LINEWISE },
[MOVE_PARAGRAPH_PREV] = { .txt = text_paragraph_prev, .type = LINEWISE|JUMP },
[MOVE_PARAGRAPH_NEXT] = { .txt = text_paragraph_next, .type = LINEWISE|JUMP },
[MOVE_BRACKET_MATCH] = { .txt = text_bracket_match, .type = LINEWISE|INCLUSIVE|JUMP },
[MOVE_FILE_BEGIN] = { .txt = text_begin, .type = LINEWISE|JUMP },
[MOVE_FILE_END] = { .txt = text_end, .type = LINEWISE|JUMP },
[MOVE_LEFT_TO] = { .cmd = to_left, .type = LINEWISE },
[MOVE_RIGHT_TO] = { .cmd = to, .type = LINEWISE|INCLUSIVE },
[MOVE_LEFT_TILL] = { .cmd = till_left, .type = LINEWISE },
[MOVE_RIGHT_TILL] = { .cmd = till, .type = LINEWISE|INCLUSIVE },
[MOVE_MARK] = { .cmd = mark_goto, .type = LINEWISE|JUMP },
[MOVE_MARK_LINE] = { .cmd = mark_line_goto, .type = LINEWISE|JUMP },
[MOVE_SEARCH_WORD_FORWARD] = { .cmd = search_word_forward, .type = LINEWISE|JUMP },
[MOVE_SEARCH_WORD_BACKWARD]= { .cmd = search_word_backward, .type = LINEWISE|JUMP },
[MOVE_SEARCH_FORWARD] = { .cmd = search_forward, .type = LINEWISE|JUMP },
[MOVE_SEARCH_BACKWARD] = { .cmd = search_backward, .type = LINEWISE|JUMP },
[MOVE_WINDOW_LINE_TOP] = { .cmd = window_lines_top, .type = LINEWISE|JUMP },
[MOVE_WINDOW_LINE_MIDDLE] = { .cmd = window_lines_middle, .type = LINEWISE|JUMP },
[MOVE_WINDOW_LINE_BOTTOM] = { .cmd = window_lines_bottom, .type = LINEWISE|JUMP },
};
/* these can be passed as int argument to textobj(&(const Arg){ .i = TEXT_OBJ_* }) */
enum {
TEXT_OBJ_INNER_WORD,
TEXT_OBJ_OUTER_WORD,
TEXT_OBJ_INNER_LONGWORD,
TEXT_OBJ_OUTER_LONGWORD,
TEXT_OBJ_LINE_UP,
TEXT_OBJ_LINE_DOWN,
TEXT_OBJ_SENTENCE,
TEXT_OBJ_PARAGRAPH,
TEXT_OBJ_OUTER_SQUARE_BRACKET,
TEXT_OBJ_INNER_SQUARE_BRACKET,
TEXT_OBJ_OUTER_CURLY_BRACKET,
TEXT_OBJ_INNER_CURLY_BRACKET,
TEXT_OBJ_OUTER_ANGLE_BRACKET,
TEXT_OBJ_INNER_ANGLE_BRACKET,
TEXT_OBJ_OUTER_PARANTHESE,
TEXT_OBJ_INNER_PARANTHESE,
TEXT_OBJ_OUTER_QUOTE,
TEXT_OBJ_INNER_QUOTE,
TEXT_OBJ_OUTER_SINGLE_QUOTE,
TEXT_OBJ_INNER_SINGLE_QUOTE,
TEXT_OBJ_OUTER_BACKTICK,
TEXT_OBJ_INNER_BACKTICK,
};
static TextObject textobjs[] = {
[TEXT_OBJ_INNER_WORD] = { text_object_word },
[TEXT_OBJ_OUTER_WORD] = { text_object_word_outer },
[TEXT_OBJ_INNER_LONGWORD] = { text_object_longword },
[TEXT_OBJ_OUTER_LONGWORD] = { text_object_longword_outer },
[TEXT_OBJ_LINE_UP] = { text_object_line },
[TEXT_OBJ_LINE_DOWN] = { text_object_line },
[TEXT_OBJ_SENTENCE] = { text_object_sentence },
[TEXT_OBJ_PARAGRAPH] = { text_object_paragraph },
[TEXT_OBJ_OUTER_SQUARE_BRACKET] = { text_object_square_bracket, OUTER },
[TEXT_OBJ_INNER_SQUARE_BRACKET] = { text_object_square_bracket, INNER },
[TEXT_OBJ_OUTER_CURLY_BRACKET] = { text_object_curly_bracket, OUTER },
[TEXT_OBJ_INNER_CURLY_BRACKET] = { text_object_curly_bracket, INNER },
[TEXT_OBJ_OUTER_ANGLE_BRACKET] = { text_object_angle_bracket, OUTER },
[TEXT_OBJ_INNER_ANGLE_BRACKET] = { text_object_angle_bracket, INNER },
[TEXT_OBJ_OUTER_PARANTHESE] = { text_object_paranthese, OUTER },
[TEXT_OBJ_INNER_PARANTHESE] = { text_object_paranthese, INNER },
[TEXT_OBJ_OUTER_QUOTE] = { text_object_quote, OUTER },
[TEXT_OBJ_INNER_QUOTE] = { text_object_quote, INNER },
[TEXT_OBJ_OUTER_SINGLE_QUOTE] = { text_object_single_quote, OUTER },
[TEXT_OBJ_INNER_SINGLE_QUOTE] = { text_object_single_quote, INNER },
[TEXT_OBJ_OUTER_BACKTICK] = { text_object_backtick, OUTER },
[TEXT_OBJ_INNER_BACKTICK] = { text_object_backtick, INNER },
};
/* if some movements are forced to be linewise, they are translated to text objects */
static TextObject *moves_linewise[] = {
[MOVE_LINE_UP] = &textobjs[TEXT_OBJ_LINE_UP],
[MOVE_LINE_DOWN] = &textobjs[TEXT_OBJ_LINE_DOWN],
[MOVE_SCREEN_LINE_UP] = &textobjs[TEXT_OBJ_LINE_UP],
[MOVE_SCREEN_LINE_DOWN] = &textobjs[TEXT_OBJ_LINE_DOWN],
};
/** functions to be called from keybindings */
/* navigate jump list either in forward (arg->i>0) or backward (arg->i<0) direction */
static void jumplist(const Arg *arg);
/* navigate change list either in forward (arg->i>0) or backward (arg->i<0) direction */
static void changelist(const Arg *arg);
static void macro_record(const Arg *arg);
static void macro_replay(const Arg *arg);
/* temporarily suspend the editor and return to the shell, type 'fg' to get back */
static void suspend(const Arg *arg);
/* switch to mode indicated by arg->i */
static void switchmode(const Arg *arg);
/* set mark indicated by arg->i to current cursor position */
static void mark_set(const Arg *arg);
/* insert arg->s at the current cursor position */
static void insert(const Arg *arg);
/* insert a tab or the needed amount of spaces at the current cursor position */
static void insert_tab(const Arg *arg);
/* inserts a newline (either \n or \r\n depending on file type) */
static void insert_newline(const Arg *arg);
/* put register content either before (if arg->i < 0) or after (if arg->i > 0)
* current cursor position */
static void put(const Arg *arg);
/* add a new line either before or after the one where the cursor currently is */
static void openline(const Arg *arg);
/* join lines from current cursor position to movement indicated by arg */
static void join(const Arg *arg);
/* execute arg->s as if it was typed on command prompt */
static void cmd(const Arg *arg);
/* perform last action i.e. action_prev again */
static void repeat(const Arg *arg);
/* replace character at cursor with one read form keyboard */
static void replace(const Arg *arg);
/* adjust action.count by arg->i */
static void count(const Arg *arg);
/* move to the action.count-th line or if not given either to the first (arg->i < 0)
* or last (arg->i > 0) line of file */
static void gotoline(const Arg *arg);
/* force operator to linewise (if arg->b is set) */
static void linewise(const Arg *arg);
/* make the current action use the operator indicated by arg->i */
static void operator(const Arg *arg);
/* execute operator twice useful for synonyms (e.g. 'cc') */
static void operator_twice(const Arg *arg);
/* change case of a file range to upper (arg->i > 0) or lowercase (arg->i < 0) */
static void changecase(const Arg *arg);
/* blocks to read a key and performs movement indicated by arg->i which
* should be one of MOVE_{RIGHT,LEFT}_{TO,TILL} */
static void movement_key(const Arg *arg);
/* perform the movement as indicated by arg->i */
static void movement(const Arg *arg);
/* let the current operator affect the range indicated by the text object arg->i */
static void textobj(const Arg *arg);
/* use register indicated by arg->i for the current operator */
static void reg(const Arg *arg);
/* perform a movement to mark arg->i */
static void mark(const Arg *arg);
/* perform a movement to the first non-blank on the line pointed by mark arg->i */
static void mark_line(const Arg *arg);
/* {un,re}do last action, redraw window */
static void undo(const Arg *arg);
static void redo(const Arg *arg);
/* either part of multiplier or a movement to begin of line */
static void zero(const Arg *arg);
/* hange/delete from the current cursor position to the end of
* movement as indicated by arg->i */
static void change(const Arg *arg);
static void delete(const Arg *arg);
/* perform movement according to arg->i, then switch to insert mode */
static void insertmode(const Arg *arg);
/* insert register content indicated by arg->i at current cursor position */
static void insert_register(const Arg *arg);
/* show a user prompt to get input with title arg->s */
static void prompt_search(const Arg *arg);
static void prompt_cmd(const Arg *arg);
/* evaluate user input at prompt, perform search or execute a command */
static void prompt_enter(const Arg *arg);
/* cycle through past user inputs */
static void prompt_up(const Arg *arg);
static void prompt_down(const Arg *arg);
/* exit command mode if the last char is deleted */
static void prompt_backspace(const Arg *arg);
/* blocks to read 3 consecutive digits and inserts the corresponding byte value */
static void insert_verbatim(const Arg *arg);
/* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
* or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
* negative values scroll back, positive forward. */
static void wscroll(const Arg *arg);
/* similar to scroll, but do only move window content not cursor position */
static void wslide(const Arg *arg);
/* call editor function as indicated by arg->f */
static void call(const Arg *arg);
/* call window function as indicated by arg->w */
static void window(const Arg *arg);
/* quit editor, discard all changes */
static void quit(const Arg *arg);
/** commands to enter at the ':'-prompt */
/* set various runtime options */
static bool cmd_set(Filerange*, const char *argv[]);
/* for each argument create a new window and open the corresponding file */
static bool cmd_open(Filerange*, const char *argv[]);
/* close current window (discard modifications if argv[0] contains '!')
* and open argv[1], if no argv[1] is given re-read to current file from disk */
static bool cmd_edit(Filerange*, const char *argv[]);
/* close the current window, if argv[0] contains a '!' discard modifications */
static bool cmd_quit(Filerange*, const char *argv[]);
/* close all windows which show current file. if argv[0] contains a '!' discard modifications */
static bool cmd_bdelete(Filerange*, const char *argv[]);
/* close all windows, exit editor, if argv[0] contains a '!' discard modifications */
static bool cmd_qall(Filerange*, const char *argv[]);
/* for each argument try to insert the file content at current cursor postion */
static bool cmd_read(Filerange*, const char *argv[]);
static bool cmd_substitute(Filerange*, const char *argv[]);
/* if no argument are given, split the current window horizontally,
* otherwise open the file */
static bool cmd_split(Filerange*, const char *argv[]);
/* if no argument are given, split the current window vertically,
* otherwise open the file */
static bool cmd_vsplit(Filerange*, const char *argv[]);
/* create a new empty window and arrange all windows either horizontally or vertically */
static bool cmd_new(Filerange*, const char *argv[]);
static bool cmd_vnew(Filerange*, const char *argv[]);
/* save the file displayed in the current window and close it */
static bool cmd_wq(Filerange*, const char *argv[]);
/* save the file displayed in the current window if it was changed, then close the window */
static bool cmd_xit(Filerange*, const char *argv[]);
/* save the file displayed in the current window to the name given.
* do not change internal filname association. further :w commands
* without arguments will still write to the old filename */
static bool cmd_write(Filerange*, const char *argv[]);
/* save the file displayed in the current window to the name given,
* associate the new name with the buffer. further :w commands
* without arguments will write to the new filename */
static bool cmd_saveas(Filerange*, const char *argv[]);
static void action_reset(Action *a);
static void switchmode_to(Mode *new_mode);
static bool vis_window_new(const char *file);
static bool vis_window_split(EditorWin *win);
#include "config.h"
static Key getkey(void);
static void keypress(Key *key);
static void action_do(Action *a);
static bool exec_command(char type, char *cmdline);
/** operator implementations of type: void (*op)(OperatorContext*) */
static void op_delete(OperatorContext *c) {
size_t len = c->range.end - c->range.start;
c->reg->linewise = c->linewise;
register_put(c->reg, vis->win->text, &c->range);
editor_delete(vis, c->range.start, len);
window_cursor_to(vis->win->win, c->range.start);
}
static void op_change(OperatorContext *c) {
op_delete(c);
switchmode(&(const Arg){ .i = VIS_MODE_INSERT });
}
static void op_yank(OperatorContext *c) {
c->reg->linewise = c->linewise;
register_put(c->reg, vis->win->text, &c->range);
}
static void op_put(OperatorContext *c) {
Text *txt = vis->win->text;
size_t pos = window_cursor_get(vis->win->win);
if (c->arg->i > 0) {
if (c->reg->linewise)
pos = text_line_next(txt, pos);
else
pos = text_char_next(txt, pos);
} else {
if (c->reg->linewise)
pos = text_line_begin(txt, pos);
}
editor_insert(vis, pos, c->reg->data, c->reg->len);
if (c->reg->linewise)
window_cursor_to(vis->win->win, text_line_start(txt, pos));
else
window_cursor_to(vis->win->win, pos + c->reg->len);
}
static const char *expand_tab(void) {
static char spaces[9];
int tabwidth = editor_tabwidth_get(vis);
tabwidth = MIN(tabwidth, LENGTH(spaces) - 1);
for (int i = 0; i < tabwidth; i++)
spaces[i] = ' ';
spaces[tabwidth] = '\0';
return vis->expandtab ? spaces : "\t";
}
static void op_shift_right(OperatorContext *c) {
Text *txt = vis->win->text;
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
const char *tab = expand_tab();
size_t tablen = strlen(tab);
/* if range ends at the begin of a line, skip line break */
if (pos == c->range.end)
pos = text_line_prev(txt, pos);
do {
prev_pos = pos = text_line_begin(txt, pos);
text_insert(txt, pos, tab, tablen);
pos = text_line_prev(txt, pos);
} while (pos >= c->range.start && pos != prev_pos);
window_cursor_to(vis->win->win, c->pos + tablen);
editor_draw(vis);
}
static void op_shift_left(OperatorContext *c) {
Text *txt = vis->win->text;
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
size_t tabwidth = editor_tabwidth_get(vis), tablen;
/* if range ends at the begin of a line, skip line break */
if (pos == c->range.end)
pos = text_line_prev(txt, pos);
do {
char c;
size_t len = 0;
prev_pos = pos = text_line_begin(txt, pos);
Iterator it = text_iterator_get(txt, pos);
if (text_iterator_byte_get(&it, &c) && c == '\t') {
len = 1;
} else {
for (len = 0; text_iterator_byte_get(&it, &c) && c == ' '; len++)
text_iterator_byte_next(&it, NULL);
}
tablen = MIN(len, tabwidth);
text_delete(txt, pos, tablen);
pos = text_line_prev(txt, pos);
} while (pos >= c->range.start && pos != prev_pos);
window_cursor_to(vis->win->win, c->pos - tablen);
editor_draw(vis);
}
static void op_case_change(OperatorContext *c) {
size_t len = c->range.end - c->range.start;
char *buf = malloc(len);
if (!buf)
return;
len = text_bytes_get(vis->win->text, c->range.start, len, buf);
size_t rem = len;
for (char *cur = buf; rem > 0; cur++, rem--) {
int ch = (unsigned char)*cur;
if (isascii(ch)) {
if (c->arg->i == 0)
*cur = islower(ch) ? toupper(ch) : tolower(ch);
else if (c->arg->i > 0)
*cur = toupper(ch);
else
*cur = tolower(ch);
}
}
text_delete(vis->win->text, c->range.start, len);
text_insert(vis->win->text, c->range.start, buf, len);
editor_draw(vis);
free(buf);
}
static void op_join(OperatorContext *c) {
Text *txt = vis->win->text;
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
Filerange sel = window_selection_get(vis->win->win);
/* if a selection ends at the begin of a line, skip line break */
if (pos == c->range.end && text_range_valid(&sel))
pos = text_line_prev(txt, pos);
do {
prev_pos = pos;
size_t end = text_line_start(txt, pos);
pos = text_char_next(txt, text_line_finish(txt, text_line_prev(txt, end)));
if (pos >= c->range.start && end > pos) {
text_delete(txt, pos, end - pos);
text_insert(txt, pos, " ", 1);
} else {
break;
}
} while (pos != prev_pos);
window_cursor_to(vis->win->win, c->range.start);
editor_draw(vis);
}
static void op_repeat_insert(OperatorContext *c) {
if (!buffer_repeat.len)
return;
editor_insert(vis, c->pos, buffer_repeat.data, buffer_repeat.len);
window_cursor_to(vis->win->win, c->pos + buffer_repeat.len);
}
static void op_repeat_replace(OperatorContext *c) {
if (!buffer_repeat.len)
return;
size_t chars = 0;
for (size_t i = 0; i < buffer_repeat.len; i++) {
if (ISUTF8(buffer_repeat.data[i]))
chars++;
}
Iterator it = text_iterator_get(vis->win->text, c->pos);
while (chars-- > 0)
text_iterator_char_next(&it, NULL);
editor_delete(vis, c->pos, it.pos - c->pos);
op_repeat_insert(c);
}
/** movement implementations of type: size_t (*move)(const Arg*) */
static char *get_word_under_cursor() {
Filerange word = text_object_word(vis->win->text, window_cursor_get(vis->win->win));
if (!text_range_valid(&word))
return NULL;
size_t len = word.end - word.start;
char *buf = malloc(len+1);
if (!buf)
return NULL;
len = text_bytes_get(vis->win->text, word.start, len, buf);
buf[len] = '\0';
return buf;
}
static size_t search_word_forward(const Arg *arg) {
size_t pos = window_cursor_get(vis->win->win);
char *word = get_word_under_cursor();
if (word && !text_regex_compile(vis->search_pattern, word, REG_EXTENDED))
pos = text_search_forward(vis->win->text, pos, vis->search_pattern);
free(word);
return pos;
}
static size_t search_word_backward(const Arg *arg) {
size_t pos = window_cursor_get(vis->win->win);
char *word = get_word_under_cursor();
if (word && !text_regex_compile(vis->search_pattern, word, REG_EXTENDED))
pos = text_search_backward(vis->win->text, pos, vis->search_pattern);
free(word);
return pos;
}
static size_t search_forward(const Arg *arg) {
size_t pos = window_cursor_get(vis->win->win);
return text_search_forward(vis->win->text, pos, vis->search_pattern);
}
static size_t search_backward(const Arg *arg) {
size_t pos = window_cursor_get(vis->win->win);
return text_search_backward(vis->win->text, pos, vis->search_pattern);
}
static void mark_set(const Arg *arg) {
text_mark_intern_set(vis->win->text, arg->i, window_cursor_get(vis->win->win));
}
static size_t mark_goto(const Arg *arg) {
return text_mark_intern_get(vis->win->text, action.mark);
}
static size_t mark_line_goto(const Arg *arg) {
return text_line_start(vis->win->text, mark_goto(arg));
}
static size_t to(const Arg *arg) {
return text_find_char_next(vis->win->text, window_cursor_get(vis->win->win) + 1,
action.key.str, strlen(action.key.str));
}
static size_t till(const Arg *arg) {
return text_char_prev(vis->win->text, to(arg));
}
static size_t to_left(const Arg *arg) {
return text_find_char_prev(vis->win->text, window_cursor_get(vis->win->win) - 1,
action.key.str, strlen(action.key.str));
}
static size_t till_left(const Arg *arg) {
return text_char_next(vis->win->text, to_left(arg));
}
static size_t line(const Arg *arg) {
return text_pos_by_lineno(vis->win->text, action.count);
}
static size_t column(const Arg *arg) {
size_t pos = window_cursor_get(vis->win->win);
return text_line_offset(vis->win->text, pos, action.count);
}
static size_t window_lines_top(const Arg *arg) {
return window_screenline_goto(vis->win->win, action.count);
}
static size_t window_lines_middle(const Arg *arg) {
return window_screenline_goto(vis->win->win, vis->win->height / 2);
}
static size_t window_lines_bottom(const Arg *arg) {
return window_screenline_goto(vis->win->win, vis->win->height - action.count);
}
/** key bindings functions of type: void (*func)(const Arg*) */
static void jumplist(const Arg *arg) {
size_t pos;
if (arg->i > 0)
pos = editor_window_jumplist_next(vis->win);
else
pos = editor_window_jumplist_prev(vis->win);
if (pos != EPOS)
window_cursor_to(vis->win->win, pos);
}
static void changelist(const Arg *arg) {
size_t pos;
if (arg->i > 0)
pos = editor_window_changelist_next(vis->win);
else
pos = editor_window_changelist_prev(vis->win);
if (pos != EPOS)
window_cursor_to(vis->win->win, pos);
}
static Macro *key2macro(const Arg *arg) {
if (arg->i)
return &vis->macros[arg->i];
Key key = getkey();
if (key.str[0] >= 'a' && key.str[0] <= 'z')
return &vis->macros[key.str[0] - 'a'];
if (key.str[0] == '@')
return vis->last_recording;
return NULL;
}
static void macro_record(const Arg *arg) {
if (vis->recording) {
/* hack to remove last recorded key, otherwise upon replay
* we would start another recording */
vis->recording->len -= sizeof(Key);
vis->last_recording = vis->recording;
vis->recording = NULL;
} else {
vis->recording = key2macro(arg);
if (vis->recording)
macro_reset(vis->recording);
}
editor_draw(vis);
}
static void macro_replay(const Arg *arg) {
Macro *macro = key2macro(arg);
if (!macro || macro == vis->recording)
return;
for (size_t i = 0; i < macro->len; i += sizeof(Key))
keypress((Key*)(macro->data + i));
}
static void suspend(const Arg *arg) {
endwin();
raise(SIGSTOP);
}
static void repeat(const Arg *arg) {
action = action_prev;
action_do(&action);
}
static void replace(const Arg *arg) {
Key k = getkey();
if (!k.str[0])
return;
size_t pos = window_cursor_get(vis->win->win);
action_reset(&action_prev);
action_prev.op = &ops[OP_REPEAT_REPLACE];
buffer_put(&buffer_repeat, k.str, strlen(k.str));
editor_delete_key(vis);
editor_insert_key(vis, k.str, strlen(k.str));
window_cursor_to(vis->win->win, pos);
}
static void count(const Arg *arg) {
action.count = action.count * 10 + arg->i;
}
static void gotoline(const Arg *arg) {
if (action.count)
movement(&(const Arg){ .i = MOVE_LINE });
else if (arg->i < 0)
movement(&(const Arg){ .i = MOVE_FILE_BEGIN });
else
movement(&(const Arg){ .i = MOVE_FILE_END });
}
static void linewise(const Arg *arg) {
action.linewise = arg->b;
}
static void operator(const Arg *arg) {
Operator *op = &ops[arg->i];
if (mode->visual) {
action.op = op;
action_do(&action);
return;
}
/* switch to operator mode inorder to make operator options and
* text-object available */
switchmode(&(const Arg){ .i = VIS_MODE_OPERATOR });
if (action.op == op) {
/* hacky way to handle double operators i.e. things like
* dd, yy etc where the second char isn't a movement */
action.linewise = true;
action.textobj = moves_linewise[MOVE_SCREEN_LINE_DOWN];
action_do(&action);
} else {
action.op = op;
}
}
static void operator_twice(const Arg *arg) {
operator(arg);
operator(arg);
}
static void changecase(const Arg *arg) {
action.arg = *arg;
operator(&(const Arg){ .i = OP_CASE_CHANGE });
}
static void movement_key(const Arg *arg) {
Key k = getkey();
if (!k.str[0]) {
action_reset(&action);
return;
}
action.key = k;
action.movement = &moves[arg->i];
action_do(&action);
}
static void movement(const Arg *arg) {
if (action.linewise && arg->i < LENGTH(moves_linewise))
action.textobj = moves_linewise[arg->i];
else
action.movement = &moves[arg->i];
action_do(&action);
}
static void textobj(const Arg *arg) {
action.textobj = &textobjs[arg->i];
action_do(&action);
}
static void reg(const Arg *arg) {
action.reg = &vis->registers[arg->i];
}
static void mark(const Arg *arg) {
action.mark = arg->i;
action.movement = &moves[MOVE_MARK];
action_do(&action);
}
static void mark_line(const Arg *arg) {
action.mark = arg->i;
action.movement = &moves[MOVE_MARK_LINE];
action_do(&action);
}
static void undo(const Arg *arg) {
size_t pos = text_undo(vis->win->text);
if (pos != EPOS) {
window_cursor_to(vis->win->win, pos);
/* redraw all windows in case some display the same file */
editor_draw(vis);
}
}
static void redo(const Arg *arg) {
size_t pos = text_redo(vis->win->text);
if (pos != EPOS) {
window_cursor_to(vis->win->win, pos);
/* redraw all windows in case some display the same file */
editor_draw(vis);
}
}
static void zero(const Arg *arg) {
if (action.count == 0)
movement(&(const Arg){ .i = MOVE_LINE_BEGIN });
else
count(&(const Arg){ .i = 0 });
}
static void insertmode(const Arg *arg) {
movement(arg);
switchmode(&(const Arg){ .i = VIS_MODE_INSERT });
}
static void change(const Arg *arg) {