xref: /vim-8.2.3635/src/edit.c (revision 2e693a88)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * edit.c: functions for Insert mode
12  */
13 
14 #include "vim.h"
15 
16 #define BACKSPACE_CHAR		    1
17 #define BACKSPACE_WORD		    2
18 #define BACKSPACE_WORD_NOT_SPACE    3
19 #define BACKSPACE_LINE		    4
20 
21 /* Set when doing something for completion that may call edit() recursively,
22  * which is not allowed. */
23 static int	compl_busy = FALSE;
24 
25 
26 static void ins_ctrl_v(void);
27 #ifdef FEAT_JOB_CHANNEL
28 static void init_prompt(int cmdchar_todo);
29 #endif
30 static void insert_special(int, int, int);
31 static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
32 static void check_auto_format(int);
33 static void redo_literal(int c);
34 static void start_arrow_common(pos_T *end_insert_pos, int change);
35 #ifdef FEAT_SPELL
36 static void check_spell_redraw(void);
37 #endif
38 static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
39 static int  echeck_abbr(int);
40 static void mb_replace_pop_ins(int cc);
41 static void replace_flush(void);
42 static void replace_do_bs(int limit_col);
43 static int del_char_after_col(int limit_col);
44 static void ins_reg(void);
45 static void ins_ctrl_g(void);
46 static void ins_ctrl_hat(void);
47 static int  ins_esc(long *count, int cmdchar, int nomove);
48 #ifdef FEAT_RIGHTLEFT
49 static void ins_ctrl_(void);
50 #endif
51 static int ins_start_select(int c);
52 static void ins_insert(int replaceState);
53 static void ins_ctrl_o(void);
54 static void ins_shift(int c, int lastc);
55 static void ins_del(void);
56 static int  ins_bs(int c, int mode, int *inserted_space_p);
57 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
58 static void ins_tabline(int c);
59 #endif
60 static void ins_left(void);
61 static void ins_home(int c);
62 static void ins_end(int c);
63 static void ins_s_left(void);
64 static void ins_right(void);
65 static void ins_s_right(void);
66 static void ins_up(int startcol);
67 static void ins_pageup(void);
68 static void ins_down(int startcol);
69 static void ins_pagedown(void);
70 #ifdef FEAT_DND
71 static void ins_drop(void);
72 #endif
73 static int  ins_tab(void);
74 #ifdef FEAT_DIGRAPHS
75 static int  ins_digraph(void);
76 #endif
77 static int  ins_ctrl_ey(int tc);
78 #if defined(FEAT_EVAL)
79 static char_u *do_insert_char_pre(int c);
80 #endif
81 
82 static colnr_T	Insstart_textlen;	/* length of line when insert started */
83 static colnr_T	Insstart_blank_vcol;	/* vcol for first inserted blank */
84 static int	update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
85 
86 static char_u	*last_insert = NULL;	/* the text of the previous insert,
87 					   K_SPECIAL and CSI are escaped */
88 static int	last_insert_skip; /* nr of chars in front of previous insert */
89 static int	new_insert_skip;  /* nr of chars in front of current insert */
90 static int	did_restart_edit;	/* "restart_edit" when calling edit() */
91 
92 #ifdef FEAT_CINDENT
93 static int	can_cindent;		/* may do cindenting on this line */
94 #endif
95 
96 #ifdef FEAT_RIGHTLEFT
97 static int	revins_on;		/* reverse insert mode on */
98 static int	revins_chars;		/* how much to skip after edit */
99 static int	revins_legal;		/* was the last char 'legal'? */
100 static int	revins_scol;		/* start column of revins session */
101 #endif
102 
103 static int	ins_need_undo;		/* call u_save() before inserting a
104 					   char.  Set when edit() is called.
105 					   after that arrow_used is used. */
106 
107 static int	did_add_space = FALSE;	// auto_format() added an extra space
108 					// under the cursor
109 static int	dont_sync_undo = FALSE;	// CTRL-G U prevents syncing undo for
110 					// the next left/right cursor key
111 
112 /*
113  * edit(): Start inserting text.
114  *
115  * "cmdchar" can be:
116  * 'i'	normal insert command
117  * 'a'	normal append command
118  * K_PS bracketed paste
119  * 'R'	replace command
120  * 'r'	"r<CR>" command: insert one <CR>.  Note: count can be > 1, for redo,
121  *	but still only one <CR> is inserted.  The <Esc> is not used for redo.
122  * 'g'	"gI" command.
123  * 'V'	"gR" command for Virtual Replace mode.
124  * 'v'	"gr" command for single character Virtual Replace mode.
125  *
126  * This function is not called recursively.  For CTRL-O commands, it returns
127  * and lets the caller handle the Normal-mode command.
128  *
129  * Return TRUE if a CTRL-O command caused the return (insert mode pending).
130  */
131     int
132 edit(
133     int		cmdchar,
134     int		startln,	/* if set, insert at start of line */
135     long	count)
136 {
137     int		c = 0;
138     char_u	*ptr;
139     int		lastc = 0;
140     int		mincol;
141     static linenr_T o_lnum = 0;
142     int		i;
143     int		did_backspace = TRUE;	    /* previous char was backspace */
144 #ifdef FEAT_CINDENT
145     int		line_is_white = FALSE;	    /* line is empty before insert */
146 #endif
147     linenr_T	old_topline = 0;	    /* topline before insertion */
148 #ifdef FEAT_DIFF
149     int		old_topfill = -1;
150 #endif
151     int		inserted_space = FALSE;     /* just inserted a space */
152     int		replaceState = REPLACE;
153     int		nomove = FALSE;		    /* don't move cursor on return */
154 #ifdef FEAT_JOB_CHANNEL
155     int		cmdchar_todo = cmdchar;
156 #endif
157 
158     /* Remember whether editing was restarted after CTRL-O. */
159     did_restart_edit = restart_edit;
160 
161     /* sleep before redrawing, needed for "CTRL-O :" that results in an
162      * error message */
163     check_for_delay(TRUE);
164 
165     /* set Insstart_orig to Insstart */
166     update_Insstart_orig = TRUE;
167 
168 #ifdef HAVE_SANDBOX
169     /* Don't allow inserting in the sandbox. */
170     if (sandbox != 0)
171     {
172 	emsg(_(e_sandbox));
173 	return FALSE;
174     }
175 #endif
176     /* Don't allow changes in the buffer while editing the cmdline.  The
177      * caller of getcmdline() may get confused. */
178     if (textlock != 0)
179     {
180 	emsg(_(e_secure));
181 	return FALSE;
182     }
183 
184     /* Don't allow recursive insert mode when busy with completion. */
185     if (ins_compl_active() || compl_busy || pum_visible())
186     {
187 	emsg(_(e_secure));
188 	return FALSE;
189     }
190     ins_compl_clear();	    /* clear stuff for CTRL-X mode */
191 
192     /*
193      * Trigger InsertEnter autocommands.  Do not do this for "r<CR>" or "grx".
194      */
195     if (cmdchar != 'r' && cmdchar != 'v')
196     {
197 	pos_T   save_cursor = curwin->w_cursor;
198 
199 #ifdef FEAT_EVAL
200 	if (cmdchar == 'R')
201 	    ptr = (char_u *)"r";
202 	else if (cmdchar == 'V')
203 	    ptr = (char_u *)"v";
204 	else
205 	    ptr = (char_u *)"i";
206 	set_vim_var_string(VV_INSERTMODE, ptr, 1);
207 	set_vim_var_string(VV_CHAR, NULL, -1);  /* clear v:char */
208 #endif
209 	ins_apply_autocmds(EVENT_INSERTENTER);
210 
211 	/* Make sure the cursor didn't move.  Do call check_cursor_col() in
212 	 * case the text was modified.  Since Insert mode was not started yet
213 	 * a call to check_cursor_col() may move the cursor, especially with
214 	 * the "A" command, thus set State to avoid that. Also check that the
215 	 * line number is still valid (lines may have been deleted).
216 	 * Do not restore if v:char was set to a non-empty string. */
217 	if (!EQUAL_POS(curwin->w_cursor, save_cursor)
218 #ifdef FEAT_EVAL
219 		&& *get_vim_var_str(VV_CHAR) == NUL
220 #endif
221 		&& save_cursor.lnum <= curbuf->b_ml.ml_line_count)
222 	{
223 	    int save_state = State;
224 
225 	    curwin->w_cursor = save_cursor;
226 	    State = INSERT;
227 	    check_cursor_col();
228 	    State = save_state;
229 	}
230     }
231 
232 #ifdef FEAT_CONCEAL
233     /* Check if the cursor line needs redrawing before changing State.  If
234      * 'concealcursor' is "n" it needs to be redrawn without concealing. */
235     conceal_check_cursor_line();
236 #endif
237 
238 #ifdef FEAT_MOUSE
239     /*
240      * When doing a paste with the middle mouse button, Insstart is set to
241      * where the paste started.
242      */
243     if (where_paste_started.lnum != 0)
244 	Insstart = where_paste_started;
245     else
246 #endif
247     {
248 	Insstart = curwin->w_cursor;
249 	if (startln)
250 	    Insstart.col = 0;
251     }
252     Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
253     Insstart_blank_vcol = MAXCOL;
254     if (!did_ai)
255 	ai_col = 0;
256 
257     if (cmdchar != NUL && restart_edit == 0)
258     {
259 	ResetRedobuff();
260 	AppendNumberToRedobuff(count);
261 	if (cmdchar == 'V' || cmdchar == 'v')
262 	{
263 	    /* "gR" or "gr" command */
264 	    AppendCharToRedobuff('g');
265 	    AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
266 	}
267 	else
268 	{
269 	    if (cmdchar == K_PS)
270 		AppendCharToRedobuff('a');
271 	    else
272 		AppendCharToRedobuff(cmdchar);
273 	    if (cmdchar == 'g')		    /* "gI" command */
274 		AppendCharToRedobuff('I');
275 	    else if (cmdchar == 'r')	    /* "r<CR>" command */
276 		count = 1;		    /* insert only one <CR> */
277 	}
278     }
279 
280     if (cmdchar == 'R')
281     {
282 	State = REPLACE;
283     }
284     else if (cmdchar == 'V' || cmdchar == 'v')
285     {
286 	State = VREPLACE;
287 	replaceState = VREPLACE;
288 	orig_line_count = curbuf->b_ml.ml_line_count;
289 	vr_lines_changed = 1;
290     }
291     else
292 	State = INSERT;
293 
294     stop_insert_mode = FALSE;
295 
296     /*
297      * Need to recompute the cursor position, it might move when the cursor is
298      * on a TAB or special character.
299      */
300     curs_columns(TRUE);
301 
302     /*
303      * Enable langmap or IME, indicated by 'iminsert'.
304      * Note that IME may enabled/disabled without us noticing here, thus the
305      * 'iminsert' value may not reflect what is actually used.  It is updated
306      * when hitting <Esc>.
307      */
308     if (curbuf->b_p_iminsert == B_IMODE_LMAP)
309 	State |= LANGMAP;
310 #ifdef HAVE_INPUT_METHOD
311     im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
312 #endif
313 
314     setmouse();
315 #ifdef FEAT_CMDL_INFO
316     clear_showcmd();
317 #endif
318 #ifdef FEAT_RIGHTLEFT
319     /* there is no reverse replace mode */
320     revins_on = (State == INSERT && p_ri);
321     if (revins_on)
322 	undisplay_dollar();
323     revins_chars = 0;
324     revins_legal = 0;
325     revins_scol = -1;
326 #endif
327     if (!p_ek)
328 	/* Disable bracketed paste mode, we won't recognize the escape
329 	 * sequences. */
330 	out_str(T_BD);
331 
332     /*
333      * Handle restarting Insert mode.
334      * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
335      * here with something in the stuff buffer.
336      */
337     if (restart_edit != 0 && stuff_empty())
338     {
339 #ifdef FEAT_MOUSE
340 	/*
341 	 * After a paste we consider text typed to be part of the insert for
342 	 * the pasted text. You can backspace over the pasted text too.
343 	 */
344 	if (where_paste_started.lnum)
345 	    arrow_used = FALSE;
346 	else
347 #endif
348 	    arrow_used = TRUE;
349 	restart_edit = 0;
350 
351 	/*
352 	 * If the cursor was after the end-of-line before the CTRL-O and it is
353 	 * now at the end-of-line, put it after the end-of-line (this is not
354 	 * correct in very rare cases).
355 	 * Also do this if curswant is greater than the current virtual
356 	 * column.  Eg after "^O$" or "^O80|".
357 	 */
358 	validate_virtcol();
359 	update_curswant();
360 	if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
361 		    || curwin->w_curswant > curwin->w_virtcol)
362 		&& *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
363 	{
364 	    if (ptr[1] == NUL)
365 		++curwin->w_cursor.col;
366 	    else if (has_mbyte)
367 	    {
368 		i = (*mb_ptr2len)(ptr);
369 		if (ptr[i] == NUL)
370 		    curwin->w_cursor.col += i;
371 	    }
372 	}
373 	ins_at_eol = FALSE;
374     }
375     else
376 	arrow_used = FALSE;
377 
378     /* we are in insert mode now, don't need to start it anymore */
379     need_start_insertmode = FALSE;
380 
381     /* Need to save the line for undo before inserting the first char. */
382     ins_need_undo = TRUE;
383 
384 #ifdef FEAT_MOUSE
385     where_paste_started.lnum = 0;
386 #endif
387 #ifdef FEAT_CINDENT
388     can_cindent = TRUE;
389 #endif
390 #ifdef FEAT_FOLDING
391     /* The cursor line is not in a closed fold, unless 'insertmode' is set or
392      * restarting. */
393     if (!p_im && did_restart_edit == 0)
394 	foldOpenCursor();
395 #endif
396 
397     /*
398      * If 'showmode' is set, show the current (insert/replace/..) mode.
399      * A warning message for changing a readonly file is given here, before
400      * actually changing anything.  It's put after the mode, if any.
401      */
402     i = 0;
403     if (p_smd && msg_silent == 0)
404 	i = showmode();
405 
406     if (!p_im && did_restart_edit == 0)
407 	change_warning(i == 0 ? 0 : i + 1);
408 
409 #ifdef CURSOR_SHAPE
410     ui_cursor_shape();		/* may show different cursor shape */
411 #endif
412 #ifdef FEAT_DIGRAPHS
413     do_digraph(-1);		/* clear digraphs */
414 #endif
415 
416     /*
417      * Get the current length of the redo buffer, those characters have to be
418      * skipped if we want to get to the inserted characters.
419      */
420     ptr = get_inserted();
421     if (ptr == NULL)
422 	new_insert_skip = 0;
423     else
424     {
425 	new_insert_skip = (int)STRLEN(ptr);
426 	vim_free(ptr);
427     }
428 
429     old_indent = 0;
430 
431     /*
432      * Main loop in Insert mode: repeat until Insert mode is left.
433      */
434     for (;;)
435     {
436 #ifdef FEAT_RIGHTLEFT
437 	if (!revins_legal)
438 	    revins_scol = -1;	    /* reset on illegal motions */
439 	else
440 	    revins_legal = 0;
441 #endif
442 	if (arrow_used)	    /* don't repeat insert when arrow key used */
443 	    count = 0;
444 
445 	if (update_Insstart_orig)
446 	    Insstart_orig = Insstart;
447 
448 	if (stop_insert_mode && !pum_visible())
449 	{
450 	    /* ":stopinsert" used or 'insertmode' reset */
451 	    count = 0;
452 	    goto doESCkey;
453 	}
454 
455 	/* set curwin->w_curswant for next K_DOWN or K_UP */
456 	if (!arrow_used)
457 	    curwin->w_set_curswant = TRUE;
458 
459 	/* If there is no typeahead may check for timestamps (e.g., for when a
460 	 * menu invoked a shell command). */
461 	if (stuff_empty())
462 	{
463 	    did_check_timestamps = FALSE;
464 	    if (need_check_timestamps)
465 		check_timestamps(FALSE);
466 	}
467 
468 	/*
469 	 * When emsg() was called msg_scroll will have been set.
470 	 */
471 	msg_scroll = FALSE;
472 
473 #ifdef FEAT_GUI
474 	/* When 'mousefocus' is set a mouse movement may have taken us to
475 	 * another window.  "need_mouse_correct" may then be set because of an
476 	 * autocommand. */
477 	if (need_mouse_correct)
478 	    gui_mouse_correct();
479 #endif
480 
481 #ifdef FEAT_FOLDING
482 	/* Open fold at the cursor line, according to 'foldopen'. */
483 	if (fdo_flags & FDO_INSERT)
484 	    foldOpenCursor();
485 	/* Close folds where the cursor isn't, according to 'foldclose' */
486 	if (!char_avail())
487 	    foldCheckClose();
488 #endif
489 
490 #ifdef FEAT_JOB_CHANNEL
491 	if (bt_prompt(curbuf))
492 	{
493 	    init_prompt(cmdchar_todo);
494 	    cmdchar_todo = NUL;
495 	}
496 #endif
497 
498 	/*
499 	 * If we inserted a character at the last position of the last line in
500 	 * the window, scroll the window one line up. This avoids an extra
501 	 * redraw.
502 	 * This is detected when the cursor column is smaller after inserting
503 	 * something.
504 	 * Don't do this when the topline changed already, it has
505 	 * already been adjusted (by insertchar() calling open_line())).
506 	 */
507 	if (curbuf->b_mod_set
508 		&& curwin->w_p_wrap
509 		&& !did_backspace
510 		&& curwin->w_topline == old_topline
511 #ifdef FEAT_DIFF
512 		&& curwin->w_topfill == old_topfill
513 #endif
514 		)
515 	{
516 	    mincol = curwin->w_wcol;
517 	    validate_cursor_col();
518 
519 	    if (
520 #ifdef FEAT_VARTABS
521 		(int)curwin->w_wcol < mincol - tabstop_at(
522 					  get_nolist_virtcol(), curbuf->b_p_ts,
523 							 curbuf->b_p_vts_array)
524 #else
525 		(int)curwin->w_wcol < mincol - curbuf->b_p_ts
526 #endif
527 		    && curwin->w_wrow == W_WINROW(curwin)
528 				 + curwin->w_height - 1 - get_scrolloff_value()
529 		    && (curwin->w_cursor.lnum != curwin->w_topline
530 #ifdef FEAT_DIFF
531 			|| curwin->w_topfill > 0
532 #endif
533 		    ))
534 	    {
535 #ifdef FEAT_DIFF
536 		if (curwin->w_topfill > 0)
537 		    --curwin->w_topfill;
538 		else
539 #endif
540 #ifdef FEAT_FOLDING
541 		if (hasFolding(curwin->w_topline, NULL, &old_topline))
542 		    set_topline(curwin, old_topline + 1);
543 		else
544 #endif
545 		    set_topline(curwin, curwin->w_topline + 1);
546 	    }
547 	}
548 
549 	/* May need to adjust w_topline to show the cursor. */
550 	update_topline();
551 
552 	did_backspace = FALSE;
553 
554 	validate_cursor();		/* may set must_redraw */
555 
556 	/*
557 	 * Redraw the display when no characters are waiting.
558 	 * Also shows mode, ruler and positions cursor.
559 	 */
560 	ins_redraw(TRUE);
561 
562 	if (curwin->w_p_scb)
563 	    do_check_scrollbind(TRUE);
564 
565 	if (curwin->w_p_crb)
566 	    do_check_cursorbind();
567 	update_curswant();
568 	old_topline = curwin->w_topline;
569 #ifdef FEAT_DIFF
570 	old_topfill = curwin->w_topfill;
571 #endif
572 
573 #ifdef USE_ON_FLY_SCROLL
574 	dont_scroll = FALSE;		/* allow scrolling here */
575 #endif
576 
577 	/*
578 	 * Get a character for Insert mode.  Ignore K_IGNORE and K_NOP.
579 	 */
580 	if (c != K_CURSORHOLD)
581 	    lastc = c;		/* remember the previous char for CTRL-D */
582 
583 	/* After using CTRL-G U the next cursor key will not break undo. */
584 	if (dont_sync_undo == MAYBE)
585 	    dont_sync_undo = TRUE;
586 	else
587 	    dont_sync_undo = FALSE;
588 	if (cmdchar == K_PS)
589 	    /* Got here from normal mode when bracketed paste started. */
590 	    c = K_PS;
591 	else
592 	    do
593 	    {
594 		c = safe_vgetc();
595 
596 		if (stop_insert_mode)
597 		{
598 		    // Insert mode ended, possibly from a callback.
599 		    count = 0;
600 		    nomove = TRUE;
601 		    goto doESCkey;
602 		}
603 	    } while (c == K_IGNORE || c == K_NOP);
604 
605 	/* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
606 	did_cursorhold = TRUE;
607 
608 #ifdef FEAT_RIGHTLEFT
609 	if (p_hkmap && KeyTyped)
610 	    c = hkmap(c);		/* Hebrew mode mapping */
611 #endif
612 
613 	/*
614 	 * Special handling of keys while the popup menu is visible or wanted
615 	 * and the cursor is still in the completed word.  Only when there is
616 	 * a match, skip this when no matches were found.
617 	 */
618 	if (ins_compl_active()
619 		&& pum_wanted()
620 		&& curwin->w_cursor.col >= ins_compl_col()
621 		&& ins_compl_has_shown_match())
622 	{
623 	    /* BS: Delete one character from "compl_leader". */
624 	    if ((c == K_BS || c == Ctrl_H)
625 			&& curwin->w_cursor.col > ins_compl_col()
626 			&& (c = ins_compl_bs()) == NUL)
627 		continue;
628 
629 	    /* When no match was selected or it was edited. */
630 	    if (!ins_compl_used_match())
631 	    {
632 		/* CTRL-L: Add one character from the current match to
633 		 * "compl_leader".  Except when at the original match and
634 		 * there is nothing to add, CTRL-L works like CTRL-P then. */
635 		if (c == Ctrl_L
636 			&& (!ctrl_x_mode_line_or_eval()
637 			    || ins_compl_long_shown_match()))
638 		{
639 		    ins_compl_addfrommatch();
640 		    continue;
641 		}
642 
643 		/* A non-white character that fits in with the current
644 		 * completion: Add to "compl_leader". */
645 		if (ins_compl_accept_char(c))
646 		{
647 #if defined(FEAT_EVAL)
648 		    /* Trigger InsertCharPre. */
649 		    char_u *str = do_insert_char_pre(c);
650 		    char_u *p;
651 
652 		    if (str != NULL)
653 		    {
654 			for (p = str; *p != NUL; MB_PTR_ADV(p))
655 			    ins_compl_addleader(PTR2CHAR(p));
656 			vim_free(str);
657 		    }
658 		    else
659 #endif
660 			ins_compl_addleader(c);
661 		    continue;
662 		}
663 
664 		/* Pressing CTRL-Y selects the current match.  When
665 		 * ins_compl_enter_selects() is set the Enter key does the
666 		 * same. */
667 		if ((c == Ctrl_Y || (ins_compl_enter_selects()
668 				    && (c == CAR || c == K_KENTER || c == NL)))
669 			&& stop_arrow() == OK)
670 		{
671 		    ins_compl_delete();
672 		    ins_compl_insert(FALSE);
673 		}
674 	    }
675 	}
676 
677 	/* Prepare for or stop CTRL-X mode.  This doesn't do completion, but
678 	 * it does fix up the text when finishing completion. */
679 	ins_compl_init_get_longest();
680 	if (ins_compl_prep(c))
681 	    continue;
682 
683 	/* CTRL-\ CTRL-N goes to Normal mode,
684 	 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
685 	 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.  */
686 	if (c == Ctrl_BSL)
687 	{
688 	    /* may need to redraw when no more chars available now */
689 	    ins_redraw(FALSE);
690 	    ++no_mapping;
691 	    ++allow_keys;
692 	    c = plain_vgetc();
693 	    --no_mapping;
694 	    --allow_keys;
695 	    if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
696 	    {
697 		/* it's something else */
698 		vungetc(c);
699 		c = Ctrl_BSL;
700 	    }
701 	    else if (c == Ctrl_G && p_im)
702 		continue;
703 	    else
704 	    {
705 		if (c == Ctrl_O)
706 		{
707 		    ins_ctrl_o();
708 		    ins_at_eol = FALSE;	/* cursor keeps its column */
709 		    nomove = TRUE;
710 		}
711 		count = 0;
712 		goto doESCkey;
713 	    }
714 	}
715 
716 #ifdef FEAT_DIGRAPHS
717 	c = do_digraph(c);
718 #endif
719 
720 	if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
721 	    goto docomplete;
722 	if (c == Ctrl_V || c == Ctrl_Q)
723 	{
724 	    ins_ctrl_v();
725 	    c = Ctrl_V;	/* pretend CTRL-V is last typed character */
726 	    continue;
727 	}
728 
729 #ifdef FEAT_CINDENT
730 	if (cindent_on() && ctrl_x_mode_none())
731 	{
732 	    /* A key name preceded by a bang means this key is not to be
733 	     * inserted.  Skip ahead to the re-indenting below.
734 	     * A key name preceded by a star means that indenting has to be
735 	     * done before inserting the key. */
736 	    line_is_white = inindent(0);
737 	    if (in_cinkeys(c, '!', line_is_white))
738 		goto force_cindent;
739 	    if (can_cindent && in_cinkeys(c, '*', line_is_white)
740 							&& stop_arrow() == OK)
741 		do_c_expr_indent();
742 	}
743 #endif
744 
745 #ifdef FEAT_RIGHTLEFT
746 	if (curwin->w_p_rl)
747 	    switch (c)
748 	    {
749 		case K_LEFT:	c = K_RIGHT; break;
750 		case K_S_LEFT:	c = K_S_RIGHT; break;
751 		case K_C_LEFT:	c = K_C_RIGHT; break;
752 		case K_RIGHT:	c = K_LEFT; break;
753 		case K_S_RIGHT: c = K_S_LEFT; break;
754 		case K_C_RIGHT: c = K_C_LEFT; break;
755 	    }
756 #endif
757 
758 	/*
759 	 * If 'keymodel' contains "startsel", may start selection.  If it
760 	 * does, a CTRL-O and c will be stuffed, we need to get these
761 	 * characters.
762 	 */
763 	if (ins_start_select(c))
764 	    continue;
765 
766 	/*
767 	 * The big switch to handle a character in insert mode.
768 	 */
769 	switch (c)
770 	{
771 	case ESC:	/* End input mode */
772 	    if (echeck_abbr(ESC + ABBR_OFF))
773 		break;
774 	    /* FALLTHROUGH */
775 
776 	case Ctrl_C:	/* End input mode */
777 #ifdef FEAT_CMDWIN
778 	    if (c == Ctrl_C && cmdwin_type != 0)
779 	    {
780 		/* Close the cmdline window. */
781 		cmdwin_result = K_IGNORE;
782 		got_int = FALSE; /* don't stop executing autocommands et al. */
783 		nomove = TRUE;
784 		goto doESCkey;
785 	    }
786 #endif
787 #ifdef FEAT_JOB_CHANNEL
788 	    if (c == Ctrl_C && bt_prompt(curbuf))
789 	    {
790 		if (invoke_prompt_interrupt())
791 		{
792 		    if (!bt_prompt(curbuf))
793 			// buffer changed to a non-prompt buffer, get out of
794 			// Insert mode
795 			goto doESCkey;
796 		    break;
797 		}
798 	    }
799 #endif
800 
801 #ifdef UNIX
802 do_intr:
803 #endif
804 	    /* when 'insertmode' set, and not halfway a mapping, don't leave
805 	     * Insert mode */
806 	    if (goto_im())
807 	    {
808 		if (got_int)
809 		{
810 		    (void)vgetc();		/* flush all buffers */
811 		    got_int = FALSE;
812 		}
813 		else
814 		    vim_beep(BO_IM);
815 		break;
816 	    }
817 doESCkey:
818 	    /*
819 	     * This is the ONLY return from edit()!
820 	     */
821 	    /* Always update o_lnum, so that a "CTRL-O ." that adds a line
822 	     * still puts the cursor back after the inserted text. */
823 	    if (ins_at_eol && gchar_cursor() == NUL)
824 		o_lnum = curwin->w_cursor.lnum;
825 
826 	    if (ins_esc(&count, cmdchar, nomove))
827 	    {
828 		// When CTRL-C was typed got_int will be set, with the result
829 		// that the autocommands won't be executed. When mapped got_int
830 		// is not set, but let's keep the behavior the same.
831 		if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
832 		    ins_apply_autocmds(EVENT_INSERTLEAVE);
833 		did_cursorhold = FALSE;
834 		return (c == Ctrl_O);
835 	    }
836 	    continue;
837 
838 	case Ctrl_Z:	/* suspend when 'insertmode' set */
839 	    if (!p_im)
840 		goto normalchar;	/* insert CTRL-Z as normal char */
841 	    do_cmdline_cmd((char_u *)"stop");
842 #ifdef CURSOR_SHAPE
843 	    ui_cursor_shape();		/* may need to update cursor shape */
844 #endif
845 	    continue;
846 
847 	case Ctrl_O:	/* execute one command */
848 #ifdef FEAT_COMPL_FUNC
849 	    if (ctrl_x_mode_omni())
850 		goto docomplete;
851 #endif
852 	    if (echeck_abbr(Ctrl_O + ABBR_OFF))
853 		break;
854 	    ins_ctrl_o();
855 
856 	    /* don't move the cursor left when 'virtualedit' has "onemore". */
857 	    if (ve_flags & VE_ONEMORE)
858 	    {
859 		ins_at_eol = FALSE;
860 		nomove = TRUE;
861 	    }
862 	    count = 0;
863 	    goto doESCkey;
864 
865 	case K_INS:	/* toggle insert/replace mode */
866 	case K_KINS:
867 	    ins_insert(replaceState);
868 	    break;
869 
870 	case K_SELECT:	/* end of Select mode mapping - ignore */
871 	    break;
872 
873 	case K_HELP:	/* Help key works like <ESC> <Help> */
874 	case K_F1:
875 	case K_XF1:
876 	    stuffcharReadbuff(K_HELP);
877 	    if (p_im)
878 		need_start_insertmode = TRUE;
879 	    goto doESCkey;
880 
881 #ifdef FEAT_NETBEANS_INTG
882 	case K_F21:	/* NetBeans command */
883 	    ++no_mapping;		/* don't map the next key hits */
884 	    i = plain_vgetc();
885 	    --no_mapping;
886 	    netbeans_keycommand(i);
887 	    break;
888 #endif
889 
890 	case K_ZERO:	/* Insert the previously inserted text. */
891 	case NUL:
892 	case Ctrl_A:
893 	    /* For ^@ the trailing ESC will end the insert, unless there is an
894 	     * error.  */
895 	    if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
896 						   && c != Ctrl_A && !p_im)
897 		goto doESCkey;		/* quit insert mode */
898 	    inserted_space = FALSE;
899 	    break;
900 
901 	case Ctrl_R:	/* insert the contents of a register */
902 	    ins_reg();
903 	    auto_format(FALSE, TRUE);
904 	    inserted_space = FALSE;
905 	    break;
906 
907 	case Ctrl_G:	/* commands starting with CTRL-G */
908 	    ins_ctrl_g();
909 	    break;
910 
911 	case Ctrl_HAT:	/* switch input mode and/or langmap */
912 	    ins_ctrl_hat();
913 	    break;
914 
915 #ifdef FEAT_RIGHTLEFT
916 	case Ctrl__:	/* switch between languages */
917 	    if (!p_ari)
918 		goto normalchar;
919 	    ins_ctrl_();
920 	    break;
921 #endif
922 
923 	case Ctrl_D:	/* Make indent one shiftwidth smaller. */
924 #if defined(FEAT_FIND_ID)
925 	    if (ctrl_x_mode_path_defines())
926 		goto docomplete;
927 #endif
928 	    /* FALLTHROUGH */
929 
930 	case Ctrl_T:	/* Make indent one shiftwidth greater. */
931 	    if (c == Ctrl_T && ctrl_x_mode_thesaurus())
932 	    {
933 		if (has_compl_option(FALSE))
934 		    goto docomplete;
935 		break;
936 	    }
937 
938 	    ins_shift(c, lastc);
939 	    auto_format(FALSE, TRUE);
940 	    inserted_space = FALSE;
941 	    break;
942 
943 	case K_DEL:	/* delete character under the cursor */
944 	case K_KDEL:
945 	    ins_del();
946 	    auto_format(FALSE, TRUE);
947 	    break;
948 
949 	case K_BS:	/* delete character before the cursor */
950 	case Ctrl_H:
951 	    did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
952 	    auto_format(FALSE, TRUE);
953 	    break;
954 
955 	case Ctrl_W:	/* delete word before the cursor */
956 #ifdef FEAT_JOB_CHANNEL
957 	    if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
958 	    {
959 		// In a prompt window CTRL-W is used for window commands.
960 		// Use Shift-CTRL-W to delete a word.
961 		stuffcharReadbuff(Ctrl_W);
962 		restart_edit = 'A';
963 		nomove = TRUE;
964 		count = 0;
965 		goto doESCkey;
966 	    }
967 #endif
968 	    did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
969 	    auto_format(FALSE, TRUE);
970 	    break;
971 
972 	case Ctrl_U:	/* delete all inserted text in current line */
973 # ifdef FEAT_COMPL_FUNC
974 	    /* CTRL-X CTRL-U completes with 'completefunc'. */
975 	    if (ctrl_x_mode_function())
976 		goto docomplete;
977 # endif
978 	    did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
979 	    auto_format(FALSE, TRUE);
980 	    inserted_space = FALSE;
981 	    break;
982 
983 #ifdef FEAT_MOUSE
984 	case K_LEFTMOUSE:   /* mouse keys */
985 	case K_LEFTMOUSE_NM:
986 	case K_LEFTDRAG:
987 	case K_LEFTRELEASE:
988 	case K_LEFTRELEASE_NM:
989 	case K_MOUSEMOVE:
990 	case K_MIDDLEMOUSE:
991 	case K_MIDDLEDRAG:
992 	case K_MIDDLERELEASE:
993 	case K_RIGHTMOUSE:
994 	case K_RIGHTDRAG:
995 	case K_RIGHTRELEASE:
996 	case K_X1MOUSE:
997 	case K_X1DRAG:
998 	case K_X1RELEASE:
999 	case K_X2MOUSE:
1000 	case K_X2DRAG:
1001 	case K_X2RELEASE:
1002 	    ins_mouse(c);
1003 	    break;
1004 
1005 	case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
1006 	    ins_mousescroll(MSCR_DOWN);
1007 	    break;
1008 
1009 	case K_MOUSEUP:	/* Default action for scroll wheel down: scroll down */
1010 	    ins_mousescroll(MSCR_UP);
1011 	    break;
1012 
1013 	case K_MOUSELEFT: /* Scroll wheel left */
1014 	    ins_mousescroll(MSCR_LEFT);
1015 	    break;
1016 
1017 	case K_MOUSERIGHT: /* Scroll wheel right */
1018 	    ins_mousescroll(MSCR_RIGHT);
1019 	    break;
1020 #endif
1021 	case K_PS:
1022 	    bracketed_paste(PASTE_INSERT, FALSE, NULL);
1023 	    if (cmdchar == K_PS)
1024 		/* invoked from normal mode, bail out */
1025 		goto doESCkey;
1026 	    break;
1027 	case K_PE:
1028 	    /* Got K_PE without K_PS, ignore. */
1029 	    break;
1030 
1031 #ifdef FEAT_GUI_TABLINE
1032 	case K_TABLINE:
1033 	case K_TABMENU:
1034 	    ins_tabline(c);
1035 	    break;
1036 #endif
1037 
1038 	case K_IGNORE:	/* Something mapped to nothing */
1039 	    break;
1040 
1041 	case K_CURSORHOLD:	/* Didn't type something for a while. */
1042 	    ins_apply_autocmds(EVENT_CURSORHOLDI);
1043 	    did_cursorhold = TRUE;
1044 	    break;
1045 
1046 #ifdef FEAT_GUI_MSWIN
1047 	    /* On MS-Windows ignore <M-F4>, we get it when closing the window
1048 	     * was cancelled. */
1049 	case K_F4:
1050 	    if (mod_mask != MOD_MASK_ALT)
1051 		goto normalchar;
1052 	    break;
1053 #endif
1054 
1055 #ifdef FEAT_GUI
1056 	case K_VER_SCROLLBAR:
1057 	    ins_scroll();
1058 	    break;
1059 
1060 	case K_HOR_SCROLLBAR:
1061 	    ins_horscroll();
1062 	    break;
1063 #endif
1064 
1065 	case K_HOME:	/* <Home> */
1066 	case K_KHOME:
1067 	case K_S_HOME:
1068 	case K_C_HOME:
1069 	    ins_home(c);
1070 	    break;
1071 
1072 	case K_END:	/* <End> */
1073 	case K_KEND:
1074 	case K_S_END:
1075 	case K_C_END:
1076 	    ins_end(c);
1077 	    break;
1078 
1079 	case K_LEFT:	/* <Left> */
1080 	    if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1081 		ins_s_left();
1082 	    else
1083 		ins_left();
1084 	    break;
1085 
1086 	case K_S_LEFT:	/* <S-Left> */
1087 	case K_C_LEFT:
1088 	    ins_s_left();
1089 	    break;
1090 
1091 	case K_RIGHT:	/* <Right> */
1092 	    if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1093 		ins_s_right();
1094 	    else
1095 		ins_right();
1096 	    break;
1097 
1098 	case K_S_RIGHT:	/* <S-Right> */
1099 	case K_C_RIGHT:
1100 	    ins_s_right();
1101 	    break;
1102 
1103 	case K_UP:	/* <Up> */
1104 	    if (pum_visible())
1105 		goto docomplete;
1106 	    if (mod_mask & MOD_MASK_SHIFT)
1107 		ins_pageup();
1108 	    else
1109 		ins_up(FALSE);
1110 	    break;
1111 
1112 	case K_S_UP:	/* <S-Up> */
1113 	case K_PAGEUP:
1114 	case K_KPAGEUP:
1115 	    if (pum_visible())
1116 		goto docomplete;
1117 	    ins_pageup();
1118 	    break;
1119 
1120 	case K_DOWN:	/* <Down> */
1121 	    if (pum_visible())
1122 		goto docomplete;
1123 	    if (mod_mask & MOD_MASK_SHIFT)
1124 		ins_pagedown();
1125 	    else
1126 		ins_down(FALSE);
1127 	    break;
1128 
1129 	case K_S_DOWN:	/* <S-Down> */
1130 	case K_PAGEDOWN:
1131 	case K_KPAGEDOWN:
1132 	    if (pum_visible())
1133 		goto docomplete;
1134 	    ins_pagedown();
1135 	    break;
1136 
1137 #ifdef FEAT_DND
1138 	case K_DROP:	/* drag-n-drop event */
1139 	    ins_drop();
1140 	    break;
1141 #endif
1142 
1143 	case K_S_TAB:	/* When not mapped, use like a normal TAB */
1144 	    c = TAB;
1145 	    /* FALLTHROUGH */
1146 
1147 	case TAB:	/* TAB or Complete patterns along path */
1148 #if defined(FEAT_FIND_ID)
1149 	    if (ctrl_x_mode_path_patterns())
1150 		goto docomplete;
1151 #endif
1152 	    inserted_space = FALSE;
1153 	    if (ins_tab())
1154 		goto normalchar;	/* insert TAB as a normal char */
1155 	    auto_format(FALSE, TRUE);
1156 	    break;
1157 
1158 	case K_KENTER:	/* <Enter> */
1159 	    c = CAR;
1160 	    /* FALLTHROUGH */
1161 	case CAR:
1162 	case NL:
1163 #if defined(FEAT_QUICKFIX)
1164 	    /* In a quickfix window a <CR> jumps to the error under the
1165 	     * cursor. */
1166 	    if (bt_quickfix(curbuf) && c == CAR)
1167 	    {
1168 		if (curwin->w_llist_ref == NULL)    /* quickfix window */
1169 		    do_cmdline_cmd((char_u *)".cc");
1170 		else				    /* location list window */
1171 		    do_cmdline_cmd((char_u *)".ll");
1172 		break;
1173 	    }
1174 #endif
1175 #ifdef FEAT_CMDWIN
1176 	    if (cmdwin_type != 0)
1177 	    {
1178 		/* Execute the command in the cmdline window. */
1179 		cmdwin_result = CAR;
1180 		goto doESCkey;
1181 	    }
1182 #endif
1183 #ifdef FEAT_JOB_CHANNEL
1184 	    if (bt_prompt(curbuf))
1185 	    {
1186 		invoke_prompt_callback();
1187 		if (!bt_prompt(curbuf))
1188 		    // buffer changed to a non-prompt buffer, get out of
1189 		    // Insert mode
1190 		    goto doESCkey;
1191 		break;
1192 	    }
1193 #endif
1194 	    if (ins_eol(c) == FAIL && !p_im)
1195 		goto doESCkey;	    /* out of memory */
1196 	    auto_format(FALSE, FALSE);
1197 	    inserted_space = FALSE;
1198 	    break;
1199 
1200 	case Ctrl_K:	    /* digraph or keyword completion */
1201 	    if (ctrl_x_mode_dictionary())
1202 	    {
1203 		if (has_compl_option(TRUE))
1204 		    goto docomplete;
1205 		break;
1206 	    }
1207 #ifdef FEAT_DIGRAPHS
1208 	    c = ins_digraph();
1209 	    if (c == NUL)
1210 		break;
1211 #endif
1212 	    goto normalchar;
1213 
1214 	case Ctrl_X:	/* Enter CTRL-X mode */
1215 	    ins_ctrl_x();
1216 	    break;
1217 
1218 	case Ctrl_RSB:	/* Tag name completion after ^X */
1219 	    if (!ctrl_x_mode_tags())
1220 		goto normalchar;
1221 	    goto docomplete;
1222 
1223 	case Ctrl_F:	/* File name completion after ^X */
1224 	    if (!ctrl_x_mode_files())
1225 		goto normalchar;
1226 	    goto docomplete;
1227 
1228 	case 's':	/* Spelling completion after ^X */
1229 	case Ctrl_S:
1230 	    if (!ctrl_x_mode_spell())
1231 		goto normalchar;
1232 	    goto docomplete;
1233 
1234 	case Ctrl_L:	/* Whole line completion after ^X */
1235 	    if (!ctrl_x_mode_whole_line())
1236 	    {
1237 		/* CTRL-L with 'insertmode' set: Leave Insert mode */
1238 		if (p_im)
1239 		{
1240 		    if (echeck_abbr(Ctrl_L + ABBR_OFF))
1241 			break;
1242 		    goto doESCkey;
1243 		}
1244 		goto normalchar;
1245 	    }
1246 	    /* FALLTHROUGH */
1247 
1248 	case Ctrl_P:	/* Do previous/next pattern completion */
1249 	case Ctrl_N:
1250 	    /* if 'complete' is empty then plain ^P is no longer special,
1251 	     * but it is under other ^X modes */
1252 	    if (*curbuf->b_p_cpt == NUL
1253 		    && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
1254 		    && !(compl_cont_status & CONT_LOCAL))
1255 		goto normalchar;
1256 
1257 docomplete:
1258 	    compl_busy = TRUE;
1259 #ifdef FEAT_FOLDING
1260 	    disable_fold_update++;  /* don't redraw folds here */
1261 #endif
1262 	    if (ins_complete(c, TRUE) == FAIL)
1263 		compl_cont_status = 0;
1264 #ifdef FEAT_FOLDING
1265 	    disable_fold_update--;
1266 #endif
1267 	    compl_busy = FALSE;
1268 	    break;
1269 
1270 	case Ctrl_Y:	/* copy from previous line or scroll down */
1271 	case Ctrl_E:	/* copy from next line	   or scroll up */
1272 	    c = ins_ctrl_ey(c);
1273 	    break;
1274 
1275 	  default:
1276 #ifdef UNIX
1277 	    if (c == intr_char)		/* special interrupt char */
1278 		goto do_intr;
1279 #endif
1280 
1281 normalchar:
1282 	    /*
1283 	     * Insert a normal character.
1284 	     */
1285 #if defined(FEAT_EVAL)
1286 	    if (!p_paste)
1287 	    {
1288 		/* Trigger InsertCharPre. */
1289 		char_u *str = do_insert_char_pre(c);
1290 		char_u *p;
1291 
1292 		if (str != NULL)
1293 		{
1294 		    if (*str != NUL && stop_arrow() != FAIL)
1295 		    {
1296 			/* Insert the new value of v:char literally. */
1297 			for (p = str; *p != NUL; MB_PTR_ADV(p))
1298 			{
1299 			    c = PTR2CHAR(p);
1300 			    if (c == CAR || c == K_KENTER || c == NL)
1301 				ins_eol(c);
1302 			    else
1303 				ins_char(c);
1304 			}
1305 			AppendToRedobuffLit(str, -1);
1306 		    }
1307 		    vim_free(str);
1308 		    c = NUL;
1309 		}
1310 
1311 		/* If the new value is already inserted or an empty string
1312 		 * then don't insert any character. */
1313 		if (c == NUL)
1314 		    break;
1315 	    }
1316 #endif
1317 #ifdef FEAT_SMARTINDENT
1318 	    /* Try to perform smart-indenting. */
1319 	    ins_try_si(c);
1320 #endif
1321 
1322 	    if (c == ' ')
1323 	    {
1324 		inserted_space = TRUE;
1325 #ifdef FEAT_CINDENT
1326 		if (inindent(0))
1327 		    can_cindent = FALSE;
1328 #endif
1329 		if (Insstart_blank_vcol == MAXCOL
1330 			&& curwin->w_cursor.lnum == Insstart.lnum)
1331 		    Insstart_blank_vcol = get_nolist_virtcol();
1332 	    }
1333 
1334 	    /* Insert a normal character and check for abbreviations on a
1335 	     * special character.  Let CTRL-] expand abbreviations without
1336 	     * inserting it. */
1337 	    if (vim_iswordc(c) || (!echeck_abbr(
1338 			// Add ABBR_OFF for characters above 0x100, this is
1339 			// what check_abbr() expects.
1340 				(has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1341 			&& c != Ctrl_RSB))
1342 	    {
1343 		insert_special(c, FALSE, FALSE);
1344 #ifdef FEAT_RIGHTLEFT
1345 		revins_legal++;
1346 		revins_chars++;
1347 #endif
1348 	    }
1349 
1350 	    auto_format(FALSE, TRUE);
1351 
1352 #ifdef FEAT_FOLDING
1353 	    /* When inserting a character the cursor line must never be in a
1354 	     * closed fold. */
1355 	    foldOpenCursor();
1356 #endif
1357 	    break;
1358 	}   /* end of switch (c) */
1359 
1360 	/* If typed something may trigger CursorHoldI again. */
1361 	if (c != K_CURSORHOLD
1362 #ifdef FEAT_COMPL_FUNC
1363 		/* but not in CTRL-X mode, a script can't restore the state */
1364 		&& ctrl_x_mode_normal()
1365 #endif
1366 	       )
1367 	    did_cursorhold = FALSE;
1368 
1369 	/* If the cursor was moved we didn't just insert a space */
1370 	if (arrow_used)
1371 	    inserted_space = FALSE;
1372 
1373 #ifdef FEAT_CINDENT
1374 	if (can_cindent && cindent_on() && ctrl_x_mode_normal())
1375 	{
1376 force_cindent:
1377 	    /*
1378 	     * Indent now if a key was typed that is in 'cinkeys'.
1379 	     */
1380 	    if (in_cinkeys(c, ' ', line_is_white))
1381 	    {
1382 		if (stop_arrow() == OK)
1383 		    /* re-indent the current line */
1384 		    do_c_expr_indent();
1385 	    }
1386 	}
1387 #endif /* FEAT_CINDENT */
1388 
1389     }	/* for (;;) */
1390     /* NOTREACHED */
1391 }
1392 
1393     int
1394 ins_need_undo_get(void)
1395 {
1396     return ins_need_undo;
1397 }
1398 
1399 /*
1400  * Redraw for Insert mode.
1401  * This is postponed until getting the next character to make '$' in the 'cpo'
1402  * option work correctly.
1403  * Only redraw when there are no characters available.  This speeds up
1404  * inserting sequences of characters (e.g., for CTRL-R).
1405  */
1406     void
1407 ins_redraw(int ready)	    // not busy with something
1408 {
1409 #ifdef FEAT_CONCEAL
1410     linenr_T	conceal_old_cursor_line = 0;
1411     linenr_T	conceal_new_cursor_line = 0;
1412     int		conceal_update_lines = FALSE;
1413 #endif
1414 
1415     if (char_avail())
1416 	return;
1417 
1418     /* Trigger CursorMoved if the cursor moved.  Not when the popup menu is
1419      * visible, the command might delete it. */
1420     if (ready && (has_cursormovedI()
1421 # ifdef FEAT_TEXT_PROP
1422 		|| popup_visible
1423 # endif
1424 # if defined(FEAT_CONCEAL)
1425 		|| curwin->w_p_cole > 0
1426 # endif
1427 		)
1428 	    && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
1429 	    && !pum_visible())
1430     {
1431 # ifdef FEAT_SYN_HL
1432 	/* Need to update the screen first, to make sure syntax
1433 	 * highlighting is correct after making a change (e.g., inserting
1434 	 * a "(".  The autocommand may also require a redraw, so it's done
1435 	 * again below, unfortunately. */
1436 	if (syntax_present(curwin) && must_redraw)
1437 	    update_screen(0);
1438 # endif
1439 	if (has_cursormovedI())
1440 	{
1441 	    /* Make sure curswant is correct, an autocommand may call
1442 	     * getcurpos(). */
1443 	    update_curswant();
1444 	    ins_apply_autocmds(EVENT_CURSORMOVEDI);
1445 	}
1446 #ifdef FEAT_TEXT_PROP
1447 	if (popup_visible)
1448 	    popup_check_cursor_pos();
1449 #endif
1450 # ifdef FEAT_CONCEAL
1451 	if (curwin->w_p_cole > 0)
1452 	{
1453 	    conceal_old_cursor_line = last_cursormoved.lnum;
1454 	    conceal_new_cursor_line = curwin->w_cursor.lnum;
1455 	    conceal_update_lines = TRUE;
1456 	}
1457 # endif
1458 	last_cursormoved = curwin->w_cursor;
1459     }
1460 
1461     /* Trigger TextChangedI if b_changedtick differs. */
1462     if (ready && has_textchangedI()
1463 	    && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
1464 	    && !pum_visible())
1465     {
1466 	aco_save_T	aco;
1467 	varnumber_T	tick = CHANGEDTICK(curbuf);
1468 
1469 	// save and restore curwin and curbuf, in case the autocmd changes them
1470 	aucmd_prepbuf(&aco, curbuf);
1471 	apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1472 	aucmd_restbuf(&aco);
1473 	curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
1474 	if (tick != CHANGEDTICK(curbuf))  // see ins_apply_autocmds()
1475 	    u_save(curwin->w_cursor.lnum,
1476 					(linenr_T)(curwin->w_cursor.lnum + 1));
1477     }
1478 
1479     /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1480      * TextChangedI will need to trigger for backwards compatibility, thus use
1481      * different b_last_changedtick* variables. */
1482     if (ready && has_textchangedP()
1483 	    && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1484 	    && pum_visible())
1485     {
1486 	aco_save_T	aco;
1487 	varnumber_T	tick = CHANGEDTICK(curbuf);
1488 
1489 	// save and restore curwin and curbuf, in case the autocmd changes them
1490 	aucmd_prepbuf(&aco, curbuf);
1491 	apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1492 	aucmd_restbuf(&aco);
1493 	curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1494 	if (tick != CHANGEDTICK(curbuf))  // see ins_apply_autocmds()
1495 	    u_save(curwin->w_cursor.lnum,
1496 					(linenr_T)(curwin->w_cursor.lnum + 1));
1497     }
1498 
1499     // Trigger SafeState if nothing is pending.
1500     may_trigger_safestate(ready
1501 	    && !ins_compl_active()
1502 	    && !pum_visible());
1503 
1504 #if defined(FEAT_CONCEAL)
1505     if ((conceal_update_lines
1506 	    && (conceal_old_cursor_line != conceal_new_cursor_line
1507 		|| conceal_cursor_line(curwin)))
1508 	    || need_cursor_line_redraw)
1509     {
1510 	if (conceal_old_cursor_line != conceal_new_cursor_line)
1511 	    redrawWinline(curwin, conceal_old_cursor_line);
1512 	redrawWinline(curwin, conceal_new_cursor_line == 0
1513 			    ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1514 	curwin->w_valid &= ~VALID_CROW;
1515 	need_cursor_line_redraw = FALSE;
1516     }
1517 #endif
1518     if (must_redraw)
1519 	update_screen(0);
1520     else if (clear_cmdline || redraw_cmdline)
1521 	showmode();		/* clear cmdline and show mode */
1522     showruler(FALSE);
1523     setcursor();
1524     emsg_on_display = FALSE;	/* may remove error message now */
1525 }
1526 
1527 /*
1528  * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1529  */
1530     static void
1531 ins_ctrl_v(void)
1532 {
1533     int		c;
1534     int		did_putchar = FALSE;
1535 
1536     /* may need to redraw when no more chars available now */
1537     ins_redraw(FALSE);
1538 
1539     if (redrawing() && !char_avail())
1540     {
1541 	edit_putchar('^', TRUE);
1542 	did_putchar = TRUE;
1543     }
1544     AppendToRedobuff((char_u *)CTRL_V_STR);	/* CTRL-V */
1545 
1546 #ifdef FEAT_CMDL_INFO
1547     add_to_showcmd_c(Ctrl_V);
1548 #endif
1549 
1550     c = get_literal();
1551     if (did_putchar)
1552 	/* when the line fits in 'columns' the '^' is at the start of the next
1553 	 * line and will not removed by the redraw */
1554 	edit_unputchar();
1555 #ifdef FEAT_CMDL_INFO
1556     clear_showcmd();
1557 #endif
1558     insert_special(c, FALSE, TRUE);
1559 #ifdef FEAT_RIGHTLEFT
1560     revins_chars++;
1561     revins_legal++;
1562 #endif
1563 }
1564 
1565 /*
1566  * Put a character directly onto the screen.  It's not stored in a buffer.
1567  * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1568  */
1569 static int  pc_status;
1570 #define PC_STATUS_UNSET	0	/* pc_bytes was not set */
1571 #define PC_STATUS_RIGHT	1	/* right halve of double-wide char */
1572 #define PC_STATUS_LEFT	2	/* left halve of double-wide char */
1573 #define PC_STATUS_SET	3	/* pc_bytes was filled */
1574 static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1575 static int  pc_attr;
1576 static int  pc_row;
1577 static int  pc_col;
1578 
1579     void
1580 edit_putchar(int c, int highlight)
1581 {
1582     int	    attr;
1583 
1584     if (ScreenLines != NULL)
1585     {
1586 	update_topline();	/* just in case w_topline isn't valid */
1587 	validate_cursor();
1588 	if (highlight)
1589 	    attr = HL_ATTR(HLF_8);
1590 	else
1591 	    attr = 0;
1592 	pc_row = W_WINROW(curwin) + curwin->w_wrow;
1593 	pc_col = curwin->w_wincol;
1594 	pc_status = PC_STATUS_UNSET;
1595 #ifdef FEAT_RIGHTLEFT
1596 	if (curwin->w_p_rl)
1597 	{
1598 	    pc_col += curwin->w_width - 1 - curwin->w_wcol;
1599 	    if (has_mbyte)
1600 	    {
1601 		int fix_col = mb_fix_col(pc_col, pc_row);
1602 
1603 		if (fix_col != pc_col)
1604 		{
1605 		    screen_putchar(' ', pc_row, fix_col, attr);
1606 		    --curwin->w_wcol;
1607 		    pc_status = PC_STATUS_RIGHT;
1608 		}
1609 	    }
1610 	}
1611 	else
1612 #endif
1613 	{
1614 	    pc_col += curwin->w_wcol;
1615 	    if (mb_lefthalve(pc_row, pc_col))
1616 		pc_status = PC_STATUS_LEFT;
1617 	}
1618 
1619 	/* save the character to be able to put it back */
1620 	if (pc_status == PC_STATUS_UNSET)
1621 	{
1622 	    screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1623 	    pc_status = PC_STATUS_SET;
1624 	}
1625 	screen_putchar(c, pc_row, pc_col, attr);
1626     }
1627 }
1628 
1629 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1630 /*
1631  * Return the effective prompt for the current buffer.
1632  */
1633     char_u *
1634 prompt_text(void)
1635 {
1636     if (curbuf->b_prompt_text == NULL)
1637 	return (char_u *)"% ";
1638     return curbuf->b_prompt_text;
1639 }
1640 
1641 /*
1642  * Prepare for prompt mode: Make sure the last line has the prompt text.
1643  * Move the cursor to this line.
1644  */
1645     static void
1646 init_prompt(int cmdchar_todo)
1647 {
1648     char_u *prompt = prompt_text();
1649     char_u *text;
1650 
1651     curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1652     text = ml_get_curline();
1653     if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1654     {
1655 	// prompt is missing, insert it or append a line with it
1656 	if (*text == NUL)
1657 	    ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1658 	else
1659 	    ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1660 	curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1661 	coladvance((colnr_T)MAXCOL);
1662 	changed_bytes(curbuf->b_ml.ml_line_count, 0);
1663     }
1664 
1665     // Insert always starts after the prompt, allow editing text after it.
1666     if (Insstart_orig.lnum != curwin->w_cursor.lnum
1667 				   || Insstart_orig.col != (int)STRLEN(prompt))
1668     {
1669 	Insstart.lnum = curwin->w_cursor.lnum;
1670 	Insstart.col = (int)STRLEN(prompt);
1671 	Insstart_orig = Insstart;
1672 	Insstart_textlen = Insstart.col;
1673 	Insstart_blank_vcol = MAXCOL;
1674 	arrow_used = FALSE;
1675     }
1676 
1677     if (cmdchar_todo == 'A')
1678 	coladvance((colnr_T)MAXCOL);
1679     if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
1680 	curwin->w_cursor.col = (int)STRLEN(prompt);
1681     /* Make sure the cursor is in a valid position. */
1682     check_cursor();
1683 }
1684 
1685 /*
1686  * Return TRUE if the cursor is in the editable position of the prompt line.
1687  */
1688     int
1689 prompt_curpos_editable()
1690 {
1691     return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1692 	&& curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1693 }
1694 #endif
1695 
1696 /*
1697  * Undo the previous edit_putchar().
1698  */
1699     void
1700 edit_unputchar(void)
1701 {
1702     if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1703     {
1704 	if (pc_status == PC_STATUS_RIGHT)
1705 	    ++curwin->w_wcol;
1706 	if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1707 	    redrawWinline(curwin, curwin->w_cursor.lnum);
1708 	else
1709 	    screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1710     }
1711 }
1712 
1713 /*
1714  * Called when p_dollar is set: display a '$' at the end of the changed text
1715  * Only works when cursor is in the line that changes.
1716  */
1717     void
1718 display_dollar(colnr_T col)
1719 {
1720     colnr_T save_col;
1721 
1722     if (!redrawing())
1723 	return;
1724 
1725     cursor_off();
1726     save_col = curwin->w_cursor.col;
1727     curwin->w_cursor.col = col;
1728     if (has_mbyte)
1729     {
1730 	char_u *p;
1731 
1732 	/* If on the last byte of a multi-byte move to the first byte. */
1733 	p = ml_get_curline();
1734 	curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1735     }
1736     curs_columns(FALSE);	    /* recompute w_wrow and w_wcol */
1737     if (curwin->w_wcol < curwin->w_width)
1738     {
1739 	edit_putchar('$', FALSE);
1740 	dollar_vcol = curwin->w_virtcol;
1741     }
1742     curwin->w_cursor.col = save_col;
1743 }
1744 
1745 /*
1746  * Call this function before moving the cursor from the normal insert position
1747  * in insert mode.
1748  */
1749     void
1750 undisplay_dollar(void)
1751 {
1752     if (dollar_vcol >= 0)
1753     {
1754 	dollar_vcol = -1;
1755 	redrawWinline(curwin, curwin->w_cursor.lnum);
1756     }
1757 }
1758 
1759 /*
1760  * Truncate the space at the end of a line.  This is to be used only in an
1761  * insert mode.  It handles fixing the replace stack for REPLACE and VREPLACE
1762  * modes.
1763  */
1764     void
1765 truncate_spaces(char_u *line)
1766 {
1767     int	    i;
1768 
1769     /* find start of trailing white space */
1770     for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
1771     {
1772 	if (State & REPLACE_FLAG)
1773 	    replace_join(0);	    /* remove a NUL from the replace stack */
1774     }
1775     line[i + 1] = NUL;
1776 }
1777 
1778 /*
1779  * Backspace the cursor until the given column.  Handles REPLACE and VREPLACE
1780  * modes correctly.  May also be used when not in insert mode at all.
1781  * Will attempt not to go before "col" even when there is a composing
1782  * character.
1783  */
1784     void
1785 backspace_until_column(int col)
1786 {
1787     while ((int)curwin->w_cursor.col > col)
1788     {
1789 	curwin->w_cursor.col--;
1790 	if (State & REPLACE_FLAG)
1791 	    replace_do_bs(col);
1792 	else if (!del_char_after_col(col))
1793 	    break;
1794     }
1795 }
1796 
1797 /*
1798  * Like del_char(), but make sure not to go before column "limit_col".
1799  * Only matters when there are composing characters.
1800  * Return TRUE when something was deleted.
1801  */
1802    static int
1803 del_char_after_col(int limit_col UNUSED)
1804 {
1805     if (enc_utf8 && limit_col >= 0)
1806     {
1807 	colnr_T ecol = curwin->w_cursor.col + 1;
1808 
1809 	/* Make sure the cursor is at the start of a character, but
1810 	 * skip forward again when going too far back because of a
1811 	 * composing character. */
1812 	mb_adjust_cursor();
1813 	while (curwin->w_cursor.col < (colnr_T)limit_col)
1814 	{
1815 	    int l = utf_ptr2len(ml_get_cursor());
1816 
1817 	    if (l == 0)  /* end of line */
1818 		break;
1819 	    curwin->w_cursor.col += l;
1820 	}
1821 	if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1822 	    return FALSE;
1823 	del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
1824     }
1825     else
1826 	(void)del_char(FALSE);
1827     return TRUE;
1828 }
1829 
1830 /*
1831  * Next character is interpreted literally.
1832  * A one, two or three digit decimal number is interpreted as its byte value.
1833  * If one or two digits are entered, the next character is given to vungetc().
1834  * For Unicode a character > 255 may be returned.
1835  */
1836     int
1837 get_literal(void)
1838 {
1839     int		cc;
1840     int		nc;
1841     int		i;
1842     int		hex = FALSE;
1843     int		octal = FALSE;
1844     int		unicode = 0;
1845 
1846     if (got_int)
1847 	return Ctrl_C;
1848 
1849 #ifdef FEAT_GUI
1850     /*
1851      * In GUI there is no point inserting the internal code for a special key.
1852      * It is more useful to insert the string "<KEY>" instead.	This would
1853      * probably be useful in a text window too, but it would not be
1854      * vi-compatible (maybe there should be an option for it?) -- webb
1855      */
1856     if (gui.in_use)
1857 	++allow_keys;
1858 #endif
1859 #ifdef USE_ON_FLY_SCROLL
1860     dont_scroll = TRUE;		/* disallow scrolling here */
1861 #endif
1862     ++no_mapping;		/* don't map the next key hits */
1863     cc = 0;
1864     i = 0;
1865     for (;;)
1866     {
1867 	nc = plain_vgetc();
1868 #ifdef FEAT_CMDL_INFO
1869 	if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
1870 	    add_to_showcmd(nc);
1871 #endif
1872 	if (nc == 'x' || nc == 'X')
1873 	    hex = TRUE;
1874 	else if (nc == 'o' || nc == 'O')
1875 	    octal = TRUE;
1876 	else if (nc == 'u' || nc == 'U')
1877 	    unicode = nc;
1878 	else
1879 	{
1880 	    if (hex || unicode != 0)
1881 	    {
1882 		if (!vim_isxdigit(nc))
1883 		    break;
1884 		cc = cc * 16 + hex2nr(nc);
1885 	    }
1886 	    else if (octal)
1887 	    {
1888 		if (nc < '0' || nc > '7')
1889 		    break;
1890 		cc = cc * 8 + nc - '0';
1891 	    }
1892 	    else
1893 	    {
1894 		if (!VIM_ISDIGIT(nc))
1895 		    break;
1896 		cc = cc * 10 + nc - '0';
1897 	    }
1898 
1899 	    ++i;
1900 	}
1901 
1902 	if (cc > 255 && unicode == 0)
1903 	    cc = 255;		/* limit range to 0-255 */
1904 	nc = 0;
1905 
1906 	if (hex)		/* hex: up to two chars */
1907 	{
1908 	    if (i >= 2)
1909 		break;
1910 	}
1911 	else if (unicode)	/* Unicode: up to four or eight chars */
1912 	{
1913 	    if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1914 		break;
1915 	}
1916 	else if (i >= 3)	/* decimal or octal: up to three chars */
1917 	    break;
1918     }
1919     if (i == 0)	    /* no number entered */
1920     {
1921 	if (nc == K_ZERO)   /* NUL is stored as NL */
1922 	{
1923 	    cc = '\n';
1924 	    nc = 0;
1925 	}
1926 	else
1927 	{
1928 	    cc = nc;
1929 	    nc = 0;
1930 	}
1931     }
1932 
1933     if (cc == 0)	/* NUL is stored as NL */
1934 	cc = '\n';
1935     if (enc_dbcs && (cc & 0xff) == 0)
1936 	cc = '?';	/* don't accept an illegal DBCS char, the NUL in the
1937 			   second byte will cause trouble! */
1938 
1939     --no_mapping;
1940 #ifdef FEAT_GUI
1941     if (gui.in_use)
1942 	--allow_keys;
1943 #endif
1944     if (nc)
1945 	vungetc(nc);
1946     got_int = FALSE;	    /* CTRL-C typed after CTRL-V is not an interrupt */
1947     return cc;
1948 }
1949 
1950 /*
1951  * Insert character, taking care of special keys and mod_mask
1952  */
1953     static void
1954 insert_special(
1955     int	    c,
1956     int	    allow_modmask,
1957     int	    ctrlv)	    /* c was typed after CTRL-V */
1958 {
1959     char_u  *p;
1960     int	    len;
1961 
1962     /*
1963      * Special function key, translate into "<Key>". Up to the last '>' is
1964      * inserted with ins_str(), so as not to replace characters in replace
1965      * mode.
1966      * Only use mod_mask for special keys, to avoid things like <S-Space>,
1967      * unless 'allow_modmask' is TRUE.
1968      */
1969 #ifdef MACOS_X
1970     /* Command-key never produces a normal key */
1971     if (mod_mask & MOD_MASK_CMD)
1972 	allow_modmask = TRUE;
1973 #endif
1974     if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
1975     {
1976 	p = get_special_key_name(c, mod_mask);
1977 	len = (int)STRLEN(p);
1978 	c = p[len - 1];
1979 	if (len > 2)
1980 	{
1981 	    if (stop_arrow() == FAIL)
1982 		return;
1983 	    p[len - 1] = NUL;
1984 	    ins_str(p);
1985 	    AppendToRedobuffLit(p, -1);
1986 	    ctrlv = FALSE;
1987 	}
1988     }
1989     if (stop_arrow() == OK)
1990 	insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
1991 }
1992 
1993 /*
1994  * Special characters in this context are those that need processing other
1995  * than the simple insertion that can be performed here. This includes ESC
1996  * which terminates the insert, and CR/NL which need special processing to
1997  * open up a new line. This routine tries to optimize insertions performed by
1998  * the "redo", "undo" or "put" commands, so it needs to know when it should
1999  * stop and defer processing to the "normal" mechanism.
2000  * '0' and '^' are special, because they can be followed by CTRL-D.
2001  */
2002 #ifdef EBCDIC
2003 # define ISSPECIAL(c)	((c) < ' ' || (c) == '0' || (c) == '^')
2004 #else
2005 # define ISSPECIAL(c)	((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2006 #endif
2007 
2008 #define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
2009 
2010 /*
2011  * "flags": INSCHAR_FORMAT - force formatting
2012  *	    INSCHAR_CTRLV  - char typed just after CTRL-V
2013  *	    INSCHAR_NO_FEX - don't use 'formatexpr'
2014  *
2015  *   NOTE: passes the flags value straight through to internal_format() which,
2016  *	   beside INSCHAR_FORMAT (above), is also looking for these:
2017  *	    INSCHAR_DO_COM   - format comments
2018  *	    INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2019  */
2020     void
2021 insertchar(
2022     int		c,			/* character to insert or NUL */
2023     int		flags,			/* INSCHAR_FORMAT, etc. */
2024     int		second_indent)		/* indent for second line if >= 0 */
2025 {
2026     int		textwidth;
2027     char_u	*p;
2028     int		fo_ins_blank;
2029     int		force_format = flags & INSCHAR_FORMAT;
2030 
2031     textwidth = comp_textwidth(force_format);
2032     fo_ins_blank = has_format_option(FO_INS_BLANK);
2033 
2034     /*
2035      * Try to break the line in two or more pieces when:
2036      * - Always do this if we have been called to do formatting only.
2037      * - Always do this when 'formatoptions' has the 'a' flag and the line
2038      *   ends in white space.
2039      * - Otherwise:
2040      *	 - Don't do this if inserting a blank
2041      *	 - Don't do this if an existing character is being replaced, unless
2042      *	   we're in VREPLACE mode.
2043      *	 - Do this if the cursor is not on the line where insert started
2044      *	 or - 'formatoptions' doesn't have 'l' or the line was not too long
2045      *	       before the insert.
2046      *	    - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2047      *	      before 'textwidth'
2048      */
2049     if (textwidth > 0
2050 	    && (force_format
2051 		|| (!VIM_ISWHITE(c)
2052 		    && !((State & REPLACE_FLAG)
2053 			&& !(State & VREPLACE_FLAG)
2054 			&& *ml_get_cursor() != NUL)
2055 		    && (curwin->w_cursor.lnum != Insstart.lnum
2056 			|| ((!has_format_option(FO_INS_LONG)
2057 				|| Insstart_textlen <= (colnr_T)textwidth)
2058 			    && (!fo_ins_blank
2059 				|| Insstart_blank_vcol <= (colnr_T)textwidth
2060 			    ))))))
2061     {
2062 	/* Format with 'formatexpr' when it's set.  Use internal formatting
2063 	 * when 'formatexpr' isn't set or it returns non-zero. */
2064 #if defined(FEAT_EVAL)
2065 	int     do_internal = TRUE;
2066 	colnr_T virtcol = get_nolist_virtcol()
2067 				  + char2cells(c != NUL ? c : gchar_cursor());
2068 
2069 	if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2070 		&& (force_format || virtcol > (colnr_T)textwidth))
2071 	{
2072 	    do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2073 	    /* It may be required to save for undo again, e.g. when setline()
2074 	     * was called. */
2075 	    ins_need_undo = TRUE;
2076 	}
2077 	if (do_internal)
2078 #endif
2079 	    internal_format(textwidth, second_indent, flags, c == NUL, c);
2080     }
2081 
2082     if (c == NUL)	    /* only formatting was wanted */
2083 	return;
2084 
2085     // Check whether this character should end a comment.
2086     if (did_ai && (int)c == end_comment_pending)
2087     {
2088 	char_u  *line;
2089 	char_u	lead_end[COM_MAX_LEN];	    // end-comment string
2090 	int	middle_len, end_len;
2091 	int	i;
2092 
2093 	/*
2094 	 * Need to remove existing (middle) comment leader and insert end
2095 	 * comment leader.  First, check what comment leader we can find.
2096 	 */
2097 	i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
2098 	if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL)	// Just checking
2099 	{
2100 	    // Skip middle-comment string
2101 	    while (*p && p[-1] != ':')	// find end of middle flags
2102 		++p;
2103 	    middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2104 	    // Don't count trailing white space for middle_len
2105 	    while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
2106 		--middle_len;
2107 
2108 	    // Find the end-comment string
2109 	    while (*p && p[-1] != ':')	// find end of end flags
2110 		++p;
2111 	    end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2112 
2113 	    // Skip white space before the cursor
2114 	    i = curwin->w_cursor.col;
2115 	    while (--i >= 0 && VIM_ISWHITE(line[i]))
2116 		;
2117 	    i++;
2118 
2119 	    // Skip to before the middle leader
2120 	    i -= middle_len;
2121 
2122 	    // Check some expected things before we go on
2123 	    if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2124 	    {
2125 		// Backspace over all the stuff we want to replace
2126 		backspace_until_column(i);
2127 
2128 		// Insert the end-comment string, except for the last
2129 		// character, which will get inserted as normal later.
2130 		ins_bytes_len(lead_end, end_len - 1);
2131 	    }
2132 	}
2133     }
2134     end_comment_pending = NUL;
2135 
2136     did_ai = FALSE;
2137 #ifdef FEAT_SMARTINDENT
2138     did_si = FALSE;
2139     can_si = FALSE;
2140     can_si_back = FALSE;
2141 #endif
2142 
2143     /*
2144      * If there's any pending input, grab up to INPUT_BUFLEN at once.
2145      * This speeds up normal text input considerably.
2146      * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2147      * need to re-indent at a ':', or any other character (but not what
2148      * 'paste' is set)..
2149      * Don't do this when there an InsertCharPre autocommand is defined,
2150      * because we need to fire the event for every character.
2151      * Do the check for InsertCharPre before the call to vpeekc() because the
2152      * InsertCharPre autocommand could change the input buffer.
2153      */
2154 #ifdef USE_ON_FLY_SCROLL
2155     dont_scroll = FALSE;		/* allow scrolling here */
2156 #endif
2157 
2158     if (       !ISSPECIAL(c)
2159 	    && (!has_mbyte || (*mb_char2len)(c) == 1)
2160 	    && !has_insertcharpre()
2161 	    && vpeekc() != NUL
2162 	    && !(State & REPLACE_FLAG)
2163 #ifdef FEAT_CINDENT
2164 	    && !cindent_on()
2165 #endif
2166 #ifdef FEAT_RIGHTLEFT
2167 	    && !p_ri
2168 #endif
2169 	   )
2170     {
2171 #define INPUT_BUFLEN 100
2172 	char_u		buf[INPUT_BUFLEN + 1];
2173 	int		i;
2174 	colnr_T		virtcol = 0;
2175 
2176 	buf[0] = c;
2177 	i = 1;
2178 	if (textwidth > 0)
2179 	    virtcol = get_nolist_virtcol();
2180 	/*
2181 	 * Stop the string when:
2182 	 * - no more chars available
2183 	 * - finding a special character (command key)
2184 	 * - buffer is full
2185 	 * - running into the 'textwidth' boundary
2186 	 * - need to check for abbreviation: A non-word char after a word-char
2187 	 */
2188 	while (	   (c = vpeekc()) != NUL
2189 		&& !ISSPECIAL(c)
2190 		&& (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
2191 		&& i < INPUT_BUFLEN
2192 		&& (textwidth == 0
2193 		    || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2194 		&& !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2195 	{
2196 #ifdef FEAT_RIGHTLEFT
2197 	    c = vgetc();
2198 	    if (p_hkmap && KeyTyped)
2199 		c = hkmap(c);		    /* Hebrew mode mapping */
2200 	    buf[i++] = c;
2201 #else
2202 	    buf[i++] = vgetc();
2203 #endif
2204 	}
2205 
2206 #ifdef FEAT_DIGRAPHS
2207 	do_digraph(-1);			/* clear digraphs */
2208 	do_digraph(buf[i-1]);		/* may be the start of a digraph */
2209 #endif
2210 	buf[i] = NUL;
2211 	ins_str(buf);
2212 	if (flags & INSCHAR_CTRLV)
2213 	{
2214 	    redo_literal(*buf);
2215 	    i = 1;
2216 	}
2217 	else
2218 	    i = 0;
2219 	if (buf[i] != NUL)
2220 	    AppendToRedobuffLit(buf + i, -1);
2221     }
2222     else
2223     {
2224 	int		cc;
2225 
2226 	if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2227 	{
2228 	    char_u	buf[MB_MAXBYTES + 1];
2229 
2230 	    (*mb_char2bytes)(c, buf);
2231 	    buf[cc] = NUL;
2232 	    ins_char_bytes(buf, cc);
2233 	    AppendCharToRedobuff(c);
2234 	}
2235 	else
2236 	{
2237 	    ins_char(c);
2238 	    if (flags & INSCHAR_CTRLV)
2239 		redo_literal(c);
2240 	    else
2241 		AppendCharToRedobuff(c);
2242 	}
2243     }
2244 }
2245 
2246 /*
2247  * Format text at the current insert position.
2248  *
2249  * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2250  * will be the comment leader length sent to open_line().
2251  */
2252     static void
2253 internal_format(
2254     int		textwidth,
2255     int		second_indent,
2256     int		flags,
2257     int		format_only,
2258     int		c) /* character to be inserted (can be NUL) */
2259 {
2260     int		cc;
2261     int		save_char = NUL;
2262     int		haveto_redraw = FALSE;
2263     int		fo_ins_blank = has_format_option(FO_INS_BLANK);
2264     int		fo_multibyte = has_format_option(FO_MBYTE_BREAK);
2265     int		fo_white_par = has_format_option(FO_WHITE_PAR);
2266     int		first_line = TRUE;
2267     colnr_T	leader_len;
2268     int		no_leader = FALSE;
2269     int		do_comments = (flags & INSCHAR_DO_COM);
2270 #ifdef FEAT_LINEBREAK
2271     int		has_lbr = curwin->w_p_lbr;
2272 
2273     /* make sure win_lbr_chartabsize() counts correctly */
2274     curwin->w_p_lbr = FALSE;
2275 #endif
2276 
2277     /*
2278      * When 'ai' is off we don't want a space under the cursor to be
2279      * deleted.  Replace it with an 'x' temporarily.
2280      */
2281     if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
2282     {
2283 	cc = gchar_cursor();
2284 	if (VIM_ISWHITE(cc))
2285 	{
2286 	    save_char = cc;
2287 	    pchar_cursor('x');
2288 	}
2289     }
2290 
2291     /*
2292      * Repeat breaking lines, until the current line is not too long.
2293      */
2294     while (!got_int)
2295     {
2296 	int	startcol;		/* Cursor column at entry */
2297 	int	wantcol;		/* column at textwidth border */
2298 	int	foundcol;		/* column for start of spaces */
2299 	int	end_foundcol = 0;	/* column for start of word */
2300 	colnr_T	len;
2301 	colnr_T	virtcol;
2302 	int	orig_col = 0;
2303 	char_u	*saved_text = NULL;
2304 	colnr_T	col;
2305 	colnr_T	end_col;
2306 	int	wcc;			// counter for whitespace chars
2307 
2308 	virtcol = get_nolist_virtcol()
2309 		+ char2cells(c != NUL ? c : gchar_cursor());
2310 	if (virtcol <= (colnr_T)textwidth)
2311 	    break;
2312 
2313 	if (no_leader)
2314 	    do_comments = FALSE;
2315 	else if (!(flags & INSCHAR_FORMAT)
2316 				       && has_format_option(FO_WRAP_COMS))
2317 	    do_comments = TRUE;
2318 
2319 	/* Don't break until after the comment leader */
2320 	if (do_comments)
2321 	    leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
2322 	else
2323 	    leader_len = 0;
2324 
2325 	/* If the line doesn't start with a comment leader, then don't
2326 	 * start one in a following broken line.  Avoids that a %word
2327 	 * moved to the start of the next line causes all following lines
2328 	 * to start with %. */
2329 	if (leader_len == 0)
2330 	    no_leader = TRUE;
2331 	if (!(flags & INSCHAR_FORMAT)
2332 		&& leader_len == 0
2333 		&& !has_format_option(FO_WRAP))
2334 
2335 	    break;
2336 	if ((startcol = curwin->w_cursor.col) == 0)
2337 	    break;
2338 
2339 	/* find column of textwidth border */
2340 	coladvance((colnr_T)textwidth);
2341 	wantcol = curwin->w_cursor.col;
2342 
2343 	curwin->w_cursor.col = startcol;
2344 	foundcol = 0;
2345 
2346 	/*
2347 	 * Find position to break at.
2348 	 * Stop at first entered white when 'formatoptions' has 'v'
2349 	 */
2350 	while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
2351 		    || (flags & INSCHAR_FORMAT)
2352 		    || curwin->w_cursor.lnum != Insstart.lnum
2353 		    || curwin->w_cursor.col >= Insstart.col)
2354 	{
2355 	    if (curwin->w_cursor.col == startcol && c != NUL)
2356 		cc = c;
2357 	    else
2358 		cc = gchar_cursor();
2359 	    if (WHITECHAR(cc))
2360 	    {
2361 		/* remember position of blank just before text */
2362 		end_col = curwin->w_cursor.col;
2363 
2364 		// find start of sequence of blanks
2365 		wcc = 0;
2366 		while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2367 		{
2368 		    dec_cursor();
2369 		    cc = gchar_cursor();
2370 
2371 		    // Increment count of how many whitespace chars in this
2372 		    // group; we only need to know if it's more than one.
2373 		    if (wcc < 2)
2374 		        wcc++;
2375 		}
2376 		if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2377 		    break;		/* only spaces in front of text */
2378 
2379 		// Don't break after a period when 'formatoptions' has 'p' and
2380 		// there are less than two spaces.
2381 		if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2382 		    continue;
2383 
2384 		/* Don't break until after the comment leader */
2385 		if (curwin->w_cursor.col < leader_len)
2386 		    break;
2387 		if (has_format_option(FO_ONE_LETTER))
2388 		{
2389 		    /* do not break after one-letter words */
2390 		    if (curwin->w_cursor.col == 0)
2391 			break;	/* one-letter word at begin */
2392 		    /* do not break "#a b" when 'tw' is 2 */
2393 		    if (curwin->w_cursor.col <= leader_len)
2394 			break;
2395 		    col = curwin->w_cursor.col;
2396 		    dec_cursor();
2397 		    cc = gchar_cursor();
2398 
2399 		    if (WHITECHAR(cc))
2400 			continue;	/* one-letter, continue */
2401 		    curwin->w_cursor.col = col;
2402 		}
2403 
2404 		inc_cursor();
2405 
2406 		end_foundcol = end_col + 1;
2407 		foundcol = curwin->w_cursor.col;
2408 		if (curwin->w_cursor.col <= (colnr_T)wantcol)
2409 		    break;
2410 	    }
2411 	    else if (cc >= 0x100 && fo_multibyte)
2412 	    {
2413 		/* Break after or before a multi-byte character. */
2414 		if (curwin->w_cursor.col != startcol)
2415 		{
2416 		    /* Don't break until after the comment leader */
2417 		    if (curwin->w_cursor.col < leader_len)
2418 			break;
2419 		    col = curwin->w_cursor.col;
2420 		    inc_cursor();
2421 		    /* Don't change end_foundcol if already set. */
2422 		    if (foundcol != curwin->w_cursor.col)
2423 		    {
2424 			foundcol = curwin->w_cursor.col;
2425 			end_foundcol = foundcol;
2426 			if (curwin->w_cursor.col <= (colnr_T)wantcol)
2427 			    break;
2428 		    }
2429 		    curwin->w_cursor.col = col;
2430 		}
2431 
2432 		if (curwin->w_cursor.col == 0)
2433 		    break;
2434 
2435 		col = curwin->w_cursor.col;
2436 
2437 		dec_cursor();
2438 		cc = gchar_cursor();
2439 
2440 		if (WHITECHAR(cc))
2441 		    continue;		/* break with space */
2442 		/* Don't break until after the comment leader */
2443 		if (curwin->w_cursor.col < leader_len)
2444 		    break;
2445 
2446 		curwin->w_cursor.col = col;
2447 
2448 		foundcol = curwin->w_cursor.col;
2449 		end_foundcol = foundcol;
2450 		if (curwin->w_cursor.col <= (colnr_T)wantcol)
2451 		    break;
2452 	    }
2453 	    if (curwin->w_cursor.col == 0)
2454 		break;
2455 	    dec_cursor();
2456 	}
2457 
2458 	if (foundcol == 0)		/* no spaces, cannot break line */
2459 	{
2460 	    curwin->w_cursor.col = startcol;
2461 	    break;
2462 	}
2463 
2464 	/* Going to break the line, remove any "$" now. */
2465 	undisplay_dollar();
2466 
2467 	/*
2468 	 * Offset between cursor position and line break is used by replace
2469 	 * stack functions.  VREPLACE does not use this, and backspaces
2470 	 * over the text instead.
2471 	 */
2472 	if (State & VREPLACE_FLAG)
2473 	    orig_col = startcol;	/* Will start backspacing from here */
2474 	else
2475 	    replace_offset = startcol - end_foundcol;
2476 
2477 	/*
2478 	 * adjust startcol for spaces that will be deleted and
2479 	 * characters that will remain on top line
2480 	 */
2481 	curwin->w_cursor.col = foundcol;
2482 	while ((cc = gchar_cursor(), WHITECHAR(cc))
2483 		    && (!fo_white_par || curwin->w_cursor.col < startcol))
2484 	    inc_cursor();
2485 	startcol -= curwin->w_cursor.col;
2486 	if (startcol < 0)
2487 	    startcol = 0;
2488 
2489 	if (State & VREPLACE_FLAG)
2490 	{
2491 	    /*
2492 	     * In VREPLACE mode, we will backspace over the text to be
2493 	     * wrapped, so save a copy now to put on the next line.
2494 	     */
2495 	    saved_text = vim_strsave(ml_get_cursor());
2496 	    curwin->w_cursor.col = orig_col;
2497 	    if (saved_text == NULL)
2498 		break;	/* Can't do it, out of memory */
2499 	    saved_text[startcol] = NUL;
2500 
2501 	    /* Backspace over characters that will move to the next line */
2502 	    if (!fo_white_par)
2503 		backspace_until_column(foundcol);
2504 	}
2505 	else
2506 	{
2507 	    /* put cursor after pos. to break line */
2508 	    if (!fo_white_par)
2509 		curwin->w_cursor.col = foundcol;
2510 	}
2511 
2512 	/*
2513 	 * Split the line just before the margin.
2514 	 * Only insert/delete lines, but don't really redraw the window.
2515 	 */
2516 	open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2517 		+ (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
2518 		+ (do_comments ? OPENLINE_DO_COM : 0)
2519 		+ ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
2520 		, ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2521 	if (!(flags & INSCHAR_COM_LIST))
2522 	    old_indent = 0;
2523 
2524 	replace_offset = 0;
2525 	if (first_line)
2526 	{
2527 	    if (!(flags & INSCHAR_COM_LIST))
2528 	    {
2529 		/*
2530 		 * This section is for auto-wrap of numeric lists.  When not
2531 		 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2532 		 * flag will be set and open_line() will handle it (as seen
2533 		 * above).  The code here (and in get_number_indent()) will
2534 		 * recognize comments if needed...
2535 		 */
2536 		if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
2537 		    second_indent =
2538 				 get_number_indent(curwin->w_cursor.lnum - 1);
2539 		if (second_indent >= 0)
2540 		{
2541 		    if (State & VREPLACE_FLAG)
2542 			change_indent(INDENT_SET, second_indent,
2543 							    FALSE, NUL, TRUE);
2544 		    else
2545 			if (leader_len > 0 && second_indent - leader_len > 0)
2546 		    {
2547 			int i;
2548 			int padding = second_indent - leader_len;
2549 
2550 			/* We started at the first_line of a numbered list
2551 			 * that has a comment.  the open_line() function has
2552 			 * inserted the proper comment leader and positioned
2553 			 * the cursor at the end of the split line.  Now we
2554 			 * add the additional whitespace needed after the
2555 			 * comment leader for the numbered list.  */
2556 			for (i = 0; i < padding; i++)
2557 			    ins_str((char_u *)" ");
2558 		    }
2559 		    else
2560 		    {
2561 			(void)set_indent(second_indent, SIN_CHANGED);
2562 		    }
2563 		}
2564 	    }
2565 	    first_line = FALSE;
2566 	}
2567 
2568 	if (State & VREPLACE_FLAG)
2569 	{
2570 	    /*
2571 	     * In VREPLACE mode we have backspaced over the text to be
2572 	     * moved, now we re-insert it into the new line.
2573 	     */
2574 	    ins_bytes(saved_text);
2575 	    vim_free(saved_text);
2576 	}
2577 	else
2578 	{
2579 	    /*
2580 	     * Check if cursor is not past the NUL off the line, cindent
2581 	     * may have added or removed indent.
2582 	     */
2583 	    curwin->w_cursor.col += startcol;
2584 	    len = (colnr_T)STRLEN(ml_get_curline());
2585 	    if (curwin->w_cursor.col > len)
2586 		curwin->w_cursor.col = len;
2587 	}
2588 
2589 	haveto_redraw = TRUE;
2590 #ifdef FEAT_CINDENT
2591 	can_cindent = TRUE;
2592 #endif
2593 	/* moved the cursor, don't autoindent or cindent now */
2594 	did_ai = FALSE;
2595 #ifdef FEAT_SMARTINDENT
2596 	did_si = FALSE;
2597 	can_si = FALSE;
2598 	can_si_back = FALSE;
2599 #endif
2600 	line_breakcheck();
2601     }
2602 
2603     if (save_char != NUL)		/* put back space after cursor */
2604 	pchar_cursor(save_char);
2605 
2606 #ifdef FEAT_LINEBREAK
2607     curwin->w_p_lbr = has_lbr;
2608 #endif
2609     if (!format_only && haveto_redraw)
2610     {
2611 	update_topline();
2612 	redraw_curbuf_later(VALID);
2613     }
2614 }
2615 
2616 /*
2617  * Called after inserting or deleting text: When 'formatoptions' includes the
2618  * 'a' flag format from the current line until the end of the paragraph.
2619  * Keep the cursor at the same position relative to the text.
2620  * The caller must have saved the cursor line for undo, following ones will be
2621  * saved here.
2622  */
2623     void
2624 auto_format(
2625     int		trailblank,	/* when TRUE also format with trailing blank */
2626     int		prev_line)	/* may start in previous line */
2627 {
2628     pos_T	pos;
2629     colnr_T	len;
2630     char_u	*old;
2631     char_u	*new, *pnew;
2632     int		wasatend;
2633     int		cc;
2634 
2635     if (!has_format_option(FO_AUTO))
2636 	return;
2637 
2638     pos = curwin->w_cursor;
2639     old = ml_get_curline();
2640 
2641     /* may remove added space */
2642     check_auto_format(FALSE);
2643 
2644     /* Don't format in Insert mode when the cursor is on a trailing blank, the
2645      * user might insert normal text next.  Also skip formatting when "1" is
2646      * in 'formatoptions' and there is a single character before the cursor.
2647      * Otherwise the line would be broken and when typing another non-white
2648      * next they are not joined back together. */
2649     wasatend = (pos.col == (colnr_T)STRLEN(old));
2650     if (*old != NUL && !trailblank && wasatend)
2651     {
2652 	dec_cursor();
2653 	cc = gchar_cursor();
2654 	if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2655 					  && has_format_option(FO_ONE_LETTER))
2656 	    dec_cursor();
2657 	cc = gchar_cursor();
2658 	if (WHITECHAR(cc))
2659 	{
2660 	    curwin->w_cursor = pos;
2661 	    return;
2662 	}
2663 	curwin->w_cursor = pos;
2664     }
2665 
2666     /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2667      * comments. */
2668     if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
2669 				&& get_leader_len(old, NULL, FALSE, TRUE) == 0)
2670 	return;
2671 
2672     /*
2673      * May start formatting in a previous line, so that after "x" a word is
2674      * moved to the previous line if it fits there now.  Only when this is not
2675      * the start of a paragraph.
2676      */
2677     if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2678     {
2679 	--curwin->w_cursor.lnum;
2680 	if (u_save_cursor() == FAIL)
2681 	    return;
2682     }
2683 
2684     /*
2685      * Do the formatting and restore the cursor position.  "saved_cursor" will
2686      * be adjusted for the text formatting.
2687      */
2688     saved_cursor = pos;
2689     format_lines((linenr_T)-1, FALSE);
2690     curwin->w_cursor = saved_cursor;
2691     saved_cursor.lnum = 0;
2692 
2693     if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2694     {
2695 	/* "cannot happen" */
2696 	curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2697 	coladvance((colnr_T)MAXCOL);
2698     }
2699     else
2700 	check_cursor_col();
2701 
2702     /* Insert mode: If the cursor is now after the end of the line while it
2703      * previously wasn't, the line was broken.  Because of the rule above we
2704      * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2705      * formatted. */
2706     if (!wasatend && has_format_option(FO_WHITE_PAR))
2707     {
2708 	new = ml_get_curline();
2709 	len = (colnr_T)STRLEN(new);
2710 	if (curwin->w_cursor.col == len)
2711 	{
2712 	    pnew = vim_strnsave(new, len + 2);
2713 	    pnew[len] = ' ';
2714 	    pnew[len + 1] = NUL;
2715 	    ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2716 	    /* remove the space later */
2717 	    did_add_space = TRUE;
2718 	}
2719 	else
2720 	    /* may remove added space */
2721 	    check_auto_format(FALSE);
2722     }
2723 
2724     check_cursor();
2725 }
2726 
2727 /*
2728  * When an extra space was added to continue a paragraph for auto-formatting,
2729  * delete it now.  The space must be under the cursor, just after the insert
2730  * position.
2731  */
2732     static void
2733 check_auto_format(
2734     int		end_insert)	    /* TRUE when ending Insert mode */
2735 {
2736     int		c = ' ';
2737     int		cc;
2738 
2739     if (did_add_space)
2740     {
2741 	cc = gchar_cursor();
2742 	if (!WHITECHAR(cc))
2743 	    /* Somehow the space was removed already. */
2744 	    did_add_space = FALSE;
2745 	else
2746 	{
2747 	    if (!end_insert)
2748 	    {
2749 		inc_cursor();
2750 		c = gchar_cursor();
2751 		dec_cursor();
2752 	    }
2753 	    if (c != NUL)
2754 	    {
2755 		/* The space is no longer at the end of the line, delete it. */
2756 		del_char(FALSE);
2757 		did_add_space = FALSE;
2758 	    }
2759 	}
2760     }
2761 }
2762 
2763 /*
2764  * Find out textwidth to be used for formatting:
2765  *	if 'textwidth' option is set, use it
2766  *	else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
2767  *	if invalid value, use 0.
2768  *	Set default to window width (maximum 79) for "gq" operator.
2769  */
2770     int
2771 comp_textwidth(
2772     int		ff)	/* force formatting (for "gq" command) */
2773 {
2774     int		textwidth;
2775 
2776     textwidth = curbuf->b_p_tw;
2777     if (textwidth == 0 && curbuf->b_p_wm)
2778     {
2779 	/* The width is the window width minus 'wrapmargin' minus all the
2780 	 * things that add to the margin. */
2781 	textwidth = curwin->w_width - curbuf->b_p_wm;
2782 #ifdef FEAT_CMDWIN
2783 	if (cmdwin_type != 0)
2784 	    textwidth -= 1;
2785 #endif
2786 #ifdef FEAT_FOLDING
2787 	textwidth -= curwin->w_p_fdc;
2788 #endif
2789 #ifdef FEAT_SIGNS
2790 	if (signcolumn_on(curwin))
2791 	    textwidth -= 1;
2792 #endif
2793 	if (curwin->w_p_nu || curwin->w_p_rnu)
2794 	    textwidth -= 8;
2795     }
2796     if (textwidth < 0)
2797 	textwidth = 0;
2798     if (ff && textwidth == 0)
2799     {
2800 	textwidth = curwin->w_width - 1;
2801 	if (textwidth > 79)
2802 	    textwidth = 79;
2803     }
2804     return textwidth;
2805 }
2806 
2807 /*
2808  * Put a character in the redo buffer, for when just after a CTRL-V.
2809  */
2810     static void
2811 redo_literal(int c)
2812 {
2813     char_u	buf[10];
2814 
2815     /* Only digits need special treatment.  Translate them into a string of
2816      * three digits. */
2817     if (VIM_ISDIGIT(c))
2818     {
2819 	vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
2820 	AppendToRedobuff(buf);
2821     }
2822     else
2823 	AppendCharToRedobuff(c);
2824 }
2825 
2826 /*
2827  * start_arrow() is called when an arrow key is used in insert mode.
2828  * For undo/redo it resembles hitting the <ESC> key.
2829  */
2830     void
2831 start_arrow(
2832     pos_T    *end_insert_pos)		/* can be NULL */
2833 {
2834     start_arrow_common(end_insert_pos, TRUE);
2835 }
2836 
2837 /*
2838  * Like start_arrow() but with end_change argument.
2839  * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2840  */
2841     static void
2842 start_arrow_with_change(
2843     pos_T    *end_insert_pos,		/* can be NULL */
2844     int	      end_change)		/* end undoable change */
2845 {
2846     start_arrow_common(end_insert_pos, end_change);
2847     if (!end_change)
2848     {
2849 	AppendCharToRedobuff(Ctrl_G);
2850 	AppendCharToRedobuff('U');
2851     }
2852 }
2853 
2854     static void
2855 start_arrow_common(
2856     pos_T    *end_insert_pos,		/* can be NULL */
2857     int	      end_change)		/* end undoable change */
2858 {
2859     if (!arrow_used && end_change)	/* something has been inserted */
2860     {
2861 	AppendToRedobuff(ESC_STR);
2862 	stop_insert(end_insert_pos, FALSE, FALSE);
2863 	arrow_used = TRUE;	/* this means we stopped the current insert */
2864     }
2865 #ifdef FEAT_SPELL
2866     check_spell_redraw();
2867 #endif
2868 }
2869 
2870 #ifdef FEAT_SPELL
2871 /*
2872  * If we skipped highlighting word at cursor, do it now.
2873  * It may be skipped again, thus reset spell_redraw_lnum first.
2874  */
2875     static void
2876 check_spell_redraw(void)
2877 {
2878     if (spell_redraw_lnum != 0)
2879     {
2880 	linenr_T	lnum = spell_redraw_lnum;
2881 
2882 	spell_redraw_lnum = 0;
2883 	redrawWinline(curwin, lnum);
2884     }
2885 }
2886 
2887 #endif
2888 
2889 /*
2890  * stop_arrow() is called before a change is made in insert mode.
2891  * If an arrow key has been used, start a new insertion.
2892  * Returns FAIL if undo is impossible, shouldn't insert then.
2893  */
2894     int
2895 stop_arrow(void)
2896 {
2897     if (arrow_used)
2898     {
2899 	Insstart = curwin->w_cursor;	/* new insertion starts here */
2900 	if (Insstart.col > Insstart_orig.col && !ins_need_undo)
2901 	    /* Don't update the original insert position when moved to the
2902 	     * right, except when nothing was inserted yet. */
2903 	    update_Insstart_orig = FALSE;
2904 	Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2905 
2906 	if (u_save_cursor() == OK)
2907 	{
2908 	    arrow_used = FALSE;
2909 	    ins_need_undo = FALSE;
2910 	}
2911 
2912 	ai_col = 0;
2913 	if (State & VREPLACE_FLAG)
2914 	{
2915 	    orig_line_count = curbuf->b_ml.ml_line_count;
2916 	    vr_lines_changed = 1;
2917 	}
2918 	ResetRedobuff();
2919 	AppendToRedobuff((char_u *)"1i");   /* pretend we start an insertion */
2920 	new_insert_skip = 2;
2921     }
2922     else if (ins_need_undo)
2923     {
2924 	if (u_save_cursor() == OK)
2925 	    ins_need_undo = FALSE;
2926     }
2927 
2928 #ifdef FEAT_FOLDING
2929     /* Always open fold at the cursor line when inserting something. */
2930     foldOpenCursor();
2931 #endif
2932 
2933     return (arrow_used || ins_need_undo ? FAIL : OK);
2934 }
2935 
2936 /*
2937  * Do a few things to stop inserting.
2938  * "end_insert_pos" is where insert ended.  It is NULL when we already jumped
2939  * to another window/buffer.
2940  */
2941     static void
2942 stop_insert(
2943     pos_T	*end_insert_pos,
2944     int		esc,			/* called by ins_esc() */
2945     int		nomove)			/* <c-\><c-o>, don't move cursor */
2946 {
2947     int		cc;
2948     char_u	*ptr;
2949 
2950     stop_redo_ins();
2951     replace_flush();		/* abandon replace stack */
2952 
2953     /*
2954      * Save the inserted text for later redo with ^@ and CTRL-A.
2955      * Don't do it when "restart_edit" was set and nothing was inserted,
2956      * otherwise CTRL-O w and then <Left> will clear "last_insert".
2957      */
2958     ptr = get_inserted();
2959     if (did_restart_edit == 0 || (ptr != NULL
2960 				       && (int)STRLEN(ptr) > new_insert_skip))
2961     {
2962 	vim_free(last_insert);
2963 	last_insert = ptr;
2964 	last_insert_skip = new_insert_skip;
2965     }
2966     else
2967 	vim_free(ptr);
2968 
2969     if (!arrow_used && end_insert_pos != NULL)
2970     {
2971 	/* Auto-format now.  It may seem strange to do this when stopping an
2972 	 * insertion (or moving the cursor), but it's required when appending
2973 	 * a line and having it end in a space.  But only do it when something
2974 	 * was actually inserted, otherwise undo won't work. */
2975 	if (!ins_need_undo && has_format_option(FO_AUTO))
2976 	{
2977 	    pos_T   tpos = curwin->w_cursor;
2978 
2979 	    /* When the cursor is at the end of the line after a space the
2980 	     * formatting will move it to the following word.  Avoid that by
2981 	     * moving the cursor onto the space. */
2982 	    cc = 'x';
2983 	    if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2984 	    {
2985 		dec_cursor();
2986 		cc = gchar_cursor();
2987 		if (!VIM_ISWHITE(cc))
2988 		    curwin->w_cursor = tpos;
2989 	    }
2990 
2991 	    auto_format(TRUE, FALSE);
2992 
2993 	    if (VIM_ISWHITE(cc))
2994 	    {
2995 		if (gchar_cursor() != NUL)
2996 		    inc_cursor();
2997 		// If the cursor is still at the same character, also keep
2998 		// the "coladd".
2999 		if (gchar_cursor() == NUL
3000 			&& curwin->w_cursor.lnum == tpos.lnum
3001 			&& curwin->w_cursor.col == tpos.col)
3002 		    curwin->w_cursor.coladd = tpos.coladd;
3003 	    }
3004 	}
3005 
3006 	/* If a space was inserted for auto-formatting, remove it now. */
3007 	check_auto_format(TRUE);
3008 
3009 	/* If we just did an auto-indent, remove the white space from the end
3010 	 * of the line, and put the cursor back.
3011 	 * Do this when ESC was used or moving the cursor up/down.
3012 	 * Check for the old position still being valid, just in case the text
3013 	 * got changed unexpectedly. */
3014 	if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
3015 			&& curwin->w_cursor.lnum != end_insert_pos->lnum))
3016 		&& end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
3017 	{
3018 	    pos_T	tpos = curwin->w_cursor;
3019 
3020 	    curwin->w_cursor = *end_insert_pos;
3021 	    check_cursor_col();  /* make sure it is not past the line */
3022 	    for (;;)
3023 	    {
3024 		if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3025 		    --curwin->w_cursor.col;
3026 		cc = gchar_cursor();
3027 		if (!VIM_ISWHITE(cc))
3028 		    break;
3029 		if (del_char(TRUE) == FAIL)
3030 		    break;  /* should not happen */
3031 	    }
3032 	    if (curwin->w_cursor.lnum != tpos.lnum)
3033 		curwin->w_cursor = tpos;
3034 	    else
3035 	    {
3036 		/* reset tpos, could have been invalidated in the loop above */
3037 		tpos = curwin->w_cursor;
3038 		tpos.col++;
3039 		if (cc != NUL && gchar_pos(&tpos) == NUL)
3040 		    ++curwin->w_cursor.col;	/* put cursor back on the NUL */
3041 	    }
3042 
3043 	    /* <C-S-Right> may have started Visual mode, adjust the position for
3044 	     * deleted characters. */
3045 	    if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3046 	    {
3047 		int len = (int)STRLEN(ml_get_curline());
3048 
3049 		if (VIsual.col > len)
3050 		{
3051 		    VIsual.col = len;
3052 		    VIsual.coladd = 0;
3053 		}
3054 	    }
3055 	}
3056     }
3057     did_ai = FALSE;
3058 #ifdef FEAT_SMARTINDENT
3059     did_si = FALSE;
3060     can_si = FALSE;
3061     can_si_back = FALSE;
3062 #endif
3063 
3064     /* Set '[ and '] to the inserted text.  When end_insert_pos is NULL we are
3065      * now in a different buffer. */
3066     if (end_insert_pos != NULL)
3067     {
3068 	curbuf->b_op_start = Insstart;
3069 	curbuf->b_op_start_orig = Insstart_orig;
3070 	curbuf->b_op_end = *end_insert_pos;
3071     }
3072 }
3073 
3074 /*
3075  * Set the last inserted text to a single character.
3076  * Used for the replace command.
3077  */
3078     void
3079 set_last_insert(int c)
3080 {
3081     char_u	*s;
3082 
3083     vim_free(last_insert);
3084     last_insert = alloc(MB_MAXBYTES * 3 + 5);
3085     if (last_insert != NULL)
3086     {
3087 	s = last_insert;
3088 	/* Use the CTRL-V only when entering a special char */
3089 	if (c < ' ' || c == DEL)
3090 	    *s++ = Ctrl_V;
3091 	s = add_char2buf(c, s);
3092 	*s++ = ESC;
3093 	*s++ = NUL;
3094 	last_insert_skip = 0;
3095     }
3096 }
3097 
3098 #if defined(EXITFREE) || defined(PROTO)
3099     void
3100 free_last_insert(void)
3101 {
3102     VIM_CLEAR(last_insert);
3103 }
3104 #endif
3105 
3106 /*
3107  * Add character "c" to buffer "s".  Escape the special meaning of K_SPECIAL
3108  * and CSI.  Handle multi-byte characters.
3109  * Returns a pointer to after the added bytes.
3110  */
3111     char_u *
3112 add_char2buf(int c, char_u *s)
3113 {
3114     char_u	temp[MB_MAXBYTES + 1];
3115     int		i;
3116     int		len;
3117 
3118     len = (*mb_char2bytes)(c, temp);
3119     for (i = 0; i < len; ++i)
3120     {
3121 	c = temp[i];
3122 	/* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3123 	if (c == K_SPECIAL)
3124 	{
3125 	    *s++ = K_SPECIAL;
3126 	    *s++ = KS_SPECIAL;
3127 	    *s++ = KE_FILLER;
3128 	}
3129 #ifdef FEAT_GUI
3130 	else if (c == CSI)
3131 	{
3132 	    *s++ = CSI;
3133 	    *s++ = KS_EXTRA;
3134 	    *s++ = (int)KE_CSI;
3135 	}
3136 #endif
3137 	else
3138 	    *s++ = c;
3139     }
3140     return s;
3141 }
3142 
3143 /*
3144  * move cursor to start of line
3145  * if flags & BL_WHITE	move to first non-white
3146  * if flags & BL_SOL	move to first non-white if startofline is set,
3147  *			    otherwise keep "curswant" column
3148  * if flags & BL_FIX	don't leave the cursor on a NUL.
3149  */
3150     void
3151 beginline(int flags)
3152 {
3153     if ((flags & BL_SOL) && !p_sol)
3154 	coladvance(curwin->w_curswant);
3155     else
3156     {
3157 	curwin->w_cursor.col = 0;
3158 	curwin->w_cursor.coladd = 0;
3159 
3160 	if (flags & (BL_WHITE | BL_SOL))
3161 	{
3162 	    char_u  *ptr;
3163 
3164 	    for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
3165 			       && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3166 		++curwin->w_cursor.col;
3167 	}
3168 	curwin->w_set_curswant = TRUE;
3169     }
3170 }
3171 
3172 /*
3173  * oneright oneleft cursor_down cursor_up
3174  *
3175  * Move one char {right,left,down,up}.
3176  * Doesn't move onto the NUL past the end of the line, unless it is allowed.
3177  * Return OK when successful, FAIL when we hit a line of file boundary.
3178  */
3179 
3180     int
3181 oneright(void)
3182 {
3183     char_u	*ptr;
3184     int		l;
3185 
3186     if (virtual_active())
3187     {
3188 	pos_T	prevpos = curwin->w_cursor;
3189 
3190 	/* Adjust for multi-wide char (excluding TAB) */
3191 	ptr = ml_get_cursor();
3192 	coladvance(getviscol() + ((*ptr != TAB
3193 					  && vim_isprintc((*mb_ptr2char)(ptr)))
3194 		    ? ptr2cells(ptr) : 1));
3195 	curwin->w_set_curswant = TRUE;
3196 	/* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3197 	return (prevpos.col != curwin->w_cursor.col
3198 		    || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3199     }
3200 
3201     ptr = ml_get_cursor();
3202     if (*ptr == NUL)
3203 	return FAIL;	    /* already at the very end */
3204 
3205     if (has_mbyte)
3206 	l = (*mb_ptr2len)(ptr);
3207     else
3208 	l = 1;
3209 
3210     /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3211      * contains "onemore". */
3212     if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
3213 	return FAIL;
3214     curwin->w_cursor.col += l;
3215 
3216     curwin->w_set_curswant = TRUE;
3217     return OK;
3218 }
3219 
3220     int
3221 oneleft(void)
3222 {
3223     if (virtual_active())
3224     {
3225 #ifdef FEAT_LINEBREAK
3226 	int width;
3227 #endif
3228 	int v = getviscol();
3229 
3230 	if (v == 0)
3231 	    return FAIL;
3232 
3233 #ifdef FEAT_LINEBREAK
3234 	/* We might get stuck on 'showbreak', skip over it. */
3235 	width = 1;
3236 	for (;;)
3237 	{
3238 	    coladvance(v - width);
3239 	    /* getviscol() is slow, skip it when 'showbreak' is empty,
3240 	     * 'breakindent' is not set and there are no multi-byte
3241 	     * characters */
3242 	    if ((*p_sbr == NUL && !curwin->w_p_bri
3243 					     && !has_mbyte) || getviscol() < v)
3244 		break;
3245 	    ++width;
3246 	}
3247 #else
3248 	coladvance(v - 1);
3249 #endif
3250 
3251 	if (curwin->w_cursor.coladd == 1)
3252 	{
3253 	    char_u *ptr;
3254 
3255 	    /* Adjust for multi-wide char (not a TAB) */
3256 	    ptr = ml_get_cursor();
3257 	    if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3258 							 && ptr2cells(ptr) > 1)
3259 		curwin->w_cursor.coladd = 0;
3260 	}
3261 
3262 	curwin->w_set_curswant = TRUE;
3263 	return OK;
3264     }
3265 
3266     if (curwin->w_cursor.col == 0)
3267 	return FAIL;
3268 
3269     curwin->w_set_curswant = TRUE;
3270     --curwin->w_cursor.col;
3271 
3272     /* if the character on the left of the current cursor is a multi-byte
3273      * character, move to its first byte */
3274     if (has_mbyte)
3275 	mb_adjust_cursor();
3276     return OK;
3277 }
3278 
3279     int
3280 cursor_up(
3281     long	n,
3282     int		upd_topline)	    /* When TRUE: update topline */
3283 {
3284     linenr_T	lnum;
3285 
3286     if (n > 0)
3287     {
3288 	lnum = curwin->w_cursor.lnum;
3289 	/* This fails if the cursor is already in the first line or the count
3290 	 * is larger than the line number and '-' is in 'cpoptions' */
3291 	if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
3292 	    return FAIL;
3293 	if (n >= lnum)
3294 	    lnum = 1;
3295 	else
3296 #ifdef FEAT_FOLDING
3297 	    if (hasAnyFolding(curwin))
3298 	{
3299 	    /*
3300 	     * Count each sequence of folded lines as one logical line.
3301 	     */
3302 	    /* go to the start of the current fold */
3303 	    (void)hasFolding(lnum, &lnum, NULL);
3304 
3305 	    while (n--)
3306 	    {
3307 		/* move up one line */
3308 		--lnum;
3309 		if (lnum <= 1)
3310 		    break;
3311 		/* If we entered a fold, move to the beginning, unless in
3312 		 * Insert mode or when 'foldopen' contains "all": it will open
3313 		 * in a moment. */
3314 		if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3315 		    (void)hasFolding(lnum, &lnum, NULL);
3316 	    }
3317 	    if (lnum < 1)
3318 		lnum = 1;
3319 	}
3320 	else
3321 #endif
3322 	    lnum -= n;
3323 	curwin->w_cursor.lnum = lnum;
3324     }
3325 
3326     /* try to advance to the column we want to be at */
3327     coladvance(curwin->w_curswant);
3328 
3329     if (upd_topline)
3330 	update_topline();	/* make sure curwin->w_topline is valid */
3331 
3332     return OK;
3333 }
3334 
3335 /*
3336  * Cursor down a number of logical lines.
3337  */
3338     int
3339 cursor_down(
3340     long	n,
3341     int		upd_topline)	    /* When TRUE: update topline */
3342 {
3343     linenr_T	lnum;
3344 
3345     if (n > 0)
3346     {
3347 	lnum = curwin->w_cursor.lnum;
3348 #ifdef FEAT_FOLDING
3349 	/* Move to last line of fold, will fail if it's the end-of-file. */
3350 	(void)hasFolding(lnum, NULL, &lnum);
3351 #endif
3352 	/* This fails if the cursor is already in the last line or would move
3353 	 * beyond the last line and '-' is in 'cpoptions' */
3354 	if (lnum >= curbuf->b_ml.ml_line_count
3355 		|| (lnum + n > curbuf->b_ml.ml_line_count
3356 		    && vim_strchr(p_cpo, CPO_MINUS) != NULL))
3357 	    return FAIL;
3358 	if (lnum + n >= curbuf->b_ml.ml_line_count)
3359 	    lnum = curbuf->b_ml.ml_line_count;
3360 	else
3361 #ifdef FEAT_FOLDING
3362 	if (hasAnyFolding(curwin))
3363 	{
3364 	    linenr_T	last;
3365 
3366 	    /* count each sequence of folded lines as one logical line */
3367 	    while (n--)
3368 	    {
3369 		if (hasFolding(lnum, NULL, &last))
3370 		    lnum = last + 1;
3371 		else
3372 		    ++lnum;
3373 		if (lnum >= curbuf->b_ml.ml_line_count)
3374 		    break;
3375 	    }
3376 	    if (lnum > curbuf->b_ml.ml_line_count)
3377 		lnum = curbuf->b_ml.ml_line_count;
3378 	}
3379 	else
3380 #endif
3381 	    lnum += n;
3382 	curwin->w_cursor.lnum = lnum;
3383     }
3384 
3385     /* try to advance to the column we want to be at */
3386     coladvance(curwin->w_curswant);
3387 
3388     if (upd_topline)
3389 	update_topline();	/* make sure curwin->w_topline is valid */
3390 
3391     return OK;
3392 }
3393 
3394 /*
3395  * Stuff the last inserted text in the read buffer.
3396  * Last_insert actually is a copy of the redo buffer, so we
3397  * first have to remove the command.
3398  */
3399     int
3400 stuff_inserted(
3401     int	    c,		/* Command character to be inserted */
3402     long    count,	/* Repeat this many times */
3403     int	    no_esc)	/* Don't add an ESC at the end */
3404 {
3405     char_u	*esc_ptr;
3406     char_u	*ptr;
3407     char_u	*last_ptr;
3408     char_u	last = NUL;
3409 
3410     ptr = get_last_insert();
3411     if (ptr == NULL)
3412     {
3413 	emsg(_(e_noinstext));
3414 	return FAIL;
3415     }
3416 
3417     /* may want to stuff the command character, to start Insert mode */
3418     if (c != NUL)
3419 	stuffcharReadbuff(c);
3420     if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3421 	*esc_ptr = NUL;	    /* remove the ESC */
3422 
3423     /* when the last char is either "0" or "^" it will be quoted if no ESC
3424      * comes after it OR if it will inserted more than once and "ptr"
3425      * starts with ^D.	-- Acevedo
3426      */
3427     last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3428     if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3429 	    && (no_esc || (*ptr == Ctrl_D && count > 1)))
3430     {
3431 	last = *last_ptr;
3432 	*last_ptr = NUL;
3433     }
3434 
3435     do
3436     {
3437 	stuffReadbuff(ptr);
3438 	/* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3439 	if (last)
3440 	    stuffReadbuff((char_u *)(last == '0'
3441 			? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3442 			: IF_EB("\026^", CTRL_V_STR "^")));
3443     }
3444     while (--count > 0);
3445 
3446     if (last)
3447 	*last_ptr = last;
3448 
3449     if (esc_ptr != NULL)
3450 	*esc_ptr = ESC;	    /* put the ESC back */
3451 
3452     /* may want to stuff a trailing ESC, to get out of Insert mode */
3453     if (!no_esc)
3454 	stuffcharReadbuff(ESC);
3455 
3456     return OK;
3457 }
3458 
3459     char_u *
3460 get_last_insert(void)
3461 {
3462     if (last_insert == NULL)
3463 	return NULL;
3464     return last_insert + last_insert_skip;
3465 }
3466 
3467 /*
3468  * Get last inserted string, and remove trailing <Esc>.
3469  * Returns pointer to allocated memory (must be freed) or NULL.
3470  */
3471     char_u *
3472 get_last_insert_save(void)
3473 {
3474     char_u	*s;
3475     int		len;
3476 
3477     if (last_insert == NULL)
3478 	return NULL;
3479     s = vim_strsave(last_insert + last_insert_skip);
3480     if (s != NULL)
3481     {
3482 	len = (int)STRLEN(s);
3483 	if (len > 0 && s[len - 1] == ESC)	/* remove trailing ESC */
3484 	    s[len - 1] = NUL;
3485     }
3486     return s;
3487 }
3488 
3489 /*
3490  * Check the word in front of the cursor for an abbreviation.
3491  * Called when the non-id character "c" has been entered.
3492  * When an abbreviation is recognized it is removed from the text and
3493  * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3494  */
3495     static int
3496 echeck_abbr(int c)
3497 {
3498     /* Don't check for abbreviation in paste mode, when disabled and just
3499      * after moving around with cursor keys. */
3500     if (p_paste || no_abbr || arrow_used)
3501 	return FALSE;
3502 
3503     return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3504 		curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3505 }
3506 
3507 /*
3508  * replace-stack functions
3509  *
3510  * When replacing characters, the replaced characters are remembered for each
3511  * new character.  This is used to re-insert the old text when backspacing.
3512  *
3513  * There is a NUL headed list of characters for each character that is
3514  * currently in the file after the insertion point.  When BS is used, one NUL
3515  * headed list is put back for the deleted character.
3516  *
3517  * For a newline, there are two NUL headed lists.  One contains the characters
3518  * that the NL replaced.  The extra one stores the characters after the cursor
3519  * that were deleted (always white space).
3520  *
3521  * Replace_offset is normally 0, in which case replace_push will add a new
3522  * character at the end of the stack.  If replace_offset is not 0, that many
3523  * characters will be left on the stack above the newly inserted character.
3524  */
3525 
3526 static char_u	*replace_stack = NULL;
3527 static long	replace_stack_nr = 0;	    /* next entry in replace stack */
3528 static long	replace_stack_len = 0;	    /* max. number of entries */
3529 
3530     void
3531 replace_push(
3532     int	    c)	    /* character that is replaced (NUL is none) */
3533 {
3534     char_u  *p;
3535 
3536     if (replace_stack_nr < replace_offset)	/* nothing to do */
3537 	return;
3538     if (replace_stack_len <= replace_stack_nr)
3539     {
3540 	replace_stack_len += 50;
3541 	p = ALLOC_MULT(char_u, replace_stack_len);
3542 	if (p == NULL)	    /* out of memory */
3543 	{
3544 	    replace_stack_len -= 50;
3545 	    return;
3546 	}
3547 	if (replace_stack != NULL)
3548 	{
3549 	    mch_memmove(p, replace_stack,
3550 				 (size_t)(replace_stack_nr * sizeof(char_u)));
3551 	    vim_free(replace_stack);
3552 	}
3553 	replace_stack = p;
3554     }
3555     p = replace_stack + replace_stack_nr - replace_offset;
3556     if (replace_offset)
3557 	mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3558     *p = c;
3559     ++replace_stack_nr;
3560 }
3561 
3562 /*
3563  * Push a character onto the replace stack.  Handles a multi-byte character in
3564  * reverse byte order, so that the first byte is popped off first.
3565  * Return the number of bytes done (includes composing characters).
3566  */
3567     int
3568 replace_push_mb(char_u *p)
3569 {
3570     int l = (*mb_ptr2len)(p);
3571     int j;
3572 
3573     for (j = l - 1; j >= 0; --j)
3574 	replace_push(p[j]);
3575     return l;
3576 }
3577 
3578 /*
3579  * Pop one item from the replace stack.
3580  * return -1 if stack empty
3581  * return replaced character or NUL otherwise
3582  */
3583     static int
3584 replace_pop(void)
3585 {
3586     if (replace_stack_nr == 0)
3587 	return -1;
3588     return (int)replace_stack[--replace_stack_nr];
3589 }
3590 
3591 /*
3592  * Join the top two items on the replace stack.  This removes to "off"'th NUL
3593  * encountered.
3594  */
3595     void
3596 replace_join(
3597     int	    off)	/* offset for which NUL to remove */
3598 {
3599     int	    i;
3600 
3601     for (i = replace_stack_nr; --i >= 0; )
3602 	if (replace_stack[i] == NUL && off-- <= 0)
3603 	{
3604 	    --replace_stack_nr;
3605 	    mch_memmove(replace_stack + i, replace_stack + i + 1,
3606 					      (size_t)(replace_stack_nr - i));
3607 	    return;
3608 	}
3609 }
3610 
3611 /*
3612  * Pop bytes from the replace stack until a NUL is found, and insert them
3613  * before the cursor.  Can only be used in REPLACE or VREPLACE mode.
3614  */
3615     static void
3616 replace_pop_ins(void)
3617 {
3618     int	    cc;
3619     int	    oldState = State;
3620 
3621     State = NORMAL;			/* don't want REPLACE here */
3622     while ((cc = replace_pop()) > 0)
3623     {
3624 	mb_replace_pop_ins(cc);
3625 	dec_cursor();
3626     }
3627     State = oldState;
3628 }
3629 
3630 /*
3631  * Insert bytes popped from the replace stack. "cc" is the first byte.  If it
3632  * indicates a multi-byte char, pop the other bytes too.
3633  */
3634     static void
3635 mb_replace_pop_ins(int cc)
3636 {
3637     int		n;
3638     char_u	buf[MB_MAXBYTES + 1];
3639     int		i;
3640     int		c;
3641 
3642     if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3643     {
3644 	buf[0] = cc;
3645 	for (i = 1; i < n; ++i)
3646 	    buf[i] = replace_pop();
3647 	ins_bytes_len(buf, n);
3648     }
3649     else
3650 	ins_char(cc);
3651 
3652     if (enc_utf8)
3653 	/* Handle composing chars. */
3654 	for (;;)
3655 	{
3656 	    c = replace_pop();
3657 	    if (c == -1)	    /* stack empty */
3658 		break;
3659 	    if ((n = MB_BYTE2LEN(c)) == 1)
3660 	    {
3661 		/* Not a multi-byte char, put it back. */
3662 		replace_push(c);
3663 		break;
3664 	    }
3665 	    else
3666 	    {
3667 		buf[0] = c;
3668 		for (i = 1; i < n; ++i)
3669 		    buf[i] = replace_pop();
3670 		if (utf_iscomposing(utf_ptr2char(buf)))
3671 		    ins_bytes_len(buf, n);
3672 		else
3673 		{
3674 		    /* Not a composing char, put it back. */
3675 		    for (i = n - 1; i >= 0; --i)
3676 			replace_push(buf[i]);
3677 		    break;
3678 		}
3679 	    }
3680 	}
3681 }
3682 
3683 /*
3684  * make the replace stack empty
3685  * (called when exiting replace mode)
3686  */
3687     static void
3688 replace_flush(void)
3689 {
3690     VIM_CLEAR(replace_stack);
3691     replace_stack_len = 0;
3692     replace_stack_nr = 0;
3693 }
3694 
3695 /*
3696  * Handle doing a BS for one character.
3697  * cc < 0: replace stack empty, just move cursor
3698  * cc == 0: character was inserted, delete it
3699  * cc > 0: character was replaced, put cc (first byte of original char) back
3700  * and check for more characters to be put back
3701  * When "limit_col" is >= 0, don't delete before this column.  Matters when
3702  * using composing characters, use del_char_after_col() instead of del_char().
3703  */
3704     static void
3705 replace_do_bs(int limit_col)
3706 {
3707     int		cc;
3708     int		orig_len = 0;
3709     int		ins_len;
3710     int		orig_vcols = 0;
3711     colnr_T	start_vcol;
3712     char_u	*p;
3713     int		i;
3714     int		vcol;
3715 
3716     cc = replace_pop();
3717     if (cc > 0)
3718     {
3719 #ifdef FEAT_TEXT_PROP
3720 	size_t	len_before = 0;  // init to shut up GCC
3721 
3722 	if (curbuf->b_has_textprop)
3723 	{
3724 	    // Do not adjust text properties for individual delete and insert
3725 	    // operations, do it afterwards on the resulting text.
3726 	    len_before = STRLEN(ml_get_curline());
3727 	    ++text_prop_frozen;
3728 	}
3729 #endif
3730 	if (State & VREPLACE_FLAG)
3731 	{
3732 	    /* Get the number of screen cells used by the character we are
3733 	     * going to delete. */
3734 	    getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3735 	    orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3736 	}
3737 	if (has_mbyte)
3738 	{
3739 	    (void)del_char_after_col(limit_col);
3740 	    if (State & VREPLACE_FLAG)
3741 		orig_len = (int)STRLEN(ml_get_cursor());
3742 	    replace_push(cc);
3743 	}
3744 	else
3745 	{
3746 	    pchar_cursor(cc);
3747 	    if (State & VREPLACE_FLAG)
3748 		orig_len = (int)STRLEN(ml_get_cursor()) - 1;
3749 	}
3750 	replace_pop_ins();
3751 
3752 	if (State & VREPLACE_FLAG)
3753 	{
3754 	    /* Get the number of screen cells used by the inserted characters */
3755 	    p = ml_get_cursor();
3756 	    ins_len = (int)STRLEN(p) - orig_len;
3757 	    vcol = start_vcol;
3758 	    for (i = 0; i < ins_len; ++i)
3759 	    {
3760 		vcol += chartabsize(p + i, vcol);
3761 		i += (*mb_ptr2len)(p) - 1;
3762 	    }
3763 	    vcol -= start_vcol;
3764 
3765 	    /* Delete spaces that were inserted after the cursor to keep the
3766 	     * text aligned. */
3767 	    curwin->w_cursor.col += ins_len;
3768 	    while (vcol > orig_vcols && gchar_cursor() == ' ')
3769 	    {
3770 		del_char(FALSE);
3771 		++orig_vcols;
3772 	    }
3773 	    curwin->w_cursor.col -= ins_len;
3774 	}
3775 
3776 	// mark the buffer as changed and prepare for displaying
3777 	changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
3778 
3779 #ifdef FEAT_TEXT_PROP
3780 	if (curbuf->b_has_textprop)
3781 	{
3782 	    size_t len_now = STRLEN(ml_get_curline());
3783 
3784 	    --text_prop_frozen;
3785 	    adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
3786 					   (int)(len_now - len_before), 0);
3787 	}
3788 #endif
3789     }
3790     else if (cc == 0)
3791 	(void)del_char_after_col(limit_col);
3792 }
3793 
3794 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3795 /*
3796  * Map Hebrew keyboard when in hkmap mode.
3797  */
3798     int
3799 hkmap(int c)
3800 {
3801     if (p_hkmapp)   /* phonetic mapping, by Ilya Dogolazky */
3802     {
3803 	enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3804 	    KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3805 	    PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3806 	static char_u map[26] =
3807 	    {(char_u)hALEF/*a*/, (char_u)BET  /*b*/, (char_u)hKAF    /*c*/,
3808 	     (char_u)DALET/*d*/, (char_u)-1   /*e*/, (char_u)PEIsofit/*f*/,
3809 	     (char_u)GIMEL/*g*/, (char_u)HEI  /*h*/, (char_u)IUD     /*i*/,
3810 	     (char_u)HET  /*j*/, (char_u)KOF  /*k*/, (char_u)LAMED   /*l*/,
3811 	     (char_u)MEM  /*m*/, (char_u)NUN  /*n*/, (char_u)SAMEH   /*o*/,
3812 	     (char_u)PEI  /*p*/, (char_u)-1   /*q*/, (char_u)RESH    /*r*/,
3813 	     (char_u)ZAIN /*s*/, (char_u)TAV  /*t*/, (char_u)TET     /*u*/,
3814 	     (char_u)VAV  /*v*/, (char_u)hSHIN/*w*/, (char_u)-1      /*x*/,
3815 	     (char_u)AIN  /*y*/, (char_u)ZADI /*z*/};
3816 
3817 	if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3818 	    return (int)(map[CharOrd(c)] - 1 + p_aleph);
3819 							    /* '-1'='sofit' */
3820 	else if (c == 'x')
3821 	    return 'X';
3822 	else if (c == 'q')
3823 	    return '\''; /* {geresh}={'} */
3824 	else if (c == 246)
3825 	    return ' ';  /* \"o --> ' ' for a german keyboard */
3826 	else if (c == 228)
3827 	    return ' ';  /* \"a --> ' '      -- / --	       */
3828 	else if (c == 252)
3829 	    return ' ';  /* \"u --> ' '      -- / --	       */
3830 #ifdef EBCDIC
3831 	else if (islower(c))
3832 #else
3833 	/* NOTE: islower() does not do the right thing for us on Linux so we
3834 	 * do this the same was as 5.7 and previous, so it works correctly on
3835 	 * all systems.  Specifically, the e.g. Delete and Arrow keys are
3836 	 * munged and won't work if e.g. searching for Hebrew text.
3837 	 */
3838 	else if (c >= 'a' && c <= 'z')
3839 #endif
3840 	    return (int)(map[CharOrdLow(c)] + p_aleph);
3841 	else
3842 	    return c;
3843     }
3844     else
3845     {
3846 	switch (c)
3847 	{
3848 	    case '`':	return ';';
3849 	    case '/':	return '.';
3850 	    case '\'':	return ',';
3851 	    case 'q':	return '/';
3852 	    case 'w':	return '\'';
3853 
3854 			/* Hebrew letters - set offset from 'a' */
3855 	    case ',':	c = '{'; break;
3856 	    case '.':	c = 'v'; break;
3857 	    case ';':	c = 't'; break;
3858 	    default: {
3859 			 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3860 
3861 #ifdef EBCDIC
3862 			 /* see note about islower() above */
3863 			 if (!islower(c))
3864 #else
3865 			 if (c < 'a' || c > 'z')
3866 #endif
3867 			     return c;
3868 			 c = str[CharOrdLow(c)];
3869 			 break;
3870 		     }
3871 	}
3872 
3873 	return (int)(CharOrdLow(c) + p_aleph);
3874     }
3875 }
3876 #endif
3877 
3878     static void
3879 ins_reg(void)
3880 {
3881     int		need_redraw = FALSE;
3882     int		regname;
3883     int		literally = 0;
3884     int		vis_active = VIsual_active;
3885 
3886     /*
3887      * If we are going to wait for a character, show a '"'.
3888      */
3889     pc_status = PC_STATUS_UNSET;
3890     if (redrawing() && !char_avail())
3891     {
3892 	/* may need to redraw when no more chars available now */
3893 	ins_redraw(FALSE);
3894 
3895 	edit_putchar('"', TRUE);
3896 #ifdef FEAT_CMDL_INFO
3897 	add_to_showcmd_c(Ctrl_R);
3898 #endif
3899     }
3900 
3901 #ifdef USE_ON_FLY_SCROLL
3902     dont_scroll = TRUE;		/* disallow scrolling here */
3903 #endif
3904 
3905     /*
3906      * Don't map the register name. This also prevents the mode message to be
3907      * deleted when ESC is hit.
3908      */
3909     ++no_mapping;
3910     regname = plain_vgetc();
3911     LANGMAP_ADJUST(regname, TRUE);
3912     if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3913     {
3914 	/* Get a third key for literal register insertion */
3915 	literally = regname;
3916 #ifdef FEAT_CMDL_INFO
3917 	add_to_showcmd_c(literally);
3918 #endif
3919 	regname = plain_vgetc();
3920 	LANGMAP_ADJUST(regname, TRUE);
3921     }
3922     --no_mapping;
3923 
3924 #ifdef FEAT_EVAL
3925     /* Don't call u_sync() while typing the expression or giving an error
3926      * message for it. Only call it explicitly. */
3927     ++no_u_sync;
3928     if (regname == '=')
3929     {
3930 	pos_T	curpos = curwin->w_cursor;
3931 # ifdef HAVE_INPUT_METHOD
3932 	int	im_on = im_get_status();
3933 # endif
3934 	/* Sync undo when evaluating the expression calls setline() or
3935 	 * append(), so that it can be undone separately. */
3936 	u_sync_once = 2;
3937 
3938 	regname = get_expr_register();
3939 
3940 	// Cursor may be moved back a column.
3941 	curwin->w_cursor = curpos;
3942 	check_cursor();
3943 # ifdef HAVE_INPUT_METHOD
3944 	// Restore the Input Method.
3945 	if (im_on)
3946 	    im_set_active(TRUE);
3947 # endif
3948     }
3949     if (regname == NUL || !valid_yank_reg(regname, FALSE))
3950     {
3951 	vim_beep(BO_REG);
3952 	need_redraw = TRUE;	/* remove the '"' */
3953     }
3954     else
3955     {
3956 #endif
3957 	if (literally == Ctrl_O || literally == Ctrl_P)
3958 	{
3959 	    /* Append the command to the redo buffer. */
3960 	    AppendCharToRedobuff(Ctrl_R);
3961 	    AppendCharToRedobuff(literally);
3962 	    AppendCharToRedobuff(regname);
3963 
3964 	    do_put(regname, BACKWARD, 1L,
3965 		 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3966 	}
3967 	else if (insert_reg(regname, literally) == FAIL)
3968 	{
3969 	    vim_beep(BO_REG);
3970 	    need_redraw = TRUE;	/* remove the '"' */
3971 	}
3972 	else if (stop_insert_mode)
3973 	    /* When the '=' register was used and a function was invoked that
3974 	     * did ":stopinsert" then stuff_empty() returns FALSE but we won't
3975 	     * insert anything, need to remove the '"' */
3976 	    need_redraw = TRUE;
3977 
3978 #ifdef FEAT_EVAL
3979     }
3980     --no_u_sync;
3981     if (u_sync_once == 1)
3982 	ins_need_undo = TRUE;
3983     u_sync_once = 0;
3984 #endif
3985 #ifdef FEAT_CMDL_INFO
3986     clear_showcmd();
3987 #endif
3988 
3989     /* If the inserted register is empty, we need to remove the '"' */
3990     if (need_redraw || stuff_empty())
3991 	edit_unputchar();
3992 
3993     /* Disallow starting Visual mode here, would get a weird mode. */
3994     if (!vis_active && VIsual_active)
3995 	end_visual_mode();
3996 }
3997 
3998 /*
3999  * CTRL-G commands in Insert mode.
4000  */
4001     static void
4002 ins_ctrl_g(void)
4003 {
4004     int		c;
4005 
4006     // Right after CTRL-X the cursor will be after the ruler.
4007     setcursor();
4008 
4009     /*
4010      * Don't map the second key. This also prevents the mode message to be
4011      * deleted when ESC is hit.
4012      */
4013     ++no_mapping;
4014     c = plain_vgetc();
4015     --no_mapping;
4016     switch (c)
4017     {
4018 	/* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4019 	case K_UP:
4020 	case Ctrl_K:
4021 	case 'k': ins_up(TRUE);
4022 		  break;
4023 
4024 	/* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4025 	case K_DOWN:
4026 	case Ctrl_J:
4027 	case 'j': ins_down(TRUE);
4028 		  break;
4029 
4030 	/* CTRL-G u: start new undoable edit */
4031 	case 'u': u_sync(TRUE);
4032 		  ins_need_undo = TRUE;
4033 
4034 		  /* Need to reset Insstart, esp. because a BS that joins
4035 		   * a line to the previous one must save for undo. */
4036 		  update_Insstart_orig = FALSE;
4037 		  Insstart = curwin->w_cursor;
4038 		  break;
4039 
4040 	/* CTRL-G U: do not break undo with the next char */
4041 	case 'U':
4042 		  /* Allow one left/right cursor movement with the next char,
4043 		   * without breaking undo. */
4044 		  dont_sync_undo = MAYBE;
4045 		  break;
4046 
4047 	/* Unknown CTRL-G command, reserved for future expansion. */
4048 	default:  vim_beep(BO_CTRLG);
4049     }
4050 }
4051 
4052 /*
4053  * CTRL-^ in Insert mode.
4054  */
4055     static void
4056 ins_ctrl_hat(void)
4057 {
4058     if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
4059     {
4060 	/* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4061 	if (State & LANGMAP)
4062 	{
4063 	    curbuf->b_p_iminsert = B_IMODE_NONE;
4064 	    State &= ~LANGMAP;
4065 	}
4066 	else
4067 	{
4068 	    curbuf->b_p_iminsert = B_IMODE_LMAP;
4069 	    State |= LANGMAP;
4070 #ifdef HAVE_INPUT_METHOD
4071 	    im_set_active(FALSE);
4072 #endif
4073 	}
4074     }
4075 #ifdef HAVE_INPUT_METHOD
4076     else
4077     {
4078 	/* There are no ":lmap" mappings, toggle IM */
4079 	if (im_get_status())
4080 	{
4081 	    curbuf->b_p_iminsert = B_IMODE_NONE;
4082 	    im_set_active(FALSE);
4083 	}
4084 	else
4085 	{
4086 	    curbuf->b_p_iminsert = B_IMODE_IM;
4087 	    State &= ~LANGMAP;
4088 	    im_set_active(TRUE);
4089 	}
4090     }
4091 #endif
4092     set_iminsert_global();
4093     showmode();
4094 #ifdef FEAT_GUI
4095     /* may show different cursor shape or color */
4096     if (gui.in_use)
4097 	gui_update_cursor(TRUE, FALSE);
4098 #endif
4099 #if defined(FEAT_KEYMAP)
4100     /* Show/unshow value of 'keymap' in status lines. */
4101     status_redraw_curbuf();
4102 #endif
4103 }
4104 
4105 /*
4106  * Handle ESC in insert mode.
4107  * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4108  * insert.
4109  */
4110     static int
4111 ins_esc(
4112     long	*count,
4113     int		cmdchar,
4114     int		nomove)	    /* don't move cursor */
4115 {
4116     int		temp;
4117     static int	disabled_redraw = FALSE;
4118 
4119 #ifdef FEAT_SPELL
4120     check_spell_redraw();
4121 #endif
4122 #if defined(FEAT_HANGULIN)
4123 # if defined(ESC_CHG_TO_ENG_MODE)
4124     hangul_input_state_set(0);
4125 # endif
4126     if (composing_hangul)
4127     {
4128 	push_raw_key(composing_hangul_buffer, 2);
4129 	composing_hangul = 0;
4130     }
4131 #endif
4132 
4133     temp = curwin->w_cursor.col;
4134     if (disabled_redraw)
4135     {
4136 	--RedrawingDisabled;
4137 	disabled_redraw = FALSE;
4138     }
4139     if (!arrow_used)
4140     {
4141 	/*
4142 	 * Don't append the ESC for "r<CR>" and "grx".
4143 	 * When 'insertmode' is set only CTRL-L stops Insert mode.  Needed for
4144 	 * when "count" is non-zero.
4145 	 */
4146 	if (cmdchar != 'r' && cmdchar != 'v')
4147 	    AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
4148 
4149 	/*
4150 	 * Repeating insert may take a long time.  Check for
4151 	 * interrupt now and then.
4152 	 */
4153 	if (*count > 0)
4154 	{
4155 	    line_breakcheck();
4156 	    if (got_int)
4157 		*count = 0;
4158 	}
4159 
4160 	if (--*count > 0)	/* repeat what was typed */
4161 	{
4162 	    /* Vi repeats the insert without replacing characters. */
4163 	    if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4164 		State &= ~REPLACE_FLAG;
4165 
4166 	    (void)start_redo_ins();
4167 	    if (cmdchar == 'r' || cmdchar == 'v')
4168 		stuffRedoReadbuff(ESC_STR);	/* no ESC in redo buffer */
4169 	    ++RedrawingDisabled;
4170 	    disabled_redraw = TRUE;
4171 	    return FALSE;	/* repeat the insert */
4172 	}
4173 	stop_insert(&curwin->w_cursor, TRUE, nomove);
4174 	undisplay_dollar();
4175     }
4176 
4177     /* When an autoindent was removed, curswant stays after the
4178      * indent */
4179     if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4180 	curwin->w_set_curswant = TRUE;
4181 
4182     /* Remember the last Insert position in the '^ mark. */
4183     if (!cmdmod.keepjumps)
4184 	curbuf->b_last_insert = curwin->w_cursor;
4185 
4186     /*
4187      * The cursor should end up on the last inserted character.
4188      * Don't do it for CTRL-O, unless past the end of the line.
4189      */
4190     if (!nomove
4191 	    && (curwin->w_cursor.col != 0
4192 		|| curwin->w_cursor.coladd > 0)
4193 	    && (restart_edit == NUL
4194 		   || (gchar_cursor() == NUL && !VIsual_active))
4195 #ifdef FEAT_RIGHTLEFT
4196 	    && !revins_on
4197 #endif
4198 				      )
4199     {
4200 	if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4201 	{
4202 	    oneleft();
4203 	    if (restart_edit != NUL)
4204 		++curwin->w_cursor.coladd;
4205 	}
4206 	else
4207 	{
4208 	    --curwin->w_cursor.col;
4209 	    /* Correct cursor for multi-byte character. */
4210 	    if (has_mbyte)
4211 		mb_adjust_cursor();
4212 	}
4213     }
4214 
4215 #ifdef HAVE_INPUT_METHOD
4216     /* Disable IM to allow typing English directly for Normal mode commands.
4217      * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4218      * well). */
4219     if (!(State & LANGMAP))
4220 	im_save_status(&curbuf->b_p_iminsert);
4221     im_set_active(FALSE);
4222 #endif
4223 
4224     State = NORMAL;
4225     /* need to position cursor again (e.g. when on a TAB ) */
4226     changed_cline_bef_curs();
4227 
4228     setmouse();
4229 #ifdef CURSOR_SHAPE
4230     ui_cursor_shape();		/* may show different cursor shape */
4231 #endif
4232     if (!p_ek)
4233 	/* Re-enable bracketed paste mode. */
4234 	out_str(T_BE);
4235 
4236     // When recording or for CTRL-O, need to display the new mode.
4237     // Otherwise remove the mode message.
4238     if (reg_recording != 0 || restart_edit != NUL)
4239 	showmode();
4240     else if (p_smd && (got_int || !skip_showmode()))
4241 	msg("");
4242 
4243     return TRUE;	    /* exit Insert mode */
4244 }
4245 
4246 #ifdef FEAT_RIGHTLEFT
4247 /*
4248  * Toggle language: hkmap and revins_on.
4249  * Move to end of reverse inserted text.
4250  */
4251     static void
4252 ins_ctrl_(void)
4253 {
4254     if (revins_on && revins_chars && revins_scol >= 0)
4255     {
4256 	while (gchar_cursor() != NUL && revins_chars--)
4257 	    ++curwin->w_cursor.col;
4258     }
4259     p_ri = !p_ri;
4260     revins_on = (State == INSERT && p_ri);
4261     if (revins_on)
4262     {
4263 	revins_scol = curwin->w_cursor.col;
4264 	revins_legal++;
4265 	revins_chars = 0;
4266 	undisplay_dollar();
4267     }
4268     else
4269 	revins_scol = -1;
4270     p_hkmap = curwin->w_p_rl ^ p_ri;    // be consistent!
4271     showmode();
4272 }
4273 #endif
4274 
4275 /*
4276  * If 'keymodel' contains "startsel", may start selection.
4277  * Returns TRUE when a CTRL-O and other keys stuffed.
4278  */
4279     static int
4280 ins_start_select(int c)
4281 {
4282     if (km_startsel)
4283 	switch (c)
4284 	{
4285 	    case K_KHOME:
4286 	    case K_KEND:
4287 	    case K_PAGEUP:
4288 	    case K_KPAGEUP:
4289 	    case K_PAGEDOWN:
4290 	    case K_KPAGEDOWN:
4291 # ifdef MACOS_X
4292 	    case K_LEFT:
4293 	    case K_RIGHT:
4294 	    case K_UP:
4295 	    case K_DOWN:
4296 	    case K_END:
4297 	    case K_HOME:
4298 # endif
4299 		if (!(mod_mask & MOD_MASK_SHIFT))
4300 		    break;
4301 		/* FALLTHROUGH */
4302 	    case K_S_LEFT:
4303 	    case K_S_RIGHT:
4304 	    case K_S_UP:
4305 	    case K_S_DOWN:
4306 	    case K_S_END:
4307 	    case K_S_HOME:
4308 		/* Start selection right away, the cursor can move with
4309 		 * CTRL-O when beyond the end of the line. */
4310 		start_selection();
4311 
4312 		/* Execute the key in (insert) Select mode. */
4313 		stuffcharReadbuff(Ctrl_O);
4314 		if (mod_mask)
4315 		{
4316 		    char_u	    buf[4];
4317 
4318 		    buf[0] = K_SPECIAL;
4319 		    buf[1] = KS_MODIFIER;
4320 		    buf[2] = mod_mask;
4321 		    buf[3] = NUL;
4322 		    stuffReadbuff(buf);
4323 		}
4324 		stuffcharReadbuff(c);
4325 		return TRUE;
4326 	}
4327     return FALSE;
4328 }
4329 
4330 /*
4331  * <Insert> key in Insert mode: toggle insert/replace mode.
4332  */
4333     static void
4334 ins_insert(int replaceState)
4335 {
4336 #ifdef FEAT_EVAL
4337     set_vim_var_string(VV_INSERTMODE,
4338 		   (char_u *)((State & REPLACE_FLAG) ? "i"
4339 		          : replaceState == VREPLACE ? "v"
4340 						     : "r"), 1);
4341 #endif
4342     ins_apply_autocmds(EVENT_INSERTCHANGE);
4343     if (State & REPLACE_FLAG)
4344 	State = INSERT | (State & LANGMAP);
4345     else
4346 	State = replaceState | (State & LANGMAP);
4347     AppendCharToRedobuff(K_INS);
4348     showmode();
4349 #ifdef CURSOR_SHAPE
4350     ui_cursor_shape();		/* may show different cursor shape */
4351 #endif
4352 }
4353 
4354 /*
4355  * Pressed CTRL-O in Insert mode.
4356  */
4357     static void
4358 ins_ctrl_o(void)
4359 {
4360     if (State & VREPLACE_FLAG)
4361 	restart_edit = 'V';
4362     else
4363 	if (State & REPLACE_FLAG)
4364 	restart_edit = 'R';
4365     else
4366 	restart_edit = 'I';
4367     if (virtual_active())
4368 	ins_at_eol = FALSE;	/* cursor always keeps its column */
4369     else
4370 	ins_at_eol = (gchar_cursor() == NUL);
4371 }
4372 
4373 /*
4374  * If the cursor is on an indent, ^T/^D insert/delete one
4375  * shiftwidth.	Otherwise ^T/^D behave like a "<<" or ">>".
4376  * Always round the indent to 'shiftwidth', this is compatible
4377  * with vi.  But vi only supports ^T and ^D after an
4378  * autoindent, we support it everywhere.
4379  */
4380     static void
4381 ins_shift(int c, int lastc)
4382 {
4383     if (stop_arrow() == FAIL)
4384 	return;
4385     AppendCharToRedobuff(c);
4386 
4387     /*
4388      * 0^D and ^^D: remove all indent.
4389      */
4390     if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4391 						  && curwin->w_cursor.col > 0)
4392     {
4393 	--curwin->w_cursor.col;
4394 	(void)del_char(FALSE);		/* delete the '^' or '0' */
4395 	/* In Replace mode, restore the characters that '^' or '0' replaced. */
4396 	if (State & REPLACE_FLAG)
4397 	    replace_pop_ins();
4398 	if (lastc == '^')
4399 	    old_indent = get_indent();	/* remember curr. indent */
4400 	change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
4401     }
4402     else
4403 	change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
4404 
4405     if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4406 	did_ai = FALSE;
4407 #ifdef FEAT_SMARTINDENT
4408     did_si = FALSE;
4409     can_si = FALSE;
4410     can_si_back = FALSE;
4411 #endif
4412 #ifdef FEAT_CINDENT
4413     can_cindent = FALSE;	/* no cindenting after ^D or ^T */
4414 #endif
4415 }
4416 
4417     static void
4418 ins_del(void)
4419 {
4420     int	    temp;
4421 
4422     if (stop_arrow() == FAIL)
4423 	return;
4424     if (gchar_cursor() == NUL)		/* delete newline */
4425     {
4426 	temp = curwin->w_cursor.col;
4427 	if (!can_bs(BS_EOL)		/* only if "eol" included */
4428 		|| do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
4429 	    vim_beep(BO_BS);
4430 	else
4431 	{
4432 	    curwin->w_cursor.col = temp;
4433 	    /* Adjust orig_line_count in case more lines have been deleted than
4434 	     * have been added. That makes sure, that open_line() later
4435 	     * can access all buffer lines correctly */
4436 	    if (State & VREPLACE_FLAG &&
4437 		    orig_line_count > curbuf->b_ml.ml_line_count)
4438 		orig_line_count = curbuf->b_ml.ml_line_count;
4439 	}
4440     }
4441     else if (del_char(FALSE) == FAIL)  /* delete char under cursor */
4442 	vim_beep(BO_BS);
4443     did_ai = FALSE;
4444 #ifdef FEAT_SMARTINDENT
4445     did_si = FALSE;
4446     can_si = FALSE;
4447     can_si_back = FALSE;
4448 #endif
4449     AppendCharToRedobuff(K_DEL);
4450 }
4451 
4452 /*
4453  * Delete one character for ins_bs().
4454  */
4455     static void
4456 ins_bs_one(colnr_T *vcolp)
4457 {
4458     dec_cursor();
4459     getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4460     if (State & REPLACE_FLAG)
4461     {
4462 	/* Don't delete characters before the insert point when in
4463 	 * Replace mode */
4464 	if (curwin->w_cursor.lnum != Insstart.lnum
4465 		|| curwin->w_cursor.col >= Insstart.col)
4466 	    replace_do_bs(-1);
4467     }
4468     else
4469 	(void)del_char(FALSE);
4470 }
4471 
4472 /*
4473  * Handle Backspace, delete-word and delete-line in Insert mode.
4474  * Return TRUE when backspace was actually used.
4475  */
4476     static int
4477 ins_bs(
4478     int		c,
4479     int		mode,
4480     int		*inserted_space_p)
4481 {
4482     linenr_T	lnum;
4483     int		cc;
4484     int		temp = 0;	    /* init for GCC */
4485     colnr_T	save_col;
4486     colnr_T	mincol;
4487     int		did_backspace = FALSE;
4488     int		in_indent;
4489     int		oldState;
4490     int		cpc[MAX_MCO];	    /* composing characters */
4491 
4492     /*
4493      * can't delete anything in an empty file
4494      * can't backup past first character in buffer
4495      * can't backup past starting point unless 'backspace' > 1
4496      * can backup to a previous line if 'backspace' == 0
4497      */
4498     if (       BUFEMPTY()
4499 	    || (
4500 #ifdef FEAT_RIGHTLEFT
4501 		!revins_on &&
4502 #endif
4503 		((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4504 		    || (!can_bs(BS_START)
4505 			&& (arrow_used
4506 			    || (curwin->w_cursor.lnum == Insstart_orig.lnum
4507 				&& curwin->w_cursor.col <= Insstart_orig.col)))
4508 		    || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4509 					 && curwin->w_cursor.col <= ai_col)
4510 		    || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4511     {
4512 	vim_beep(BO_BS);
4513 	return FALSE;
4514     }
4515 
4516     if (stop_arrow() == FAIL)
4517 	return FALSE;
4518     in_indent = inindent(0);
4519 #ifdef FEAT_CINDENT
4520     if (in_indent)
4521 	can_cindent = FALSE;
4522 #endif
4523     end_comment_pending = NUL;	/* After BS, don't auto-end comment */
4524 #ifdef FEAT_RIGHTLEFT
4525     if (revins_on)	    /* put cursor after last inserted char */
4526 	inc_cursor();
4527 #endif
4528 
4529     /* Virtualedit:
4530      *	BACKSPACE_CHAR eats a virtual space
4531      *	BACKSPACE_WORD eats all coladd
4532      *	BACKSPACE_LINE eats all coladd and keeps going
4533      */
4534     if (curwin->w_cursor.coladd > 0)
4535     {
4536 	if (mode == BACKSPACE_CHAR)
4537 	{
4538 	    --curwin->w_cursor.coladd;
4539 	    return TRUE;
4540 	}
4541 	if (mode == BACKSPACE_WORD)
4542 	{
4543 	    curwin->w_cursor.coladd = 0;
4544 	    return TRUE;
4545 	}
4546 	curwin->w_cursor.coladd = 0;
4547     }
4548 
4549     /*
4550      * Delete newline!
4551      */
4552     if (curwin->w_cursor.col == 0)
4553     {
4554 	lnum = Insstart.lnum;
4555 	if (curwin->w_cursor.lnum == lnum
4556 #ifdef FEAT_RIGHTLEFT
4557 			|| revins_on
4558 #endif
4559 				    )
4560 	{
4561 	    if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4562 			       (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4563 		return FALSE;
4564 	    --Insstart.lnum;
4565 	    Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
4566 	}
4567 	/*
4568 	 * In replace mode:
4569 	 * cc < 0: NL was inserted, delete it
4570 	 * cc >= 0: NL was replaced, put original characters back
4571 	 */
4572 	cc = -1;
4573 	if (State & REPLACE_FLAG)
4574 	    cc = replace_pop();	    /* returns -1 if NL was inserted */
4575 	/*
4576 	 * In replace mode, in the line we started replacing, we only move the
4577 	 * cursor.
4578 	 */
4579 	if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4580 	{
4581 	    dec_cursor();
4582 	}
4583 	else
4584 	{
4585 	    if (!(State & VREPLACE_FLAG)
4586 				   || curwin->w_cursor.lnum > orig_line_count)
4587 	    {
4588 		temp = gchar_cursor();	/* remember current char */
4589 		--curwin->w_cursor.lnum;
4590 
4591 		/* When "aw" is in 'formatoptions' we must delete the space at
4592 		 * the end of the line, otherwise the line will be broken
4593 		 * again when auto-formatting. */
4594 		if (has_format_option(FO_AUTO)
4595 					   && has_format_option(FO_WHITE_PAR))
4596 		{
4597 		    char_u  *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4598 									TRUE);
4599 		    int	    len;
4600 
4601 		    len = (int)STRLEN(ptr);
4602 		    if (len > 0 && ptr[len - 1] == ' ')
4603 			ptr[len - 1] = NUL;
4604 		}
4605 
4606 		(void)do_join(2, FALSE, FALSE, FALSE, FALSE);
4607 		if (temp == NUL && gchar_cursor() != NUL)
4608 		    inc_cursor();
4609 	    }
4610 	    else
4611 		dec_cursor();
4612 
4613 	    /*
4614 	     * In REPLACE mode we have to put back the text that was replaced
4615 	     * by the NL. On the replace stack is first a NUL-terminated
4616 	     * sequence of characters that were deleted and then the
4617 	     * characters that NL replaced.
4618 	     */
4619 	    if (State & REPLACE_FLAG)
4620 	    {
4621 		/*
4622 		 * Do the next ins_char() in NORMAL state, to
4623 		 * prevent ins_char() from replacing characters and
4624 		 * avoiding showmatch().
4625 		 */
4626 		oldState = State;
4627 		State = NORMAL;
4628 		/*
4629 		 * restore characters (blanks) deleted after cursor
4630 		 */
4631 		while (cc > 0)
4632 		{
4633 		    save_col = curwin->w_cursor.col;
4634 		    mb_replace_pop_ins(cc);
4635 		    curwin->w_cursor.col = save_col;
4636 		    cc = replace_pop();
4637 		}
4638 		/* restore the characters that NL replaced */
4639 		replace_pop_ins();
4640 		State = oldState;
4641 	    }
4642 	}
4643 	did_ai = FALSE;
4644     }
4645     else
4646     {
4647 	/*
4648 	 * Delete character(s) before the cursor.
4649 	 */
4650 #ifdef FEAT_RIGHTLEFT
4651 	if (revins_on)		/* put cursor on last inserted char */
4652 	    dec_cursor();
4653 #endif
4654 	mincol = 0;
4655 						/* keep indent */
4656 	if (mode == BACKSPACE_LINE
4657 		&& (curbuf->b_p_ai
4658 #ifdef FEAT_CINDENT
4659 		    || cindent_on()
4660 #endif
4661 		   )
4662 #ifdef FEAT_RIGHTLEFT
4663 		&& !revins_on
4664 #endif
4665 			    )
4666 	{
4667 	    save_col = curwin->w_cursor.col;
4668 	    beginline(BL_WHITE);
4669 	    if (curwin->w_cursor.col < save_col)
4670 		mincol = curwin->w_cursor.col;
4671 	    curwin->w_cursor.col = save_col;
4672 	}
4673 
4674 	/*
4675 	 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4676 	 */
4677 	if (	   mode == BACKSPACE_CHAR
4678 		&& ((p_sta && in_indent)
4679 		    || ((get_sts_value() != 0
4680 #ifdef FEAT_VARTABS
4681 			|| tabstop_count(curbuf->b_p_vsts_array)
4682 #endif
4683 			)
4684 			&& curwin->w_cursor.col > 0
4685 			&& (*(ml_get_cursor() - 1) == TAB
4686 			    || (*(ml_get_cursor() - 1) == ' '
4687 				&& (!*inserted_space_p
4688 				    || arrow_used))))))
4689 	{
4690 	    int		ts;
4691 	    colnr_T	vcol;
4692 	    colnr_T	want_vcol;
4693 	    colnr_T	start_vcol;
4694 
4695 	    *inserted_space_p = FALSE;
4696 	    /* Compute the virtual column where we want to be.  Since
4697 	     * 'showbreak' may get in the way, need to get the last column of
4698 	     * the previous character. */
4699 	    getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4700 	    start_vcol = vcol;
4701 	    dec_cursor();
4702 	    getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4703 	    inc_cursor();
4704 #ifdef FEAT_VARTABS
4705 	    if (p_sta && in_indent)
4706 	    {
4707 		ts = (int)get_sw_value(curbuf);
4708 		want_vcol = (want_vcol / ts) * ts;
4709 	    }
4710 	    else
4711 		want_vcol = tabstop_start(want_vcol, get_sts_value(),
4712 						     curbuf->b_p_vsts_array);
4713 #else
4714 	    if (p_sta && in_indent)
4715 		ts = (int)get_sw_value(curbuf);
4716 	    else
4717 		ts = (int)get_sts_value();
4718 	    want_vcol = (want_vcol / ts) * ts;
4719 #endif
4720 
4721 	    /* delete characters until we are at or before want_vcol */
4722 	    while (vcol > want_vcol
4723 		    && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
4724 		ins_bs_one(&vcol);
4725 
4726 	    /* insert extra spaces until we are at want_vcol */
4727 	    while (vcol < want_vcol)
4728 	    {
4729 		/* Remember the first char we inserted */
4730 		if (curwin->w_cursor.lnum == Insstart_orig.lnum
4731 				   && curwin->w_cursor.col < Insstart_orig.col)
4732 		    Insstart_orig.col = curwin->w_cursor.col;
4733 
4734 		if (State & VREPLACE_FLAG)
4735 		    ins_char(' ');
4736 		else
4737 		{
4738 		    ins_str((char_u *)" ");
4739 		    if ((State & REPLACE_FLAG))
4740 			replace_push(NUL);
4741 		}
4742 		getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4743 	    }
4744 
4745 	    /* If we are now back where we started delete one character.  Can
4746 	     * happen when using 'sts' and 'linebreak'. */
4747 	    if (vcol >= start_vcol)
4748 		ins_bs_one(&vcol);
4749 	}
4750 
4751 	/*
4752 	 * Delete upto starting point, start of line or previous word.
4753 	 */
4754 	else
4755 	{
4756 	    int cclass = 0, prev_cclass = 0;
4757 
4758 	    if (has_mbyte)
4759 		cclass = mb_get_class(ml_get_cursor());
4760 	    do
4761 	    {
4762 #ifdef FEAT_RIGHTLEFT
4763 		if (!revins_on) /* put cursor on char to be deleted */
4764 #endif
4765 		    dec_cursor();
4766 
4767 		cc = gchar_cursor();
4768 		/* look multi-byte character class */
4769 		if (has_mbyte)
4770 		{
4771 		    prev_cclass = cclass;
4772 		    cclass = mb_get_class(ml_get_cursor());
4773 		}
4774 
4775 		/* start of word? */
4776 		if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4777 		{
4778 		    mode = BACKSPACE_WORD_NOT_SPACE;
4779 		    temp = vim_iswordc(cc);
4780 		}
4781 		/* end of word? */
4782 		else if (mode == BACKSPACE_WORD_NOT_SPACE
4783 			&& ((vim_isspace(cc) || vim_iswordc(cc) != temp)
4784 			|| prev_cclass != cclass))
4785 		{
4786 #ifdef FEAT_RIGHTLEFT
4787 		    if (!revins_on)
4788 #endif
4789 			inc_cursor();
4790 #ifdef FEAT_RIGHTLEFT
4791 		    else if (State & REPLACE_FLAG)
4792 			dec_cursor();
4793 #endif
4794 		    break;
4795 		}
4796 		if (State & REPLACE_FLAG)
4797 		    replace_do_bs(-1);
4798 		else
4799 		{
4800 		    if (enc_utf8 && p_deco)
4801 			(void)utfc_ptr2char(ml_get_cursor(), cpc);
4802 		    (void)del_char(FALSE);
4803 		    /*
4804 		     * If there are combining characters and 'delcombine' is set
4805 		     * move the cursor back.  Don't back up before the base
4806 		     * character.
4807 		     */
4808 		    if (enc_utf8 && p_deco && cpc[0] != NUL)
4809 			inc_cursor();
4810 #ifdef FEAT_RIGHTLEFT
4811 		    if (revins_chars)
4812 		    {
4813 			revins_chars--;
4814 			revins_legal++;
4815 		    }
4816 		    if (revins_on && gchar_cursor() == NUL)
4817 			break;
4818 #endif
4819 		}
4820 		/* Just a single backspace?: */
4821 		if (mode == BACKSPACE_CHAR)
4822 		    break;
4823 	    } while (
4824 #ifdef FEAT_RIGHTLEFT
4825 		    revins_on ||
4826 #endif
4827 		    (curwin->w_cursor.col > mincol
4828 		    && (curwin->w_cursor.lnum != Insstart_orig.lnum
4829 			|| curwin->w_cursor.col != Insstart_orig.col)));
4830 	}
4831 	did_backspace = TRUE;
4832     }
4833 #ifdef FEAT_SMARTINDENT
4834     did_si = FALSE;
4835     can_si = FALSE;
4836     can_si_back = FALSE;
4837 #endif
4838     if (curwin->w_cursor.col <= 1)
4839 	did_ai = FALSE;
4840     /*
4841      * It's a little strange to put backspaces into the redo
4842      * buffer, but it makes auto-indent a lot easier to deal
4843      * with.
4844      */
4845     AppendCharToRedobuff(c);
4846 
4847     /* If deleted before the insertion point, adjust it */
4848     if (curwin->w_cursor.lnum == Insstart_orig.lnum
4849 				  && curwin->w_cursor.col < Insstart_orig.col)
4850 	Insstart_orig.col = curwin->w_cursor.col;
4851 
4852     /* vi behaviour: the cursor moves backward but the character that
4853      *		     was there remains visible
4854      * Vim behaviour: the cursor moves backward and the character that
4855      *		      was there is erased from the screen.
4856      * We can emulate the vi behaviour by pretending there is a dollar
4857      * displayed even when there isn't.
4858      *  --pkv Sun Jan 19 01:56:40 EST 2003 */
4859     if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
4860 	dollar_vcol = curwin->w_virtcol;
4861 
4862 #ifdef FEAT_FOLDING
4863     /* When deleting a char the cursor line must never be in a closed fold.
4864      * E.g., when 'foldmethod' is indent and deleting the first non-white
4865      * char before a Tab. */
4866     if (did_backspace)
4867 	foldOpenCursor();
4868 #endif
4869 
4870     return did_backspace;
4871 }
4872 
4873 /*
4874  * Handle receiving P_PS: start paste mode.  Inserts the following text up to
4875  * P_PE literally.
4876  * When "drop" is TRUE then consume the text and drop it.
4877  */
4878     int
4879 bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4880 {
4881     int		c;
4882     char_u	buf[NUMBUFLEN + MB_MAXBYTES];
4883     int		idx = 0;
4884     char_u	*end = find_termcode((char_u *)"PE");
4885     int		ret_char = -1;
4886     int		save_allow_keys = allow_keys;
4887     int		save_paste = p_paste;
4888 
4889     /* If the end code is too long we can't detect it, read everything. */
4890     if (STRLEN(end) >= NUMBUFLEN)
4891 	end = NULL;
4892     ++no_mapping;
4893     allow_keys = 0;
4894     if (!p_paste)
4895 	// Also have the side effects of setting 'paste' to make it work much
4896 	// faster.
4897 	set_option_value((char_u *)"paste", TRUE, NULL, 0);
4898 
4899     for (;;)
4900     {
4901 	// When the end is not defined read everything there is.
4902 	if (end == NULL && vpeekc() == NUL)
4903 	    break;
4904 	do
4905 	    c = vgetc();
4906 	while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
4907 	if (c == NUL || got_int)
4908 	    // When CTRL-C was encountered the typeahead will be flushed and we
4909 	    // won't get the end sequence.
4910 	    break;
4911 
4912 	if (has_mbyte)
4913 	    idx += (*mb_char2bytes)(c, buf + idx);
4914 	else
4915 	    buf[idx++] = c;
4916 	buf[idx] = NUL;
4917 	if (end != NULL && STRNCMP(buf, end, idx) == 0)
4918 	{
4919 	    if (end[idx] == NUL)
4920 		break; /* Found the end of paste code. */
4921 	    continue;
4922 	}
4923 	if (!drop)
4924 	{
4925 	    switch (mode)
4926 	    {
4927 		case PASTE_CMDLINE:
4928 		    put_on_cmdline(buf, idx, TRUE);
4929 		    break;
4930 
4931 		case PASTE_EX:
4932 		    if (gap != NULL && ga_grow(gap, idx) == OK)
4933 		    {
4934 			mch_memmove((char *)gap->ga_data + gap->ga_len,
4935 							     buf, (size_t)idx);
4936 			gap->ga_len += idx;
4937 		    }
4938 		    break;
4939 
4940 		case PASTE_INSERT:
4941 		    if (stop_arrow() == OK)
4942 		    {
4943 			c = buf[0];
4944 			if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4945 			    ins_eol(c);
4946 			else
4947 			{
4948 			    ins_char_bytes(buf, idx);
4949 			    AppendToRedobuffLit(buf, idx);
4950 			}
4951 		    }
4952 		    break;
4953 
4954 		case PASTE_ONE_CHAR:
4955 		    if (ret_char == -1)
4956 		    {
4957 			if (has_mbyte)
4958 			    ret_char = (*mb_ptr2char)(buf);
4959 			else
4960 			    ret_char = buf[0];
4961 		    }
4962 		    break;
4963 	    }
4964 	}
4965 	idx = 0;
4966     }
4967 
4968     --no_mapping;
4969     allow_keys = save_allow_keys;
4970     if (!save_paste)
4971 	set_option_value((char_u *)"paste", FALSE, NULL, 0);
4972 
4973     return ret_char;
4974 }
4975 
4976 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4977     static void
4978 ins_tabline(int c)
4979 {
4980     /* We will be leaving the current window, unless closing another tab. */
4981     if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4982 		|| (current_tab != 0 && current_tab != tabpage_index(curtab)))
4983     {
4984 	undisplay_dollar();
4985 	start_arrow(&curwin->w_cursor);
4986 # ifdef FEAT_CINDENT
4987 	can_cindent = TRUE;
4988 # endif
4989     }
4990 
4991     if (c == K_TABLINE)
4992 	goto_tabpage(current_tab);
4993     else
4994     {
4995 	handle_tabmenu();
4996 	redraw_statuslines();	/* will redraw the tabline when needed */
4997     }
4998 }
4999 #endif
5000 
5001 #if defined(FEAT_GUI) || defined(PROTO)
5002     void
5003 ins_scroll(void)
5004 {
5005     pos_T	tpos;
5006 
5007     undisplay_dollar();
5008     tpos = curwin->w_cursor;
5009     if (gui_do_scroll())
5010     {
5011 	start_arrow(&tpos);
5012 # ifdef FEAT_CINDENT
5013 	can_cindent = TRUE;
5014 # endif
5015     }
5016 }
5017 
5018     void
5019 ins_horscroll(void)
5020 {
5021     pos_T	tpos;
5022 
5023     undisplay_dollar();
5024     tpos = curwin->w_cursor;
5025     if (gui_do_horiz_scroll(scrollbar_value, FALSE))
5026     {
5027 	start_arrow(&tpos);
5028 # ifdef FEAT_CINDENT
5029 	can_cindent = TRUE;
5030 # endif
5031     }
5032 }
5033 #endif
5034 
5035     static void
5036 ins_left(void)
5037 {
5038     pos_T	tpos;
5039     int		end_change = dont_sync_undo == FALSE; // end undoable change
5040 
5041 #ifdef FEAT_FOLDING
5042     if ((fdo_flags & FDO_HOR) && KeyTyped)
5043 	foldOpenCursor();
5044 #endif
5045     undisplay_dollar();
5046     tpos = curwin->w_cursor;
5047     if (oneleft() == OK)
5048     {
5049 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5050 	/* Only call start_arrow() when not busy with preediting, it will
5051 	 * break undo.  K_LEFT is inserted in im_correct_cursor(). */
5052 	if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
5053 #endif
5054 	{
5055 	    start_arrow_with_change(&tpos, end_change);
5056 	    if (!end_change)
5057 		AppendCharToRedobuff(K_LEFT);
5058 	}
5059 #ifdef FEAT_RIGHTLEFT
5060 	/* If exit reversed string, position is fixed */
5061 	if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5062 	    revins_legal++;
5063 	revins_chars++;
5064 #endif
5065     }
5066 
5067     /*
5068      * if 'whichwrap' set for cursor in insert mode may go to
5069      * previous line
5070      */
5071     else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5072     {
5073 	/* always break undo when moving upwards/downwards, else undo may break */
5074 	start_arrow(&tpos);
5075 	--(curwin->w_cursor.lnum);
5076 	coladvance((colnr_T)MAXCOL);
5077 	curwin->w_set_curswant = TRUE;	/* so we stay at the end */
5078     }
5079     else
5080 	vim_beep(BO_CRSR);
5081     dont_sync_undo = FALSE;
5082 }
5083 
5084     static void
5085 ins_home(int c)
5086 {
5087     pos_T	tpos;
5088 
5089 #ifdef FEAT_FOLDING
5090     if ((fdo_flags & FDO_HOR) && KeyTyped)
5091 	foldOpenCursor();
5092 #endif
5093     undisplay_dollar();
5094     tpos = curwin->w_cursor;
5095     if (c == K_C_HOME)
5096 	curwin->w_cursor.lnum = 1;
5097     curwin->w_cursor.col = 0;
5098     curwin->w_cursor.coladd = 0;
5099     curwin->w_curswant = 0;
5100     start_arrow(&tpos);
5101 }
5102 
5103     static void
5104 ins_end(int c)
5105 {
5106     pos_T	tpos;
5107 
5108 #ifdef FEAT_FOLDING
5109     if ((fdo_flags & FDO_HOR) && KeyTyped)
5110 	foldOpenCursor();
5111 #endif
5112     undisplay_dollar();
5113     tpos = curwin->w_cursor;
5114     if (c == K_C_END)
5115 	curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5116     coladvance((colnr_T)MAXCOL);
5117     curwin->w_curswant = MAXCOL;
5118 
5119     start_arrow(&tpos);
5120 }
5121 
5122     static void
5123 ins_s_left()
5124 {
5125     int end_change = dont_sync_undo == FALSE; // end undoable change
5126 #ifdef FEAT_FOLDING
5127     if ((fdo_flags & FDO_HOR) && KeyTyped)
5128 	foldOpenCursor();
5129 #endif
5130     undisplay_dollar();
5131     if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5132     {
5133 	start_arrow_with_change(&curwin->w_cursor, end_change);
5134 	if (!end_change)
5135 	    AppendCharToRedobuff(K_S_LEFT);
5136 	(void)bck_word(1L, FALSE, FALSE);
5137 	curwin->w_set_curswant = TRUE;
5138     }
5139     else
5140 	vim_beep(BO_CRSR);
5141     dont_sync_undo = FALSE;
5142 }
5143 
5144     static void
5145 ins_right(void)
5146 {
5147     int end_change = dont_sync_undo == FALSE; // end undoable change
5148 
5149 #ifdef FEAT_FOLDING
5150     if ((fdo_flags & FDO_HOR) && KeyTyped)
5151 	foldOpenCursor();
5152 #endif
5153     undisplay_dollar();
5154     if (gchar_cursor() != NUL || virtual_active())
5155     {
5156 	start_arrow_with_change(&curwin->w_cursor, end_change);
5157 	if (!end_change)
5158 	    AppendCharToRedobuff(K_RIGHT);
5159 	curwin->w_set_curswant = TRUE;
5160 	if (virtual_active())
5161 	    oneright();
5162 	else
5163 	{
5164 	    if (has_mbyte)
5165 		curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
5166 	    else
5167 		++curwin->w_cursor.col;
5168 	}
5169 
5170 #ifdef FEAT_RIGHTLEFT
5171 	revins_legal++;
5172 	if (revins_chars)
5173 	    revins_chars--;
5174 #endif
5175     }
5176     /* if 'whichwrap' set for cursor in insert mode, may move the
5177      * cursor to the next line */
5178     else if (vim_strchr(p_ww, ']') != NULL
5179 	    && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5180     {
5181 	start_arrow(&curwin->w_cursor);
5182 	curwin->w_set_curswant = TRUE;
5183 	++curwin->w_cursor.lnum;
5184 	curwin->w_cursor.col = 0;
5185     }
5186     else
5187 	vim_beep(BO_CRSR);
5188     dont_sync_undo = FALSE;
5189 }
5190 
5191     static void
5192 ins_s_right()
5193 {
5194     int end_change = dont_sync_undo == FALSE; // end undoable change
5195 #ifdef FEAT_FOLDING
5196     if ((fdo_flags & FDO_HOR) && KeyTyped)
5197 	foldOpenCursor();
5198 #endif
5199     undisplay_dollar();
5200     if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5201 	    || gchar_cursor() != NUL)
5202     {
5203 	start_arrow_with_change(&curwin->w_cursor, end_change);
5204 	if (!end_change)
5205 	    AppendCharToRedobuff(K_S_RIGHT);
5206 	(void)fwd_word(1L, FALSE, 0);
5207 	curwin->w_set_curswant = TRUE;
5208     }
5209     else
5210 	vim_beep(BO_CRSR);
5211     dont_sync_undo = FALSE;
5212 }
5213 
5214     static void
5215 ins_up(
5216     int		startcol)	/* when TRUE move to Insstart.col */
5217 {
5218     pos_T	tpos;
5219     linenr_T	old_topline = curwin->w_topline;
5220 #ifdef FEAT_DIFF
5221     int		old_topfill = curwin->w_topfill;
5222 #endif
5223 
5224     undisplay_dollar();
5225     tpos = curwin->w_cursor;
5226     if (cursor_up(1L, TRUE) == OK)
5227     {
5228 	if (startcol)
5229 	    coladvance(getvcol_nolist(&Insstart));
5230 	if (old_topline != curwin->w_topline
5231 #ifdef FEAT_DIFF
5232 		|| old_topfill != curwin->w_topfill
5233 #endif
5234 		)
5235 	    redraw_later(VALID);
5236 	start_arrow(&tpos);
5237 #ifdef FEAT_CINDENT
5238 	can_cindent = TRUE;
5239 #endif
5240     }
5241     else
5242 	vim_beep(BO_CRSR);
5243 }
5244 
5245     static void
5246 ins_pageup(void)
5247 {
5248     pos_T	tpos;
5249 
5250     undisplay_dollar();
5251 
5252     if (mod_mask & MOD_MASK_CTRL)
5253     {
5254 	/* <C-PageUp>: tab page back */
5255 	if (first_tabpage->tp_next != NULL)
5256 	{
5257 	    start_arrow(&curwin->w_cursor);
5258 	    goto_tabpage(-1);
5259 	}
5260 	return;
5261     }
5262 
5263     tpos = curwin->w_cursor;
5264     if (onepage(BACKWARD, 1L) == OK)
5265     {
5266 	start_arrow(&tpos);
5267 #ifdef FEAT_CINDENT
5268 	can_cindent = TRUE;
5269 #endif
5270     }
5271     else
5272 	vim_beep(BO_CRSR);
5273 }
5274 
5275     static void
5276 ins_down(
5277     int		startcol)	/* when TRUE move to Insstart.col */
5278 {
5279     pos_T	tpos;
5280     linenr_T	old_topline = curwin->w_topline;
5281 #ifdef FEAT_DIFF
5282     int		old_topfill = curwin->w_topfill;
5283 #endif
5284 
5285     undisplay_dollar();
5286     tpos = curwin->w_cursor;
5287     if (cursor_down(1L, TRUE) == OK)
5288     {
5289 	if (startcol)
5290 	    coladvance(getvcol_nolist(&Insstart));
5291 	if (old_topline != curwin->w_topline
5292 #ifdef FEAT_DIFF
5293 		|| old_topfill != curwin->w_topfill
5294 #endif
5295 		)
5296 	    redraw_later(VALID);
5297 	start_arrow(&tpos);
5298 #ifdef FEAT_CINDENT
5299 	can_cindent = TRUE;
5300 #endif
5301     }
5302     else
5303 	vim_beep(BO_CRSR);
5304 }
5305 
5306     static void
5307 ins_pagedown(void)
5308 {
5309     pos_T	tpos;
5310 
5311     undisplay_dollar();
5312 
5313     if (mod_mask & MOD_MASK_CTRL)
5314     {
5315 	/* <C-PageDown>: tab page forward */
5316 	if (first_tabpage->tp_next != NULL)
5317 	{
5318 	    start_arrow(&curwin->w_cursor);
5319 	    goto_tabpage(0);
5320 	}
5321 	return;
5322     }
5323 
5324     tpos = curwin->w_cursor;
5325     if (onepage(FORWARD, 1L) == OK)
5326     {
5327 	start_arrow(&tpos);
5328 #ifdef FEAT_CINDENT
5329 	can_cindent = TRUE;
5330 #endif
5331     }
5332     else
5333 	vim_beep(BO_CRSR);
5334 }
5335 
5336 #ifdef FEAT_DND
5337     static void
5338 ins_drop(void)
5339 {
5340     do_put('~', BACKWARD, 1L, PUT_CURSEND);
5341 }
5342 #endif
5343 
5344 /*
5345  * Handle TAB in Insert or Replace mode.
5346  * Return TRUE when the TAB needs to be inserted like a normal character.
5347  */
5348     static int
5349 ins_tab(void)
5350 {
5351     int		ind;
5352     int		i;
5353     int		temp;
5354 
5355     if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5356 	Insstart_blank_vcol = get_nolist_virtcol();
5357     if (echeck_abbr(TAB + ABBR_OFF))
5358 	return FALSE;
5359 
5360     ind = inindent(0);
5361 #ifdef FEAT_CINDENT
5362     if (ind)
5363 	can_cindent = FALSE;
5364 #endif
5365 
5366     /*
5367      * When nothing special, insert TAB like a normal character.
5368      */
5369     if (!curbuf->b_p_et
5370 #ifdef FEAT_VARTABS
5371 	    && !(p_sta && ind
5372 		/* These five lines mean 'tabstop' != 'shiftwidth' */
5373 		&& ((tabstop_count(curbuf->b_p_vts_array) > 1)
5374 		    || (tabstop_count(curbuf->b_p_vts_array) == 1
5375 		        && tabstop_first(curbuf->b_p_vts_array)
5376 						       != get_sw_value(curbuf))
5377 	            || (tabstop_count(curbuf->b_p_vts_array) == 0
5378 		        && curbuf->b_p_ts != get_sw_value(curbuf))))
5379 	    && tabstop_count(curbuf->b_p_vsts_array) == 0
5380 #else
5381 	    && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
5382 #endif
5383 	    && get_sts_value() == 0)
5384 	return TRUE;
5385 
5386     if (stop_arrow() == FAIL)
5387 	return TRUE;
5388 
5389     did_ai = FALSE;
5390 #ifdef FEAT_SMARTINDENT
5391     did_si = FALSE;
5392     can_si = FALSE;
5393     can_si_back = FALSE;
5394 #endif
5395     AppendToRedobuff((char_u *)"\t");
5396 
5397 #ifdef FEAT_VARTABS
5398     if (p_sta && ind)		/* insert tab in indent, use 'shiftwidth' */
5399     {
5400 	temp = (int)get_sw_value(curbuf);
5401 	temp -= get_nolist_virtcol() % temp;
5402     }
5403     else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
5404 	                        /* use 'softtabstop' when set */
5405 	temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
5406 						     curbuf->b_p_vsts_array);
5407     else			/* otherwise use 'tabstop' */
5408 	temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5409 						     curbuf->b_p_vts_array);
5410 #else
5411     if (p_sta && ind)		/* insert tab in indent, use 'shiftwidth' */
5412 	temp = (int)get_sw_value(curbuf);
5413     else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5414 	temp = (int)get_sts_value();
5415     else			/* otherwise use 'tabstop' */
5416 	temp = (int)curbuf->b_p_ts;
5417     temp -= get_nolist_virtcol() % temp;
5418 #endif
5419 
5420     /*
5421      * Insert the first space with ins_char().	It will delete one char in
5422      * replace mode.  Insert the rest with ins_str(); it will not delete any
5423      * chars.  For VREPLACE mode, we use ins_char() for all characters.
5424      */
5425     ins_char(' ');
5426     while (--temp > 0)
5427     {
5428 	if (State & VREPLACE_FLAG)
5429 	    ins_char(' ');
5430 	else
5431 	{
5432 	    ins_str((char_u *)" ");
5433 	    if (State & REPLACE_FLAG)	    /* no char replaced */
5434 		replace_push(NUL);
5435 	}
5436     }
5437 
5438     /*
5439      * When 'expandtab' not set: Replace spaces by TABs where possible.
5440      */
5441 #ifdef FEAT_VARTABS
5442     if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5443                             || get_sts_value() > 0
5444 			    || (p_sta && ind)))
5445 #else
5446     if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
5447 #endif
5448     {
5449 	char_u		*ptr;
5450 	char_u		*saved_line = NULL;	/* init for GCC */
5451 	pos_T		pos;
5452 	pos_T		fpos;
5453 	pos_T		*cursor;
5454 	colnr_T		want_vcol, vcol;
5455 	int		change_col = -1;
5456 	int		save_list = curwin->w_p_list;
5457 
5458 	/*
5459 	 * Get the current line.  For VREPLACE mode, don't make real changes
5460 	 * yet, just work on a copy of the line.
5461 	 */
5462 	if (State & VREPLACE_FLAG)
5463 	{
5464 	    pos = curwin->w_cursor;
5465 	    cursor = &pos;
5466 	    saved_line = vim_strsave(ml_get_curline());
5467 	    if (saved_line == NULL)
5468 		return FALSE;
5469 	    ptr = saved_line + pos.col;
5470 	}
5471 	else
5472 	{
5473 	    ptr = ml_get_cursor();
5474 	    cursor = &curwin->w_cursor;
5475 	}
5476 
5477 	/* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5478 	if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5479 	    curwin->w_p_list = FALSE;
5480 
5481 	/* Find first white before the cursor */
5482 	fpos = curwin->w_cursor;
5483 	while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
5484 	{
5485 	    --fpos.col;
5486 	    --ptr;
5487 	}
5488 
5489 	/* In Replace mode, don't change characters before the insert point. */
5490 	if ((State & REPLACE_FLAG)
5491 		&& fpos.lnum == Insstart.lnum
5492 		&& fpos.col < Insstart.col)
5493 	{
5494 	    ptr += Insstart.col - fpos.col;
5495 	    fpos.col = Insstart.col;
5496 	}
5497 
5498 	/* compute virtual column numbers of first white and cursor */
5499 	getvcol(curwin, &fpos, &vcol, NULL, NULL);
5500 	getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5501 
5502 	/* Use as many TABs as possible.  Beware of 'breakindent', 'showbreak'
5503 	 * and 'linebreak' adding extra virtual columns. */
5504 	while (VIM_ISWHITE(*ptr))
5505 	{
5506 	    i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
5507 	    if (vcol + i > want_vcol)
5508 		break;
5509 	    if (*ptr != TAB)
5510 	    {
5511 		*ptr = TAB;
5512 		if (change_col < 0)
5513 		{
5514 		    change_col = fpos.col;  /* Column of first change */
5515 		    /* May have to adjust Insstart */
5516 		    if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5517 			Insstart.col = fpos.col;
5518 		}
5519 	    }
5520 	    ++fpos.col;
5521 	    ++ptr;
5522 	    vcol += i;
5523 	}
5524 
5525 	if (change_col >= 0)
5526 	{
5527 	    int repl_off = 0;
5528 	    char_u *line = ptr;
5529 
5530 	    /* Skip over the spaces we need. */
5531 	    while (vcol < want_vcol && *ptr == ' ')
5532 	    {
5533 		vcol += lbr_chartabsize(line, ptr, vcol);
5534 		++ptr;
5535 		++repl_off;
5536 	    }
5537 	    if (vcol > want_vcol)
5538 	    {
5539 		/* Must have a char with 'showbreak' just before it. */
5540 		--ptr;
5541 		--repl_off;
5542 	    }
5543 	    fpos.col += repl_off;
5544 
5545 	    /* Delete following spaces. */
5546 	    i = cursor->col - fpos.col;
5547 	    if (i > 0)
5548 	    {
5549 		STRMOVE(ptr, ptr + i);
5550 		/* correct replace stack. */
5551 		if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
5552 		    for (temp = i; --temp >= 0; )
5553 			replace_join(repl_off);
5554 #ifdef FEAT_TEXT_PROP
5555 		curbuf->b_ml.ml_line_len -= i;
5556 #endif
5557 	    }
5558 #ifdef FEAT_NETBEANS_INTG
5559 	    if (netbeans_active())
5560 	    {
5561 		netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
5562 		netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5563 							   (char_u *)"\t", 1);
5564 	    }
5565 #endif
5566 	    cursor->col -= i;
5567 
5568 	    /*
5569 	     * In VREPLACE mode, we haven't changed anything yet.  Do it now by
5570 	     * backspacing over the changed spacing and then inserting the new
5571 	     * spacing.
5572 	     */
5573 	    if (State & VREPLACE_FLAG)
5574 	    {
5575 		/* Backspace from real cursor to change_col */
5576 		backspace_until_column(change_col);
5577 
5578 		/* Insert each char in saved_line from changed_col to
5579 		 * ptr-cursor */
5580 		ins_bytes_len(saved_line + change_col,
5581 						    cursor->col - change_col);
5582 	    }
5583 	}
5584 
5585 	if (State & VREPLACE_FLAG)
5586 	    vim_free(saved_line);
5587 	curwin->w_p_list = save_list;
5588     }
5589 
5590     return FALSE;
5591 }
5592 
5593 /*
5594  * Handle CR or NL in insert mode.
5595  * Return FAIL when out of memory or can't undo.
5596  */
5597     int
5598 ins_eol(int c)
5599 {
5600     int	    i;
5601 
5602     if (echeck_abbr(c + ABBR_OFF))
5603 	return OK;
5604     if (stop_arrow() == FAIL)
5605 	return FAIL;
5606     undisplay_dollar();
5607 
5608     /*
5609      * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5610      * character under the cursor.  Only push a NUL on the replace stack,
5611      * nothing to put back when the NL is deleted.
5612      */
5613     if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
5614 	replace_push(NUL);
5615 
5616     /*
5617      * In VREPLACE mode, a NL replaces the rest of the line, and starts
5618      * replacing the next line, so we push all of the characters left on the
5619      * line onto the replace stack.  This is not done here though, it is done
5620      * in open_line().
5621      */
5622 
5623     /* Put cursor on NUL if on the last char and coladd is 1 (happens after
5624      * CTRL-O). */
5625     if (virtual_active() && curwin->w_cursor.coladd > 0)
5626 	coladvance(getviscol());
5627 
5628 #ifdef FEAT_RIGHTLEFT
5629     /* NL in reverse insert will always start in the end of
5630      * current line. */
5631     if (revins_on)
5632 	curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5633 #endif
5634 
5635     AppendToRedobuff(NL_STR);
5636     i = open_line(FORWARD,
5637 	    has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
5638     old_indent = 0;
5639 #ifdef FEAT_CINDENT
5640     can_cindent = TRUE;
5641 #endif
5642 #ifdef FEAT_FOLDING
5643     /* When inserting a line the cursor line must never be in a closed fold. */
5644     foldOpenCursor();
5645 #endif
5646 
5647     return i;
5648 }
5649 
5650 #ifdef FEAT_DIGRAPHS
5651 /*
5652  * Handle digraph in insert mode.
5653  * Returns character still to be inserted, or NUL when nothing remaining to be
5654  * done.
5655  */
5656     static int
5657 ins_digraph(void)
5658 {
5659     int	    c;
5660     int	    cc;
5661     int	    did_putchar = FALSE;
5662 
5663     pc_status = PC_STATUS_UNSET;
5664     if (redrawing() && !char_avail())
5665     {
5666 	/* may need to redraw when no more chars available now */
5667 	ins_redraw(FALSE);
5668 
5669 	edit_putchar('?', TRUE);
5670 	did_putchar = TRUE;
5671 #ifdef FEAT_CMDL_INFO
5672 	add_to_showcmd_c(Ctrl_K);
5673 #endif
5674     }
5675 
5676 #ifdef USE_ON_FLY_SCROLL
5677     dont_scroll = TRUE;		/* disallow scrolling here */
5678 #endif
5679 
5680     /* don't map the digraph chars. This also prevents the
5681      * mode message to be deleted when ESC is hit */
5682     ++no_mapping;
5683     ++allow_keys;
5684     c = plain_vgetc();
5685     --no_mapping;
5686     --allow_keys;
5687     if (did_putchar)
5688 	/* when the line fits in 'columns' the '?' is at the start of the next
5689 	 * line and will not be removed by the redraw */
5690 	edit_unputchar();
5691 
5692     if (IS_SPECIAL(c) || mod_mask)	    /* special key */
5693     {
5694 #ifdef FEAT_CMDL_INFO
5695 	clear_showcmd();
5696 #endif
5697 	insert_special(c, TRUE, FALSE);
5698 	return NUL;
5699     }
5700     if (c != ESC)
5701     {
5702 	did_putchar = FALSE;
5703 	if (redrawing() && !char_avail())
5704 	{
5705 	    /* may need to redraw when no more chars available now */
5706 	    ins_redraw(FALSE);
5707 
5708 	    if (char2cells(c) == 1)
5709 	    {
5710 		ins_redraw(FALSE);
5711 		edit_putchar(c, TRUE);
5712 		did_putchar = TRUE;
5713 	    }
5714 #ifdef FEAT_CMDL_INFO
5715 	    add_to_showcmd_c(c);
5716 #endif
5717 	}
5718 	++no_mapping;
5719 	++allow_keys;
5720 	cc = plain_vgetc();
5721 	--no_mapping;
5722 	--allow_keys;
5723 	if (did_putchar)
5724 	    /* when the line fits in 'columns' the '?' is at the start of the
5725 	     * next line and will not be removed by a redraw */
5726 	    edit_unputchar();
5727 	if (cc != ESC)
5728 	{
5729 	    AppendToRedobuff((char_u *)CTRL_V_STR);
5730 	    c = getdigraph(c, cc, TRUE);
5731 #ifdef FEAT_CMDL_INFO
5732 	    clear_showcmd();
5733 #endif
5734 	    return c;
5735 	}
5736     }
5737 #ifdef FEAT_CMDL_INFO
5738     clear_showcmd();
5739 #endif
5740     return NUL;
5741 }
5742 #endif
5743 
5744 /*
5745  * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5746  * Returns the char to be inserted, or NUL if none found.
5747  */
5748     int
5749 ins_copychar(linenr_T lnum)
5750 {
5751     int	    c;
5752     int	    temp;
5753     char_u  *ptr, *prev_ptr;
5754     char_u  *line;
5755 
5756     if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5757     {
5758 	vim_beep(BO_COPY);
5759 	return NUL;
5760     }
5761 
5762     /* try to advance to the cursor column */
5763     temp = 0;
5764     line = ptr = ml_get(lnum);
5765     prev_ptr = ptr;
5766     validate_virtcol();
5767     while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5768     {
5769 	prev_ptr = ptr;
5770 	temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
5771     }
5772     if ((colnr_T)temp > curwin->w_virtcol)
5773 	ptr = prev_ptr;
5774 
5775     c = (*mb_ptr2char)(ptr);
5776     if (c == NUL)
5777 	vim_beep(BO_COPY);
5778     return c;
5779 }
5780 
5781 /*
5782  * CTRL-Y or CTRL-E typed in Insert mode.
5783  */
5784     static int
5785 ins_ctrl_ey(int tc)
5786 {
5787     int	    c = tc;
5788 
5789     if (ctrl_x_mode_scroll())
5790     {
5791 	if (c == Ctrl_Y)
5792 	    scrolldown_clamp();
5793 	else
5794 	    scrollup_clamp();
5795 	redraw_later(VALID);
5796     }
5797     else
5798     {
5799 	c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5800 	if (c != NUL)
5801 	{
5802 	    long	tw_save;
5803 
5804 	    /* The character must be taken literally, insert like it
5805 	     * was typed after a CTRL-V, and pretend 'textwidth'
5806 	     * wasn't set.  Digits, 'o' and 'x' are special after a
5807 	     * CTRL-V, don't use it for these. */
5808 	    if (c < 256 && !isalnum(c))
5809 		AppendToRedobuff((char_u *)CTRL_V_STR);	/* CTRL-V */
5810 	    tw_save = curbuf->b_p_tw;
5811 	    curbuf->b_p_tw = -1;
5812 	    insert_special(c, TRUE, FALSE);
5813 	    curbuf->b_p_tw = tw_save;
5814 #ifdef FEAT_RIGHTLEFT
5815 	    revins_chars++;
5816 	    revins_legal++;
5817 #endif
5818 	    c = Ctrl_V;	/* pretend CTRL-V is last character */
5819 	    auto_format(FALSE, TRUE);
5820 	}
5821     }
5822     return c;
5823 }
5824 
5825 /*
5826  * Get the value that w_virtcol would have when 'list' is off.
5827  * Unless 'cpo' contains the 'L' flag.
5828  */
5829     colnr_T
5830 get_nolist_virtcol(void)
5831 {
5832     // check validity of cursor in current buffer
5833     if (curwin->w_buffer == NULL
5834 	|| curwin->w_buffer->b_ml.ml_mfp == NULL
5835 	|| curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5836 	return 0;
5837     if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5838 	return getvcol_nolist(&curwin->w_cursor);
5839     validate_virtcol();
5840     return curwin->w_virtcol;
5841 }
5842 
5843 #if defined(FEAT_EVAL)
5844 /*
5845  * Handle the InsertCharPre autocommand.
5846  * "c" is the character that was typed.
5847  * Return a pointer to allocated memory with the replacement string.
5848  * Return NULL to continue inserting "c".
5849  */
5850     static char_u *
5851 do_insert_char_pre(int c)
5852 {
5853     char_u	*res;
5854     char_u	buf[MB_MAXBYTES + 1];
5855     int		save_State = State;
5856 
5857     /* Return quickly when there is nothing to do. */
5858     if (!has_insertcharpre())
5859 	return NULL;
5860 
5861     if (has_mbyte)
5862 	buf[(*mb_char2bytes)(c, buf)] = NUL;
5863     else
5864     {
5865 	buf[0] = c;
5866 	buf[1] = NUL;
5867     }
5868 
5869     /* Lock the text to avoid weird things from happening. */
5870     ++textlock;
5871     set_vim_var_string(VV_CHAR, buf, -1);  /* set v:char */
5872 
5873     res = NULL;
5874     if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
5875     {
5876 	/* Get the value of v:char.  It may be empty or more than one
5877 	 * character.  Only use it when changed, otherwise continue with the
5878 	 * original character to avoid breaking autoindent. */
5879 	if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5880 	    res = vim_strsave(get_vim_var_str(VV_CHAR));
5881     }
5882 
5883     set_vim_var_string(VV_CHAR, NULL, -1);  /* clear v:char */
5884     --textlock;
5885 
5886     // Restore the State, it may have been changed.
5887     State = save_State;
5888 
5889     return res;
5890 }
5891 #endif
5892 
5893 #if defined(FEAT_CINDENT) || defined(PROTO)
5894     int
5895 get_can_cindent(void)
5896 {
5897     return can_cindent;
5898 }
5899 
5900     void
5901 set_can_cindent(int val)
5902 {
5903     can_cindent = val;
5904 }
5905 #endif
5906 
5907 /*
5908  * Trigger "event" and take care of fixing undo.
5909  */
5910     int
5911 ins_apply_autocmds(event_T event)
5912 {
5913     varnumber_T	tick = CHANGEDTICK(curbuf);
5914     int r;
5915 
5916     r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5917 
5918     // If u_savesub() was called then we are not prepared to start
5919     // a new line.  Call u_save() with no contents to fix that.
5920     if (tick != CHANGEDTICK(curbuf))
5921 	u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5922 
5923     return r;
5924 }
5925