-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathnormal.c
7573 lines (6914 loc) · 179 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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar et al.
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* 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 "vim.h"
static int VIsual_mode_orig = NUL; // saved Visual mode
#ifdef FEAT_EVAL
static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
#endif
static void unshift_special(cmdarg_T *cap);
static void del_from_showcmd(int);
/*
* 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 void nv_ignore(cmdarg_T *cap);
static void nv_nop(cmdarg_T *cap);
static void nv_error(cmdarg_T *cap);
static void nv_help(cmdarg_T *cap);
static void nv_addsub(cmdarg_T *cap);
static void nv_page(cmdarg_T *cap);
static void nv_zet(cmdarg_T *cap);
#ifdef FEAT_GUI
static void nv_ver_scrollbar(cmdarg_T *cap);
static void nv_hor_scrollbar(cmdarg_T *cap);
#endif
#ifdef FEAT_GUI_TABLINE
static void nv_tabline(cmdarg_T *cap);
static void nv_tabmenu(cmdarg_T *cap);
#endif
static void nv_exmode(cmdarg_T *cap);
static void nv_colon(cmdarg_T *cap);
static void nv_ctrlg(cmdarg_T *cap);
static void nv_ctrlh(cmdarg_T *cap);
static void nv_clear(cmdarg_T *cap);
static void nv_ctrlo(cmdarg_T *cap);
static void nv_hat(cmdarg_T *cap);
static void nv_Zet(cmdarg_T *cap);
static void nv_ident(cmdarg_T *cap);
static void nv_tagpop(cmdarg_T *cap);
static void nv_scroll(cmdarg_T *cap);
static void nv_right(cmdarg_T *cap);
static void nv_left(cmdarg_T *cap);
static void nv_up(cmdarg_T *cap);
static void nv_down(cmdarg_T *cap);
static void nv_end(cmdarg_T *cap);
static void nv_dollar(cmdarg_T *cap);
static void nv_search(cmdarg_T *cap);
static void nv_next(cmdarg_T *cap);
static int normal_search(cmdarg_T *cap, int dir, char_u *pat, size_t patlen, int opt, int *wrapped);
static void nv_csearch(cmdarg_T *cap);
static void nv_brackets(cmdarg_T *cap);
static void nv_percent(cmdarg_T *cap);
static void nv_brace(cmdarg_T *cap);
static void nv_mark(cmdarg_T *cap);
static void nv_findpar(cmdarg_T *cap);
static void nv_undo(cmdarg_T *cap);
static void nv_kundo(cmdarg_T *cap);
static void nv_Replace(cmdarg_T *cap);
static void nv_replace(cmdarg_T *cap);
static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
static void v_visop(cmdarg_T *cap);
static void nv_subst(cmdarg_T *cap);
static void nv_abbrev(cmdarg_T *cap);
static void nv_optrans(cmdarg_T *cap);
static void nv_gomark(cmdarg_T *cap);
static void nv_pcmark(cmdarg_T *cap);
static void nv_regname(cmdarg_T *cap);
static void nv_visual(cmdarg_T *cap);
static void n_start_visual_mode(int c);
static void nv_window(cmdarg_T *cap);
static void nv_suspend(cmdarg_T *cap);
static void nv_g_cmd(cmdarg_T *cap);
static void nv_dot(cmdarg_T *cap);
static void nv_redo_or_register(cmdarg_T *cap);
static void nv_Undo(cmdarg_T *cap);
static void nv_tilde(cmdarg_T *cap);
static void nv_operator(cmdarg_T *cap);
#ifdef FEAT_EVAL
static void set_op_var(int optype);
#endif
static void nv_lineop(cmdarg_T *cap);
static void nv_home(cmdarg_T *cap);
static void nv_pipe(cmdarg_T *cap);
static void nv_bck_word(cmdarg_T *cap);
static void nv_wordcmd(cmdarg_T *cap);
static void nv_beginline(cmdarg_T *cap);
static void adjust_cursor(oparg_T *oap);
static void adjust_for_sel(cmdarg_T *cap);
static void nv_select(cmdarg_T *cap);
static void nv_goto(cmdarg_T *cap);
static void nv_normal(cmdarg_T *cap);
static void nv_esc(cmdarg_T *oap);
static void nv_edit(cmdarg_T *cap);
static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
static void nv_object(cmdarg_T *cap);
static void nv_record(cmdarg_T *cap);
static void nv_at(cmdarg_T *cap);
static void nv_halfpage(cmdarg_T *cap);
static void nv_join(cmdarg_T *cap);
static void nv_put(cmdarg_T *cap);
static void nv_put_opt(cmdarg_T *cap, int fix_indent);
static void nv_open(cmdarg_T *cap);
#ifdef FEAT_NETBEANS_INTG
static void nv_nbcmd(cmdarg_T *cap);
#endif
#ifdef FEAT_DND
static void nv_drop(cmdarg_T *cap);
#endif
static void nv_cursorhold(cmdarg_T *cap);
// Declare nv_cmds[].
#define DO_DECLARE_NVCMD
#include "nv_cmds.h"
// Include the lookuptable generated by create_nvcmdidx.vim.
#include "nv_cmdidxs.h"
/*
* 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[].
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;
}
/*
* If currently editing a cmdline or text is locked: beep and give an error
* message, return TRUE.
*/
static int
check_text_locked(oparg_T *oap)
{
if (!text_locked())
return FALSE;
if (oap != NULL)
clearopbeep(oap);
text_locked_msg();
return TRUE;
}
/*
* If text is locked, "curbuf_lock" or "allbuf_lock" is set:
* Give an error message, possibly beep and return TRUE.
* "oap" may be NULL.
*/
int
check_text_or_curbuf_locked(oparg_T *oap)
{
if (check_text_locked(oap))
return TRUE;
if (!curbuf_locked())
return FALSE;
if (oap != NULL)
clearop(oap);
return TRUE;
}
/*
* Handle the count before a normal command and set cap->count0.
*/
static int
normal_cmd_get_count(
cmdarg_T *cap,
int c,
int toplevel UNUSED,
int set_prevcount UNUSED,
int *ctrl_w,
int *need_flushbuf UNUSED)
{
getcount:
if (!(VIsual_active && VIsual_select))
{
// 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 ((c >= '1' && c <= '9')
|| (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
|| c == '0')))
{
if (c == K_DEL || c == K_KDEL)
{
cap->count0 /= 10;
del_from_showcmd(4); // delete the digit and ~@%
}
else if (cap->count0 > 99999999L)
{
cap->count0 = 999999999L;
}
else
{
cap->count0 = cap->count0 * 10 + (c - '0');
}
#ifdef FEAT_EVAL
// 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 (toplevel && readbuf1_empty())
set_vcount_ca(cap, &set_prevcount);
#endif
if (*ctrl_w)
{
++no_mapping;
++allow_keys; // no mapping for nchar, but keys
}
++no_zero_mapping; // don't map zero here
c = plain_vgetc();
LANGMAP_ADJUST(c, TRUE);
--no_zero_mapping;
if (*ctrl_w)
{
--no_mapping;
--allow_keys;
}
*need_flushbuf |= add_to_showcmd(c);
}
// If we got CTRL-W there may be a/another count
if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
{
*ctrl_w = TRUE;
cap->opcount = cap->count0; // remember first count
cap->count0 = 0;
++no_mapping;
++allow_keys; // no mapping for nchar, but keys
c = plain_vgetc(); // get next character
LANGMAP_ADJUST(c, TRUE);
--no_mapping;
--allow_keys;
*need_flushbuf |= add_to_showcmd(c);
goto getcount; // jump back
}
}
if (c == K_CURSORHOLD)
{
// Save the count values so that ca.opcount and ca.count0 are exactly
// the same when coming back here after handling K_CURSORHOLD.
cap->oap->prev_opcount = cap->opcount;
cap->oap->prev_count0 = cap->count0;
}
else if (cap->opcount != 0)
{
// If we're in the middle of an operator (including after entering a
// yank buffer with '"') AND we had a count before the operator, then
// that count overrides the current value of ca.count0.
// What this means effectively, is that commands like "3dw" get turned
// into "d3w" which makes things fall into place pretty neatly.
// If you give a count before AND after the operator, they are
// multiplied.
if (cap->count0)
{
if (cap->opcount >= 999999999L / cap->count0)
cap->count0 = 999999999L;
else
cap->count0 *= cap->opcount;
}
else
cap->count0 = cap->opcount;
}
// Always remember the count. It will be set to zero (on the next call,
// above) when there is no pending operator.
// When called from main(), save the count for use by the "count" built-in
// variable.
cap->opcount = cap->count0;
cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
#ifdef FEAT_EVAL
// Only set v:count when called from main() and not a stuffed command.
// Do set it for redo.
if (toplevel && readbuf1_empty())
set_vcount(cap->count0, cap->count1, set_prevcount);
#endif
return c;
}
/*
* Returns TRUE if the normal command (cap) needs a second character.
*/
static int
normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
{
return ((cmd_flags & NV_NCH)
&& (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
&& cap->oap->op_type == OP_NOP)
|| (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
|| (cap->cmdchar == 'q'
&& cap->oap->op_type == OP_NOP
&& reg_recording == 0
&& reg_executing == 0)
|| ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
&& (cap->oap->op_type != OP_NOP || VIsual_active))));
}
/*
* Get one or more additional characters for a normal command.
* Return the updated command index (if changed).
*/
static int
normal_cmd_get_more_chars(
int idx_arg,
cmdarg_T *cap,
int *need_flushbuf UNUSED)
{
int idx = idx_arg;
int c;
int *cp;
int repl = FALSE; // get character for replace mode
int lit = FALSE; // get extra character literally
int langmap_active = FALSE; // using :lmap mappings
int lang; // getting a text character
#ifdef HAVE_INPUT_METHOD
int save_smd; // saved value of p_smd
#endif
++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 (cap->cmdchar == 'g')
{
/*
* For 'g' get the next character now, so that we can check for
* "gr", "g'" and "g`".
*/
cap->nchar = plain_vgetc();
LANGMAP_ADJUST(cap->nchar, TRUE);
*need_flushbuf |= add_to_showcmd(cap->nchar);
if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
|| cap->nchar == Ctrl_BSL)
{
cp = &cap->extra_char; // need to get a third character
if (cap->nchar != 'r')
lit = TRUE; // get it literally
else
repl = TRUE; // get it in replace mode
}
else
cp = NULL; // no third character needed
}
else
{
if (cap->cmdchar == 'r') // get it in replace mode
repl = TRUE;
cp = &cap->nchar;
}
lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
/*
* Get a second or third character.
*/
if (cp != NULL)
{
if (repl)
{
State = MODE_REPLACE; // pretend Replace mode
#ifdef CURSOR_SHAPE
ui_cursor_shape(); // show different cursor shape
#endif
#ifdef FEAT_MOUSESHAPE
update_mouseshape(-1);
#endif
}
if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
{
// Allow mappings defined with ":lmap".
--no_mapping;
--allow_keys;
if (repl)
State = MODE_LREPLACE;
else
State = MODE_LANGMAP;
langmap_active = TRUE;
}
#ifdef HAVE_INPUT_METHOD
save_smd = p_smd;
p_smd = FALSE; // Don't let the IM code show the mode here
if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
im_set_active(TRUE);
#endif
if ((State & MODE_INSERT) && !p_ek)
{
MAY_WANT_TO_LOG_THIS;
// Disable bracketed paste and modifyOtherKeys here, we won't
// recognize the escape sequences with 'esckeys' off.
out_str(T_BD);
out_str_t_TE();
}
*cp = plain_vgetc();
if ((State & MODE_INSERT) && !p_ek)
{
MAY_WANT_TO_LOG_THIS;
// Re-enable bracketed paste mode and modifyOtherKeys
out_str_t_BE();
out_str_t_TI();
}
if (langmap_active)
{
// Undo the decrement done above
++no_mapping;
++allow_keys;
State = MODE_NORMAL_BUSY;
}
#ifdef HAVE_INPUT_METHOD
if (lang)
{
if (curbuf->b_p_iminsert != B_IMODE_LMAP)
im_save_status(&curbuf->b_p_iminsert);
im_set_active(FALSE);
}
p_smd = save_smd;
#endif
State = MODE_NORMAL_BUSY;
*need_flushbuf |= add_to_showcmd(*cp);
if (!lit)
{
#ifdef FEAT_DIGRAPHS
// Typing CTRL-K gets a digraph.
if (*cp == Ctrl_K
&& ((nv_cmds[idx].cmd_flags & NV_LANG)
|| cp == &cap->extra_char)
&& vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
{
c = get_digraph(FALSE);
if (c > 0)
{
*cp = c;
// Guessing how to update showcmd here...
del_from_showcmd(3);
*need_flushbuf |= add_to_showcmd(*cp);
}
}
#endif
// adjust chars > 127, except after "tTfFr" commands
LANGMAP_ADJUST(*cp, !lang);
#ifdef FEAT_RIGHTLEFT
// adjust Hebrew mapped char
if (p_hkmap && lang && KeyTyped)
*cp = hkmap(*cp);
#endif
}
// When the next character is CTRL-\ a following CTRL-N means the
// command is aborted and we go to Normal mode.
if (cp == &cap->extra_char
&& cap->nchar == Ctrl_BSL
&& (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
{
cap->cmdchar = Ctrl_BSL;
cap->nchar = cap->extra_char;
idx = find_command(cap->cmdchar);
}
else if ((cap->nchar == 'n' || cap->nchar == 'N')
&& cap->cmdchar == 'g')
cap->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 ((c = vpeekc()) <= 0 && towait > 0L)
{
do_sleep(towait > 50L ? 50L : towait, FALSE);
towait -= 50L;
}
if (c > 0)
{
c = plain_vgetc();
if (c != Ctrl_N && c != Ctrl_G)
vungetc(c);
else
{
cap->cmdchar = Ctrl_BSL;
cap->nchar = c;
idx = find_command(cap->cmdchar);
}
}
}
if (enc_utf8 && lang)
{
// 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 ((c = vpeekc()) > 0
&& (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
{
c = plain_vgetc();
if (!utf_iscomposing(c))
{
vungetc(c); // it wasn't, put it back
break;
}
else if (cap->ncharC1 == 0)
cap->ncharC1 = c;
else
cap->ncharC2 = c;
}
++no_mapping;
// Vim may be in a different mode when the user types the next key,
// but when replaying a recording the next key is already in the
// typeahead buffer, so record an <Ignore> before that to prevent
// the vpeekc() above from applying wrong mappings when replaying.
++no_u_sync;
gotchars_ignore();
--no_u_sync;
}
}
--no_mapping;
--allow_keys;
return idx;
}
/*
* Returns TRUE if after processing a normal mode command, need to wait for a
* moment when a message is displayed that will be overwritten by the mode
* message.
*/
static int
normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
{
// 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.
return ( ((p_smd
&& msg_silent == 0
&& (restart_edit != 0
|| (VIsual_active
&& old_pos->lnum == curwin->w_cursor.lnum
&& old_pos->col == curwin->w_cursor.col)
)
&& (clear_cmdline
|| redraw_cmdline)
&& (msg_didout || (msg_didany && msg_scroll))
&& !msg_nowait
&& KeyTyped)
|| (restart_edit != 0
&& !VIsual_active
&& (msg_scroll
|| emsg_on_display)))
&& cap->oap->regname == 0
&& !(cap->retval & CA_COMMAND_BUSY)
&& stuff_empty()
&& typebuf_typed()
&& emsg_silent == 0
&& !in_assert_fails
&& !did_wait_return
&& cap->oap->op_type == OP_NOP);
}
/*
* After processing a normal mode command, wait for a moment when a message is
* displayed that will be overwritten by the mode message.
*/
static void
normal_cmd_wait_for_msg(void)
{
int save_State = State;
// Draw the cursor with the right shape here
if (restart_edit != 0)
State = MODE_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.
// First update w_topline.
setcursor();
update_screen(0);
// now reset it, otherwise it's put in the history again
keep_msg = kmsg;
kmsg = vim_strsave(keep_msg);
if (kmsg != NULL)
{
msg_attr((char *)kmsg, keep_msg_attr);
vim_free(kmsg);
}
}
setcursor();
#ifdef CURSOR_SHAPE
ui_cursor_shape(); // may show different cursor shape
#endif
cursor_on();
out_flush();
if (msg_scroll || emsg_on_display)
ui_delay(1003L, TRUE); // wait at least one second
ui_delay(3003L, FALSE); // wait up to three seconds
State = save_State;
msg_scroll = FALSE;
emsg_on_display = FALSE;
}
/*
* Execute a command in Normal mode.
*/
void
normal_cmd(
oparg_T *oap,
int toplevel UNUSED) // TRUE when called from main()
{
cmdarg_T ca; // command arguments
int c;
int ctrl_w = FALSE; // got CTRL-W command
int old_col = curwin->w_curswant;
int need_flushbuf = FALSE; // need to call out_flush()
pos_T old_pos; // cursor position before command
int mapped_len;
static int old_mapped_len = 0;
int idx;
int set_prevcount = FALSE;
int save_did_cursorhold = did_cursorhold;
CLEAR_FIELD(ca); // also resets ca.retval
ca.oap = oap;
// 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".
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).
#ifdef CURSOR_SHAPE
c = finish_op;
#endif
finish_op = (oap->op_type != OP_NOP);
#ifdef CURSOR_SHAPE
if (finish_op != c)
{
ui_cursor_shape(); // may show different cursor shape
# ifdef FEAT_MOUSESHAPE
update_mouseshape(-1);
# endif
}
#endif
may_trigger_modechanged();
// When not finishing an operator and no register name typed, reset the
// count.
if (!finish_op && !oap->regname)
{
ca.opcount = 0;
#ifdef FEAT_EVAL
set_prevcount = TRUE;
#endif
}
// Restore counts from before receiving K_CURSORHOLD. This means after
// typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
// "3 * 2".
if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
{
ca.opcount = oap->prev_opcount;
ca.count0 = oap->prev_count0;
oap->prev_opcount = 0;
oap->prev_count0 = 0;
}
mapped_len = typebuf_maplen();
State = MODE_NORMAL_BUSY;
#ifdef USE_ON_FLY_SCROLL
dont_scroll = FALSE; // allow scrolling here
#endif
#ifdef FEAT_EVAL
// 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 (toplevel && readbuf1_empty())
set_vcount_ca(&ca, &set_prevcount);
#endif
/*
* Get the command character from the user.
*/
c = safe_vgetc();
LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
// 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)
old_mapped_len = 0;
else if (old_mapped_len
|| (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
old_mapped_len = typebuf_maplen();
if (c == NUL)
c = K_ZERO;
// In Select mode, typed text replaces the selection.
if (VIsual_active
&& VIsual_select
&& (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
{
int len;
// 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.
len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
// When recording and gotchars() was called the character will be
// recorded again, remove the previous recording.
if (KeyTyped)
ungetchars(len);
if (restart_edit != 0)
c = 'd';
else
c = 'c';
msg_nowait = TRUE; // don't delay going to insert mode
old_mapped_len = 0; // do go to Insert mode
}
// If the window was made so small that nothing shows, make it at least one
// line and one column when typing a command.
if (KeyTyped && !KeyStuffed)
win_ensure_size();
need_flushbuf = add_to_showcmd(c);
// Get the command count
c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
&need_flushbuf);
// Find the command character in the table of commands.
// For CTRL-W we already got nchar when looking for a count.
if (ctrl_w)
{
ca.nchar = c;
ca.cmdchar = Ctrl_W;
}
else
ca.cmdchar = c;
idx = find_command(ca.cmdchar);
if (idx < 0)
{
// Not a known command: beep.
clearopbeep(oap);
goto normal_end;
}
if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
// this command is not allowed now
goto normal_end;
// In Visual/Select mode, a few keys are handled in a special way.
if (VIsual_active)
{
// when 'keymodel' contains "stopsel" may stop Select/Visual mode
if (km_stopsel
&& (nv_cmds[idx].cmd_flags & NV_STS)
&& !(mod_mask & MOD_MASK_SHIFT))
{
end_visual_mode();
redraw_curbuf_later(UPD_INVERTED);
}
// Keys that work different when 'keymodel' contains "startsel"
if (km_startsel)
{
if (nv_cmds[idx].cmd_flags & NV_SS)
{
unshift_special(&ca);
idx = find_command(ca.cmdchar);
if (idx < 0)
{
// Just in case
clearopbeep(oap);
goto normal_end;
}
}
else if ((nv_cmds[idx].cmd_flags & NV_SSS)
&& (mod_mask & MOD_MASK_SHIFT))
mod_mask &= ~MOD_MASK_SHIFT;
}
}
#ifdef FEAT_RIGHTLEFT
if (curwin->w_p_rl && KeyTyped && !KeyStuffed
&& (nv_cmds[idx].cmd_flags & NV_RL))
{
// Invert horizontal movements and operations. Only when typed by the
// user directly, not when the result of a mapping or "x" translated
// to "dl".
switch (ca.cmdchar)
{
case 'l': ca.cmdchar = 'h'; break;
case K_RIGHT: ca.cmdchar = K_LEFT; break;
case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
case 'h': ca.cmdchar = 'l'; break;
case K_LEFT: ca.cmdchar = K_RIGHT; break;
case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
case '>': ca.cmdchar = '<'; break;
case '<': ca.cmdchar = '>'; break;
}
idx = find_command(ca.cmdchar);
}
#endif
// Get additional characters if we need them.
if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
// Flush the showcmd characters onto the screen so we can see them while
// the command is being executed. Only do this when the shown command was
// actually displayed, otherwise this will slow down a lot when executing
// mappings.
if (need_flushbuf)
out_flush();
if (ca.cmdchar != K_IGNORE)
{
if (ex_normal_busy)
did_cursorhold = save_did_cursorhold;
else
did_cursorhold = FALSE;
}
State = MODE_NORMAL;
if (ca.nchar == ESC || ca.extra_char == ESC)
{
clearop(oap);
if (restart_edit == 0 && goto_im())
restart_edit = 'a';
goto normal_end;
}
if (ca.cmdchar != K_IGNORE)
{
msg_didout = FALSE; // don't scroll screen up for normal command
msg_col = 0;
}
old_pos = curwin->w_cursor; // remember where the cursor was
// When 'keymodel' contains "startsel" some keys start Select/Visual
// mode.
if (!VIsual_active && km_startsel)
{
if (nv_cmds[idx].cmd_flags & NV_SS)
{
start_selection();
unshift_special(&ca);
idx = find_command(ca.cmdchar);
}
else if ((nv_cmds[idx].cmd_flags & NV_SSS)
&& (mod_mask & MOD_MASK_SHIFT))
{
start_selection();
mod_mask &= ~MOD_MASK_SHIFT;
}
}
// Execute the command!
// Call the command function found in the commands table.
ca.arg = nv_cmds[idx].cmd_arg;
(nv_cmds[idx].cmd_func)(&ca);
// If we didn't start or finish an operator, reset oap->regname, unless we
// need it later.
if (!finish_op
&& !oap->op_type
&& (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
{
clearop(oap);
#ifdef FEAT_EVAL
reset_reg_var();
#endif
}
// Get the length of mapped chars again after typing a count, second
// character or "z333<cr>".
if (old_mapped_len > 0)
old_mapped_len = typebuf_maplen();
// If an operation is pending, handle it. But not for K_IGNORE or
// K_MOUSEMOVE.
if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
do_pending_operator(&ca, old_col, FALSE);
// Wait for a moment when a message is displayed that will be overwritten
// by the mode message.
if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
normal_cmd_wait_for_msg();
// Finish up after executing a Normal mode command.
normal_end:
msg_nowait = FALSE;
#ifdef FEAT_EVAL
if (finish_op)
reset_reg_var();
#endif
#ifdef CURSOR_SHAPE
int prev_finish_op = finish_op;
#endif
if (oap->op_type == OP_NOP)
{
// Reset finish_op, in case it was set
finish_op = FALSE;
may_trigger_modechanged();
}
#ifdef CURSOR_SHAPE