summaryrefslogtreecommitdiffstats
path: root/src/edit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/edit.c')
-rw-r--r--src/edit.c7614
1 files changed, 7614 insertions, 0 deletions
diff --git a/src/edit.c b/src/edit.c
new file mode 100644
index 0000000000..9a42f5ae86
--- /dev/null
+++ b/src/edit.c
@@ -0,0 +1,7614 @@
+/* vi:set ts=8 sts=4 sw=4:
+ *
+ * VIM - Vi IMproved by Bram Moolenaar
+ *
+ * Do ":help uganda" in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * edit.c: functions for Insert mode
+ */
+
+#include "vim.h"
+
+#ifdef FEAT_INS_EXPAND
+/*
+ * definitions used for CTRL-X submode
+ */
+#define CTRL_X_WANT_IDENT 0x100
+
+#define CTRL_X_NOT_DEFINED_YET 1
+#define CTRL_X_SCROLL 2
+#define CTRL_X_WHOLE_LINE 3
+#define CTRL_X_FILES 4
+#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
+#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
+#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
+#define CTRL_X_FINISHED 8
+#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
+#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
+#define CTRL_X_CMDLINE 11
+
+#define CHECK_KEYS_TIME 30
+
+#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
+
+static char *ctrl_x_msgs[] =
+{
+ N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
+ N_(" ^X mode (^E^Y^L^]^F^I^K^D^V^N^P)"),
+ /* Scroll has it's own msgs, in it's place there is the msg for local
+ * ctrl_x_mode = 0 (eg continue_status & CONT_LOCAL) -- Acevedo */
+ N_(" Keyword Local completion (^N^P)"),
+ N_(" Whole line completion (^L^N^P)"),
+ N_(" File name completion (^F^N^P)"),
+ N_(" Tag completion (^]^N^P)"),
+ N_(" Path pattern completion (^N^P)"),
+ N_(" Definition completion (^D^N^P)"),
+ NULL,
+ N_(" Dictionary completion (^K^N^P)"),
+ N_(" Thesaurus completion (^T^N^P)"),
+ N_(" Command-line completion (^V^N^P)")
+};
+
+static char_u e_hitend[] = N_("Hit end of paragraph");
+
+/*
+ * Structure used to store one match for insert completion.
+ */
+struct Completion
+{
+ struct Completion *next;
+ struct Completion *prev;
+ char_u *str; /* matched text */
+ char_u *fname; /* file containing the match */
+ int original; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
+ int number; /* sequence number */
+};
+
+/* the original text when the expansion begun */
+#define ORIGINAL_TEXT (1)
+#define FREE_FNAME (2)
+
+/*
+ * All the current matches are stored in a list.
+ * "first_match" points to the start of the list.
+ * "curr_match" points to the currently selected entry.
+ * "shown_match" is different from curr_match during ins_compl_get_exp().
+ */
+static struct Completion *first_match = NULL;
+static struct Completion *curr_match = NULL;
+static struct Completion *shown_match = NULL;
+
+static int started_completion = FALSE;
+static int completion_matches = 0;
+static char_u *complete_pat = NULL;
+static int complete_direction = FORWARD;
+static int shown_direction = FORWARD;
+static int completion_pending = FALSE;
+static pos_T initial_pos;
+static colnr_T complete_col = 0; /* column where the text starts
+ that is being completed */
+static int save_sm;
+static char_u *original_text = NULL; /* text before completion */
+static int continue_mode = 0;
+static expand_T complete_xp;
+
+static int ins_compl_add __ARGS((char_u *str, int len, char_u *, int dir, int reuse));
+static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir));
+static int ins_compl_make_cyclic __ARGS((void));
+static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
+static void ins_compl_free __ARGS((void));
+static void ins_compl_clear __ARGS((void));
+static void ins_compl_prep __ARGS((int c));
+static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
+static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
+static void ins_compl_delete __ARGS((void));
+static void ins_compl_insert __ARGS((void));
+static int ins_compl_next __ARGS((int allow_get_expansion));
+static int ins_complete __ARGS((int c));
+static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
+#endif /* FEAT_INS_EXPAND */
+
+#define BACKSPACE_CHAR 1
+#define BACKSPACE_WORD 2
+#define BACKSPACE_WORD_NOT_SPACE 3
+#define BACKSPACE_LINE 4
+
+static void ins_redraw __ARGS((void));
+static void ins_ctrl_v __ARGS((void));
+static void undisplay_dollar __ARGS((void));
+static void insert_special __ARGS((int, int, int));
+static void check_auto_format __ARGS((int));
+static void redo_literal __ARGS((int c));
+static void start_arrow __ARGS((pos_T *end_insert_pos));
+static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
+static int echeck_abbr __ARGS((int));
+static void replace_push_off __ARGS((int c));
+static int replace_pop __ARGS((void));
+static void replace_join __ARGS((int off));
+static void replace_pop_ins __ARGS((void));
+#ifdef FEAT_MBYTE
+static void mb_replace_pop_ins __ARGS((int cc));
+#endif
+static void replace_flush __ARGS((void));
+static void replace_do_bs __ARGS((void));
+#ifdef FEAT_CINDENT
+static int cindent_on __ARGS((void));
+#endif
+static void ins_reg __ARGS((void));
+static void ins_ctrl_g __ARGS((void));
+static int ins_esc __ARGS((long *count, int cmdchar));
+#ifdef FEAT_RIGHTLEFT
+static void ins_ctrl_ __ARGS((void));
+#endif
+#ifdef FEAT_VISUAL
+static int ins_start_select __ARGS((int c));
+#endif
+static void ins_shift __ARGS((int c, int lastc));
+static void ins_del __ARGS((void));
+static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
+#ifdef FEAT_MOUSE
+static void ins_mouse __ARGS((int c));
+static void ins_mousescroll __ARGS((int up));
+#endif
+static void ins_left __ARGS((void));
+static void ins_home __ARGS((int c));
+static void ins_end __ARGS((int c));
+static void ins_s_left __ARGS((void));
+static void ins_right __ARGS((void));
+static void ins_s_right __ARGS((void));
+static void ins_up __ARGS((int startcol));
+static void ins_pageup __ARGS((void));
+static void ins_down __ARGS((int startcol));
+static void ins_pagedown __ARGS((void));
+#ifdef FEAT_DND
+static void ins_drop __ARGS((void));
+#endif
+static int ins_tab __ARGS((void));
+static int ins_eol __ARGS((int c));
+#ifdef FEAT_DIGRAPHS
+static int ins_digraph __ARGS((void));
+#endif
+static int ins_copychar __ARGS((linenr_T lnum));
+#ifdef FEAT_SMARTINDENT
+static void ins_try_si __ARGS((int c));
+#endif
+static colnr_T get_nolist_virtcol __ARGS((void));
+
+static colnr_T Insstart_textlen; /* length of line when insert started */
+static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
+
+static char_u *last_insert = NULL; /* the text of the previous insert,
+ K_SPECIAL and CSI are escaped */
+static int last_insert_skip; /* nr of chars in front of previous insert */
+static int new_insert_skip; /* nr of chars in front of current insert */
+
+#ifdef FEAT_CINDENT
+static int can_cindent; /* may do cindenting on this line */
+#endif
+
+static int old_indent = 0; /* for ^^D command in insert mode */
+
+#ifdef FEAT_RIGHTLEFT
+int revins_on; /* reverse insert mode on */
+int revins_chars; /* how much to skip after edit */
+int revins_legal; /* was the last char 'legal'? */
+int revins_scol; /* start column of revins session */
+#endif
+
+#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
+static short previous_script = smRoman;
+#endif
+
+static int ins_need_undo; /* call u_save() before inserting a
+ char. Set when edit() is called.
+ after that arrow_used is used. */
+
+static int did_add_space = FALSE; /* auto_format() added an extra space
+ under the cursor */
+
+/*
+ * edit(): Start inserting text.
+ *
+ * "cmdchar" can be:
+ * 'i' normal insert command
+ * 'a' normal append command
+ * 'R' replace command
+ * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
+ * but still only one <CR> is inserted. The <Esc> is not used for redo.
+ * 'g' "gI" command.
+ * 'V' "gR" command for Virtual Replace mode.
+ * 'v' "gr" command for single character Virtual Replace mode.
+ *
+ * This function is not called recursively. For CTRL-O commands, it returns
+ * and lets the caller handle the Normal-mode command.
+ *
+ * Return TRUE if a CTRL-O command caused the return (insert mode pending).
+ */
+ int
+edit(cmdchar, startln, count)
+ int cmdchar;
+ int startln; /* if set, insert at start of line */
+ long count;
+{
+ int c = 0;
+ char_u *ptr;
+ int lastc;
+ colnr_T mincol;
+ static linenr_T o_lnum = 0;
+ static int o_eol = FALSE;
+ int i;
+ int did_backspace = TRUE; /* previous char was backspace */
+#ifdef FEAT_CINDENT
+ int line_is_white = FALSE; /* line is empty before insert */
+#endif
+ linenr_T old_topline = 0; /* topline before insertion */
+#ifdef FEAT_DIFF
+ int old_topfill = -1;
+#endif
+ int inserted_space = FALSE; /* just inserted a space */
+ int replaceState = REPLACE;
+ int did_restart_edit = restart_edit;
+
+ /* sleep before redrawing, needed for "CTRL-O :" that results in an
+ * error message */
+ check_for_delay(TRUE);
+
+#ifdef HAVE_SANDBOX
+ /* Don't allow inserting in the sandbox. */
+ if (sandbox != 0)
+ {
+ EMSG(_(e_sandbox));
+ return FALSE;
+ }
+#endif
+
+#ifdef FEAT_INS_EXPAND
+ ins_compl_clear(); /* clear stuff for CTRL-X mode */
+#endif
+
+#ifdef FEAT_MOUSE
+ /*
+ * When doing a paste with the middle mouse button, Insstart is set to
+ * where the paste started.
+ */
+ if (where_paste_started.lnum != 0)
+ Insstart = where_paste_started;
+ else
+#endif
+ {
+ Insstart = curwin->w_cursor;
+ if (startln)
+ Insstart.col = 0;
+ }
+ Insstart_textlen = linetabsize(ml_get_curline());
+ Insstart_blank_vcol = MAXCOL;
+ if (!did_ai)
+ ai_col = 0;
+
+ if (cmdchar != NUL && restart_edit == 0)
+ {
+ ResetRedobuff();
+ AppendNumberToRedobuff(count);
+#ifdef FEAT_VREPLACE
+ if (cmdchar == 'V' || cmdchar == 'v')
+ {
+ /* "gR" or "gr" command */
+ AppendCharToRedobuff('g');
+ AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
+ }
+ else
+#endif
+ {
+ AppendCharToRedobuff(cmdchar);
+ if (cmdchar == 'g') /* "gI" command */
+ AppendCharToRedobuff('I');
+ else if (cmdchar == 'r') /* "r<CR>" command */
+ count = 1; /* insert only one <CR> */
+ }
+ }
+
+ if (cmdchar == 'R')
+ {
+#ifdef FEAT_FKMAP
+ if (p_fkmap && p_ri)
+ {
+ beep_flush();
+ EMSG(farsi_text_3); /* encoded in Farsi */
+ State = INSERT;
+ }
+ else
+#endif
+ State = REPLACE;
+ }
+#ifdef FEAT_VREPLACE
+ else if (cmdchar == 'V' || cmdchar == 'v')
+ {
+ State = VREPLACE;
+ replaceState = VREPLACE;
+ orig_line_count = curbuf->b_ml.ml_line_count;
+ vr_lines_changed = 1;
+ }
+#endif
+ else
+ State = INSERT;
+
+ stop_insert_mode = FALSE;
+
+ /*
+ * Need to recompute the cursor position, it might move when the cursor is
+ * on a TAB or special character.
+ */
+ curs_columns(TRUE);
+
+ /*
+ * Enable langmap or IME, indicated by 'iminsert'.
+ * Note that IME may enabled/disabled without us noticing here, thus the
+ * 'iminsert' value may not reflect what is actually used. It is updated
+ * when hitting <Esc>.
+ */
+ if (curbuf->b_p_iminsert == B_IMODE_LMAP)
+ State |= LANGMAP;
+#ifdef USE_IM_CONTROL
+ im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
+#endif
+
+#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
+ KeyScript(previous_script);
+#endif
+
+#ifdef FEAT_MOUSE
+ setmouse();
+#endif
+#ifdef FEAT_CMDL_INFO
+ clear_showcmd();
+#endif
+#ifdef FEAT_RIGHTLEFT
+ /* there is no reverse replace mode */
+ revins_on = (State == INSERT && p_ri);
+ if (revins_on)
+ undisplay_dollar();
+ revins_chars = 0;
+ revins_legal = 0;
+ revins_scol = -1;
+#endif
+
+ /*
+ * Handle restarting Insert mode.
+ * Don't do this for "CTRL-O ." (repeat an insert): we get here with
+ * restart_edit non-zero, and something in the stuff buffer.
+ */
+ if (restart_edit != 0 && stuff_empty())
+ {
+#ifdef FEAT_MOUSE
+ /*
+ * After a paste we consider text typed to be part of the insert for
+ * the pasted text. You can backspace over the pasted text too.
+ */
+ if (where_paste_started.lnum)
+ arrow_used = FALSE;
+ else
+#endif
+ arrow_used = TRUE;
+ restart_edit = 0;
+
+ /*
+ * If the cursor was after the end-of-line before the CTRL-O and it is
+ * now at the end-of-line, put it after the end-of-line (this is not
+ * correct in very rare cases).
+ * Also do this if curswant is greater than the current virtual
+ * column. Eg after "^O$" or "^O80|".
+ */
+ validate_virtcol();
+ update_curswant();
+ if (((o_eol && curwin->w_cursor.lnum == o_lnum)
+ || curwin->w_curswant > curwin->w_virtcol)
+ && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
+ {
+ if (ptr[1] == NUL)
+ ++curwin->w_cursor.col;
+#ifdef FEAT_MBYTE
+ else if (has_mbyte)
+ {
+ i = (*mb_ptr2len_check)(ptr);
+ if (ptr[i] == NUL)
+ curwin->w_cursor.col += i;
+ }
+#endif
+ }
+ o_eol = FALSE;
+ }
+ else
+ arrow_used = FALSE;
+
+ /* we are in insert mode now, don't need to start it anymore */
+ need_start_insertmode = FALSE;
+
+ /* Need to save the line for undo before inserting the first char. */
+ ins_need_undo = TRUE;
+
+#ifdef FEAT_MOUSE
+ where_paste_started.lnum = 0;
+#endif
+#ifdef FEAT_CINDENT
+ can_cindent = TRUE;
+#endif
+#ifdef FEAT_FOLDING
+ /* The cursor line is not in a closed fold, unless 'insertmode' is set or
+ * restarting. */
+ if (!p_im && did_restart_edit == 0)
+ foldOpenCursor();
+#endif
+
+ /*
+ * If 'showmode' is set, show the current (insert/replace/..) mode.
+ * A warning message for changing a readonly file is given here, before
+ * actually changing anything. It's put after the mode, if any.
+ */
+ i = 0;
+ if (p_smd)
+ i = showmode();
+
+ if (!p_im && did_restart_edit == 0)
+ change_warning(i + 1);
+
+#ifdef CURSOR_SHAPE
+ ui_cursor_shape(); /* may show different cursor shape */
+#endif
+#ifdef FEAT_DIGRAPHS
+ do_digraph(-1); /* clear digraphs */
+#endif
+
+/*
+ * Get the current length of the redo buffer, those characters have to be
+ * skipped if we want to get to the inserted characters.
+ */
+ ptr = get_inserted();
+ if (ptr == NULL)
+ new_insert_skip = 0;
+ else
+ {
+ new_insert_skip = (int)STRLEN(ptr);
+ vim_free(ptr);
+ }
+
+ old_indent = 0;
+
+ /*
+ * Main loop in Insert mode: repeat until Insert mode is left.
+ */
+ for (;;)
+ {
+#ifdef FEAT_RIGHTLEFT
+ if (!revins_legal)
+ revins_scol = -1; /* reset on illegal motions */
+ else
+ revins_legal = 0;
+#endif
+ if (arrow_used) /* don't repeat insert when arrow key used */
+ count = 0;
+
+ if (stop_insert_mode)
+ {
+ /* ":stopinsert" used or 'insertmode' reset */
+ count = 0;
+ goto doESCkey;
+ }
+
+ /* set curwin->w_curswant for next K_DOWN or K_UP */
+ if (!arrow_used)
+ curwin->w_set_curswant = TRUE;
+
+ /* If there is no typeahead may check for timestamps (e.g., for when a
+ * menu invoked a shell command). */
+ if (stuff_empty())
+ {
+ did_check_timestamps = FALSE;
+ if (need_check_timestamps)
+ check_timestamps(FALSE);
+ }
+
+ /*
+ * When emsg() was called msg_scroll will have been set.
+ */
+ msg_scroll = FALSE;
+
+#ifdef FEAT_GUI
+ /* When 'mousefocus' is set a mouse movement may have taken us to
+ * another window. "need_mouse_correct" may then be set because of an
+ * autocommand. */
+ if (need_mouse_correct)
+ gui_mouse_correct();
+#endif
+
+#ifdef FEAT_FOLDING
+ /* Open fold at the cursor line, according to 'foldopen'. */
+ if (fdo_flags & FDO_INSERT)
+ foldOpenCursor();
+ /* Close folds where the cursor isn't, according to 'foldclose' */
+ if (!char_avail())
+ foldCheckClose();
+#endif
+
+ /*
+ * If we inserted a character at the last position of the last line in
+ * the window, scroll the window one line up. This avoids an extra
+ * redraw.
+ * This is detected when the cursor column is smaller after inserting
+ * something.
+ * Don't do this when the topline changed already, it has
+ * already been adjusted (by insertchar() calling open_line())).
+ */
+ if (curbuf->b_mod_set
+ && curwin->w_p_wrap
+ && !did_backspace
+ && curwin->w_topline == old_topline
+#ifdef FEAT_DIFF
+ && curwin->w_topfill == old_topfill
+#endif
+ )
+ {
+ mincol = curwin->w_wcol;
+ validate_cursor_col();
+
+ if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
+ && curwin->w_wrow == W_WINROW(curwin)
+ + curwin->w_height - 1 - p_so
+ && (curwin->w_cursor.lnum != curwin->w_topline
+#ifdef FEAT_DIFF
+ || curwin->w_topfill > 0
+#endif
+ ))
+ {
+#ifdef FEAT_DIFF
+ if (curwin->w_topfill > 0)
+ --curwin->w_topfill;
+ else
+#endif
+#ifdef FEAT_FOLDING
+ if (hasFolding(curwin->w_topline, NULL, &old_topline))
+ set_topline(curwin, old_topline + 1);
+ else
+#endif
+ set_topline(curwin, curwin->w_topline + 1);
+ }
+ }
+
+ /* May need to adjust w_topline to show the cursor. */
+ update_topline();
+
+ did_backspace = FALSE;
+
+ validate_cursor(); /* may set must_redraw */
+
+ /*
+ * Redraw the display when no characters are waiting.
+ * Also shows mode, ruler and positions cursor.
+ */
+ ins_redraw();
+
+#ifdef FEAT_SCROLLBIND
+ if (curwin->w_p_scb)
+ do_check_scrollbind(TRUE);
+#endif
+
+ update_curswant();
+ old_topline = curwin->w_topline;
+#ifdef FEAT_DIFF
+ old_topfill = curwin->w_topfill;
+#endif
+
+#ifdef USE_ON_FLY_SCROLL
+ dont_scroll = FALSE; /* allow scrolling here */
+#endif
+
+ /*
+ * Get a character for Insert mode.
+ */
+ lastc = c; /* remember previous char for CTRL-D */
+ c = safe_vgetc();
+
+#ifdef FEAT_RIGHTLEFT
+ if (p_hkmap && KeyTyped)
+ c = hkmap(c); /* Hebrew mode mapping */
+#endif
+#ifdef FEAT_FKMAP
+ if (p_fkmap && KeyTyped)
+ c = fkmap(c); /* Farsi mode mapping */
+#endif
+
+#ifdef FEAT_INS_EXPAND
+ /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
+ * it does fix up the text when finishing completion. */
+ ins_compl_prep(c);
+#endif
+
+ /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to mode
+ * selected with 'insertmode'. */
+ if (c == Ctrl_BSL)
+ {
+ /* may need to redraw when no more chars available now */
+ ins_redraw();
+ ++no_mapping;
+ ++allow_keys;
+ c = safe_vgetc();
+ --no_mapping;
+ --allow_keys;
+ if (c != Ctrl_N && c != Ctrl_G) /* it's something else */
+ {
+ vungetc(c);
+ c = Ctrl_BSL;
+ }
+ else if (c == Ctrl_G && p_im)
+ continue;
+ else
+ {
+ count = 0;
+ goto doESCkey;
+ }
+ }
+
+#ifdef FEAT_DIGRAPHS
+ c = do_digraph(c);
+#endif
+
+#ifdef FEAT_INS_EXPAND
+ if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
+ goto docomplete;
+#endif
+ if (c == Ctrl_V || c == Ctrl_Q)
+ {
+ ins_ctrl_v();
+ c = Ctrl_V; /* pretend CTRL-V is last typed character */
+ continue;
+ }
+
+#ifdef FEAT_CINDENT
+ if (cindent_on()
+# ifdef FEAT_INS_EXPAND
+ && ctrl_x_mode == 0
+# endif
+ )
+ {
+ /* A key name preceded by a bang means this key is not to be
+ * inserted. Skip ahead to the re-indenting below.
+ * A key name preceded by a star means that indenting has to be
+ * done before inserting the key. */
+ line_is_white = inindent(0);
+ if (in_cinkeys(c, '!', line_is_white))
+ goto force_cindent;
+ if (can_cindent && in_cinkeys(c, '*', line_is_white)
+ && stop_arrow() == OK)
+ do_c_expr_indent();
+ }
+#endif
+
+#ifdef FEAT_RIGHTLEFT
+ if (curwin->w_p_rl)
+ switch (c)
+ {
+ case K_LEFT: c = K_RIGHT; break;
+ case K_S_LEFT: c = K_S_RIGHT; break;
+ case K_C_LEFT: c = K_C_RIGHT; break;
+ case K_RIGHT: c = K_LEFT; break;
+ case K_S_RIGHT: c = K_S_LEFT; break;
+ case K_C_RIGHT: c = K_C_LEFT; break;
+ }
+#endif
+
+#ifdef FEAT_VISUAL
+ /*
+ * If 'keymodel' contains "startsel", may start selection. If it
+ * does, a CTRL-O and c will be stuffed, we need to get these
+ * characters.
+ */
+ if (ins_start_select(c))
+ continue;
+#endif
+
+ /*
+ * The big switch to handle a character in insert mode.
+ */
+ switch (c)
+ {
+ /* toggle insert/replace mode */
+ case K_INS:
+ case K_KINS:
+#ifdef FEAT_FKMAP
+ if (p_fkmap && p_ri)
+ {
+ beep_flush();
+ EMSG(farsi_text_3); /* encoded in Farsi */
+ break;
+ }
+#endif
+ if (State & REPLACE_FLAG)
+ State = INSERT | (State & LANGMAP);
+ else
+ State = replaceState | (State & LANGMAP);
+ AppendCharToRedobuff(K_INS);
+ showmode();
+#ifdef CURSOR_SHAPE
+ ui_cursor_shape(); /* may show different cursor shape */
+#endif
+ break;
+
+#ifdef FEAT_INS_EXPAND
+ /* Enter CTRL-X mode */
+ case Ctrl_X:
+ /* CTRL-X after CTRL-V CTRL-X doesn't do anything, so that CTRL-X
+ * CTRL-V works like CTRL-N */
+ if (ctrl_x_mode != CTRL_X_CMDLINE)
+ {
+ /* if the next ^X<> won't ADD nothing, then reset
+ * continue_status */
+ if (continue_status & CONT_N_ADDS)
+ continue_status = (continue_status | CONT_INTRPT);
+ else
+ continue_status = 0;
+ /* We're not sure which CTRL-X mode it will be yet */
+ ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
+ edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
+ edit_submode_pre = NULL;
+ showmode();
+ }
+ break;
+#endif
+
+ /* end of Select mode mapping - ignore */
+ case K_SELECT:
+ break;
+
+ /* suspend when 'insertmode' set */
+ case Ctrl_Z:
+ if (!p_im)
+ goto normalchar; /* insert CTRL-Z as normal char */
+ stuffReadbuff((char_u *)":st\r");
+ c = Ctrl_O;
+ /*FALLTHROUGH*/
+
+ /* execute one command */
+ case Ctrl_O:
+ if (echeck_abbr(Ctrl_O + ABBR_OFF))
+ break;
+ count = 0;
+#ifdef FEAT_VREPLACE
+ if (State & VREPLACE_FLAG)
+ restart_edit = 'V';
+ else
+#endif
+ if (State & REPLACE_FLAG)
+ restart_edit = 'R';
+ else
+ restart_edit = 'I';
+#ifdef FEAT_VIRTUALEDIT
+ if (virtual_active())
+ o_eol = FALSE; /* cursor always keeps its column */
+ else
+#endif
+ o_eol = (gchar_cursor() == NUL);
+ goto doESCkey;
+
+#ifdef FEAT_SNIFF
+ case K_SNIFF:
+ stuffcharReadbuff(K_SNIFF);
+ goto doESCkey;
+#endif
+
+ /* Hitting the help key in insert mode is like <ESC> <Help> */
+ case K_HELP:
+ case K_F1:
+ case K_XF1:
+ stuffcharReadbuff(K_HELP);
+ if (p_im)
+ need_start_insertmode = TRUE;
+ goto doESCkey;
+
+#ifdef FEAT_NETBEANS_INTG
+ case K_F21:
+ ++no_mapping; /* don't map the next key hits */
+ i = safe_vgetc();
+ --no_mapping;
+ netbeans_keycommand(i);
+ break;
+#endif
+
+ /* an escape ends input mode */
+ case ESC:
+ if (echeck_abbr(ESC + ABBR_OFF))
+ break;
+ /*FALLTHROUGH*/
+
+ case Ctrl_C:
+#ifdef FEAT_CMDWIN
+ if (c == Ctrl_C && cmdwin_type != 0)
+ {
+ /* Close the cmdline window. */
+ cmdwin_result = K_IGNORE;
+ got_int = FALSE; /* don't stop executing autocommands et al. */
+ goto doESCkey;
+ }
+#endif
+
+#ifdef UNIX
+do_intr:
+#endif
+ /* when 'insertmode' set, and not halfway a mapping, don't leave
+ * Insert mode */
+ if (goto_im())
+ {
+ if (got_int)
+ {
+ (void)vgetc(); /* flush all buffers */
+ got_int = FALSE;
+ }
+ else
+ vim_beep();
+ break;
+ }
+doESCkey:
+ /*
+ * This is the ONLY return from edit()!
+ */
+ /* Always update o_lnum, so that a "CTRL-O ." that adds a line
+ * still puts the cursor back after the inserted text. */
+ if (o_eol && gchar_cursor() == NUL)
+ o_lnum = curwin->w_cursor.lnum;
+
+ if (ins_esc(&count, cmdchar))
+ return (c == Ctrl_O);
+ continue;
+
+ /*
+ * Insert the previously inserted text.
+ * For ^@ the trailing ESC will end the insert, unless there is an
+ * error.
+ */
+ case K_ZERO:
+ case NUL:
+ case Ctrl_A:
+ if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
+ && c != Ctrl_A && !p_im)
+ goto doESCkey; /* quit insert mode */
+ inserted_space = FALSE;
+ break;
+
+ /* insert the contents of a register */
+ case Ctrl_R:
+ ins_reg();
+ auto_format(FALSE, TRUE);
+ inserted_space = FALSE;
+ break;
+
+ case Ctrl_G:
+ ins_ctrl_g();
+ break;
+
+ case Ctrl_HAT:
+ if (map_to_exists_mode((char_u *)"", LANGMAP))
+ {
+ /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
+ if (State & LANGMAP)
+ {
+ curbuf->b_p_iminsert = B_IMODE_NONE;
+ State &= ~LANGMAP;
+ }
+ else
+ {
+ curbuf->b_p_iminsert = B_IMODE_LMAP;
+ State |= LANGMAP;
+#ifdef USE_IM_CONTROL
+ im_set_active(FALSE);
+#endif
+ }
+ }
+#ifdef USE_IM_CONTROL
+ else
+ {
+ /* There are no ":lmap" mappings, toggle IM */
+ if (im_get_status())
+ {
+ curbuf->b_p_iminsert = B_IMODE_NONE;
+ im_set_active(FALSE);
+ }
+ else
+ {
+ curbuf->b_p_iminsert = B_IMODE_IM;
+ State &= ~LANGMAP;
+ im_set_active(TRUE);
+ }
+ }
+#endif
+ set_iminsert_global();
+ showmode();
+#ifdef FEAT_GUI
+ /* may show different cursor shape or color */
+ if (gui.in_use)
+ gui_update_cursor(TRUE, FALSE);
+#endif
+#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
+ /* Show/unshow value of 'keymap' in status lines. */
+ status_redraw_curbuf();
+#endif
+ break;
+
+#ifdef FEAT_RIGHTLEFT
+ case Ctrl__:
+ if (!p_ari)
+ goto normalchar;
+ ins_ctrl_();
+ break;
+#endif
+
+ /* Make indent one shiftwidth smaller. */
+ case Ctrl_D:
+#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
+ if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
+ goto docomplete;
+#endif
+ /* FALLTHROUGH */
+
+ /* Make indent one shiftwidth greater. */
+ case Ctrl_T:
+# ifdef FEAT_INS_EXPAND
+ if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
+ {
+ if (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)
+ {
+ ctrl_x_mode = 0;
+ msg_attr((char_u *)_("'thesaurus' option is empty"),
+ hl_attr(HLF_E));
+ if (emsg_silent == 0)
+ {
+ vim_beep();
+ setcursor();
+ out_flush();
+ ui_delay(2000L, FALSE);
+ }
+ break;
+ }
+ goto docomplete;
+ }
+# endif
+ ins_shift(c, lastc);
+ auto_format(FALSE, TRUE);
+ inserted_space = FALSE;
+ break;
+
+ /* delete character under the cursor */
+ case K_DEL:
+ case K_KDEL:
+ ins_del();
+ auto_format(FALSE, TRUE);
+ break;
+
+ /* delete character before the cursor */
+ case K_BS:
+ case Ctrl_H:
+ did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
+ auto_format(FALSE, TRUE);
+ break;
+
+ /* delete word before the cursor */
+ case Ctrl_W:
+ did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
+ auto_format(FALSE, TRUE);
+ break;
+
+ /* delete all inserted text in current line */
+ case Ctrl_U:
+ did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
+ auto_format(FALSE, TRUE);
+ inserted_space = FALSE;
+ break;
+
+#ifdef FEAT_MOUSE
+ case K_LEFTMOUSE:
+ case K_LEFTMOUSE_NM:
+ case K_LEFTDRAG:
+ case K_LEFTRELEASE:
+ case K_LEFTRELEASE_NM:
+ case K_MIDDLEMOUSE:
+ case K_MIDDLEDRAG:
+ case K_MIDDLERELEASE:
+ case K_RIGHTMOUSE:
+ case K_RIGHTDRAG:
+ case K_RIGHTRELEASE:
+ case K_X1MOUSE:
+ case K_X1DRAG:
+ case K_X1RELEASE:
+ case K_X2MOUSE:
+ case K_X2DRAG:
+ case K_X2RELEASE:
+ ins_mouse(c);
+ break;
+
+ /* Default action for scroll wheel up: scroll up */
+ case K_MOUSEDOWN:
+ ins_mousescroll(FALSE);
+ break;
+
+ /* Default action for scroll wheel down: scroll down */
+ case K_MOUSEUP:
+ ins_mousescroll(TRUE);
+ break;
+#endif
+
+ case K_IGNORE:
+ break;
+
+#ifdef FEAT_GUI
+ case K_VER_SCROLLBAR:
+ ins_scroll();
+ break;
+
+ case K_HOR_SCROLLBAR:
+ ins_horscroll();
+ break;
+#endif
+
+ case K_HOME:
+ case K_KHOME:
+ case K_XHOME:
+ case K_S_HOME:
+ case K_C_HOME:
+ ins_home(c);
+ break;
+
+ case K_END:
+ case K_KEND:
+ case K_XEND:
+ case K_S_END:
+ case K_C_END:
+ ins_end(c);
+ break;
+
+ case K_LEFT:
+ ins_left();
+ break;
+
+ case K_S_LEFT:
+ case K_C_LEFT:
+ ins_s_left();
+ break;
+
+ case K_RIGHT:
+ ins_right();
+ break;
+
+ case K_S_RIGHT:
+ case K_C_RIGHT:
+ ins_s_right();
+ break;
+
+ case K_UP:
+ ins_up(FALSE);
+ break;
+
+ case K_S_UP:
+ case K_PAGEUP:
+ case K_KPAGEUP:
+ ins_pageup();
+ break;
+
+ case K_DOWN:
+ ins_down(FALSE);
+ break;
+
+ case K_S_DOWN:
+ case K_PAGEDOWN:
+ case K_KPAGEDOWN:
+ ins_pagedown();
+ break;
+
+#ifdef FEAT_DND
+ case K_DROP:
+ ins_drop();
+ break;
+#endif
+
+ /* When <S-Tab> isn't mapped, use it like a normal TAB */
+ case K_S_TAB:
+ c = TAB;
+ /* FALLTHROUGH */
+
+ /* TAB or Complete patterns along path */
+ case TAB:
+#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
+ if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
+ goto docomplete;
+#endif
+ inserted_space = FALSE;
+ if (ins_tab())
+ goto normalchar; /* insert TAB as a normal char */
+ auto_format(FALSE, TRUE);
+ break;
+
+ case K_KENTER:
+ c = CAR;
+ /* FALLTHROUGH */
+ case CAR:
+ case NL:
+#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
+ /* In a quickfix window a <CR> jumps to the error under the
+ * cursor. */
+ if (bt_quickfix(curbuf) && c == CAR)
+ {
+ do_cmdline_cmd((char_u *)".cc");
+ break;
+ }
+#endif
+#ifdef FEAT_CMDWIN
+ if (cmdwin_type != 0)
+ {
+ /* Execute the command in the cmdline window. */
+ cmdwin_result = CAR;
+ goto doESCkey;
+ }
+#endif
+ if (ins_eol(c) && !p_im)
+ goto doESCkey; /* out of memory */
+ auto_format(FALSE, FALSE);
+ inserted_space = FALSE;
+ break;
+
+#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
+ case Ctrl_K:
+# ifdef FEAT_INS_EXPAND
+ if (ctrl_x_mode == CTRL_X_DICTIONARY)
+ {
+ if (*curbuf->b_p_dict == NUL && *p_dict == NUL)
+ {
+ ctrl_x_mode = 0;
+ msg_attr((char_u *)_("'dictionary' option is empty"),
+ hl_attr(HLF_E));
+ if (emsg_silent == 0)
+ {
+ vim_beep();
+ setcursor();
+ out_flush();
+ ui_delay(2000L, FALSE);
+ }
+ break;
+ }
+ goto docomplete;
+ }
+# endif
+# ifdef FEAT_DIGRAPHS
+ c = ins_digraph();
+ if (c == NUL)
+ break;
+# endif
+ goto normalchar;
+#endif /* FEAT_DIGRAPHS || FEAT_INS_EXPAND */
+
+#ifdef FEAT_INS_EXPAND
+ case Ctrl_RSB: /* Tag name completion after ^X */
+ if (ctrl_x_mode != CTRL_X_TAGS)
+ goto normalchar;
+ goto docomplete;
+
+ case Ctrl_F: /* File name completion after ^X */
+ if (ctrl_x_mode != CTRL_X_FILES)
+ goto normalchar;
+ goto docomplete;
+#endif
+
+ case Ctrl_L: /* Whole line completion after ^X */
+#ifdef FEAT_INS_EXPAND
+ if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
+#endif
+ {
+ /* CTRL-L with 'insertmode' set: Leave Insert mode */
+ if (p_im)
+ {
+ if (echeck_abbr(Ctrl_L + ABBR_OFF))
+ break;
+ goto doESCkey;
+ }
+ goto normalchar;
+ }
+#ifdef FEAT_INS_EXPAND
+ /* FALLTHROUGH */
+
+ /* Do previous/next pattern completion */
+ case Ctrl_P:
+ case Ctrl_N:
+ /* if 'complete' is empty then plain ^P is no longer special,
+ * but it is under other ^X modes */
+ if (*curbuf->b_p_cpt == NUL
+ && ctrl_x_mode != 0
+ && !(continue_status & CONT_LOCAL))
+ goto normalchar;
+
+docomplete:
+ if (ins_complete(c) == FAIL)
+ continue_status = 0;
+ break;
+#endif /* FEAT_INS_EXPAND */
+
+ case Ctrl_Y: /* copy from previous line or scroll down */
+ case Ctrl_E: /* copy from next line or scroll up */
+#ifdef FEAT_INS_EXPAND
+ if (ctrl_x_mode == CTRL_X_SCROLL)
+ {
+ if (c == Ctrl_Y)
+ scrolldown_clamp();
+ else
+ scrollup_clamp();
+ redraw_later(VALID);
+ }
+ else
+#endif
+ {
+ c = ins_copychar(curwin->w_cursor.lnum
+ + (c == Ctrl_Y ? -1 : 1));
+ if (c != NUL)
+ {
+ long tw_save;