forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.c
5410 lines (4899 loc) · 130 KB
/
edit.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
*
* 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.
*/
/*
* edit.c: functions for Insert mode
*/
#include "vim.h"
#define BACKSPACE_CHAR 1
#define BACKSPACE_WORD 2
#define BACKSPACE_WORD_NOT_SPACE 3
#define BACKSPACE_LINE 4
// Set when doing something for completion that may call edit() recursively,
// which is not allowed.
static int compl_busy = FALSE;
static void ins_ctrl_v(void);
static void insert_special(int, int, int);
static void redo_literal(int c);
static void start_arrow_common(pos_T *end_insert_pos, int change);
#ifdef FEAT_SPELL
static void check_spell_redraw(void);
#endif
static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
static int echeck_abbr(int);
static void mb_replace_pop_ins(int cc);
static void replace_flush(void);
static void replace_do_bs(int limit_col);
static int del_char_after_col(int limit_col);
static void ins_reg(void);
static void ins_ctrl_g(void);
static void ins_ctrl_hat(void);
static int ins_esc(long *count, int cmdchar, int nomove);
#ifdef FEAT_RIGHTLEFT
static void ins_ctrl_(void);
#endif
static int ins_start_select(int c);
static void ins_insert(int replaceState);
static void ins_ctrl_o(void);
static void ins_shift(int c, int lastc);
static void ins_del(void);
static int ins_bs(int c, int mode, int *inserted_space_p);
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
static void ins_tabline(int c);
#endif
static void ins_left(void);
static void ins_home(int c);
static void ins_end(int c);
static void ins_s_left(void);
static void ins_right(void);
static void ins_s_right(void);
static void ins_up(int startcol);
static void ins_pageup(void);
static void ins_down(int startcol);
static void ins_pagedown(void);
#ifdef FEAT_DND
static void ins_drop(void);
#endif
static int ins_tab(void);
#ifdef FEAT_DIGRAPHS
static int ins_digraph(void);
#endif
static int ins_ctrl_ey(int tc);
#if defined(FEAT_EVAL)
static char_u *do_insert_char_pre(int c);
#endif
static colnr_T Insstart_textlen; // length of line when insert started
static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
static int update_Insstart_orig = TRUE; // set Insstart_orig to Insstart
static char_u *last_insert = NULL; // the text of the previous insert,
// K_SPECIAL and CSI are escaped
static int last_insert_skip; // nr of chars in front of previous insert
static int new_insert_skip; // nr of chars in front of current insert
static int did_restart_edit; // "restart_edit" when calling edit()
static int can_cindent; // may do cindenting on this line
#ifdef FEAT_RIGHTLEFT
static int revins_on; // reverse insert mode on
static int revins_chars; // how much to skip after edit
static int revins_legal; // was the last char 'legal'?
static int revins_scol; // start column of revins session
#endif
static int ins_need_undo; // call u_save() before inserting a
// char. Set when edit() is called.
// after that arrow_used is used.
static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
// the next left/right cursor key
/*
* edit(): Start inserting text.
*
* "cmdchar" can be:
* 'i' normal insert command
* 'a' normal append command
* K_PS bracketed paste
* 'R' replace command
* 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
* but still only one <CR> is inserted. The <Esc> is not used for redo.
* 'g' "gI" command.
* 'V' "gR" command for Virtual Replace mode.
* 'v' "gr" command for single character Virtual Replace mode.
*
* This function is not called recursively. For CTRL-O commands, it returns
* and lets the caller handle the Normal-mode command.
*
* Return TRUE if a CTRL-O command caused the return (insert mode pending).
*/
int
edit(
int cmdchar,
int startln, // if set, insert at start of line
long count)
{
int c = 0;
char_u *ptr;
int lastc = 0;
int mincol;
static linenr_T o_lnum = 0;
int i;
int did_backspace = TRUE; // previous char was backspace
int line_is_white = FALSE; // line is empty before insert
linenr_T old_topline = 0; // topline before insertion
#ifdef FEAT_DIFF
int old_topfill = -1;
#endif
int inserted_space = FALSE; // just inserted a space
int replaceState = MODE_REPLACE;
int nomove = FALSE; // don't move cursor on return
#ifdef FEAT_JOB_CHANNEL
int cmdchar_todo = cmdchar;
#endif
#ifdef FEAT_CONCEAL
int cursor_line_was_concealed;
#endif
// Remember whether editing was restarted after CTRL-O.
did_restart_edit = restart_edit;
// sleep before redrawing, needed for "CTRL-O :" that results in an
// error message
check_for_delay(TRUE);
// set Insstart_orig to Insstart
update_Insstart_orig = TRUE;
#ifdef HAVE_SANDBOX
// Don't allow inserting in the sandbox.
if (sandbox != 0)
{
emsg(_(e_not_allowed_in_sandbox));
return FALSE;
}
#endif
// Don't allow changes in the buffer while editing the cmdline. The
// caller of getcmdline() may get confused.
// Don't allow recursive insert mode when busy with completion.
if (textlock != 0 || ins_compl_active() || compl_busy || pum_visible())
{
emsg(_(e_not_allowed_to_change_text_or_change_window));
return FALSE;
}
ins_compl_clear(); // clear stuff for CTRL-X mode
/*
* Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
*/
if (cmdchar != 'r' && cmdchar != 'v')
{
pos_T save_cursor = curwin->w_cursor;
#ifdef FEAT_EVAL
if (cmdchar == 'R')
ptr = (char_u *)"r";
else if (cmdchar == 'V')
ptr = (char_u *)"v";
else
ptr = (char_u *)"i";
set_vim_var_string(VV_INSERTMODE, ptr, 1);
set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
#endif
ins_apply_autocmds(EVENT_INSERTENTER);
// Check for changed highlighting, e.g. for ModeMsg.
if (need_highlight_changed)
highlight_changed();
// Make sure the cursor didn't move. Do call check_cursor_col() in
// case the text was modified. Since Insert mode was not started yet
// a call to check_cursor_col() may move the cursor, especially with
// the "A" command, thus set State to avoid that. Also check that the
// line number is still valid (lines may have been deleted).
// Do not restore if v:char was set to a non-empty string.
if (!EQUAL_POS(curwin->w_cursor, save_cursor)
#ifdef FEAT_EVAL
&& *get_vim_var_str(VV_CHAR) == NUL
#endif
&& save_cursor.lnum <= curbuf->b_ml.ml_line_count)
{
int save_state = State;
curwin->w_cursor = save_cursor;
State = MODE_INSERT;
check_cursor_col();
State = save_state;
}
}
#ifdef FEAT_CONCEAL
// Check if the cursor line was concealed before changing State.
cursor_line_was_concealed = curwin->w_p_cole > 0
&& conceal_cursor_line(curwin);
#endif
/*
* When doing a paste with the middle mouse button, Insstart is set to
* where the paste started.
*/
if (where_paste_started.lnum != 0)
Insstart = where_paste_started;
else
{
Insstart = curwin->w_cursor;
if (startln)
Insstart.col = 0;
}
Insstart_textlen = (colnr_T)linetabsize_str(ml_get_curline());
Insstart_blank_vcol = MAXCOL;
if (!did_ai)
ai_col = 0;
if (cmdchar != NUL && restart_edit == 0)
{
ResetRedobuff();
AppendNumberToRedobuff(count);
if (cmdchar == 'V' || cmdchar == 'v')
{
// "gR" or "gr" command
AppendCharToRedobuff('g');
AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
}
else
{
if (cmdchar == K_PS)
AppendCharToRedobuff('a');
else
AppendCharToRedobuff(cmdchar);
if (cmdchar == 'g') // "gI" command
AppendCharToRedobuff('I');
else if (cmdchar == 'r') // "r<CR>" command
count = 1; // insert only one <CR>
}
}
if (cmdchar == 'R')
{
State = MODE_REPLACE;
}
else if (cmdchar == 'V' || cmdchar == 'v')
{
State = MODE_VREPLACE;
replaceState = MODE_VREPLACE;
orig_line_count = curbuf->b_ml.ml_line_count;
vr_lines_changed = 1;
}
else
State = MODE_INSERT;
may_trigger_modechanged();
stop_insert_mode = FALSE;
#ifdef FEAT_CONCEAL
// Check if the cursor line needs redrawing after changing State. If
// 'concealcursor' is "n" it needs to be redrawn without concealing.
conceal_check_cursor_line(cursor_line_was_concealed);
#endif
// Need to position cursor again when on a TAB and when on a char with
// virtual text.
if (gchar_cursor() == TAB
#ifdef FEAT_PROP_POPUP
|| curbuf->b_has_textprop
#endif
)
curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
/*
* Enable langmap or IME, indicated by 'iminsert'.
* Note that IME may enabled/disabled without us noticing here, thus the
* 'iminsert' value may not reflect what is actually used. It is updated
* when hitting <Esc>.
*/
if (curbuf->b_p_iminsert == B_IMODE_LMAP)
State |= MODE_LANGMAP;
#ifdef HAVE_INPUT_METHOD
im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
#endif
setmouse();
clear_showcmd();
#ifdef FEAT_RIGHTLEFT
// there is no reverse replace mode
revins_on = (State == MODE_INSERT && p_ri);
if (revins_on)
undisplay_dollar();
revins_chars = 0;
revins_legal = 0;
revins_scol = -1;
#endif
if (!p_ek)
{
MAY_WANT_TO_LOG_THIS;
// Disable bracketed paste mode, we won't recognize the escape
// sequences.
out_str(T_BD);
// Disable modifyOtherKeys, keys with modifiers would cause exiting
// Insert mode.
out_str(T_CTE);
}
/*
* Handle restarting Insert mode.
* Don't do this for "CTRL-O ." (repeat an insert): In that case we get
* here with something in the stuff buffer.
*/
if (restart_edit != 0 && stuff_empty())
{
/*
* After a paste we consider text typed to be part of the insert for
* the pasted text. You can backspace over the pasted text too.
*/
if (where_paste_started.lnum)
arrow_used = FALSE;
else
arrow_used = TRUE;
restart_edit = 0;
/*
* If the cursor was after the end-of-line before the CTRL-O and it is
* now at the end-of-line, put it after the end-of-line (this is not
* correct in very rare cases).
* Also do this if curswant is greater than the current virtual
* column. Eg after "^O$" or "^O80|".
*/
validate_virtcol();
update_curswant();
if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
|| curwin->w_curswant > curwin->w_virtcol)
&& *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
{
if (ptr[1] == NUL)
++curwin->w_cursor.col;
else if (has_mbyte)
{
i = (*mb_ptr2len)(ptr);
if (ptr[i] == NUL)
curwin->w_cursor.col += i;
}
}
ins_at_eol = FALSE;
}
else
arrow_used = FALSE;
// we are in insert mode now, don't need to start it anymore
need_start_insertmode = FALSE;
// Need to save the line for undo before inserting the first char.
ins_need_undo = TRUE;
where_paste_started.lnum = 0;
can_cindent = TRUE;
#ifdef FEAT_FOLDING
// The cursor line is not in a closed fold, unless 'insertmode' is set or
// restarting.
if (!p_im && did_restart_edit == 0)
foldOpenCursor();
#endif
/*
* If 'showmode' is set, show the current (insert/replace/..) mode.
* A warning message for changing a readonly file is given here, before
* actually changing anything. It's put after the mode, if any.
*/
i = 0;
if (p_smd && msg_silent == 0)
i = showmode();
if (!p_im && did_restart_edit == 0)
change_warning(i == 0 ? 0 : i + 1);
#ifdef CURSOR_SHAPE
ui_cursor_shape(); // may show different cursor shape
#endif
#ifdef FEAT_DIGRAPHS
do_digraph(-1); // clear digraphs
#endif
/*
* Get the current length of the redo buffer, those characters have to be
* skipped if we want to get to the inserted characters.
*/
ptr = get_inserted();
if (ptr == NULL)
new_insert_skip = 0;
else
{
new_insert_skip = (int)STRLEN(ptr);
vim_free(ptr);
}
old_indent = 0;
/*
* Main loop in Insert mode: repeat until Insert mode is left.
*/
for (;;)
{
#ifdef FEAT_RIGHTLEFT
if (!revins_legal)
revins_scol = -1; // reset on illegal motions
else
revins_legal = 0;
#endif
if (arrow_used) // don't repeat insert when arrow key used
count = 0;
if (update_Insstart_orig)
Insstart_orig = Insstart;
if (stop_insert_mode && !ins_compl_active())
{
// ":stopinsert" used or 'insertmode' reset
count = 0;
goto doESCkey;
}
// set curwin->w_curswant for next K_DOWN or K_UP
if (!arrow_used)
curwin->w_set_curswant = TRUE;
// If there is no typeahead may check for timestamps (e.g., for when a
// menu invoked a shell command).
if (stuff_empty())
{
did_check_timestamps = FALSE;
if (need_check_timestamps)
check_timestamps(FALSE);
}
/*
* When emsg() was called msg_scroll will have been set.
*/
msg_scroll = FALSE;
#ifdef FEAT_GUI
// When 'mousefocus' is set a mouse movement may have taken us to
// another window. "need_mouse_correct" may then be set because of an
// autocommand.
if (need_mouse_correct)
gui_mouse_correct();
#endif
#ifdef FEAT_FOLDING
// Open fold at the cursor line, according to 'foldopen'.
if (fdo_flags & FDO_INSERT)
foldOpenCursor();
// Close folds where the cursor isn't, according to 'foldclose'
if (!char_avail())
foldCheckClose();
#endif
#ifdef FEAT_JOB_CHANNEL
if (bt_prompt(curbuf))
{
init_prompt(cmdchar_todo);
cmdchar_todo = NUL;
}
#endif
/*
* If we inserted a character at the last position of the last line in
* the window, scroll the window one line up. This avoids an extra
* redraw.
* This is detected when the cursor column is smaller after inserting
* something.
* Don't do this when the topline changed already, it has
* already been adjusted (by insertchar() calling open_line())).
*/
if (curbuf->b_mod_set
&& curwin->w_p_wrap
&& !did_backspace
&& curwin->w_topline == old_topline
#ifdef FEAT_DIFF
&& curwin->w_topfill == old_topfill
#endif
)
{
mincol = curwin->w_wcol;
validate_cursor_col();
if (
#ifdef FEAT_VARTABS
curwin->w_wcol < mincol - tabstop_at(
get_nolist_virtcol(), curbuf->b_p_ts,
curbuf->b_p_vts_array)
#else
(int)curwin->w_wcol < mincol - curbuf->b_p_ts
#endif
&& curwin->w_wrow == W_WINROW(curwin)
+ curwin->w_height - 1 - get_scrolloff_value()
&& (curwin->w_cursor.lnum != curwin->w_topline
#ifdef FEAT_DIFF
|| curwin->w_topfill > 0
#endif
))
{
#ifdef FEAT_DIFF
if (curwin->w_topfill > 0)
--curwin->w_topfill;
else
#endif
#ifdef FEAT_FOLDING
if (hasFolding(curwin->w_topline, NULL, &old_topline))
set_topline(curwin, old_topline + 1);
else
#endif
set_topline(curwin, curwin->w_topline + 1);
}
}
// May need to adjust w_topline to show the cursor.
update_topline();
did_backspace = FALSE;
validate_cursor(); // may set must_redraw
/*
* Redraw the display when no characters are waiting.
* Also shows mode, ruler and positions cursor.
*/
ins_redraw(TRUE);
if (curwin->w_p_scb)
do_check_scrollbind(TRUE);
if (curwin->w_p_crb)
do_check_cursorbind();
update_curswant();
old_topline = curwin->w_topline;
#ifdef FEAT_DIFF
old_topfill = curwin->w_topfill;
#endif
#ifdef USE_ON_FLY_SCROLL
dont_scroll = FALSE; // allow scrolling here
#endif
/*
* Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
*/
if (c != K_CURSORHOLD)
lastc = c; // remember the previous char for CTRL-D
// After using CTRL-G U the next cursor key will not break undo.
if (dont_sync_undo == MAYBE)
dont_sync_undo = TRUE;
else
dont_sync_undo = FALSE;
if (cmdchar == K_PS)
// Got here from normal mode when bracketed paste started.
c = K_PS;
else
do
{
c = safe_vgetc();
if (stop_insert_mode
#ifdef FEAT_TERMINAL
|| (c == K_IGNORE && term_use_loop())
#endif
)
{
// Insert mode ended, possibly from a callback, or a timer
// must have opened a terminal window.
if (c != K_IGNORE && c != K_NOP)
vungetc(c);
count = 0;
nomove = TRUE;
ins_compl_prep(ESC);
goto doESCkey;
}
} while (c == K_IGNORE || c == K_NOP);
// Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
did_cursorhold = TRUE;
#ifdef FEAT_RIGHTLEFT
if (p_hkmap && KeyTyped)
c = hkmap(c); // Hebrew mode mapping
#endif
// If the window was made so small that nothing shows, make it at least
// one line and one column when typing.
if (KeyTyped && !KeyStuffed)
win_ensure_size();
/*
* Special handling of keys while the popup menu is visible or wanted
* and the cursor is still in the completed word. Only when there is
* a match, skip this when no matches were found.
*/
if (ins_compl_active()
&& pum_wanted()
&& curwin->w_cursor.col >= ins_compl_col()
&& ins_compl_has_shown_match())
{
// BS: Delete one character from "compl_leader".
if ((c == K_BS || c == Ctrl_H)
&& curwin->w_cursor.col > ins_compl_col()
&& (c = ins_compl_bs()) == NUL)
continue;
// When no match was selected or it was edited.
if (!ins_compl_used_match())
{
// CTRL-L: Add one character from the current match to
// "compl_leader". Except when at the original match and
// there is nothing to add, CTRL-L works like CTRL-P then.
if (c == Ctrl_L
&& (!ctrl_x_mode_line_or_eval()
|| ins_compl_long_shown_match()))
{
ins_compl_addfrommatch();
continue;
}
// A non-white character that fits in with the current
// completion: Add to "compl_leader".
if (ins_compl_accept_char(c))
{
#if defined(FEAT_EVAL)
// Trigger InsertCharPre.
char_u *str = do_insert_char_pre(c);
char_u *p;
if (str != NULL)
{
for (p = str; *p != NUL; MB_PTR_ADV(p))
ins_compl_addleader(PTR2CHAR(p));
vim_free(str);
}
else
#endif
ins_compl_addleader(c);
continue;
}
// Pressing CTRL-Y selects the current match. When
// ins_compl_enter_selects() is set the Enter key does the
// same.
if ((c == Ctrl_Y || (ins_compl_enter_selects()
&& (c == CAR || c == K_KENTER || c == NL)))
&& stop_arrow() == OK)
{
ins_compl_delete();
ins_compl_insert(FALSE);
}
}
}
// Prepare for or stop CTRL-X mode. This doesn't do completion, but
// it does fix up the text when finishing completion.
ins_compl_init_get_longest();
if (ins_compl_prep(c))
continue;
// CTRL-\ CTRL-N goes to Normal mode,
// CTRL-\ CTRL-G goes to mode selected with 'insertmode',
// CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
if (c == Ctrl_BSL)
{
// may need to redraw when no more chars available now
ins_redraw(FALSE);
++no_mapping;
++allow_keys;
c = plain_vgetc();
--no_mapping;
--allow_keys;
if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
{
// it's something else
vungetc(c);
c = Ctrl_BSL;
}
else if (c == Ctrl_G && p_im)
continue;
else
{
if (c == Ctrl_O)
{
ins_ctrl_o();
ins_at_eol = FALSE; // cursor keeps its column
nomove = TRUE;
}
count = 0;
goto doESCkey;
}
}
#ifdef FEAT_DIGRAPHS
c = do_digraph(c);
#endif
if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
goto docomplete;
if (c == Ctrl_V || c == Ctrl_Q)
{
ins_ctrl_v();
c = Ctrl_V; // pretend CTRL-V is last typed character
continue;
}
if (cindent_on() && ctrl_x_mode_none())
{
// A key name preceded by a bang means this key is not to be
// inserted. Skip ahead to the re-indenting below.
// A key name preceded by a star means that indenting has to be
// done before inserting the key.
line_is_white = inindent(0);
if (in_cinkeys(c, '!', line_is_white))
goto force_cindent;
if (can_cindent && in_cinkeys(c, '*', line_is_white)
&& stop_arrow() == OK)
do_c_expr_indent();
}
#ifdef FEAT_RIGHTLEFT
if (curwin->w_p_rl)
switch (c)
{
case K_LEFT: c = K_RIGHT; break;
case K_S_LEFT: c = K_S_RIGHT; break;
case K_C_LEFT: c = K_C_RIGHT; break;
case K_RIGHT: c = K_LEFT; break;
case K_S_RIGHT: c = K_S_LEFT; break;
case K_C_RIGHT: c = K_C_LEFT; break;
}
#endif
/*
* If 'keymodel' contains "startsel", may start selection. If it
* does, a CTRL-O and c will be stuffed, we need to get these
* characters.
*/
if (ins_start_select(c))
continue;
/*
* The big switch to handle a character in insert mode.
*/
switch (c)
{
case ESC: // End input mode
if (echeck_abbr(ESC + ABBR_OFF))
break;
// FALLTHROUGH
case Ctrl_C: // End input mode
if (c == Ctrl_C && cmdwin_type != 0)
{
// Close the cmdline window.
cmdwin_result = K_IGNORE;
got_int = FALSE; // don't stop executing autocommands et al.
nomove = TRUE;
goto doESCkey;
}
#ifdef FEAT_JOB_CHANNEL
if (c == Ctrl_C && bt_prompt(curbuf))
{
if (invoke_prompt_interrupt())
{
if (!bt_prompt(curbuf))
// buffer changed to a non-prompt buffer, get out of
// Insert mode
goto doESCkey;
break;
}
}
#endif
#ifdef UNIX
do_intr:
#endif
// when 'insertmode' set, and not halfway a mapping, don't leave
// Insert mode
if (goto_im())
{
if (got_int)
{
(void)vgetc(); // flush all buffers
got_int = FALSE;
}
else
vim_beep(BO_IM);
break;
}
doESCkey:
/*
* This is the ONLY return from edit()!
*/
// Always update o_lnum, so that a "CTRL-O ." that adds a line
// still puts the cursor back after the inserted text.
if (ins_at_eol && gchar_cursor() == NUL)
o_lnum = curwin->w_cursor.lnum;
if (ins_esc(&count, cmdchar, nomove))
{
// When CTRL-C was typed got_int will be set, with the result
// that the autocommands won't be executed. When mapped got_int
// is not set, but let's keep the behavior the same.
if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
ins_apply_autocmds(EVENT_INSERTLEAVE);
did_cursorhold = FALSE;
return (c == Ctrl_O);
}
continue;
case Ctrl_Z: // suspend when 'insertmode' set
if (!p_im)
goto normalchar; // insert CTRL-Z as normal char
do_cmdline_cmd((char_u *)"stop");
#ifdef CURSOR_SHAPE
ui_cursor_shape(); // may need to update cursor shape
#endif
continue;
case Ctrl_O: // execute one command
#ifdef FEAT_COMPL_FUNC
if (ctrl_x_mode_omni())
goto docomplete;
#endif
if (echeck_abbr(Ctrl_O + ABBR_OFF))
break;
ins_ctrl_o();
// don't move the cursor left when 'virtualedit' has "onemore".
if (get_ve_flags() & VE_ONEMORE)
{
ins_at_eol = FALSE;
nomove = TRUE;
}
count = 0;
goto doESCkey;
case K_INS: // toggle insert/replace mode
case K_KINS:
ins_insert(replaceState);
break;
case K_SELECT: // end of Select mode mapping - ignore
break;
case K_HELP: // Help key works like <ESC> <Help>
case K_F1:
case K_XF1:
stuffcharReadbuff(K_HELP);
if (p_im)
need_start_insertmode = TRUE;
goto doESCkey;
#ifdef FEAT_NETBEANS_INTG
case K_F21: // NetBeans command
++no_mapping; // don't map the next key hits
i = plain_vgetc();
--no_mapping;
netbeans_keycommand(i);
break;
#endif
case K_ZERO: // Insert the previously inserted text.
case NUL:
case Ctrl_A:
// For ^@ the trailing ESC will end the insert, unless there is an
// error.
if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
&& c != Ctrl_A && !p_im)
goto doESCkey; // quit insert mode
inserted_space = FALSE;
break;
case Ctrl_R: // insert the contents of a register
ins_reg();
auto_format(FALSE, TRUE);
inserted_space = FALSE;
break;
case Ctrl_G: // commands starting with CTRL-G
ins_ctrl_g();
break;
case Ctrl_HAT: // switch input mode and/or langmap
ins_ctrl_hat();
break;
#ifdef FEAT_RIGHTLEFT
case Ctrl__: // switch between languages
if (!p_ari)
goto normalchar;
ins_ctrl_();
break;
#endif
case Ctrl_D: // Make indent one shiftwidth smaller.
#if defined(FEAT_FIND_ID)
if (ctrl_x_mode_path_defines())
goto docomplete;
#endif
// FALLTHROUGH
case Ctrl_T: // Make indent one shiftwidth greater.
if (c == Ctrl_T && ctrl_x_mode_thesaurus())
{
if (has_compl_option(FALSE))
goto docomplete;
break;
}
ins_shift(c, lastc);
auto_format(FALSE, TRUE);
inserted_space = FALSE;
break;
case K_DEL: // delete character under the cursor
case K_KDEL:
ins_del();
auto_format(FALSE, TRUE);
break;
case K_BS: // delete character before the cursor
case K_S_BS:
case Ctrl_H:
did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
auto_format(FALSE, TRUE);
break;
case Ctrl_W: // delete word before the cursor
#ifdef FEAT_JOB_CHANNEL
if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
{
// In a prompt window CTRL-W is used for window commands.
// Use Shift-CTRL-W to delete a word.
stuffcharReadbuff(Ctrl_W);
restart_edit = 'A';
nomove = TRUE;
count = 0;
goto doESCkey;
}
#endif
did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
auto_format(FALSE, TRUE);
break;
case Ctrl_U: // delete all inserted text in current line
# ifdef FEAT_COMPL_FUNC
// CTRL-X CTRL-U completes with 'completefunc'.
if (ctrl_x_mode_function())
goto docomplete;
# endif
did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
auto_format(FALSE, TRUE);
inserted_space = FALSE;
break;
case K_LEFTMOUSE: // mouse keys
case K_LEFTMOUSE_NM:
case K_LEFTDRAG:
case K_LEFTRELEASE:
case K_LEFTRELEASE_NM:
case K_MOUSEMOVE:
case K_MIDDLEMOUSE:
case K_MIDDLEDRAG:
case K_MIDDLERELEASE:
case K_RIGHTMOUSE: