-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.c
10770 lines (9870 loc) · 266 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"
#ifdef FEAT_INS_EXPAND
/*
* definitions used for CTRL-X submode
*/
# define CTRL_X_WANT_IDENT 0x100
# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
# define CTRL_X_NOT_DEFINED_YET 1
# define CTRL_X_SCROLL 2
# define CTRL_X_WHOLE_LINE 3
# define CTRL_X_FILES 4
# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
# define CTRL_X_FINISHED 8
# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
# define CTRL_X_CMDLINE 11
# define CTRL_X_FUNCTION 12
# define CTRL_X_OMNI 13
# define CTRL_X_SPELL 14
# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
# define CTRL_X_EVAL 16 /* for builtin function complete() */
# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
/* Message for CTRL-X mode, index is ctrl_x_mode. */
static char *ctrl_x_msgs[] =
{
N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
NULL, /* CTRL_X_SCROLL: depends on state */
N_(" Whole line completion (^L^N^P)"),
N_(" File name completion (^F^N^P)"),
N_(" Tag completion (^]^N^P)"),
N_(" Path pattern completion (^N^P)"),
N_(" Definition completion (^D^N^P)"),
NULL, /* CTRL_X_FINISHED */
N_(" Dictionary completion (^K^N^P)"),
N_(" Thesaurus completion (^T^N^P)"),
N_(" Command-line completion (^V^N^P)"),
N_(" User defined completion (^U^N^P)"),
N_(" Omni completion (^O^N^P)"),
N_(" Spelling suggestion (s^N^P)"),
N_(" Keyword Local completion (^N^P)"),
NULL, /* CTRL_X_EVAL doesn't use msg. */
};
static char e_hitend[] = N_("Hit end of paragraph");
# ifdef FEAT_COMPL_FUNC
static char e_complwin[] = N_("E839: Completion function changed window");
static char e_compldel[] = N_("E840: Completion function deleted text");
# endif
/*
* Structure used to store one match for insert completion.
*/
typedef struct compl_S compl_T;
struct compl_S
{
compl_T *cp_next;
compl_T *cp_prev;
char_u *cp_str; /* matched text */
char cp_icase; /* TRUE or FALSE: ignore case */
char_u *(cp_text[CPT_COUNT]); /* text for the menu */
char_u *cp_fname; /* file containing the match, allocated when
* cp_flags has FREE_FNAME */
int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
int cp_number; /* sequence number */
};
# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
# define FREE_FNAME (2)
/*
* All the current matches are stored in a list.
* "compl_first_match" points to the start of the list.
* "compl_curr_match" points to the currently selected entry.
* "compl_shown_match" is different from compl_curr_match during
* ins_compl_get_exp().
*/
static compl_T *compl_first_match = NULL;
static compl_T *compl_curr_match = NULL;
static compl_T *compl_shown_match = NULL;
static compl_T *compl_old_match = NULL;
/* After using a cursor key <Enter> selects a match in the popup menu,
* otherwise it inserts a line break. */
static int compl_enter_selects = FALSE;
/* When "compl_leader" is not NULL only matches that start with this string
* are used. */
static char_u *compl_leader = NULL;
static int compl_get_longest = FALSE; /* put longest common string
in compl_leader */
static int compl_no_insert = FALSE; /* FALSE: select & insert
TRUE: noinsert */
static int compl_no_select = FALSE; /* FALSE: select & insert
TRUE: noselect */
static int compl_used_match; /* Selected one of the matches. When
FALSE the match was edited or using
the longest common string. */
static int compl_was_interrupted = FALSE; /* didn't finish finding
completions. */
static int compl_restarting = FALSE; /* don't insert match */
/* When the first completion is done "compl_started" is set. When it's
* FALSE the word to be completed must be located. */
static int compl_started = FALSE;
/* Which Ctrl-X mode are we in? */
static int ctrl_x_mode = CTRL_X_NORMAL;
/* Set when doing something for completion that may call edit() recursively,
* which is not allowed. */
static int compl_busy = FALSE;
static int compl_matches = 0;
static char_u *compl_pattern = NULL;
static int compl_direction = FORWARD;
static int compl_shows_dir = FORWARD;
static int compl_pending = 0; /* > 1 for postponed CTRL-N */
static pos_T compl_startpos;
static colnr_T compl_col = 0; /* column where the text starts
* that is being completed */
static char_u *compl_orig_text = NULL; /* text as it was before
* completion started */
static int compl_cont_mode = 0;
static expand_T compl_xp;
static int compl_opt_refresh_always = FALSE;
static void ins_ctrl_x(void);
static int has_compl_option(int dict_opt);
static int ins_compl_accept_char(int c);
static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
static int ins_compl_equal(compl_T *match, char_u *str, int len);
static void ins_compl_longest_match(compl_T *match);
static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
static int ins_compl_make_cyclic(void);
static void ins_compl_upd_pum(void);
static void ins_compl_del_pum(void);
static int pum_wanted(void);
static int pum_enough_matches(void);
static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
static char_u *find_line_end(char_u *ptr);
static void ins_compl_free(void);
static void ins_compl_clear(void);
static int ins_compl_bs(void);
static int ins_compl_need_restart(void);
static void ins_compl_new_leader(void);
static void ins_compl_addleader(int c);
static int ins_compl_len(void);
static void ins_compl_restart(void);
static void ins_compl_set_original_text(char_u *str);
static void ins_compl_addfrommatch(void);
static int ins_compl_prep(int c);
static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
static void ins_compl_add_list(list_T *list);
static void ins_compl_add_dict(dict_T *dict);
# endif
static int ins_compl_get_exp(pos_T *ini);
static void ins_compl_delete(void);
static void ins_compl_insert(int in_compl_func);
static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
static int ins_compl_key2dir(int c);
static int ins_compl_pum_key(int c);
static int ins_compl_key2count(int c);
static int ins_compl_use_match(int c);
static int ins_complete(int c, int enable_pum);
static void show_pum(int prev_w_wrow, int prev_w_leftcol);
static unsigned quote_meta(char_u *dest, char_u *str, int len);
#endif /* FEAT_INS_EXPAND */
#define BACKSPACE_CHAR 1
#define BACKSPACE_WORD 2
#define BACKSPACE_WORD_NOT_SPACE 3
#define BACKSPACE_LINE 4
static void ins_redraw(int ready);
static void ins_ctrl_v(void);
#ifdef FEAT_JOB_CHANNEL
static void init_prompt(int cmdchar_todo);
#endif
static void undisplay_dollar(void);
static void insert_special(int, int, int);
static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
static void check_auto_format(int);
static void redo_literal(int c);
static void start_arrow(pos_T *end_insert_pos);
static void start_arrow_with_change(pos_T *end_insert_pos, int change);
static void start_arrow_common(pos_T *end_insert_pos, int change);
#ifdef FEAT_SPELL
static void check_spell_redraw(void);
static void spell_back_to_badword(void);
static int spell_bad_len = 0; /* length of located bad word */
#endif
static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
static int echeck_abbr(int);
static int replace_pop(void);
static void replace_join(int off);
static void replace_pop_ins(void);
#ifdef FEAT_MBYTE
static void mb_replace_pop_ins(int cc);
#endif
static void replace_flush(void);
static void replace_do_bs(int limit_col);
static int del_char_after_col(int limit_col);
#ifdef FEAT_CINDENT
static int cindent_on(void);
#endif
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);
#ifdef FEAT_MOUSE
static void ins_mouse(int c);
static void ins_mousescroll(int dir);
#endif
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
static void ins_tabline(int c);
#endif
static void ins_left(int end_change);
static void ins_home(int c);
static void ins_end(int c);
static void ins_s_left(void);
static void ins_right(int end_change);
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);
static int ins_eol(int c);
#ifdef FEAT_DIGRAPHS
static int ins_digraph(void);
#endif
static int ins_ctrl_ey(int tc);
#ifdef FEAT_SMARTINDENT
static void ins_try_si(int c);
#endif
static colnr_T get_nolist_virtcol(void);
#if defined(FEAT_EVAL)
static char_u *do_insert_char_pre(int c);
#endif
static int ins_apply_autocmds(event_T event);
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() */
#ifdef FEAT_CINDENT
static int can_cindent; /* may do cindenting on this line */
#endif
static int old_indent = 0; /* for ^^D command in insert mode */
#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 did_add_space = FALSE; /* auto_format() added an extra space
under the cursor */
static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
the next left/right cursor */
/*
* 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 */
#ifdef FEAT_CINDENT
int line_is_white = FALSE; /* line is empty before insert */
#endif
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 = REPLACE;
int nomove = FALSE; /* don't move cursor on return */
#ifdef FEAT_JOB_CHANNEL
int cmdchar_todo = cmdchar;
#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_sandbox));
return FALSE;
}
#endif
/* Don't allow changes in the buffer while editing the cmdline. The
* caller of getcmdline() may get confused. */
if (textlock != 0)
{
EMSG(_(e_secure));
return FALSE;
}
#ifdef FEAT_INS_EXPAND
/* Don't allow recursive insert mode when busy with completion. */
if (compl_started || compl_busy || pum_visible())
{
EMSG(_(e_secure));
return FALSE;
}
ins_compl_clear(); /* clear stuff for CTRL-X mode */
#endif
/*
* 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);
/* 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 = INSERT;
check_cursor_col();
State = save_state;
}
}
#ifdef FEAT_CONCEAL
/* Check if the cursor line needs redrawing before changing State. If
* 'concealcursor' is "n" it needs to be redrawn without concealing. */
conceal_check_cursor_line();
#endif
#ifdef FEAT_MOUSE
/*
* 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
#endif
{
Insstart = curwin->w_cursor;
if (startln)
Insstart.col = 0;
}
Insstart_textlen = (colnr_T)linetabsize(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')
{
#ifdef FEAT_FKMAP
if (p_fkmap && p_ri)
{
beep_flush();
EMSG(farsi_text_3); /* encoded in Farsi */
State = INSERT;
}
else
#endif
State = REPLACE;
}
else if (cmdchar == 'V' || cmdchar == 'v')
{
State = VREPLACE;
replaceState = VREPLACE;
orig_line_count = curbuf->b_ml.ml_line_count;
vr_lines_changed = 1;
}
else
State = INSERT;
stop_insert_mode = FALSE;
/*
* Need to recompute the cursor position, it might move when the cursor is
* on a TAB or special character.
*/
curs_columns(TRUE);
/*
* 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 |= LANGMAP;
#ifdef HAVE_INPUT_METHOD
im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
#endif
#ifdef FEAT_MOUSE
setmouse();
#endif
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
#ifdef FEAT_RIGHTLEFT
/* there is no reverse replace mode */
revins_on = (State == INSERT && p_ri);
if (revins_on)
undisplay_dollar();
revins_chars = 0;
revins_legal = 0;
revins_scol = -1;
#endif
if (!p_ek)
/* Disable bracketed paste mode, we won't recognize the escape
* sequences. */
out_str(T_BD);
/*
* 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())
{
#ifdef FEAT_MOUSE
/*
* 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
#endif
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;
#ifdef FEAT_MBYTE
else if (has_mbyte)
{
i = (*mb_ptr2len)(ptr);
if (ptr[i] == NUL)
curwin->w_cursor.col += i;
}
#endif
}
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;
#ifdef FEAT_MOUSE
where_paste_started.lnum = 0;
#endif
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
#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
#ifdef FEAT_INS_EXPAND
&& !pum_visible()
#endif
)
{
/* ":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
(int)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 - p_so
&& (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)
{
// Insert mode ended, possibly from a callback.
count = 0;
nomove = TRUE;
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
#ifdef FEAT_FKMAP
if (p_fkmap && KeyTyped)
c = fkmap(c); /* Farsi mode mapping */
#endif
#ifdef FEAT_INS_EXPAND
/*
* 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 (compl_started
&& pum_wanted()
&& curwin->w_cursor.col >= compl_col
&& (compl_shown_match == NULL
|| compl_shown_match != compl_shown_match->cp_next))
{
/* BS: Delete one character from "compl_leader". */
if ((c == K_BS || c == Ctrl_H)
&& curwin->w_cursor.col > compl_col
&& (c = ins_compl_bs()) == NUL)
continue;
/* When no match was selected or it was edited. */
if (!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(ctrl_x_mode)
|| (int)STRLEN(compl_shown_match->cp_str)
> curwin->w_cursor.col - compl_col))
{
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
* compl_enter_selects is set the Enter key does the same. */
if ((c == Ctrl_Y || (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. */
compl_get_longest = FALSE;
if (ins_compl_prep(c))
continue;
#endif
/* 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
#ifdef FEAT_INS_EXPAND
if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
goto docomplete;
#endif
if (c == Ctrl_V || c == Ctrl_Q)
{
ins_ctrl_v();
c = Ctrl_V; /* pretend CTRL-V is last typed character */
continue;
}
#ifdef FEAT_CINDENT
if (cindent_on()
# ifdef FEAT_INS_EXPAND
&& ctrl_x_mode == 0
# endif
)
{
/* 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();
}
#endif
#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;