forked from neovim/neovim
/
normal.c
7928 lines (7220 loc) · 231 KB
/
normal.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
/*
* normal.c: Contains the main routine for processing characters in command
* mode. Communicates closely with the code in ops.c to handle
* the operators.
*/
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/normal.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/diff.h"
#include "nvim/digraph.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_getln.h"
#include "nvim/farsi.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/getchar.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
#include "nvim/main.h"
#include "nvim/mark.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
#include "nvim/keymap.h"
#include "nvim/move.h"
#include "nvim/mouse.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/quickfix.h"
#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/spell.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
#include "nvim/ui.h"
#include "nvim/mouse.h"
#include "nvim/undo.h"
#include "nvim/window.h"
#include "nvim/state.h"
#include "nvim/event/loop.h"
#include "nvim/os/time.h"
#include "nvim/os/input.h"
typedef struct normal_state {
VimState state;
linenr_T conceal_old_cursor_line;
linenr_T conceal_new_cursor_line;
bool command_finished;
bool ctrl_w;
bool need_flushbuf;
bool conceal_update_lines;
bool set_prevcount;
bool previous_got_int; // `got_int` was true
bool cmdwin; // command-line window normal mode
bool noexmode; // true if the normal mode was pushed from
// ex mode(:global or :visual for example)
bool toplevel; // top-level normal mode
oparg_T oa; // operator arguments
cmdarg_T ca; // command arguments
int mapped_len;
int old_mapped_len;
int idx;
int c;
int old_col;
pos_T old_pos;
} NormalState;
/*
* The Visual area is remembered for reselection.
*/
static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
static linenr_T resel_VIsual_line_count; /* number of lines */
static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
static int VIsual_mode_orig = NUL; /* saved Visual mode */
static int restart_VIsual_select = 0;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "normal.c.generated.h"
#endif
static inline void normal_state_init(NormalState *s)
{
memset(s, 0, sizeof(NormalState));
s->state.check = normal_check;
s->state.execute = normal_execute;
}
/*
* nv_*(): functions called to handle Normal and Visual mode commands.
* n_*(): functions called to handle Normal mode commands.
* v_*(): functions called to handle Visual mode commands.
*/
static char *e_noident = N_("E349: No identifier under cursor");
/*
* Function to be called for a Normal or Visual mode command.
* The argument is a cmdarg_T.
*/
typedef void (*nv_func_T)(cmdarg_T *cap);
/* Values for cmd_flags. */
#define NV_NCH 0x01 /* may need to get a second char */
#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
#define NV_LANG 0x08 /* second char needs language adjustment */
#define NV_SS 0x10 /* may start selection */
#define NV_SSS 0x20 /* may start selection with shift modifier */
#define NV_STS 0x40 /* may stop selection without shift modif. */
#define NV_RL 0x80 /* 'rightleft' modifies command */
#define NV_KEEPREG 0x100 /* don't clear regname */
#define NV_NCW 0x200 /* not allowed in command-line window */
/*
* Generally speaking, every Normal mode command should either clear any
* pending operator (with *clearop*()), or set the motion type variable
* oap->motion_type.
*
* When a cursor motion command is made, it is marked as being a character or
* line oriented motion. Then, if an operator is in effect, the operation
* becomes character or line oriented accordingly.
*/
/*
* This table contains one entry for every Normal or Visual mode command.
* The order doesn't matter, init_normal_cmds() will create a sorted index.
* It is faster when all keys from zero to '~' are present.
*/
static const struct nv_cmd {
int cmd_char; /* (first) command character */
nv_func_T cmd_func; /* function for this command */
uint16_t cmd_flags; /* NV_ flags */
short cmd_arg; /* value for ca.arg */
} nv_cmds[] =
{
{ NUL, nv_error, 0, 0 },
{ Ctrl_A, nv_addsub, 0, 0 },
{ Ctrl_B, nv_page, NV_STS, BACKWARD },
{ Ctrl_C, nv_esc, 0, true },
{ Ctrl_D, nv_halfpage, 0, 0 },
{ Ctrl_E, nv_scroll_line, 0, true },
{ Ctrl_F, nv_page, NV_STS, FORWARD },
{ Ctrl_G, nv_ctrlg, 0, 0 },
{ Ctrl_H, nv_ctrlh, 0, 0 },
{ Ctrl_I, nv_pcmark, 0, 0 },
{ NL, nv_down, 0, false },
{ Ctrl_K, nv_error, 0, 0 },
{ Ctrl_L, nv_clear, 0, 0 },
{ Ctrl_M, nv_down, 0, true },
{ Ctrl_N, nv_down, NV_STS, false },
{ Ctrl_O, nv_ctrlo, 0, 0 },
{ Ctrl_P, nv_up, NV_STS, false },
{ Ctrl_Q, nv_visual, 0, false },
{ Ctrl_R, nv_redo, 0, 0 },
{ Ctrl_S, nv_ignore, 0, 0 },
{ Ctrl_T, nv_tagpop, NV_NCW, 0 },
{ Ctrl_U, nv_halfpage, 0, 0 },
{ Ctrl_V, nv_visual, 0, false },
{ 'V', nv_visual, 0, false },
{ 'v', nv_visual, 0, false },
{ Ctrl_W, nv_window, 0, 0 },
{ Ctrl_X, nv_addsub, 0, 0 },
{ Ctrl_Y, nv_scroll_line, 0, false },
{ Ctrl_Z, nv_suspend, 0, 0 },
{ ESC, nv_esc, 0, false },
{ Ctrl_BSL, nv_normal, NV_NCH_ALW, 0 },
{ Ctrl_RSB, nv_ident, NV_NCW, 0 },
{ Ctrl_HAT, nv_hat, NV_NCW, 0 },
{ Ctrl__, nv_error, 0, 0 },
{ ' ', nv_right, 0, 0 },
{ '!', nv_operator, 0, 0 },
{ '"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0 },
{ '#', nv_ident, 0, 0 },
{ '$', nv_dollar, 0, 0 },
{ '%', nv_percent, 0, 0 },
{ '&', nv_optrans, 0, 0 },
{ '\'', nv_gomark, NV_NCH_ALW, true },
{ '(', nv_brace, 0, BACKWARD },
{ ')', nv_brace, 0, FORWARD },
{ '*', nv_ident, 0, 0 },
{ '+', nv_down, 0, true },
{ ',', nv_csearch, 0, true },
{ '-', nv_up, 0, true },
{ '.', nv_dot, NV_KEEPREG, 0 },
{ '/', nv_search, 0, false },
{ '0', nv_beginline, 0, 0 },
{ '1', nv_ignore, 0, 0 },
{ '2', nv_ignore, 0, 0 },
{ '3', nv_ignore, 0, 0 },
{ '4', nv_ignore, 0, 0 },
{ '5', nv_ignore, 0, 0 },
{ '6', nv_ignore, 0, 0 },
{ '7', nv_ignore, 0, 0 },
{ '8', nv_ignore, 0, 0 },
{ '9', nv_ignore, 0, 0 },
{ ':', nv_colon, 0, 0 },
{ ';', nv_csearch, 0, false },
{ '<', nv_operator, NV_RL, 0 },
{ '=', nv_operator, 0, 0 },
{ '>', nv_operator, NV_RL, 0 },
{ '?', nv_search, 0, false },
{ '@', nv_at, NV_NCH_NOP, false },
{ 'A', nv_edit, 0, 0 },
{ 'B', nv_bck_word, 0, 1 },
{ 'C', nv_abbrev, NV_KEEPREG, 0 },
{ 'D', nv_abbrev, NV_KEEPREG, 0 },
{ 'E', nv_wordcmd, 0, true },
{ 'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD },
{ 'G', nv_goto, 0, true },
{ 'H', nv_scroll, 0, 0 },
{ 'I', nv_edit, 0, 0 },
{ 'J', nv_join, 0, 0 },
{ 'K', nv_ident, 0, 0 },
{ 'L', nv_scroll, 0, 0 },
{ 'M', nv_scroll, 0, 0 },
{ 'N', nv_next, 0, SEARCH_REV },
{ 'O', nv_open, 0, 0 },
{ 'P', nv_put, 0, 0 },
{ 'Q', nv_exmode, NV_NCW, 0 },
{ 'R', nv_Replace, 0, false },
{ 'S', nv_subst, NV_KEEPREG, 0 },
{ 'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD },
{ 'U', nv_Undo, 0, 0 },
{ 'W', nv_wordcmd, 0, true },
{ 'X', nv_abbrev, NV_KEEPREG, 0 },
{ 'Y', nv_abbrev, NV_KEEPREG, 0 },
{ 'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0 },
{ '[', nv_brackets, NV_NCH_ALW, BACKWARD },
{ '\\', nv_error, 0, 0 },
{ ']', nv_brackets, NV_NCH_ALW, FORWARD },
{ '^', nv_beginline, 0, BL_WHITE | BL_FIX },
{ '_', nv_lineop, 0, 0 },
{ '`', nv_gomark, NV_NCH_ALW, false },
{ 'a', nv_edit, NV_NCH, 0 },
{ 'b', nv_bck_word, 0, 0 },
{ 'c', nv_operator, 0, 0 },
{ 'd', nv_operator, 0, 0 },
{ 'e', nv_wordcmd, 0, false },
{ 'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD },
{ 'g', nv_g_cmd, NV_NCH_ALW, false },
{ 'h', nv_left, NV_RL, 0 },
{ 'i', nv_edit, NV_NCH, 0 },
{ 'j', nv_down, 0, false },
{ 'k', nv_up, 0, false },
{ 'l', nv_right, NV_RL, 0 },
{ 'm', nv_mark, NV_NCH_NOP, 0 },
{ 'n', nv_next, 0, 0 },
{ 'o', nv_open, 0, 0 },
{ 'p', nv_put, 0, 0 },
{ 'q', nv_record, NV_NCH, 0 },
{ 'r', nv_replace, NV_NCH_NOP|NV_LANG, 0 },
{ 's', nv_subst, NV_KEEPREG, 0 },
{ 't', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD },
{ 'u', nv_undo, 0, 0 },
{ 'w', nv_wordcmd, 0, false },
{ 'x', nv_abbrev, NV_KEEPREG, 0 },
{ 'y', nv_operator, 0, 0 },
{ 'z', nv_zet, NV_NCH_ALW, 0 },
{ '{', nv_findpar, 0, BACKWARD },
{ '|', nv_pipe, 0, 0 },
{ '}', nv_findpar, 0, FORWARD },
{ '~', nv_tilde, 0, 0 },
// pound sign
{ POUND, nv_ident, 0, 0 },
{ K_MOUSEUP, nv_mousescroll, 0, MSCR_UP },
{ K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN },
{ K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT },
{ K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT },
{ K_LEFTMOUSE, nv_mouse, 0, 0 },
{ K_LEFTMOUSE_NM, nv_mouse, 0, 0 },
{ K_LEFTDRAG, nv_mouse, 0, 0 },
{ K_LEFTRELEASE, nv_mouse, 0, 0 },
{ K_LEFTRELEASE_NM, nv_mouse, 0, 0 },
{ K_MIDDLEMOUSE, nv_mouse, 0, 0 },
{ K_MIDDLEDRAG, nv_mouse, 0, 0 },
{ K_MIDDLERELEASE, nv_mouse, 0, 0 },
{ K_RIGHTMOUSE, nv_mouse, 0, 0 },
{ K_RIGHTDRAG, nv_mouse, 0, 0 },
{ K_RIGHTRELEASE, nv_mouse, 0, 0 },
{ K_X1MOUSE, nv_mouse, 0, 0 },
{ K_X1DRAG, nv_mouse, 0, 0 },
{ K_X1RELEASE, nv_mouse, 0, 0 },
{ K_X2MOUSE, nv_mouse, 0, 0 },
{ K_X2DRAG, nv_mouse, 0, 0 },
{ K_X2RELEASE, nv_mouse, 0, 0 },
{ K_IGNORE, nv_ignore, NV_KEEPREG, 0 },
{ K_NOP, nv_nop, 0, 0 },
{ K_INS, nv_edit, 0, 0 },
{ K_KINS, nv_edit, 0, 0 },
{ K_BS, nv_ctrlh, 0, 0 },
{ K_UP, nv_up, NV_SSS|NV_STS, false },
{ K_S_UP, nv_page, NV_SS, BACKWARD },
{ K_DOWN, nv_down, NV_SSS|NV_STS, false },
{ K_S_DOWN, nv_page, NV_SS, FORWARD },
{ K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0 },
{ K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0 },
{ K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1 },
{ K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0 },
{ K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, false },
{ K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, true },
{ K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD },
{ K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD },
{ K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD },
{ K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD },
{ K_END, nv_end, NV_SSS|NV_STS, false },
{ K_KEND, nv_end, NV_SSS|NV_STS, false },
{ K_S_END, nv_end, NV_SS, false },
{ K_C_END, nv_end, NV_SSS|NV_STS, true },
{ K_HOME, nv_home, NV_SSS|NV_STS, 0 },
{ K_KHOME, nv_home, NV_SSS|NV_STS, 0 },
{ K_S_HOME, nv_home, NV_SS, 0 },
{ K_C_HOME, nv_goto, NV_SSS|NV_STS, false },
{ K_DEL, nv_abbrev, 0, 0 },
{ K_KDEL, nv_abbrev, 0, 0 },
{ K_UNDO, nv_kundo, 0, 0 },
{ K_HELP, nv_help, NV_NCW, 0 },
{ K_F1, nv_help, NV_NCW, 0 },
{ K_XF1, nv_help, NV_NCW, 0 },
{ K_SELECT, nv_select, 0, 0 },
{ K_F8, farsi_f8, 0, 0 },
{ K_F9, farsi_f9, 0, 0 },
{ K_EVENT, nv_event, NV_KEEPREG, 0 },
{ K_FOCUSGAINED, nv_focusgained, NV_KEEPREG, 0 },
{ K_FOCUSLOST, nv_focuslost, NV_KEEPREG, 0 },
};
/* Number of commands in nv_cmds[]. */
#define NV_CMDS_SIZE ARRAY_SIZE(nv_cmds)
/* Sorted index of commands in nv_cmds[]. */
static short nv_cmd_idx[NV_CMDS_SIZE];
/* The highest index for which
* nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
static int nv_max_linear;
/*
* Compare functions for qsort() below, that checks the command character
* through the index in nv_cmd_idx[].
*/
static int nv_compare(const void *s1, const void *s2)
{
int c1, c2;
/* The commands are sorted on absolute value. */
c1 = nv_cmds[*(const short *)s1].cmd_char;
c2 = nv_cmds[*(const short *)s2].cmd_char;
if (c1 < 0)
c1 = -c1;
if (c2 < 0)
c2 = -c2;
return c1 - c2;
}
/*
* Initialize the nv_cmd_idx[] table.
*/
void init_normal_cmds(void)
{
assert(NV_CMDS_SIZE <= SHRT_MAX);
/* Fill the index table with a one to one relation. */
for (short int i = 0; i < (short int)NV_CMDS_SIZE; ++i) {
nv_cmd_idx[i] = i;
}
/* Sort the commands by the command character. */
qsort(&nv_cmd_idx, NV_CMDS_SIZE, sizeof(short), nv_compare);
/* Find the first entry that can't be indexed by the command character. */
short int i;
for (i = 0; i < (short int)NV_CMDS_SIZE; ++i) {
if (i != nv_cmds[nv_cmd_idx[i]].cmd_char) {
break;
}
}
nv_max_linear = i - 1;
}
/*
* Search for a command in the commands table.
* Returns -1 for invalid command.
*/
static int find_command(int cmdchar)
{
int i;
int idx;
int top, bot;
int c;
/* A multi-byte character is never a command. */
if (cmdchar >= 0x100)
return -1;
/* We use the absolute value of the character. Special keys have a
* negative value, but are sorted on their absolute value. */
if (cmdchar < 0)
cmdchar = -cmdchar;
/* If the character is in the first part: The character is the index into
* nv_cmd_idx[]. */
assert(nv_max_linear < (int)NV_CMDS_SIZE);
if (cmdchar <= nv_max_linear)
return nv_cmd_idx[cmdchar];
/* Perform a binary search. */
bot = nv_max_linear + 1;
top = NV_CMDS_SIZE - 1;
idx = -1;
while (bot <= top) {
i = (top + bot) / 2;
c = nv_cmds[nv_cmd_idx[i]].cmd_char;
if (c < 0)
c = -c;
if (cmdchar == c) {
idx = nv_cmd_idx[i];
break;
}
if (cmdchar > c)
bot = i + 1;
else
top = i - 1;
}
return idx;
}
// Normal state entry point. This is called on:
//
// - Startup, In this case the function never returns.
// - The command-line window is opened(`q:`). Returns when `cmdwin_result` != 0.
// - The :visual command is called from :global in ex mode, `:global/PAT/visual`
// for example. Returns when re-entering ex mode(because ex mode recursion is
// not allowed)
//
// This used to be called main_loop on main.c
void normal_enter(bool cmdwin, bool noexmode)
{
NormalState state;
normal_state_init(&state);
state.cmdwin = cmdwin;
state.noexmode = noexmode;
state.toplevel = (!cmdwin || cmdwin_result == 0) && !noexmode;
state_enter(&state.state);
}
static void normal_prepare(NormalState *s)
{
memset(&s->ca, 0, sizeof(s->ca)); // also resets ca.retval
s->ca.oap = &s->oa;
// Use a count remembered from before entering an operator. After typing "3d"
// we return from normal_cmd() and come back here, the "3" is remembered in
// "opcount".
s->ca.opcount = opcount;
// If there is an operator pending, then the command we take this time will
// terminate it. Finish_op tells us to finish the operation before returning
// this time (unless the operation was cancelled).
int c = finish_op;
finish_op = (s->oa.op_type != OP_NOP);
if (finish_op != c) {
ui_cursor_shape(); // may show different cursor shape
}
// When not finishing an operator and no register name typed, reset the count.
if (!finish_op && !s->oa.regname) {
s->ca.opcount = 0;
s->set_prevcount = true;
}
// Restore counts from before receiving K_EVENT. This means after
// typing "3", handling K_EVENT and then typing "2" we get "32", not
// "3 * 2".
if (s->oa.prev_opcount > 0 || s->oa.prev_count0 > 0) {
s->ca.opcount = s->oa.prev_opcount;
s->ca.count0 = s->oa.prev_count0;
s->oa.prev_opcount = 0;
s->oa.prev_count0 = 0;
}
s->mapped_len = typebuf_maplen();
State = NORMAL_BUSY;
// Set v:count here, when called from main() and not a stuffed command, so
// that v:count can be used in an expression mapping when there is no count.
// Do set it for redo
if (s->toplevel && readbuf1_empty()) {
set_vcount_ca(&s->ca, &s->set_prevcount);
}
}
static bool normal_handle_special_visual_command(NormalState *s)
{
// when 'keymodel' contains "stopsel" may stop Select/Visual mode
if (km_stopsel
&& (nv_cmds[s->idx].cmd_flags & NV_STS)
&& !(mod_mask & MOD_MASK_SHIFT)) {
end_visual_mode();
redraw_curbuf_later(INVERTED);
}
// Keys that work different when 'keymodel' contains "startsel"
if (km_startsel) {
if (nv_cmds[s->idx].cmd_flags & NV_SS) {
unshift_special(&s->ca);
s->idx = find_command(s->ca.cmdchar);
if (s->idx < 0) {
// Just in case
clearopbeep(&s->oa);
return true;
}
} else if ((nv_cmds[s->idx].cmd_flags & NV_SSS)
&& (mod_mask & MOD_MASK_SHIFT)) {
mod_mask &= ~MOD_MASK_SHIFT;
}
}
return false;
}
static bool normal_need_aditional_char(NormalState *s)
{
int flags = nv_cmds[s->idx].cmd_flags;
bool pending_op = s->oa.op_type != OP_NOP;
int cmdchar = s->ca.cmdchar;
return
// without NV_NCH we never need to check for an additional char
flags & NV_NCH && (
// NV_NCH_NOP is set and no operator is pending, get a second char
((flags & NV_NCH_NOP) == NV_NCH_NOP && !pending_op)
// NV_NCH_ALW is set, always get a second char
|| (flags & NV_NCH_ALW) == NV_NCH_ALW
// 'q' without a pending operator, recording or executing a register,
// needs to be followed by a second char, examples:
// - qc => record using register c
// - q: => open command-line window
|| (cmdchar == 'q' && !pending_op && !Recording && !Exec_reg)
// 'a' or 'i' after an operator is a text object, examples:
// - ciw => change inside word
// - da( => delete parenthesis and everything inside.
// Also, don't do anything when these keys are received in visual mode
// so just get another char.
//
// TODO(tarruda): Visual state needs to be refactored into a
// separate state that "inherits" from normal state.
|| ((cmdchar == 'a' || cmdchar == 'i') && (pending_op || VIsual_active)));
}
static bool normal_need_redraw_mode_message(NormalState *s)
{
return (
// 'showmode' is set and messages can be printed
((p_smd && msg_silent == 0
// must restart insert mode(ctrl+o or ctrl+l) or we just entered visual
// mode
&& (restart_edit != 0 || (VIsual_active
&& s->old_pos.lnum == curwin->w_cursor.lnum
&& s->old_pos.col == curwin->w_cursor.col))
// command-line must be cleared or redrawn
&& (clear_cmdline || redraw_cmdline)
// some message was printed or scrolled
&& (msg_didout || (msg_didany && msg_scroll))
// it is fine to remove the current message
&& !msg_nowait
// the command was the result of direct user input and not a mapping
&& KeyTyped)
// must restart insert mode, not in visual mode and error message is
// being shown
|| (restart_edit != 0 && !VIsual_active && msg_scroll
&& emsg_on_display))
// no register was used
&& s->oa.regname == 0
&& !(s->ca.retval & CA_COMMAND_BUSY)
&& stuff_empty()
&& typebuf_typed()
&& emsg_silent == 0
&& !did_wait_return
&& s->oa.op_type == OP_NOP);
}
static void normal_redraw_mode_message(NormalState *s)
{
int save_State = State;
// Draw the cursor with the right shape here
if (restart_edit != 0) {
State = INSERT;
}
// If need to redraw, and there is a "keep_msg", redraw before the
// delay
if (must_redraw && keep_msg != NULL && !emsg_on_display) {
char_u *kmsg;
kmsg = keep_msg;
keep_msg = NULL;
// showmode() will clear keep_msg, but we want to use it anyway
update_screen(0);
// now reset it, otherwise it's put in the history again
keep_msg = kmsg;
msg_attr((const char *)kmsg, keep_msg_attr);
xfree(kmsg);
}
setcursor();
ui_flush();
if (msg_scroll || emsg_on_display) {
os_delay(1000L, true); // wait at least one second
}
os_delay(3000L, false); // wait up to three seconds
State = save_State;
msg_scroll = false;
emsg_on_display = false;
}
// TODO(tarruda): Split into a "normal pending" state that can handle K_EVENT
static void normal_get_additional_char(NormalState *s)
{
int *cp;
bool repl = false; // get character for replace mode
bool lit = false; // get extra character literally
bool langmap_active = false; // using :lmap mappings
int lang; // getting a text character
++no_mapping;
++allow_keys; // no mapping for nchar, but allow key codes
// Don't generate a CursorHold event here, most commands can't handle
// it, e.g., nv_replace(), nv_csearch().
did_cursorhold = true;
if (s->ca.cmdchar == 'g') {
// For 'g' get the next character now, so that we can check for
// "gr", "g'" and "g`".
s->ca.nchar = plain_vgetc();
LANGMAP_ADJUST(s->ca.nchar, true);
s->need_flushbuf |= add_to_showcmd(s->ca.nchar);
if (s->ca.nchar == 'r' || s->ca.nchar == '\'' || s->ca.nchar == '`'
|| s->ca.nchar == Ctrl_BSL) {
cp = &s->ca.extra_char; // need to get a third character
if (s->ca.nchar != 'r') {
lit = true; // get it literally
} else {
repl = true; // get it in replace mode
}
} else {
cp = NULL; // no third character needed
}
} else {
if (s->ca.cmdchar == 'r') {
// get it in replace mode
repl = true;
}
cp = &s->ca.nchar;
}
lang = (repl || (nv_cmds[s->idx].cmd_flags & NV_LANG));
// Get a second or third character.
if (cp != NULL) {
if (repl) {
State = REPLACE; // pretend Replace mode
ui_cursor_shape(); // show different cursor shape
}
if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP) {
// Allow mappings defined with ":lmap".
--no_mapping;
--allow_keys;
if (repl) {
State = LREPLACE;
} else {
State = LANGMAP;
}
langmap_active = true;
}
*cp = plain_vgetc();
if (langmap_active) {
// Undo the decrement done above
++no_mapping;
++allow_keys;
State = NORMAL_BUSY;
}
State = NORMAL_BUSY;
s->need_flushbuf |= add_to_showcmd(*cp);
if (!lit) {
// Typing CTRL-K gets a digraph.
if (*cp == Ctrl_K && ((nv_cmds[s->idx].cmd_flags & NV_LANG)
|| cp == &s->ca.extra_char)
&& vim_strchr(p_cpo, CPO_DIGRAPH) == NULL) {
s->c = get_digraph(false);
if (s->c > 0) {
*cp = s->c;
// Guessing how to update showcmd here...
del_from_showcmd(3);
s->need_flushbuf |= add_to_showcmd(*cp);
}
}
// adjust chars > 127, except after "tTfFr" commands
LANGMAP_ADJUST(*cp, !lang);
// adjust Hebrew mapped char
if (p_hkmap && lang && KeyTyped) {
*cp = hkmap(*cp);
}
// adjust Farsi mapped char
if (p_fkmap && lang && KeyTyped) {
*cp = fkmap(*cp);
}
}
// When the next character is CTRL-\ a following CTRL-N means the
// command is aborted and we go to Normal mode.
if (cp == &s->ca.extra_char
&& s->ca.nchar == Ctrl_BSL
&& (s->ca.extra_char == Ctrl_N || s->ca.extra_char == Ctrl_G)) {
s->ca.cmdchar = Ctrl_BSL;
s->ca.nchar = s->ca.extra_char;
s->idx = find_command(s->ca.cmdchar);
} else if ((s->ca.nchar == 'n' || s->ca.nchar == 'N')
&& s->ca.cmdchar == 'g') {
s->ca.oap->op_type = get_op_type(*cp, NUL);
} else if (*cp == Ctrl_BSL) {
long towait = (p_ttm >= 0 ? p_ttm : p_tm);
// There is a busy wait here when typing "f<C-\>" and then
// something different from CTRL-N. Can't be avoided.
while ((s->c = vpeekc()) <= 0 && towait > 0L) {
do_sleep(towait > 50L ? 50L : towait);
towait -= 50L;
}
if (s->c > 0) {
s->c = plain_vgetc();
if (s->c != Ctrl_N && s->c != Ctrl_G) {
vungetc(s->c);
} else {
s->ca.cmdchar = Ctrl_BSL;
s->ca.nchar = s->c;
s->idx = find_command(s->ca.cmdchar);
assert(s->idx >= 0);
}
}
}
// When getting a text character and the next character is a
// multi-byte character, it could be a composing character.
// However, don't wait for it to arrive. Also, do enable mapping,
// because if it's put back with vungetc() it's too late to apply
// mapping.
no_mapping--;
while (enc_utf8 && lang && (s->c = vpeekc()) > 0
&& (s->c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1)) {
s->c = plain_vgetc();
if (!utf_iscomposing(s->c)) {
vungetc(s->c); /* it wasn't, put it back */
break;
} else if (s->ca.ncharC1 == 0) {
s->ca.ncharC1 = s->c;
} else {
s->ca.ncharC2 = s->c;
}
}
no_mapping++;
}
--no_mapping;
--allow_keys;
}
static void normal_invert_horizontal(NormalState *s)
{
switch (s->ca.cmdchar) {
case 'l': s->ca.cmdchar = 'h'; break;
case K_RIGHT: s->ca.cmdchar = K_LEFT; break;
case K_S_RIGHT: s->ca.cmdchar = K_S_LEFT; break;
case K_C_RIGHT: s->ca.cmdchar = K_C_LEFT; break;
case 'h': s->ca.cmdchar = 'l'; break;
case K_LEFT: s->ca.cmdchar = K_RIGHT; break;
case K_S_LEFT: s->ca.cmdchar = K_S_RIGHT; break;
case K_C_LEFT: s->ca.cmdchar = K_C_RIGHT; break;
case '>': s->ca.cmdchar = '<'; break;
case '<': s->ca.cmdchar = '>'; break;
}
s->idx = find_command(s->ca.cmdchar);
}
static bool normal_get_command_count(NormalState *s)
{
if (VIsual_active && VIsual_select) {
return false;
}
// Handle a count before a command and compute ca.count0.
// Note that '0' is a command and not the start of a count, but it's
// part of a count after other digits.
while ((s->c >= '1' && s->c <= '9') || (s->ca.count0 != 0
&& (s->c == K_DEL || s->c == K_KDEL || s->c == '0'))) {
if (s->c == K_DEL || s->c == K_KDEL) {
s->ca.count0 /= 10;
del_from_showcmd(4); // delete the digit and ~@%
} else {
s->ca.count0 = s->ca.count0 * 10 + (s->c - '0');
}
if (s->ca.count0 < 0) {
// got too large!
s->ca.count0 = 999999999L;
}
// Set v:count here, when called from main() and not a stuffed
// command, so that v:count can be used in an expression mapping
// right after the count. Do set it for redo.
if (s->toplevel && readbuf1_empty()) {
set_vcount_ca(&s->ca, &s->set_prevcount);
}
if (s->ctrl_w) {
++no_mapping;
++allow_keys; // no mapping for nchar, but keys
}
++no_zero_mapping; // don't map zero here
s->c = plain_vgetc();
LANGMAP_ADJUST(s->c, true);
--no_zero_mapping;
if (s->ctrl_w) {
--no_mapping;
--allow_keys;
}
s->need_flushbuf |= add_to_showcmd(s->c);
}
// If we got CTRL-W there may be a/another count
if (s->c == Ctrl_W && !s->ctrl_w && s->oa.op_type == OP_NOP) {
s->ctrl_w = true;
s->ca.opcount = s->ca.count0; // remember first count
s->ca.count0 = 0;
++no_mapping;
++allow_keys; // no mapping for nchar, but keys
s->c = plain_vgetc(); // get next character
LANGMAP_ADJUST(s->c, true);
--no_mapping;
--allow_keys;
s->need_flushbuf |= add_to_showcmd(s->c);
return true;
}
return false;
}
static void normal_finish_command(NormalState *s)
{
if (s->command_finished) {
goto normal_end;
}
// If we didn't start or finish an operator, reset oap->regname, unless we
// need it later.
if (!finish_op
&& !s->oa.op_type
&& (s->idx < 0 || !(nv_cmds[s->idx].cmd_flags & NV_KEEPREG))) {
clearop(&s->oa);
set_reg_var(get_default_register_name());
}
// Get the length of mapped chars again after typing a count, second
// character or "z333<cr>".
if (s->old_mapped_len > 0) {
s->old_mapped_len = typebuf_maplen();
}
// If an operation is pending, handle it...
do_pending_operator(&s->ca, s->old_col, false);
// Wait for a moment when a message is displayed that will be overwritten
// by the mode message.
// In Visual mode and with "^O" in Insert mode, a short message will be
// overwritten by the mode message. Wait a bit, until a key is hit.
// In Visual mode, it's more important to keep the Visual area updated
// than keeping a message (e.g. from a /pat search).
// Only do this if the command was typed, not from a mapping.
// Don't wait when emsg_silent is non-zero.
// Also wait a bit after an error message, e.g. for "^O:".
// Don't redraw the screen, it would remove the message.
if (normal_need_redraw_mode_message(s)) {
normal_redraw_mode_message(s);
}
// Finish up after executing a Normal mode command.
normal_end:
msg_nowait = false;
// Reset finish_op, in case it was set
s->c = finish_op;
finish_op = false;
// Redraw the cursor with another shape, if we were in Operator-pending
// mode or did a replace command.
if (s->c || s->ca.cmdchar == 'r') {
ui_cursor_shape(); // may show different cursor shape
}
if (s->oa.op_type == OP_NOP && s->oa.regname == 0
&& s->ca.cmdchar != K_EVENT) {
clear_showcmd();
}
checkpcmark(); // check if we moved since setting pcmark
xfree(s->ca.searchbuf);
if (has_mbyte) {
mb_adjust_cursor();
}
if (curwin->w_p_scb && s->toplevel) {
validate_cursor(); // may need to update w_leftcol
do_check_scrollbind(true);
}
if (curwin->w_p_crb && s->toplevel) {
validate_cursor(); // may need to update w_leftcol
do_check_cursorbind();
}
// May restart edit(), if we got here with CTRL-O in Insert mode (but not
// if still inside a mapping that started in Visual mode).
// May switch from Visual to Select mode after CTRL-O command.
if (s->oa.op_type == OP_NOP
&& ((restart_edit != 0 && !VIsual_active && s->old_mapped_len == 0)
|| restart_VIsual_select == 1)
&& !(s->ca.retval & CA_COMMAND_BUSY)
&& stuff_empty()
&& s->oa.regname == 0) {
if (restart_VIsual_select == 1) {
VIsual_select = true;
showmode();
restart_VIsual_select = 0;
}
if (restart_edit != 0 && !VIsual_active && s->old_mapped_len == 0) {
(void)edit(restart_edit, false, 1L);
}
}
if (restart_VIsual_select == 2) {
restart_VIsual_select = 1;
}
// Save count before an operator for next time
opcount = s->ca.opcount;
}
static int normal_execute(VimState *state, int key)
{
NormalState *s = (NormalState *)state;
s->command_finished = false;
s->ctrl_w = false; /* got CTRL-W command */
s->old_col = curwin->w_curswant;
s->c = key;
LANGMAP_ADJUST(s->c, get_real_state() != SELECTMODE);
// If a mapping was started in Visual or Select mode, remember the length
// of the mapping. This is used below to not return to Insert mode for as
// long as the mapping is being executed.
if (restart_edit == 0) {
s->old_mapped_len = 0;
} else if (s->old_mapped_len || (VIsual_active && s->mapped_len == 0
&& typebuf_maplen() > 0)) {
s->old_mapped_len = typebuf_maplen();
}
if (s->c == NUL) {
s->c = K_ZERO;
}
// In Select mode, typed text replaces the selection.
if (VIsual_active && VIsual_select && (vim_isprintc(s->c)
|| s->c == NL || s->c == CAR || s->c == K_KENTER)) {
// Fake a "c"hange command. When "restart_edit" is set (e.g., because
// 'insertmode' is set) fake a "d"elete command, Insert mode will
// restart automatically.
// Insert the typed character in the typeahead buffer, so that it can
// be mapped in Insert mode. Required for ":lmap" to work.
ins_char_typebuf(s->c);