blob: bd6f606bba1414e15610ce5bc79e39c38ec741d7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010020# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
23# define CTRL_X_NOT_DEFINED_YET 1
24# define CTRL_X_SCROLL 2
25# define CTRL_X_WHOLE_LINE 3
26# define CTRL_X_FILES 4
27# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
28# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
30# define CTRL_X_FINISHED 8
31# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
32# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
33# define CTRL_X_CMDLINE 11
34# define CTRL_X_FUNCTION 12
35# define CTRL_X_OMNI 13
36# define CTRL_X_SPELL 14
37# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010040# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
41# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010043/* Message for CTRL-X mode, index is ctrl_x_mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static char *ctrl_x_msgs[] =
45{
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010046 N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000047 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010048 NULL, /* CTRL_X_SCROLL: depends on state */
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010054 NULL, /* CTRL_X_FINISHED */
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000057 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000059 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000060 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000061 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010062 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000063};
64
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000065static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010066# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010067static char e_complwin[] = N_("E839: Completion function changed window");
68static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
72 * Structure used to store one match for insert completion.
73 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000074typedef struct compl_S compl_T;
75struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 compl_T *cp_next;
78 compl_T *cp_prev;
79 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000080 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000081 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000082 char_u *cp_fname; /* file containing the match, allocated when
83 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000084 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
85 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086};
87
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010088# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
89# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093 * "compl_first_match" points to the start of the list.
94 * "compl_curr_match" points to the currently selected entry.
95 * "compl_shown_match" is different from compl_curr_match during
96 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000098static compl_T *compl_first_match = NULL;
99static compl_T *compl_curr_match = NULL;
100static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200101static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000103/* After using a cursor key <Enter> selects a match in the popup menu,
104 * otherwise it inserts a line break. */
105static int compl_enter_selects = FALSE;
106
Bram Moolenaara6557602006-02-04 22:43:20 +0000107/* When "compl_leader" is not NULL only matches that start with this string
108 * are used. */
109static char_u *compl_leader = NULL;
110
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000111static int compl_get_longest = FALSE; /* put longest common string
112 in compl_leader */
113
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200114static int compl_no_insert = FALSE; /* FALSE: select & insert
115 TRUE: noinsert */
116static int compl_no_select = FALSE; /* FALSE: select & insert
117 TRUE: noselect */
118
Bram Moolenaara6557602006-02-04 22:43:20 +0000119static int compl_used_match; /* Selected one of the matches. When
120 FALSE the match was edited or using
121 the longest common string. */
122
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000123static int compl_was_interrupted = FALSE; /* didn't finish finding
124 completions. */
125
126static int compl_restarting = FALSE; /* don't insert match */
127
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128/* When the first completion is done "compl_started" is set. When it's
129 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000131
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100132/* Which Ctrl-X mode are we in? */
133static int ctrl_x_mode = CTRL_X_NORMAL;
134
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000135/* Set when doing something for completion that may call edit() recursively,
136 * which is not allowed. */
137static int compl_busy = FALSE;
138
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static int compl_matches = 0;
140static char_u *compl_pattern = NULL;
141static int compl_direction = FORWARD;
142static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000143static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000144static pos_T compl_startpos;
145static colnr_T compl_col = 0; /* column where the text starts
146 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000147static char_u *compl_orig_text = NULL; /* text as it was before
148 * completion started */
149static int compl_cont_mode = 0;
150static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar82139082011-09-14 16:52:09 +0200152static int compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100153static int compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +0200154
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100155static void ins_ctrl_x(void);
156static int has_compl_option(int dict_opt);
157static int ins_compl_accept_char(int c);
158static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ins_compl_longest_match(compl_T *match);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100160static void ins_compl_del_pum(void);
161static int pum_wanted(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
163static char_u *find_line_end(char_u *ptr);
164static void ins_compl_free(void);
165static void ins_compl_clear(void);
166static int ins_compl_bs(void);
167static int ins_compl_need_restart(void);
168static void ins_compl_new_leader(void);
169static void ins_compl_addleader(int c);
170static int ins_compl_len(void);
171static void ins_compl_restart(void);
172static void ins_compl_set_original_text(char_u *str);
173static void ins_compl_addfrommatch(void);
174static int ins_compl_prep(int c);
175static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100176# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100179# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200181static void ins_compl_insert(int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100182static int ins_compl_key2dir(int c);
183static int ins_compl_pum_key(int c);
184static int ins_compl_key2count(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100185static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100186static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif /* FEAT_INS_EXPAND */
189
190#define BACKSPACE_CHAR 1
191#define BACKSPACE_WORD 2
192#define BACKSPACE_WORD_NOT_SPACE 3
193#define BACKSPACE_LINE 4
194
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ins_redraw(int ready);
196static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200197#ifdef FEAT_JOB_CHANNEL
198static void init_prompt(int cmdchar_todo);
199#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100206static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000207#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100208static void check_spell_redraw(void);
209static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000210static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000211#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
213static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void replace_join(int off);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void replace_flush(void);
219static void replace_do_bs(int limit_col);
220static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ins_reg(void);
225static void ins_ctrl_g(void);
226static void ins_ctrl_hat(void);
227static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static int ins_start_select(int c);
232static void ins_insert(int replaceState);
233static void ins_ctrl_o(void);
234static void ins_shift(int c, int lastc);
235static void ins_del(void);
236static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ins_mouse(int c);
239static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000241#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000243#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ins_left(int end_change);
245static void ins_home(int c);
246static void ins_end(int c);
247static void ins_s_left(void);
248static void ins_right(int end_change);
249static void ins_s_right(void);
250static void ins_up(int startcol);
251static void ins_pageup(void);
252static void ins_down(int startcol);
253static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static int ins_tab(void);
258static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100266#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100268#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200269static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271static colnr_T Insstart_textlen; /* length of line when insert started */
272static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100273static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275static char_u *last_insert = NULL; /* the text of the previous insert,
276 K_SPECIAL and CSI are escaped */
277static int last_insert_skip; /* nr of chars in front of previous insert */
278static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000279static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281#ifdef FEAT_CINDENT
282static int can_cindent; /* may do cindenting on this line */
283#endif
284
285static int old_indent = 0; /* for ^^D command in insert mode */
286
287#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000288static int revins_on; /* reverse insert mode on */
289static int revins_chars; /* how much to skip after edit */
290static int revins_legal; /* was the last char 'legal'? */
291static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static int ins_need_undo; /* call u_save() before inserting a
295 char. Set when edit() is called.
296 after that arrow_used is used. */
297
298static int did_add_space = FALSE; /* auto_format() added an extra space
299 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200300static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
301 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
303/*
304 * edit(): Start inserting text.
305 *
306 * "cmdchar" can be:
307 * 'i' normal insert command
308 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100309 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 * 'R' replace command
311 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
312 * but still only one <CR> is inserted. The <Esc> is not used for redo.
313 * 'g' "gI" command.
314 * 'V' "gR" command for Virtual Replace mode.
315 * 'v' "gr" command for single character Virtual Replace mode.
316 *
317 * This function is not called recursively. For CTRL-O commands, it returns
318 * and lets the caller handle the Normal-mode command.
319 *
320 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
321 */
322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100323edit(
324 int cmdchar,
325 int startln, /* if set, insert at start of line */
326 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
328 int c = 0;
329 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100330 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000331 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 int i;
334 int did_backspace = TRUE; /* previous char was backspace */
335#ifdef FEAT_CINDENT
336 int line_is_white = FALSE; /* line is empty before insert */
337#endif
338 linenr_T old_topline = 0; /* topline before insertion */
339#ifdef FEAT_DIFF
340 int old_topfill = -1;
341#endif
342 int inserted_space = FALSE; /* just inserted a space */
343 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000344 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200345#ifdef FEAT_JOB_CHANNEL
346 int cmdchar_todo = cmdchar;
347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385 /*
386 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
387 */
388 if (cmdchar != 'r' && cmdchar != 'v')
389 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390 pos_T save_cursor = curwin->w_cursor;
391
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100392#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000393 if (cmdchar == 'R')
394 ptr = (char_u *)"r";
395 else if (cmdchar == 'V')
396 ptr = (char_u *)"v";
397 else
398 ptr = (char_u *)"i";
399 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200400 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100401#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200402 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100403
Bram Moolenaar097c9922013-05-19 21:15:15 +0200404 /* Make sure the cursor didn't move. Do call check_cursor_col() in
405 * case the text was modified. Since Insert mode was not started yet
406 * a call to check_cursor_col() may move the cursor, especially with
407 * the "A" command, thus set State to avoid that. Also check that the
408 * line number is still valid (lines may have been deleted).
409 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100410 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100411#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100413#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200414 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415 {
416 int save_state = State;
417
418 curwin->w_cursor = save_cursor;
419 State = INSERT;
420 check_cursor_col();
421 State = save_state;
422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200425#ifdef FEAT_CONCEAL
426 /* Check if the cursor line needs redrawing before changing State. If
427 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200428 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200429#endif
430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef FEAT_MOUSE
432 /*
433 * When doing a paste with the middle mouse button, Insstart is set to
434 * where the paste started.
435 */
436 if (where_paste_started.lnum != 0)
437 Insstart = where_paste_started;
438 else
439#endif
440 {
441 Insstart = curwin->w_cursor;
442 if (startln)
443 Insstart.col = 0;
444 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000445 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 Insstart_blank_vcol = MAXCOL;
447 if (!did_ai)
448 ai_col = 0;
449
450 if (cmdchar != NUL && restart_edit == 0)
451 {
452 ResetRedobuff();
453 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar == 'V' || cmdchar == 'v')
455 {
456 /* "gR" or "gr" command */
457 AppendCharToRedobuff('g');
458 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
459 }
460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100462 if (cmdchar == K_PS)
463 AppendCharToRedobuff('a');
464 else
465 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (cmdchar == 'g') /* "gI" command */
467 AppendCharToRedobuff('I');
468 else if (cmdchar == 'r') /* "r<CR>" command */
469 count = 1; /* insert only one <CR> */
470 }
471 }
472
473 if (cmdchar == 'R')
474 {
475#ifdef FEAT_FKMAP
476 if (p_fkmap && p_ri)
477 {
478 beep_flush();
479 EMSG(farsi_text_3); /* encoded in Farsi */
480 State = INSERT;
481 }
482 else
483#endif
484 State = REPLACE;
485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 else if (cmdchar == 'V' || cmdchar == 'v')
487 {
488 State = VREPLACE;
489 replaceState = VREPLACE;
490 orig_line_count = curbuf->b_ml.ml_line_count;
491 vr_lines_changed = 1;
492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 else
494 State = INSERT;
495
496 stop_insert_mode = FALSE;
497
498 /*
499 * Need to recompute the cursor position, it might move when the cursor is
500 * on a TAB or special character.
501 */
502 curs_columns(TRUE);
503
504 /*
505 * Enable langmap or IME, indicated by 'iminsert'.
506 * Note that IME may enabled/disabled without us noticing here, thus the
507 * 'iminsert' value may not reflect what is actually used. It is updated
508 * when hitting <Esc>.
509 */
510 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
511 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100512#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
514#endif
515
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516#ifdef FEAT_MOUSE
517 setmouse();
518#endif
519#ifdef FEAT_CMDL_INFO
520 clear_showcmd();
521#endif
522#ifdef FEAT_RIGHTLEFT
523 /* there is no reverse replace mode */
524 revins_on = (State == INSERT && p_ri);
525 if (revins_on)
526 undisplay_dollar();
527 revins_chars = 0;
528 revins_legal = 0;
529 revins_scol = -1;
530#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100531 if (!p_ek)
532 /* Disable bracketed paste mode, we won't recognize the escape
533 * sequences. */
534 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 /*
537 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100538 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
539 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
541 if (restart_edit != 0 && stuff_empty())
542 {
543#ifdef FEAT_MOUSE
544 /*
545 * After a paste we consider text typed to be part of the insert for
546 * the pasted text. You can backspace over the pasted text too.
547 */
548 if (where_paste_started.lnum)
549 arrow_used = FALSE;
550 else
551#endif
552 arrow_used = TRUE;
553 restart_edit = 0;
554
555 /*
556 * If the cursor was after the end-of-line before the CTRL-O and it is
557 * now at the end-of-line, put it after the end-of-line (this is not
558 * correct in very rare cases).
559 * Also do this if curswant is greater than the current virtual
560 * column. Eg after "^O$" or "^O80|".
561 */
562 validate_virtcol();
563 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 || curwin->w_curswant > curwin->w_virtcol)
566 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
567 {
568 if (ptr[1] == NUL)
569 ++curwin->w_cursor.col;
570#ifdef FEAT_MBYTE
571 else if (has_mbyte)
572 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000573 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (ptr[i] == NUL)
575 curwin->w_cursor.col += i;
576 }
577#endif
578 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000579 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581 else
582 arrow_used = FALSE;
583
584 /* we are in insert mode now, don't need to start it anymore */
585 need_start_insertmode = FALSE;
586
587 /* Need to save the line for undo before inserting the first char. */
588 ins_need_undo = TRUE;
589
590#ifdef FEAT_MOUSE
591 where_paste_started.lnum = 0;
592#endif
593#ifdef FEAT_CINDENT
594 can_cindent = TRUE;
595#endif
596#ifdef FEAT_FOLDING
597 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
598 * restarting. */
599 if (!p_im && did_restart_edit == 0)
600 foldOpenCursor();
601#endif
602
603 /*
604 * If 'showmode' is set, show the current (insert/replace/..) mode.
605 * A warning message for changing a readonly file is given here, before
606 * actually changing anything. It's put after the mode, if any.
607 */
608 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000609 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 i = showmode();
611
612 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000613 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615#ifdef CURSOR_SHAPE
616 ui_cursor_shape(); /* may show different cursor shape */
617#endif
618#ifdef FEAT_DIGRAPHS
619 do_digraph(-1); /* clear digraphs */
620#endif
621
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000622 /*
623 * Get the current length of the redo buffer, those characters have to be
624 * skipped if we want to get to the inserted characters.
625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ptr = get_inserted();
627 if (ptr == NULL)
628 new_insert_skip = 0;
629 else
630 {
631 new_insert_skip = (int)STRLEN(ptr);
632 vim_free(ptr);
633 }
634
635 old_indent = 0;
636
637 /*
638 * Main loop in Insert mode: repeat until Insert mode is left.
639 */
640 for (;;)
641 {
642#ifdef FEAT_RIGHTLEFT
643 if (!revins_legal)
644 revins_scol = -1; /* reset on illegal motions */
645 else
646 revins_legal = 0;
647#endif
648 if (arrow_used) /* don't repeat insert when arrow key used */
649 count = 0;
650
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100651 if (update_Insstart_orig)
652 Insstart_orig = Insstart;
653
Bram Moolenaar00672e12016-06-26 18:38:13 +0200654 if (stop_insert_mode
655#ifdef FEAT_INS_EXPAND
656 && !pum_visible()
657#endif
658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660 /* ":stopinsert" used or 'insertmode' reset */
661 count = 0;
662 goto doESCkey;
663 }
664
665 /* set curwin->w_curswant for next K_DOWN or K_UP */
666 if (!arrow_used)
667 curwin->w_set_curswant = TRUE;
668
669 /* If there is no typeahead may check for timestamps (e.g., for when a
670 * menu invoked a shell command). */
671 if (stuff_empty())
672 {
673 did_check_timestamps = FALSE;
674 if (need_check_timestamps)
675 check_timestamps(FALSE);
676 }
677
678 /*
679 * When emsg() was called msg_scroll will have been set.
680 */
681 msg_scroll = FALSE;
682
683#ifdef FEAT_GUI
684 /* When 'mousefocus' is set a mouse movement may have taken us to
685 * another window. "need_mouse_correct" may then be set because of an
686 * autocommand. */
687 if (need_mouse_correct)
688 gui_mouse_correct();
689#endif
690
691#ifdef FEAT_FOLDING
692 /* Open fold at the cursor line, according to 'foldopen'. */
693 if (fdo_flags & FDO_INSERT)
694 foldOpenCursor();
695 /* Close folds where the cursor isn't, according to 'foldclose' */
696 if (!char_avail())
697 foldCheckClose();
698#endif
699
Bram Moolenaarf2732452018-06-03 14:47:35 +0200700#ifdef FEAT_JOB_CHANNEL
701 if (bt_prompt(curbuf))
702 {
703 init_prompt(cmdchar_todo);
704 cmdchar_todo = NUL;
705 }
706#endif
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 /*
709 * If we inserted a character at the last position of the last line in
710 * the window, scroll the window one line up. This avoids an extra
711 * redraw.
712 * This is detected when the cursor column is smaller after inserting
713 * something.
714 * Don't do this when the topline changed already, it has
715 * already been adjusted (by insertchar() calling open_line())).
716 */
717 if (curbuf->b_mod_set
718 && curwin->w_p_wrap
719 && !did_backspace
720 && curwin->w_topline == old_topline
721#ifdef FEAT_DIFF
722 && curwin->w_topfill == old_topfill
723#endif
724 )
725 {
726 mincol = curwin->w_wcol;
727 validate_cursor_col();
728
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200729 if (
730#ifdef FEAT_VARTABS
731 (int)curwin->w_wcol < mincol - tabstop_at(
732 get_nolist_virtcol(), curbuf->b_p_ts,
733 curbuf->b_p_vts_array)
734#else
735 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 && curwin->w_wrow == W_WINROW(curwin)
738 + curwin->w_height - 1 - p_so
739 && (curwin->w_cursor.lnum != curwin->w_topline
740#ifdef FEAT_DIFF
741 || curwin->w_topfill > 0
742#endif
743 ))
744 {
745#ifdef FEAT_DIFF
746 if (curwin->w_topfill > 0)
747 --curwin->w_topfill;
748 else
749#endif
750#ifdef FEAT_FOLDING
751 if (hasFolding(curwin->w_topline, NULL, &old_topline))
752 set_topline(curwin, old_topline + 1);
753 else
754#endif
755 set_topline(curwin, curwin->w_topline + 1);
756 }
757 }
758
759 /* May need to adjust w_topline to show the cursor. */
760 update_topline();
761
762 did_backspace = FALSE;
763
764 validate_cursor(); /* may set must_redraw */
765
766 /*
767 * Redraw the display when no characters are waiting.
768 * Also shows mode, ruler and positions cursor.
769 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000770 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (curwin->w_p_scb)
773 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar860cae12010-06-05 23:22:07 +0200775 if (curwin->w_p_crb)
776 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 update_curswant();
778 old_topline = curwin->w_topline;
779#ifdef FEAT_DIFF
780 old_topfill = curwin->w_topfill;
781#endif
782
783#ifdef USE_ON_FLY_SCROLL
784 dont_scroll = FALSE; /* allow scrolling here */
785#endif
786
787 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100788 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100790 if (c != K_CURSORHOLD)
791 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200792
793 /* After using CTRL-G U the next cursor key will not break undo. */
794 if (dont_sync_undo == MAYBE)
795 dont_sync_undo = TRUE;
796 else
797 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100798 if (cmdchar == K_PS)
799 /* Got here from normal mode when bracketed paste started. */
800 c = K_PS;
801 else
802 do
803 {
804 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200805
806 if (stop_insert_mode)
807 {
808 // Insert mode ended, possibly from a callback.
809 count = 0;
810 nomove = TRUE;
811 goto doESCkey;
812 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100813 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000815 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
816 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#ifdef FEAT_RIGHTLEFT
819 if (p_hkmap && KeyTyped)
820 c = hkmap(c); /* Hebrew mode mapping */
821#endif
822#ifdef FEAT_FKMAP
823 if (p_fkmap && KeyTyped)
824 c = fkmap(c); /* Farsi mode mapping */
825#endif
826
827#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /*
829 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000830 * and the cursor is still in the completed word. Only when there is
831 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000832 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000833 if (compl_started
834 && pum_wanted()
835 && curwin->w_cursor.col >= compl_col
836 && (compl_shown_match == NULL
837 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000839 /* BS: Delete one character from "compl_leader". */
840 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000841 && curwin->w_cursor.col > compl_col
842 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000843 continue;
844
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000845 /* When no match was selected or it was edited. */
846 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000847 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000849 * "compl_leader". Except when at the original match and
850 * there is nothing to add, CTRL-L works like CTRL-P then. */
851 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100852 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000853 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000854 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000855 {
856 ins_compl_addfrommatch();
857 continue;
858 }
859
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000860 /* A non-white character that fits in with the current
861 * completion: Add to "compl_leader". */
862 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000863 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100864#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100865 /* Trigger InsertCharPre. */
866 char_u *str = do_insert_char_pre(c);
867 char_u *p;
868
869 if (str != NULL)
870 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100871 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100872 ins_compl_addleader(PTR2CHAR(p));
873 vim_free(str);
874 }
875 else
876#endif
877 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000878 continue;
879 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000880
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000881 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000882 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200883 if ((c == Ctrl_Y || (compl_enter_selects
884 && (c == CAR || c == K_KENTER || c == NL)))
885 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000886 {
887 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200888 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000889 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000890 }
891 }
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
894 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000895 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000896 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000897 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#endif
899
Bram Moolenaar488c6512005-08-11 20:09:58 +0000900 /* CTRL-\ CTRL-N goes to Normal mode,
901 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
902 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (c == Ctrl_BSL)
904 {
905 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000906 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 ++no_mapping;
908 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000909 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 --no_mapping;
911 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000912 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000914 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 vungetc(c);
916 c = Ctrl_BSL;
917 }
918 else if (c == Ctrl_G && p_im)
919 continue;
920 else
921 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000922 if (c == Ctrl_O)
923 {
924 ins_ctrl_o();
925 ins_at_eol = FALSE; /* cursor keeps its column */
926 nomove = TRUE;
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 count = 0;
929 goto doESCkey;
930 }
931 }
932
933#ifdef FEAT_DIGRAPHS
934 c = do_digraph(c);
935#endif
936
937#ifdef FEAT_INS_EXPAND
938 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
939 goto docomplete;
940#endif
941 if (c == Ctrl_V || c == Ctrl_Q)
942 {
943 ins_ctrl_v();
944 c = Ctrl_V; /* pretend CTRL-V is last typed character */
945 continue;
946 }
947
948#ifdef FEAT_CINDENT
949 if (cindent_on()
950# ifdef FEAT_INS_EXPAND
951 && ctrl_x_mode == 0
952# endif
953 )
954 {
955 /* A key name preceded by a bang means this key is not to be
956 * inserted. Skip ahead to the re-indenting below.
957 * A key name preceded by a star means that indenting has to be
958 * done before inserting the key. */
959 line_is_white = inindent(0);
960 if (in_cinkeys(c, '!', line_is_white))
961 goto force_cindent;
962 if (can_cindent && in_cinkeys(c, '*', line_is_white)
963 && stop_arrow() == OK)
964 do_c_expr_indent();
965 }
966#endif
967
968#ifdef FEAT_RIGHTLEFT
969 if (curwin->w_p_rl)
970 switch (c)
971 {
972 case K_LEFT: c = K_RIGHT; break;
973 case K_S_LEFT: c = K_S_RIGHT; break;
974 case K_C_LEFT: c = K_C_RIGHT; break;
975 case K_RIGHT: c = K_LEFT; break;
976 case K_S_RIGHT: c = K_S_LEFT; break;
977 case K_C_RIGHT: c = K_C_LEFT; break;
978 }
979#endif
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 /*
982 * If 'keymodel' contains "startsel", may start selection. If it
983 * does, a CTRL-O and c will be stuffed, we need to get these
984 * characters.
985 */
986 if (ins_start_select(c))
987 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 /*
990 * The big switch to handle a character in insert mode.
991 */
992 switch (c)
993 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (echeck_abbr(ESC + ABBR_OFF))
996 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200997 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_CMDWIN
1001 if (c == Ctrl_C && cmdwin_type != 0)
1002 {
1003 /* Close the cmdline window. */
1004 cmdwin_result = K_IGNORE;
1005 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001006 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 goto doESCkey;
1008 }
1009#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001010#ifdef FEAT_JOB_CHANNEL
1011 if (c == Ctrl_C && bt_prompt(curbuf))
1012 {
1013 if (invoke_prompt_interrupt())
1014 {
1015 if (!bt_prompt(curbuf))
1016 // buffer changed to a non-prompt buffer, get out of
1017 // Insert mode
1018 goto doESCkey;
1019 break;
1020 }
1021 }
1022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024#ifdef UNIX
1025do_intr:
1026#endif
1027 /* when 'insertmode' set, and not halfway a mapping, don't leave
1028 * Insert mode */
1029 if (goto_im())
1030 {
1031 if (got_int)
1032 {
1033 (void)vgetc(); /* flush all buffers */
1034 got_int = FALSE;
1035 }
1036 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001037 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 break;
1039 }
1040doESCkey:
1041 /*
1042 * This is the ONLY return from edit()!
1043 */
1044 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1045 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001046 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 o_lnum = curwin->w_cursor.lnum;
1048
Bram Moolenaar488c6512005-08-11 20:09:58 +00001049 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001050 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001051 // When CTRL-C was typed got_int will be set, with the result
1052 // that the autocommands won't be executed. When mapped got_int
1053 // is not set, but let's keep the behavior the same.
1054 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001055 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001056 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 continue;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case Ctrl_Z: /* suspend when 'insertmode' set */
1062 if (!p_im)
1063 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001064 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001065#ifdef CURSOR_SHAPE
1066 ui_cursor_shape(); /* may need to update cursor shape */
1067#endif
1068 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069
1070 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001071#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001072 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 goto docomplete;
1074#endif
1075 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1076 break;
1077 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001078
1079#ifdef FEAT_VIRTUALEDIT
1080 /* don't move the cursor left when 'virtualedit' has "onemore". */
1081 if (ve_flags & VE_ONEMORE)
1082 {
1083 ins_at_eol = FALSE;
1084 nomove = TRUE;
1085 }
1086#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 count = 0;
1088 goto doESCkey;
1089
Bram Moolenaar572cb562005-08-05 21:35:02 +00001090 case K_INS: /* toggle insert/replace mode */
1091 case K_KINS:
1092 ins_insert(replaceState);
1093 break;
1094
1095 case K_SELECT: /* end of Select mode mapping - ignore */
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case K_HELP: /* Help key works like <ESC> <Help> */
1099 case K_F1:
1100 case K_XF1:
1101 stuffcharReadbuff(K_HELP);
1102 if (p_im)
1103 need_start_insertmode = TRUE;
1104 goto doESCkey;
1105
1106#ifdef FEAT_NETBEANS_INTG
1107 case K_F21: /* NetBeans command */
1108 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001109 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 --no_mapping;
1111 netbeans_keycommand(i);
1112 break;
1113#endif
1114
1115 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 case NUL:
1117 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 /* For ^@ the trailing ESC will end the insert, unless there is an
1119 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1121 && c != Ctrl_A && !p_im)
1122 goto doESCkey; /* quit insert mode */
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_reg();
1128 auto_format(FALSE, TRUE);
1129 inserted_space = FALSE;
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 ins_ctrl_g();
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case Ctrl_HAT: /* switch input mode and/or langmap */
1137 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 break;
1139
1140#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (!p_ari)
1143 goto normalchar;
1144 ins_ctrl_();
1145 break;
1146#endif
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1150 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1151 goto docomplete;
1152#endif
1153 /* FALLTHROUGH */
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156# ifdef FEAT_INS_EXPAND
1157 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1158 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 if (has_compl_option(FALSE))
1160 goto docomplete;
1161 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163# endif
1164 ins_shift(c, lastc);
1165 auto_format(FALSE, TRUE);
1166 inserted_space = FALSE;
1167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case K_KDEL:
1171 ins_del();
1172 auto_format(FALSE, TRUE);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 case Ctrl_H:
1177 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1178 auto_format(FALSE, TRUE);
1179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001182#ifdef FEAT_JOB_CHANNEL
1183 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1184 {
1185 // In a prompt window CTRL-W is used for window commands.
1186 // Use Shift-CTRL-W to delete a word.
1187 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001188 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001189 nomove = TRUE;
1190 count = 0;
1191 goto doESCkey;
1192 }
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1195 auto_format(FALSE, TRUE);
1196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001199# ifdef FEAT_COMPL_FUNC
1200 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001202 goto docomplete;
1203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1205 auto_format(FALSE, TRUE);
1206 inserted_space = FALSE;
1207 break;
1208
1209#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 case K_LEFTMOUSE_NM:
1212 case K_LEFTDRAG:
1213 case K_LEFTRELEASE:
1214 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001215 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 case K_MIDDLEMOUSE:
1217 case K_MIDDLEDRAG:
1218 case K_MIDDLERELEASE:
1219 case K_RIGHTMOUSE:
1220 case K_RIGHTDRAG:
1221 case K_RIGHTRELEASE:
1222 case K_X1MOUSE:
1223 case K_X1DRAG:
1224 case K_X1RELEASE:
1225 case K_X2MOUSE:
1226 case K_X2DRAG:
1227 case K_X2RELEASE:
1228 ins_mouse(c);
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001232 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 break;
1234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001235 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001236 ins_mousescroll(MSCR_UP);
1237 break;
1238
1239 case K_MOUSELEFT: /* Scroll wheel left */
1240 ins_mousescroll(MSCR_LEFT);
1241 break;
1242
1243 case K_MOUSERIGHT: /* Scroll wheel right */
1244 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 break;
1246#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001247 case K_PS:
1248 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1249 if (cmdchar == K_PS)
1250 /* invoked from normal mode, bail out */
1251 goto doESCkey;
1252 break;
1253 case K_PE:
1254 /* Got K_PE without K_PS, ignore. */
1255 break;
1256
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001257#ifdef FEAT_GUI_TABLINE
1258 case K_TABLINE:
1259 case K_TABMENU:
1260 ins_tabline(c);
1261 break;
1262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 break;
1266
Bram Moolenaar754b5602006-02-09 23:53:20 +00001267 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001268 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001269 did_cursorhold = TRUE;
1270 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001271
Bram Moolenaar4770d092006-01-12 23:22:24 +00001272#ifdef FEAT_GUI_W32
1273 /* On Win32 ignore <M-F4>, we get it when closing the window was
1274 * cancelled. */
1275 case K_F4:
1276 if (mod_mask != MOD_MASK_ALT)
1277 goto normalchar;
1278 break;
1279#endif
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281#ifdef FEAT_GUI
1282 case K_VER_SCROLLBAR:
1283 ins_scroll();
1284 break;
1285
1286 case K_HOR_SCROLLBAR:
1287 ins_horscroll();
1288 break;
1289#endif
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_S_HOME:
1294 case K_C_HOME:
1295 ins_home(c);
1296 break;
1297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001298 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 case K_S_END:
1301 case K_C_END:
1302 ins_end(c);
1303 break;
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001306 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1307 ins_s_left();
1308 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001309 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_C_LEFT:
1314 ins_s_left();
1315 break;
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001318 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1319 ins_s_right();
1320 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001321 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 case K_C_RIGHT:
1326 ins_s_right();
1327 break;
1328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001329 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001330#ifdef FEAT_INS_EXPAND
1331 if (pum_visible())
1332 goto docomplete;
1333#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001334 if (mod_mask & MOD_MASK_SHIFT)
1335 ins_pageup();
1336 else
1337 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 break;
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 case K_PAGEUP:
1342 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001343#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001344 if (pum_visible())
1345 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 ins_pageup();
1348 break;
1349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001351#ifdef FEAT_INS_EXPAND
1352 if (pum_visible())
1353 goto docomplete;
1354#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001355 if (mod_mask & MOD_MASK_SHIFT)
1356 ins_pagedown();
1357 else
1358 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 break;
1360
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001361 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 case K_PAGEDOWN:
1363 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001364#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001365 if (pum_visible())
1366 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ins_pagedown();
1369 break;
1370
1371#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 ins_drop();
1374 break;
1375#endif
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 c = TAB;
1379 /* FALLTHROUGH */
1380
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001381 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1383 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1384 goto docomplete;
1385#endif
1386 inserted_space = FALSE;
1387 if (ins_tab())
1388 goto normalchar; /* insert TAB as a normal char */
1389 auto_format(FALSE, TRUE);
1390 break;
1391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001392 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 c = CAR;
1394 /* FALLTHROUGH */
1395 case CAR:
1396 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001397#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /* In a quickfix window a <CR> jumps to the error under the
1399 * cursor. */
1400 if (bt_quickfix(curbuf) && c == CAR)
1401 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 if (curwin->w_llist_ref == NULL) /* quickfix window */
1403 do_cmdline_cmd((char_u *)".cc");
1404 else /* location list window */
1405 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 break;
1407 }
1408#endif
1409#ifdef FEAT_CMDWIN
1410 if (cmdwin_type != 0)
1411 {
1412 /* Execute the command in the cmdline window. */
1413 cmdwin_result = CAR;
1414 goto doESCkey;
1415 }
1416#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001417#ifdef FEAT_JOB_CHANNEL
1418 if (bt_prompt(curbuf))
1419 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001420 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001421 if (!bt_prompt(curbuf))
1422 // buffer changed to a non-prompt buffer, get out of
1423 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001424 goto doESCkey;
1425 break;
1426 }
1427#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001428 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 goto doESCkey; /* out of memory */
1430 auto_format(FALSE, FALSE);
1431 inserted_space = FALSE;
1432 break;
1433
Bram Moolenaar860cae12010-06-05 23:22:07 +02001434#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436# ifdef FEAT_INS_EXPAND
1437 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1438 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001439 if (has_compl_option(TRUE))
1440 goto docomplete;
1441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443# endif
1444# ifdef FEAT_DIGRAPHS
1445 c = ins_digraph();
1446 if (c == NUL)
1447 break;
1448# endif
1449 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001453 case Ctrl_X: /* Enter CTRL-X mode */
1454 ins_ctrl_x();
1455 break;
1456
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001457 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (ctrl_x_mode != CTRL_X_TAGS)
1459 goto normalchar;
1460 goto docomplete;
1461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001462 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (ctrl_x_mode != CTRL_X_FILES)
1464 goto normalchar;
1465 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001466
1467 case 's': /* Spelling completion after ^X */
1468 case Ctrl_S:
1469 if (ctrl_x_mode != CTRL_X_SPELL)
1470 goto normalchar;
1471 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472#endif
1473
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001474 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475#ifdef FEAT_INS_EXPAND
1476 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1477#endif
1478 {
1479 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1480 if (p_im)
1481 {
1482 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1483 break;
1484 goto doESCkey;
1485 }
1486 goto normalchar;
1487 }
1488#ifdef FEAT_INS_EXPAND
1489 /* FALLTHROUGH */
1490
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001491 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 case Ctrl_N:
1493 /* if 'complete' is empty then plain ^P is no longer special,
1494 * but it is under other ^X modes */
1495 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001496 && (ctrl_x_mode == CTRL_X_NORMAL
1497 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001498 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 goto normalchar;
1500
1501docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001502 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001503#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001504 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001505#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001506 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001507 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001508#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001509 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001510#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001511 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 break;
1513#endif /* FEAT_INS_EXPAND */
1514
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001515 case Ctrl_Y: /* copy from previous line or scroll down */
1516 case Ctrl_E: /* copy from next line or scroll up */
1517 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 break;
1519
1520 default:
1521#ifdef UNIX
1522 if (c == intr_char) /* special interrupt char */
1523 goto do_intr;
1524#endif
1525
Bram Moolenaare659c952011-05-19 17:25:41 +02001526normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001528 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001530#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001531 if (!p_paste)
1532 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001533 /* Trigger InsertCharPre. */
1534 char_u *str = do_insert_char_pre(c);
1535 char_u *p;
1536
1537 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001538 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001539 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001540 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001541 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001542 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001543 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001544 c = PTR2CHAR(p);
1545 if (c == CAR || c == K_KENTER || c == NL)
1546 ins_eol(c);
1547 else
1548 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001549 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001550 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001551 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001552 vim_free(str);
1553 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001554 }
1555
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001556 /* If the new value is already inserted or an empty string
1557 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001558 if (c == NUL)
1559 break;
1560 }
1561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#ifdef FEAT_SMARTINDENT
1563 /* Try to perform smart-indenting. */
1564 ins_try_si(c);
1565#endif
1566
1567 if (c == ' ')
1568 {
1569 inserted_space = TRUE;
1570#ifdef FEAT_CINDENT
1571 if (inindent(0))
1572 can_cindent = FALSE;
1573#endif
1574 if (Insstart_blank_vcol == MAXCOL
1575 && curwin->w_cursor.lnum == Insstart.lnum)
1576 Insstart_blank_vcol = get_nolist_virtcol();
1577 }
1578
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001579 /* Insert a normal character and check for abbreviations on a
1580 * special character. Let CTRL-] expand abbreviations without
1581 * inserting it. */
1582 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583#ifdef FEAT_MBYTE
1584 /* Add ABBR_OFF for characters above 0x100, this is
1585 * what check_abbr() expects. */
1586 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1587#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001588 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 insert_special(c, FALSE, FALSE);
1591#ifdef FEAT_RIGHTLEFT
1592 revins_legal++;
1593 revins_chars++;
1594#endif
1595 }
1596
1597 auto_format(FALSE, TRUE);
1598
1599#ifdef FEAT_FOLDING
1600 /* When inserting a character the cursor line must never be in a
1601 * closed fold. */
1602 foldOpenCursor();
1603#endif
1604 break;
1605 } /* end of switch (c) */
1606
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001607 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001608 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001609#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001610 /* but not in CTRL-X mode, a script can't restore the state */
1611 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001612#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001613 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001614 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 /* If the cursor was moved we didn't just insert a space */
1617 if (arrow_used)
1618 inserted_space = FALSE;
1619
1620#ifdef FEAT_CINDENT
1621 if (can_cindent && cindent_on()
1622# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001623 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624# endif
1625 )
1626 {
1627force_cindent:
1628 /*
1629 * Indent now if a key was typed that is in 'cinkeys'.
1630 */
1631 if (in_cinkeys(c, ' ', line_is_white))
1632 {
1633 if (stop_arrow() == OK)
1634 /* re-indent the current line */
1635 do_c_expr_indent();
1636 }
1637 }
1638#endif /* FEAT_CINDENT */
1639
1640 } /* for (;;) */
1641 /* NOTREACHED */
1642}
1643
1644/*
1645 * Redraw for Insert mode.
1646 * This is postponed until getting the next character to make '$' in the 'cpo'
1647 * option work correctly.
1648 * Only redraw when there are no characters available. This speeds up
1649 * inserting sequences of characters (e.g., for CTRL-R).
1650 */
1651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001652ins_redraw(
1653 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001655#ifdef FEAT_CONCEAL
1656 linenr_T conceal_old_cursor_line = 0;
1657 linenr_T conceal_new_cursor_line = 0;
1658 int conceal_update_lines = FALSE;
1659#endif
1660
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001661 if (char_avail())
1662 return;
1663
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001664#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1666 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001667 if (ready && (has_cursormovedI()
1668# if defined(FEAT_CONCEAL)
1669 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001672 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001673# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675# endif
1676 )
1677 {
1678# ifdef FEAT_SYN_HL
1679 /* Need to update the screen first, to make sure syntax
1680 * highlighting is correct after making a change (e.g., inserting
1681 * a "(". The autocommand may also require a redraw, so it's done
1682 * again below, unfortunately. */
1683 if (syntax_present(curwin) && must_redraw)
1684 update_screen(0);
1685# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001686 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001687 {
1688 /* Make sure curswant is correct, an autocommand may call
1689 * getcurpos(). */
1690 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001691 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001692 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001693# ifdef FEAT_CONCEAL
1694 if (curwin->w_p_cole > 0)
1695 {
1696 conceal_old_cursor_line = last_cursormoved.lnum;
1697 conceal_new_cursor_line = curwin->w_cursor.lnum;
1698 conceal_update_lines = TRUE;
1699 }
1700# endif
1701 last_cursormoved = curwin->w_cursor;
1702 }
1703#endif
1704
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 /* Trigger TextChangedI if b_changedtick differs. */
1706 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001707 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001709 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001710#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001711 )
1712 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001713 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001714 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001715
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001716 // save and restore curwin and curbuf, in case the autocmd changes them
1717 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001718 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001719 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001720 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001721 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1722 u_save(curwin->w_cursor.lnum,
1723 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001725
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001726#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001727 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1728 * TextChangedI will need to trigger for backwards compatibility, thus use
1729 * different b_last_changedtick* variables. */
1730 if (ready && has_textchangedP()
1731 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1732 && pum_visible())
1733 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001734 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001735 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001736
1737 // save and restore curwin and curbuf, in case the autocmd changes them
1738 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001739 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001740 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001741 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001742 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1743 u_save(curwin->w_cursor.lnum,
1744 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001745 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001746#endif
1747
1748 if (must_redraw)
1749 update_screen(0);
1750 else if (clear_cmdline || redraw_cmdline)
1751 showmode(); /* clear cmdline and show mode */
1752# if defined(FEAT_CONCEAL)
1753 if ((conceal_update_lines
1754 && (conceal_old_cursor_line != conceal_new_cursor_line
1755 || conceal_cursor_line(curwin)))
1756 || need_cursor_line_redraw)
1757 {
1758 if (conceal_old_cursor_line != conceal_new_cursor_line)
1759 update_single_line(curwin, conceal_old_cursor_line);
1760 update_single_line(curwin, conceal_new_cursor_line == 0
1761 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1762 curwin->w_valid &= ~VALID_CROW;
1763 }
1764# endif
1765 showruler(FALSE);
1766 setcursor();
1767 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
1770/*
1771 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1772 */
1773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001777 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001780 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001785 did_putchar = TRUE;
1786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1788
1789#ifdef FEAT_CMDL_INFO
1790 add_to_showcmd_c(Ctrl_V);
1791#endif
1792
1793 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001794 if (did_putchar)
1795 /* when the line fits in 'columns' the '^' is at the start of the next
1796 * line and will not removed by the redraw */
1797 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#ifdef FEAT_CMDL_INFO
1799 clear_showcmd();
1800#endif
1801 insert_special(c, FALSE, TRUE);
1802#ifdef FEAT_RIGHTLEFT
1803 revins_chars++;
1804 revins_legal++;
1805#endif
1806}
1807
1808/*
1809 * Put a character directly onto the screen. It's not stored in a buffer.
1810 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1811 */
1812static int pc_status;
1813#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1814#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1815#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1816#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818static int pc_attr;
1819static int pc_row;
1820static int pc_col;
1821
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 int attr;
1826
1827 if (ScreenLines != NULL)
1828 {
1829 update_topline(); /* just in case w_topline isn't valid */
1830 validate_cursor();
1831 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001832 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 else
1834 attr = 0;
1835 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001836 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1838 pc_status = PC_STATUS_UNSET;
1839#endif
1840#ifdef FEAT_RIGHTLEFT
1841 if (curwin->w_p_rl)
1842 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001843 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844# ifdef FEAT_MBYTE
1845 if (has_mbyte)
1846 {
1847 int fix_col = mb_fix_col(pc_col, pc_row);
1848
1849 if (fix_col != pc_col)
1850 {
1851 screen_putchar(' ', pc_row, fix_col, attr);
1852 --curwin->w_wcol;
1853 pc_status = PC_STATUS_RIGHT;
1854 }
1855 }
1856# endif
1857 }
1858 else
1859#endif
1860 {
1861 pc_col += curwin->w_wcol;
1862#ifdef FEAT_MBYTE
1863 if (mb_lefthalve(pc_row, pc_col))
1864 pc_status = PC_STATUS_LEFT;
1865#endif
1866 }
1867
1868 /* save the character to be able to put it back */
1869#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1870 if (pc_status == PC_STATUS_UNSET)
1871#endif
1872 {
1873 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1874 pc_status = PC_STATUS_SET;
1875 }
1876 screen_putchar(c, pc_row, pc_col, attr);
1877 }
1878}
1879
Bram Moolenaarf2732452018-06-03 14:47:35 +02001880#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1881/*
1882 * Return the effective prompt for the current buffer.
1883 */
1884 char_u *
1885prompt_text(void)
1886{
1887 if (curbuf->b_prompt_text == NULL)
1888 return (char_u *)"% ";
1889 return curbuf->b_prompt_text;
1890}
1891
1892/*
1893 * Prepare for prompt mode: Make sure the last line has the prompt text.
1894 * Move the cursor to this line.
1895 */
1896 static void
1897init_prompt(int cmdchar_todo)
1898{
1899 char_u *prompt = prompt_text();
1900 char_u *text;
1901
1902 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1903 text = ml_get_curline();
1904 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1905 {
1906 // prompt is missing, insert it or append a line with it
1907 if (*text == NUL)
1908 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1909 else
1910 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1911 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1912 coladvance((colnr_T)MAXCOL);
1913 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1914 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001915
1916 // Insert always starts after the prompt, allow editing text after it.
1917 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1918 || Insstart_orig.col != (int)STRLEN(prompt))
1919 {
1920 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001921 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001922 Insstart_orig = Insstart;
1923 Insstart_textlen = Insstart.col;
1924 Insstart_blank_vcol = MAXCOL;
1925 arrow_used = FALSE;
1926 }
1927
Bram Moolenaarf2732452018-06-03 14:47:35 +02001928 if (cmdchar_todo == 'A')
1929 coladvance((colnr_T)MAXCOL);
1930 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001931 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001932 /* Make sure the cursor is in a valid position. */
1933 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001934}
1935
1936/*
1937 * Return TRUE if the cursor is in the editable position of the prompt line.
1938 */
1939 int
1940prompt_curpos_editable()
1941{
1942 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1943 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1944}
1945#endif
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947/*
1948 * Undo the previous edit_putchar().
1949 */
1950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001951edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1954 {
1955#if defined(FEAT_MBYTE)
1956 if (pc_status == PC_STATUS_RIGHT)
1957 ++curwin->w_wcol;
1958 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001959 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else
1961#endif
1962 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1963 }
1964}
1965
1966/*
1967 * Called when p_dollar is set: display a '$' at the end of the changed text
1968 * Only works when cursor is in the line that changes.
1969 */
1970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001971display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973 colnr_T save_col;
1974
1975 if (!redrawing())
1976 return;
1977
1978 cursor_off();
1979 save_col = curwin->w_cursor.col;
1980 curwin->w_cursor.col = col;
1981#ifdef FEAT_MBYTE
1982 if (has_mbyte)
1983 {
1984 char_u *p;
1985
1986 /* If on the last byte of a multi-byte move to the first byte. */
1987 p = ml_get_curline();
1988 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1989 }
1990#endif
1991 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001992 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 edit_putchar('$', FALSE);
1995 dollar_vcol = curwin->w_virtcol;
1996 }
1997 curwin->w_cursor.col = save_col;
1998}
1999
2000/*
2001 * Call this function before moving the cursor from the normal insert position
2002 * in insert mode.
2003 */
2004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002005undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002007 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002009 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002010 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012}
2013
2014/*
2015 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2016 * Keep the cursor on the same character.
2017 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2018 * type == INDENT_DEC decrease indent (for CTRL-D)
2019 * type == INDENT_SET set indent to "amount"
2020 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2021 */
2022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002023change_indent(
2024 int type,
2025 int amount,
2026 int round,
2027 int replaced, /* replaced character, put on replace stack */
2028 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 int vcol;
2031 int last_vcol;
2032 int insstart_less; /* reduction for Insstart.col */
2033 int new_cursor_col;
2034 int i;
2035 char_u *ptr;
2036 int save_p_list;
2037 int start_col;
2038 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 colnr_T orig_col = 0; /* init for GCC */
2040 char_u *new_line, *orig_line = NULL; /* init for GCC */
2041
2042 /* VREPLACE mode needs to know what the line was like before changing */
2043 if (State & VREPLACE_FLAG)
2044 {
2045 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2046 orig_col = curwin->w_cursor.col;
2047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /* for the following tricks we don't want list mode */
2050 save_p_list = curwin->w_p_list;
2051 curwin->w_p_list = FALSE;
2052 vc = getvcol_nolist(&curwin->w_cursor);
2053 vcol = vc;
2054
2055 /*
2056 * For Replace mode we need to fix the replace stack later, which is only
2057 * possible when the cursor is in the indent. Remember the number of
2058 * characters before the cursor if it's possible.
2059 */
2060 start_col = curwin->w_cursor.col;
2061
2062 /* determine offset from first non-blank */
2063 new_cursor_col = curwin->w_cursor.col;
2064 beginline(BL_WHITE);
2065 new_cursor_col -= curwin->w_cursor.col;
2066
2067 insstart_less = curwin->w_cursor.col;
2068
2069 /*
2070 * If the cursor is in the indent, compute how many screen columns the
2071 * cursor is to the left of the first non-blank.
2072 */
2073 if (new_cursor_col < 0)
2074 vcol = get_indent() - vcol;
2075
2076 if (new_cursor_col > 0) /* can't fix replace stack */
2077 start_col = -1;
2078
2079 /*
2080 * Set the new indent. The cursor will be put on the first non-blank.
2081 */
2082 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002083 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 else
2085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 int save_State = State;
2087
2088 /* Avoid being called recursively. */
2089 if (State & VREPLACE_FLAG)
2090 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002091 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
2094 insstart_less -= curwin->w_cursor.col;
2095
2096 /*
2097 * Try to put cursor on same character.
2098 * If the cursor is at or after the first non-blank in the line,
2099 * compute the cursor column relative to the column of the first
2100 * non-blank character.
2101 * If we are not in insert mode, leave the cursor on the first non-blank.
2102 * If the cursor is before the first non-blank, position it relative
2103 * to the first non-blank, counted in screen columns.
2104 */
2105 if (new_cursor_col >= 0)
2106 {
2107 /*
2108 * When changing the indent while the cursor is touching it, reset
2109 * Insstart_col to 0.
2110 */
2111 if (new_cursor_col == 0)
2112 insstart_less = MAXCOL;
2113 new_cursor_col += curwin->w_cursor.col;
2114 }
2115 else if (!(State & INSERT))
2116 new_cursor_col = curwin->w_cursor.col;
2117 else
2118 {
2119 /*
2120 * Compute the screen column where the cursor should be.
2121 */
2122 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002123 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 /*
2126 * Advance the cursor until we reach the right screen column.
2127 */
2128 vcol = last_vcol = 0;
2129 new_cursor_col = -1;
2130 ptr = ml_get_curline();
2131 while (vcol <= (int)curwin->w_virtcol)
2132 {
2133 last_vcol = vcol;
2134#ifdef FEAT_MBYTE
2135 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002136 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else
2138#endif
2139 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002140 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142 vcol = last_vcol;
2143
2144 /*
2145 * May need to insert spaces to be able to position the cursor on
2146 * the right screen column.
2147 */
2148 if (vcol != (int)curwin->w_virtcol)
2149 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002150 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002152 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (ptr != NULL)
2154 {
2155 new_cursor_col += i;
2156 ptr[i] = NUL;
2157 while (--i >= 0)
2158 ptr[i] = ' ';
2159 ins_str(ptr);
2160 vim_free(ptr);
2161 }
2162 }
2163
2164 /*
2165 * When changing the indent while the cursor is in it, reset
2166 * Insstart_col to 0.
2167 */
2168 insstart_less = MAXCOL;
2169 }
2170
2171 curwin->w_p_list = save_p_list;
2172
2173 if (new_cursor_col <= 0)
2174 curwin->w_cursor.col = 0;
2175 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002176 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 curwin->w_set_curswant = TRUE;
2178 changed_cline_bef_curs();
2179
2180 /*
2181 * May have to adjust the start of the insert.
2182 */
2183 if (State & INSERT)
2184 {
2185 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2186 {
2187 if ((int)Insstart.col <= insstart_less)
2188 Insstart.col = 0;
2189 else
2190 Insstart.col -= insstart_less;
2191 }
2192 if ((int)ai_col <= insstart_less)
2193 ai_col = 0;
2194 else
2195 ai_col -= insstart_less;
2196 }
2197
2198 /*
2199 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2200 * If the number of characters before the cursor decreased, need to pop a
2201 * few characters from the replace stack.
2202 * If the number of characters before the cursor increased, need to push a
2203 * few NULs onto the replace stack.
2204 */
2205 if (REPLACE_NORMAL(State) && start_col >= 0)
2206 {
2207 while (start_col > (int)curwin->w_cursor.col)
2208 {
2209 replace_join(0); /* remove a NUL from the replace stack */
2210 --start_col;
2211 }
2212 while (start_col < (int)curwin->w_cursor.col || replaced)
2213 {
2214 replace_push(NUL);
2215 if (replaced)
2216 {
2217 replace_push(replaced);
2218 replaced = NUL;
2219 }
2220 ++start_col;
2221 }
2222 }
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 /*
2225 * For VREPLACE mode, we also have to fix the replace stack. In this case
2226 * it is always possible because we backspace over the whole line and then
2227 * put it back again the way we wanted it.
2228 */
2229 if (State & VREPLACE_FLAG)
2230 {
2231 /* If orig_line didn't allocate, just return. At least we did the job,
2232 * even if you can't backspace. */
2233 if (orig_line == NULL)
2234 return;
2235
2236 /* Save new line */
2237 new_line = vim_strsave(ml_get_curline());
2238 if (new_line == NULL)
2239 return;
2240
2241 /* We only put back the new line up to the cursor */
2242 new_line[curwin->w_cursor.col] = NUL;
2243
2244 /* Put back original line */
2245 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2246 curwin->w_cursor.col = orig_col;
2247
2248 /* Backspace from cursor to start of line */
2249 backspace_until_column(0);
2250
2251 /* Insert new stuff into line again */
2252 ins_bytes(new_line);
2253
2254 vim_free(new_line);
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256}
2257
2258/*
2259 * Truncate the space at the end of a line. This is to be used only in an
2260 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2261 * modes.
2262 */
2263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002264truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 int i;
2267
2268 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002269 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 if (State & REPLACE_FLAG)
2272 replace_join(0); /* remove a NUL from the replace stack */
2273 }
2274 line[i + 1] = NUL;
2275}
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277/*
2278 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2279 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002280 * Will attempt not to go before "col" even when there is a composing
2281 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 */
2283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002284backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 while ((int)curwin->w_cursor.col > col)
2287 {
2288 curwin->w_cursor.col--;
2289 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002290 replace_do_bs(col);
2291 else if (!del_char_after_col(col))
2292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002296/*
2297 * Like del_char(), but make sure not to go before column "limit_col".
2298 * Only matters when there are composing characters.
2299 * Return TRUE when something was deleted.
2300 */
2301 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002302del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002303{
2304#ifdef FEAT_MBYTE
2305 if (enc_utf8 && limit_col >= 0)
2306 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002307 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002308
2309 /* Make sure the cursor is at the start of a character, but
2310 * skip forward again when going too far back because of a
2311 * composing character. */
2312 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002313 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002314 {
2315 int l = utf_ptr2len(ml_get_cursor());
2316
2317 if (l == 0) /* end of line */
2318 break;
2319 curwin->w_cursor.col += l;
2320 }
2321 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2322 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002323 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002324 }
2325 else
2326#endif
2327 (void)del_char(FALSE);
2328 return TRUE;
2329}
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2332/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002333 * CTRL-X pressed in Insert mode.
2334 */
2335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002337{
2338 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2339 * CTRL-V works like CTRL-N */
2340 if (ctrl_x_mode != CTRL_X_CMDLINE)
2341 {
2342 /* if the next ^X<> won't ADD nothing, then reset
2343 * compl_cont_status */
2344 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002345 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002346 else
2347 compl_cont_status = 0;
2348 /* We're not sure which CTRL-X mode it will be yet */
2349 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2350 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2351 edit_submode_pre = NULL;
2352 showmode();
2353 }
2354}
2355
2356/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002357 * Whether other than default completion has been selected.
2358 */
2359 int
2360ctrl_x_mode_not_default(void)
2361{
2362 return ctrl_x_mode != CTRL_X_NORMAL;
2363}
2364
2365/*
2366 * Whether CTRL-X was typed without a following character.
2367 */
2368 int
2369ctrl_x_mode_not_defined_yet(void)
2370{
2371 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2372}
2373
2374/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002375 * Return TRUE if the 'dict' or 'tsr' option can be used.
2376 */
2377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002379{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002380 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002381# ifdef FEAT_SPELL
2382 && !curwin->w_p_spell
2383# endif
2384 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002385 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2386 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002387 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002388 edit_submode = NULL;
2389 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2390 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002391 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002392 if (emsg_silent == 0)
2393 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002394 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002395 setcursor();
2396 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002397#ifdef FEAT_EVAL
2398 if (!get_vim_var_nr(VV_TESTING))
2399#endif
2400 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002401 }
2402 return FALSE;
2403 }
2404 return TRUE;
2405}
2406
2407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2409 * This depends on the current mode.
2410 */
2411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002412vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002414 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (c == Ctrl_R)
2416 return TRUE;
2417
Bram Moolenaare3226be2005-12-18 22:10:00 +00002418 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002419 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002420 return TRUE;
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 switch (ctrl_x_mode)
2423 {
2424 case 0: /* Not in any CTRL-X mode */
2425 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2426 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002427 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2429 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2430 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002431 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002432 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CTRL_X_SCROLL:
2434 return (c == Ctrl_Y || c == Ctrl_E);
2435 case CTRL_X_WHOLE_LINE:
2436 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2437 case CTRL_X_FILES:
2438 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2439 case CTRL_X_DICTIONARY:
2440 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2441 case CTRL_X_THESAURUS:
2442 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2443 case CTRL_X_TAGS:
2444 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2445#ifdef FEAT_FIND_ID
2446 case CTRL_X_PATH_PATTERNS:
2447 return (c == Ctrl_P || c == Ctrl_N);
2448 case CTRL_X_PATH_DEFINES:
2449 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2450#endif
2451 case CTRL_X_CMDLINE:
2452 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2453 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002454#ifdef FEAT_COMPL_FUNC
2455 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002456 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002457 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002458 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002459#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002460 case CTRL_X_SPELL:
2461 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002462 case CTRL_X_EVAL:
2463 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002465 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 return FALSE;
2467}
2468
2469/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002470 * Return TRUE when character "c" is part of the item currently being
2471 * completed. Used to decide whether to abandon complete mode when the menu
2472 * is visible.
2473 */
2474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002475ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002476{
2477 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2478 /* When expanding an identifier only accept identifier chars. */
2479 return vim_isIDc(c);
2480
2481 switch (ctrl_x_mode)
2482 {
2483 case CTRL_X_FILES:
2484 /* When expanding file name only accept file name chars. But not
2485 * path separators, so that "proto/<Tab>" expands files in
2486 * "proto", not "proto/" as a whole */
2487 return vim_isfilec(c) && !vim_ispathsep(c);
2488
2489 case CTRL_X_CMDLINE:
2490 case CTRL_X_OMNI:
2491 /* Command line and Omni completion can work with just about any
2492 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002493 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002494
2495 case CTRL_X_WHOLE_LINE:
2496 /* For while line completion a space can be part of the line. */
2497 return vim_isprintc(c);
2498 }
2499 return vim_iswordc(c);
2500}
2501
2502/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002503 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002505 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 * the rest of the word to be in -- webb
2507 */
2508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002509ins_compl_add_infercase(
2510 char_u *str,
2511 int len,
2512 int icase,
2513 char_u *fname,
2514 int dir,
2515 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002517 char_u *p;
2518 int i, c;
2519 int actual_len; /* Take multi-byte characters */
2520 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002521 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002522 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 int has_lower = FALSE;
2524 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002526 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002528 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaara2993e12007-08-12 14:38:46 +00002530 /* Find actual length of completion. */
2531#ifdef FEAT_MBYTE
2532 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002534 p = str;
2535 actual_len = 0;
2536 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002538 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002539 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002542 else
2543#endif
2544 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
Bram Moolenaara2993e12007-08-12 14:38:46 +00002546 /* Find actual length of original text. */
2547#ifdef FEAT_MBYTE
2548 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002550 p = compl_orig_text;
2551 actual_compl_length = 0;
2552 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002554 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002555 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002558 else
2559#endif
2560 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002562 /* "actual_len" may be smaller than "actual_compl_length" when using
2563 * thesaurus, only use the minimum when comparing. */
2564 min_len = actual_len < actual_compl_length
2565 ? actual_len : actual_compl_length;
2566
Bram Moolenaara2993e12007-08-12 14:38:46 +00002567 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002568 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002569 if (wca != NULL)
2570 {
2571 p = str;
2572 for (i = 0; i < actual_len; ++i)
2573#ifdef FEAT_MBYTE
2574 if (has_mbyte)
2575 wca[i] = mb_ptr2char_adv(&p);
2576 else
2577#endif
2578 wca[i] = *(p++);
2579
2580 /* Rule 1: Were any chars converted to lower? */
2581 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002582 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002583 {
2584#ifdef FEAT_MBYTE
2585 if (has_mbyte)
2586 c = mb_ptr2char_adv(&p);
2587 else
2588#endif
2589 c = *(p++);
2590 if (MB_ISLOWER(c))
2591 {
2592 has_lower = TRUE;
2593 if (MB_ISUPPER(wca[i]))
2594 {
2595 /* Rule 1 is satisfied. */
2596 for (i = actual_compl_length; i < actual_len; ++i)
2597 wca[i] = MB_TOLOWER(wca[i]);
2598 break;
2599 }
2600 }
2601 }
2602
2603 /*
2604 * Rule 2: No lower case, 2nd consecutive letter converted to
2605 * upper case.
2606 */
2607 if (!has_lower)
2608 {
2609 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002610 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002611 {
2612#ifdef FEAT_MBYTE
2613 if (has_mbyte)
2614 c = mb_ptr2char_adv(&p);
2615 else
2616#endif
2617 c = *(p++);
2618 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2619 {
2620 /* Rule 2 is satisfied. */
2621 for (i = actual_compl_length; i < actual_len; ++i)
2622 wca[i] = MB_TOUPPER(wca[i]);
2623 break;
2624 }
2625 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2626 }
2627 }
2628
2629 /* Copy the original case of the part we typed. */
2630 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002631 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002632 {
2633#ifdef FEAT_MBYTE
2634 if (has_mbyte)
2635 c = mb_ptr2char_adv(&p);
2636 else
2637#endif
2638 c = *(p++);
2639 if (MB_ISLOWER(c))
2640 wca[i] = MB_TOLOWER(wca[i]);
2641 else if (MB_ISUPPER(c))
2642 wca[i] = MB_TOUPPER(wca[i]);
2643 }
2644
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002645 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002646 * Generate encoding specific output from wide character array.
2647 * Multi-byte characters can occupy up to five bytes more than
2648 * ASCII characters, and we also need one byte for NUL, so stay
2649 * six bytes away from the edge of IObuff.
2650 */
2651 p = IObuff;
2652 i = 0;
2653 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2654#ifdef FEAT_MBYTE
2655 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002656 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002657 else
2658#endif
2659 *(p++) = wca[i++];
2660 *p = NUL;
2661
2662 vim_free(wca);
2663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002665 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2666 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002668 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669}
2670
2671/*
2672 * Add a match to the list of matches.
2673 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002674 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678ins_compl_add(
2679 char_u *str,
2680 int len,
2681 int icase,
2682 char_u *fname,
2683 char_u **cptext, /* extra text for popup menu or NULL */
2684 int cdir,
2685 int flags,
2686 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002688 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002689 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 ui_breakcheck();
2692 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002693 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (len < 0)
2695 len = (int)STRLEN(str);
2696
2697 /*
2698 * If the same match is already present, don't add it.
2699 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002702 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 do
2704 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002705 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002706 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002707 && match->cp_str[len] == NUL)
2708 return NOTDONE;
2709 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002710 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002713 /* Remove any popup menu before changing the list of matches. */
2714 ins_compl_del_pum();
2715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 /*
2717 * Allocate a new match structure.
2718 * Copy the values to the new match structure.
2719 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002720 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002722 return FAIL;
2723 match->cp_number = -1;
2724 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002725 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002726 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
2728 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002729 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002731 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002734 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2736 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002737 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002738 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002739 && compl_curr_match->cp_fname != NULL
2740 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002741 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002742 else if (fname != NULL)
2743 {
2744 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002745 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002748 match->cp_fname = NULL;
2749 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002750
2751 if (cptext != NULL)
2752 {
2753 int i;
2754
2755 for (i = 0; i < CPT_COUNT; ++i)
2756 if (cptext[i] != NULL && *cptext[i] != NUL)
2757 match->cp_text[i] = vim_strsave(cptext[i]);
2758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760 /*
2761 * Link the new match structure in the list of matches.
2762 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002763 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002764 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else if (dir == FORWARD)
2766 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002767 match->cp_next = compl_curr_match->cp_next;
2768 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 else /* BACKWARD */
2771 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002772 match->cp_next = compl_curr_match;
2773 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002775 if (match->cp_next)
2776 match->cp_next->cp_prev = match;
2777 if (match->cp_prev)
2778 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002780 compl_first_match = match;
2781 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002783 /*
2784 * Find the longest common string if still doing that.
2785 */
2786 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2787 ins_compl_longest_match(match);
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 return OK;
2790}
2791
2792/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002793 * Return TRUE if "str[len]" matches with match->cp_str, considering
2794 * match->cp_icase.
2795 */
2796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002797ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002798{
2799 if (match->cp_icase)
2800 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2801 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2802}
2803
2804/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002805 * Reduce the longest common string for match "match".
2806 */
2807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002808ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002809{
2810 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002811 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002812 int had_match;
2813
2814 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002815 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002816 /* First match, use it as a whole. */
2817 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002818 if (compl_leader != NULL)
2819 {
2820 had_match = (curwin->w_cursor.col > compl_col);
2821 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002822 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002823 ins_redraw(FALSE);
2824
2825 /* When the match isn't there (to avoid matching itself) remove it
2826 * again after redrawing. */
2827 if (!had_match)
2828 ins_compl_delete();
2829 compl_used_match = FALSE;
2830 }
2831 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002832 else
2833 {
2834 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002835 p = compl_leader;
2836 s = match->cp_str;
2837 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002838 {
2839#ifdef FEAT_MBYTE
2840 if (has_mbyte)
2841 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002842 c1 = mb_ptr2char(p);
2843 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002844 }
2845 else
2846#endif
2847 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002848 c1 = *p;
2849 c2 = *s;
2850 }
2851 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2852 : (c1 != c2))
2853 break;
2854#ifdef FEAT_MBYTE
2855 if (has_mbyte)
2856 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002857 MB_PTR_ADV(p);
2858 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002859 }
2860 else
2861#endif
2862 {
2863 ++p;
2864 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002865 }
2866 }
2867
2868 if (*p != NUL)
2869 {
2870 /* Leader was shortened, need to change the inserted text. */
2871 *p = NUL;
2872 had_match = (curwin->w_cursor.col > compl_col);
2873 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002874 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002875 ins_redraw(FALSE);
2876
2877 /* When the match isn't there (to avoid matching itself) remove it
2878 * again after redrawing. */
2879 if (!had_match)
2880 ins_compl_delete();
2881 }
2882
2883 compl_used_match = FALSE;
2884 }
2885}
2886
2887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 * Add an array of matches to the list of matches.
2889 * Frees matches[].
2890 */
2891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002892ins_compl_add_matches(
2893 int num_matches,
2894 char_u **matches,
2895 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 int i;
2898 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002899 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
Bram Moolenaar572cb562005-08-05 21:35:02 +00002901 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002902 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002903 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002905 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 FreeWild(num_matches, matches);
2907}
2908
2909/* Make the completion list cyclic.
2910 * Return the number of matches (excluding the original).
2911 */
2912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002915 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int count = 0;
2917
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002918 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
2920 /*
2921 * Find the end of the list.
2922 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002923 match = compl_first_match;
2924 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002925 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002927 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 ++count;
2929 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002930 match->cp_next = compl_first_match;
2931 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
2933 return count;
2934}
2935
Bram Moolenaara94bc432006-03-10 21:42:59 +00002936/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002937 * Set variables that store noselect and noinsert behavior from the
2938 * 'completeopt' value.
2939 */
2940 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002941completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002942{
2943 compl_no_insert = FALSE;
2944 compl_no_select = FALSE;
2945 if (strstr((char *)p_cot, "noselect") != NULL)
2946 compl_no_select = TRUE;
2947 if (strstr((char *)p_cot, "noinsert") != NULL)
2948 compl_no_insert = TRUE;
2949}
2950
2951/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002952 * Start completion for the complete() function.
2953 * "startcol" is where the matched text starts (1 is first column).
2954 * "list" is the list of matches.
2955 */
2956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002958{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002959 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002960 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002961
Bram Moolenaara94bc432006-03-10 21:42:59 +00002962 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002963 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002964 ins_compl_prep(' ');
2965 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002966 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002967
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002968 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002969 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002970 startcol = curwin->w_cursor.col;
2971 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002972 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002973 /* compl_pattern doesn't need to be set */
2974 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2975 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002976 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002977 return;
2978
Bram Moolenaare4214502015-03-05 18:08:43 +01002979 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002980
2981 ins_compl_add_list(list);
2982 compl_matches = ins_compl_make_cyclic();
2983 compl_started = TRUE;
2984 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002985 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002986
2987 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002988 if (compl_no_insert || compl_no_select)
2989 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002990 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002991 if (compl_no_select)
2992 /* Down/Up has no real effect. */
2993 ins_complete(K_UP, FALSE);
2994 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002995 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002996 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002997 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002998
2999 /* Lazily show the popup menu, unless we got interrupted. */
3000 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003001 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003002 out_flush();
3003}
3004
3005
Bram Moolenaar9372a112005-12-06 19:59:18 +00003006/* "compl_match_array" points the currently displayed list of entries in the
3007 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003008static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003009static int compl_match_arraysize;
3010
3011/*
3012 * Update the screen and when there is any scrolling remove the popup menu.
3013 */
3014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003015ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003016{
3017 int h;
3018
3019 if (compl_match_array != NULL)
3020 {
3021 h = curwin->w_cline_height;
3022 update_screen(0);
3023 if (h != curwin->w_cline_height)
3024 ins_compl_del_pum();
3025 }
3026}
3027
3028/*
3029 * Remove any popup menu.
3030 */
3031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003033{
3034 if (compl_match_array != NULL)
3035 {
3036 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003037 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003038 }
3039}
3040
3041/*
3042 * Return TRUE if the popup menu should be displayed.
3043 */
3044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003045pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003046{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003047 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003048 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003049 return FALSE;
3050
3051 /* The display looks bad on a B&W display. */
3052 if (t_colors < 8
3053#ifdef FEAT_GUI
3054 && !gui.in_use
3055#endif
3056 )
3057 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003058 return TRUE;
3059}
3060
3061/*
3062 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003063 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003064 */
3065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003067{
3068 compl_T *compl;
3069 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003070
3071 /* Don't display the popup menu if there are no matches or there is only
3072 * one (ignoring the original text). */
3073 compl = compl_first_match;
3074 i = 0;
3075 do
3076 {
3077 if (compl == NULL
3078 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3079 break;
3080 compl = compl->cp_next;
3081 } while (compl != compl_first_match);
3082
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003083 if (strstr((char *)p_cot, "menuone") != NULL)
3084 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003085 return (i >= 2);
3086}
3087
3088/*
3089 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003090 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003094{
3095 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003096 compl_T *shown_compl = NULL;
3097 int did_find_shown_match = FALSE;
3098 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099 int i;
3100 int cur = -1;
3101 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003102 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003103
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003104 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003105 return;
3106
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003107#if defined(FEAT_EVAL)
3108 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3109 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3110#endif
3111
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003112 /* Update the screen before drawing the popup menu over it. */
3113 update_screen(0);
3114
3115 if (compl_match_array == NULL)
3116 {
3117 /* Need to build the popup menu list. */
3118 compl_match_arraysize = 0;
3119 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003120 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003121 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003122 do
3123 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003124 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3125 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003126 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003127 ++compl_match_arraysize;
3128 compl = compl->cp_next;
3129 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003130 if (compl_match_arraysize == 0)
3131 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003132 compl_match_array = (pumitem_T *)alloc_clear(
3133 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003134 * compl_match_arraysize));
3135 if (compl_match_array != NULL)
3136 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003137 /* If the current match is the original text don't find the first
3138 * match after it, don't highlight anything. */
3139 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3140 shown_match_ok = TRUE;
3141
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003142 i = 0;
3143 compl = compl_first_match;
3144 do
3145 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003146 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3147 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003148 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003149 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003150 if (!shown_match_ok)
3151 {
3152 if (compl == compl_shown_match || did_find_shown_match)
3153 {
3154 /* This item is the shown match or this is the
3155 * first displayed item after the shown match. */
3156 compl_shown_match = compl;
3157 did_find_shown_match = TRUE;
3158 shown_match_ok = TRUE;
3159 }
3160 else
3161 /* Remember this displayed match for when the
3162 * shown match is just below it. */
3163 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003164 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003165 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003166
3167 if (compl->cp_text[CPT_ABBR] != NULL)
3168 compl_match_array[i].pum_text =
3169 compl->cp_text[CPT_ABBR];
3170 else
3171 compl_match_array[i].pum_text = compl->cp_str;
3172 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3173 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3174 if (compl->cp_text[CPT_MENU] != NULL)
3175 compl_match_array[i++].pum_extra =
3176 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003177 else
3178 compl_match_array[i++].pum_extra = compl->cp_fname;
3179 }
3180
3181 if (compl == compl_shown_match)
3182 {
3183 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003184
3185 /* When the original text is the shown match don't set
3186 * compl_shown_match. */
3187 if (compl->cp_flags & ORIGINAL_TEXT)
3188 shown_match_ok = TRUE;
3189
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003190 if (!shown_match_ok && shown_compl != NULL)
3191 {
3192 /* The shown match isn't displayed, set it to the
3193 * previously displayed match. */
3194 compl_shown_match = shown_compl;
3195 shown_match_ok = TRUE;
3196 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003197 }
3198 compl = compl->cp_next;
3199 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003200
3201 if (!shown_match_ok) /* no displayed match at all */
3202 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003203 }
3204 }
3205 else
3206 {
3207 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003208 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003209 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3210 || compl_match_array[i].pum_text
3211 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003212 {
3213 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003214 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003215 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003216 }
3217
3218 if (compl_match_array != NULL)
3219 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003220 /* In Replace mode when a $ is displayed at the end of the line only
3221 * part of the screen would be updated. We do need to redraw here. */
3222 dollar_vcol = -1;
3223
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003224 /* Compute the screen column of the start of the completed text.
3225 * Use the cursor to get all wrapping and other settings right. */
3226 col = curwin->w_cursor.col;
3227 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003228 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003229 curwin->w_cursor.col = col;
3230 }
3231}
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#define DICT_FIRST (1) /* use just first element in "dict" */
3234#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003237 * Add any identifiers that match the given pattern in the list of dictionary
3238 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
3240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003241ins_compl_dictionaries(
3242 char_u *dict_start,
3243 char_u *pat,
3244 int flags, /* DICT_FIRST and/or DICT_EXACT */
3245 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003247 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 char_u *ptr;
3249 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u **files;
3252 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003254 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar0b238792006-03-02 22:49:12 +00003256 if (*dict == NUL)
3257 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003258#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003259 /* When 'dictionary' is empty and spell checking is enabled use
3260 * "spell". */
3261 if (!thesaurus && curwin->w_p_spell)
3262 dict = (char_u *)"spell";
3263 else
3264#endif
3265 return;
3266 }
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269 if (buf == NULL)
3270 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003271 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /* If 'infercase' is set, don't use 'smartcase' here */
3274 save_p_scs = p_scs;
3275 if (curbuf->b_p_inf)
3276 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277
3278 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3279 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003280 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003281 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003282 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003283 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003284 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003285
3286 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003287 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003288 len = STRLEN(pat_esc) + 10;
3289 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003290 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003291 {
3292 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003293 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003294 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003295 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003296 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3297 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003298 vim_free(ptr);
3299 }
3300 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003301 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003302 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 if (regmatch.regprog == NULL)
3304 goto theend;
3305 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003306
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3308 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003309 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 /* copy one dictionary file name into buf */
3312 if (flags == DICT_EXACT)
3313 {
3314 count = 1;
3315 files = &dict;
3316 }
3317 else
3318 {
3319 /* Expand wildcards in the dictionary name, but do not allow
3320 * backticks (for security, the 'dict' option may have been set in
3321 * a modeline). */
3322 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003323# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003324 if (!thesaurus && STRCMP(buf, "spell") == 0)
3325 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003326 else
3327# endif
3328 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 || expand_wildcards(1, &buf, &count, &files,
3330 EW_FILE|EW_SILENT) != OK)
3331 count = 0;
3332 }
3333
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003334# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003335 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003337 /* Complete from active spelling. Skip "\<" in the pattern, we
3338 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003339 if (pat[0] == '\\' && pat[1] == '<')
3340 ptr = pat + 2;
3341 else
3342 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003343 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003345 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003346# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003347 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003348 {
3349 ins_compl_files(count, files, thesaurus, flags,
3350 &regmatch, buf, &dir);
3351 if (flags != DICT_EXACT)
3352 FreeWild(count, files);
3353 }
3354 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 break;
3356 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003357
3358theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003360 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 vim_free(buf);
3362}
3363
Bram Moolenaar0b238792006-03-02 22:49:12 +00003364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365ins_compl_files(
3366 int count,
3367 char_u **files,
3368 int thesaurus,
3369 int flags,
3370 regmatch_T *regmatch,
3371 char_u *buf,
3372 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003373{
3374 char_u *ptr;
3375 int i;
3376 FILE *fp;
3377 int add_r;
3378
3379 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3380 {
3381 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3382 if (flags != DICT_EXACT)
3383 {
3384 vim_snprintf((char *)IObuff, IOSIZE,
3385 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003386 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003387 }
3388
3389 if (fp != NULL)
3390 {
3391 /*
3392 * Read dictionary file line by line.
3393 * Check each line for a match.
3394 */
3395 while (!got_int && !compl_interrupted
3396 && !vim_fgets(buf, LSIZE, fp))
3397 {
3398 ptr = buf;
3399 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3400 {
3401 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003402 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003403 ptr = find_line_end(ptr);
3404 else
3405 ptr = find_word_end(ptr);
3406 add_r = ins_compl_add_infercase(regmatch->startp[0],
3407 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003408 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003409 if (thesaurus)
3410 {
3411 char_u *wstart;
3412
3413 /*
3414 * Add the other matches on the line
3415 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003416 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003417 while (!got_int)
3418 {
3419 /* Find start of the next word. Skip white
3420 * space and punctuation. */
3421 ptr = find_word_start(ptr);
3422 if (*ptr == NUL || *ptr == NL)
3423 break;
3424 wstart = ptr;
3425
Bram Moolenaara2993e12007-08-12 14:38:46 +00003426 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003427#ifdef FEAT_MBYTE
3428 if (has_mbyte)
3429 /* Japanese words may have characters in
3430 * different classes, only separate words
3431 * with single-byte non-word characters. */
3432 while (*ptr != NUL)
3433 {
3434 int l = (*mb_ptr2len)(ptr);
3435
3436 if (l < 2 && !vim_iswordc(*ptr))
3437 break;
3438 ptr += l;
3439 }
3440 else
3441#endif
3442 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003443
3444 /* Add the word. Skip the regexp match. */
3445 if (wstart != regmatch->startp[0])
3446 add_r = ins_compl_add_infercase(wstart,
3447 (int)(ptr - wstart),
3448 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003449 }
3450 }
3451 if (add_r == OK)
3452 /* if dir was BACKWARD then honor it just once */
3453 *dir = FORWARD;
3454 else if (add_r == FAIL)
3455 break;
3456 /* avoid expensive call to vim_regexec() when at end
3457 * of line */
3458 if (*ptr == '\n' || got_int)
3459 break;
3460 }
3461 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003462 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003463 }
3464 fclose(fp);
3465 }
3466 }
3467}
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469/*
3470 * Find the start of the next word.
3471 * Returns a pointer to the first char of the word. Also stops at a NUL.
3472 */
3473 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
3476#ifdef FEAT_MBYTE
3477 if (has_mbyte)
3478 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003479 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 else
3481#endif
3482 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3483 ++ptr;
3484 return ptr;
3485}
3486
3487/*
3488 * Find the end of the word. Assumes it starts inside a word.
3489 * Returns a pointer to just after the word.
3490 */
3491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003492find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494#ifdef FEAT_MBYTE
3495 int start_class;
3496
3497 if (has_mbyte)
3498 {
3499 start_class = mb_get_class(ptr);
3500 if (start_class > 1)
3501 while (*ptr != NUL)
3502 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003503 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (mb_get_class(ptr) != start_class)
3505 break;
3506 }
3507 }
3508 else
3509#endif
3510 while (vim_iswordc(*ptr))
3511 ++ptr;
3512 return ptr;
3513}
3514
3515/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 * Find the end of the line, omitting CR and NL at the end.
3517 * Returns a pointer to just after the line.
3518 */
3519 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521{
3522 char_u *s;
3523
3524 s = ptr + STRLEN(ptr);
3525 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3526 --s;
3527 return s;
3528}
3529
3530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 * Free the list of completions
3532 */
3533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003536 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003537 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaard23a8232018-02-10 18:45:26 +01003539 VIM_CLEAR(compl_pattern);
3540 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003544
3545 ins_compl_del_pum();
3546 pum_clear();
3547
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 do
3550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003551 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003552 compl_curr_match = compl_curr_match->cp_next;
3553 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003555 if (match->cp_flags & FREE_FNAME)
3556 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003557 for (i = 0; i < CPT_COUNT; ++i)
3558 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3561 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003562 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003563 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565
3566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003567ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569 compl_cont_status = 0;
3570 compl_started = FALSE;
3571 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003572 VIM_CLEAR(compl_pattern);
3573 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003575 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003576 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003577 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003578 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579}
3580
3581/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003582 * Return TRUE when Insert completion is active.
3583 */
3584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003586{
3587 return compl_started;
3588}
3589
3590/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003591 * Delete one character before the cursor and show the subset of the matches
3592 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003593 * Returns the character to be used, NUL if the work is done and another char
3594 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003595 */
3596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003598{
3599 char_u *line;
3600 char_u *p;
3601
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003602 line = ml_get_curline();
3603 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003604 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003605
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003606 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003607 * allow the word to be deleted, we won't match everything.
3608 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003609 if ((int)(p - line) - (int)compl_col < 0
3610 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003611 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3612 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3613 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003614 return K_BS;
3615
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003616 /* Deleted more than what was used to find matches or didn't finish
3617 * finding all matches: need to look for matches all over again. */
3618 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003619 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003620 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003621
Bram Moolenaara6557602006-02-04 22:43:20 +00003622 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003623 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003624 if (compl_leader != NULL)
3625 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003626 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003627 if (compl_shown_match != NULL)
3628 /* Make sure current match is not a hidden item. */
3629 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003630 return NUL;
3631 }
3632 return K_BS;
3633}
Bram Moolenaara6557602006-02-04 22:43:20 +00003634
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003635/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003636 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3637 * be called.
3638 */
3639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003641{
3642 /* Return TRUE if we didn't complete finding matches or when the
3643 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3644 return compl_was_interrupted
3645 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3646 && compl_opt_refresh_always);
3647}
3648
3649/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003650 * Called after changing "compl_leader".
3651 * Show the popup menu with a different set of matches.
3652 * May also search for matches again if the previous search was interrupted.
3653 */
3654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003656{
3657 ins_compl_del_pum();
3658 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003659 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003660 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003661
3662 if (compl_started)
3663 ins_compl_set_original_text(compl_leader);
3664 else
3665 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003666#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003667 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003668#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003669 /*
3670 * Matches were cleared, need to search for them now. First display
3671 * the changed text before the cursor. Set "compl_restarting" to
3672 * avoid that the first match is inserted.
3673 */
3674 update_screen(0);
3675#ifdef FEAT_GUI
3676 if (gui.in_use)
3677 {
3678 /* Show the cursor after the match, not after the redrawn text. */
3679 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003680 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003681 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003682#endif
3683 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003684 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003685 compl_cont_status = 0;
3686 compl_restarting = FALSE;
3687 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003688
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003689 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003690
3691 /* Show the popup menu with a different set of matches. */
3692 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003693
3694 /* Don't let Enter select the original text when there is no popup menu. */
3695 if (compl_match_array == NULL)
3696 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003697}
3698
3699/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003700 * Return the length of the completion, from the completion start column to
3701 * the cursor column. Making sure it never goes below zero.
3702 */
3703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003704ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003705{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003706 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003707
3708 if (off < 0)
3709 return 0;
3710 return off;
3711}
3712
3713/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003714 * Append one character to the match leader. May reduce the number of
3715 * matches.
3716 */
3717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003718ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003719{
3720#ifdef FEAT_MBYTE
3721 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003722#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003723
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003724 if (stop_arrow() == FAIL)
3725 return;
3726#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003727 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3728 {
3729 char_u buf[MB_MAXBYTES + 1];
3730
3731 (*mb_char2bytes)(c, buf);
3732 buf[cc] = NUL;
3733 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003734 if (compl_opt_refresh_always)
3735 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003736 }
3737 else
3738#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003739 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003740 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003741 if (compl_opt_refresh_always)
3742 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003743 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003744
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003745 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003746 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003747 ins_compl_restart();
3748
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003749 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003750 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003751 * break redo. */
3752 if (!compl_opt_refresh_always)
3753 {
3754 vim_free(compl_leader);
3755 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003756 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003757 if (compl_leader != NULL)
3758 ins_compl_new_leader();
3759 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003760}
3761
3762/*
3763 * Setup for finding completions again without leaving CTRL-X mode. Used when
3764 * BS or a key was typed while still searching for matches.
3765 */
3766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003767ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003768{
3769 ins_compl_free();
3770 compl_started = FALSE;
3771 compl_matches = 0;
3772 compl_cont_status = 0;
3773 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003774}
3775
3776/*
3777 * Set the first match, the original text.
3778 */
3779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003780ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003781{
3782 char_u *p;
3783
Bram Moolenaare87edf32018-04-17 22:14:32 +02003784 /* Replace the original text entry.
3785 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3786 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003787 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3788 {
3789 p = vim_strsave(str);
3790 if (p != NULL)
3791 {
3792 vim_free(compl_first_match->cp_str);
3793 compl_first_match->cp_str = p;
3794 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003795 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003796 else if (compl_first_match->cp_prev != NULL
3797 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3798 {
3799 p = vim_strsave(str);
3800 if (p != NULL)
3801 {
3802 vim_free(compl_first_match->cp_prev->cp_str);
3803 compl_first_match->cp_prev->cp_str = p;
3804 }
3805 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003806}
3807
3808/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003809 * Append one character to the match leader. May reduce the number of
3810 * matches.
3811 */
3812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003814{
3815 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003816 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003817 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003818 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003819
3820 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003821 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003822 {
3823 /* When still at the original match use the first entry that matches
3824 * the leader. */
3825 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3826 {
3827 p = NULL;
3828 for (cp = compl_shown_match->cp_next; cp != NULL
3829 && cp != compl_first_match; cp = cp->cp_next)
3830 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003831 if (compl_leader == NULL
3832 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003833 (int)STRLEN(compl_leader)))
3834 {
3835 p = cp->cp_str;
3836 break;
3837 }
3838 }
3839 if (p == NULL || (int)STRLEN(p) <= len)
3840 return;
3841 }
3842 else
3843 return;
3844 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003845 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003846 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003847 ins_compl_addleader(c);
3848}
3849
3850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003852 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003853 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003856ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003860 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 /* Forget any previous 'special' messages if this is actually
3863 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3864 */
3865 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3866 edit_submode_extra = NULL;
3867
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003868 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003869 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3870 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003871 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003873 /* Set "compl_get_longest" when finding the first matches. */
3874 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003875 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003876 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003877 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003878 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003879
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003880 }
3881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3883 {
3884 /*
3885 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3886 * it will be yet. Now we decide.
3887 */
3888 switch (c)
3889 {
3890 case Ctrl_E:
3891 case Ctrl_Y:
3892 ctrl_x_mode = CTRL_X_SCROLL;
3893 if (!(State & REPLACE_FLAG))
3894 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3895 else
3896 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3897 edit_submode_pre = NULL;
3898 showmode();
3899 break;
3900 case Ctrl_L:
3901 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3902 break;
3903 case Ctrl_F:
3904 ctrl_x_mode = CTRL_X_FILES;
3905 break;
3906 case Ctrl_K:
3907 ctrl_x_mode = CTRL_X_DICTIONARY;
3908 break;
3909 case Ctrl_R:
3910 /* Simply allow ^R to happen without affecting ^X mode */
3911 break;
3912 case Ctrl_T:
3913 ctrl_x_mode = CTRL_X_THESAURUS;
3914 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003915#ifdef FEAT_COMPL_FUNC
3916 case Ctrl_U:
3917 ctrl_x_mode = CTRL_X_FUNCTION;
3918 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003920 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003922#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003923 case 's':
3924 case Ctrl_S:
3925 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003926#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003927 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003928 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003930#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 case Ctrl_RSB:
3933 ctrl_x_mode = CTRL_X_TAGS;
3934 break;
3935#ifdef FEAT_FIND_ID
3936 case Ctrl_I:
3937 case K_S_TAB:
3938 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3939 break;
3940 case Ctrl_D:
3941 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3942 break;
3943#endif
3944 case Ctrl_V:
3945 case Ctrl_Q:
3946 ctrl_x_mode = CTRL_X_CMDLINE;
3947 break;
3948 case Ctrl_P:
3949 case Ctrl_N:
3950 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3951 * just started ^X mode, or there were enough ^X's to cancel
3952 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3953 * do normal expansion when interrupting a different mode (say
3954 * ^X^F^X^P or ^P^X^X^P, see below)
3955 * nothing changes if interrupting mode 0, (eg, the flag
3956 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957 if (!(compl_cont_status & CONT_INTRPT))
3958 compl_cont_status |= CONT_LOCAL;
3959 else if (compl_cont_mode != 0)
3960 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 /* FALLTHROUGH */
3962 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003963 /* If we have typed at least 2 ^X's... for modes != 0, we set
3964 * compl_cont_status = 0 (eg, as if we had just started ^X
3965 * mode).
3966 * For mode 0, we set "compl_cont_mode" to an impossible
3967 * value, in both cases ^X^X can be used to restart the same
3968 * mode (avoiding ADDING mode).
3969 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3970 * 'complete' and local ^P expansions respectively.
3971 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3972 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (c == Ctrl_X)
3974 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003975 if (compl_cont_mode != 0)
3976 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003980 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 edit_submode = NULL;
3982 showmode();
3983 break;
3984 }
3985 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003986 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
3988 /* We're already in CTRL-X mode, do we stay in it? */
3989 if (!vim_is_ctrl_x_key(c))
3990 {
3991 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003992 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 else
3994 ctrl_x_mode = CTRL_X_FINISHED;
3995 edit_submode = NULL;
3996 }
3997 showmode();
3998 }
3999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
4002 /* Show error message from attempted keyword completion (probably
4003 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004004 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004006 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4007 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 || ctrl_x_mode == CTRL_X_FINISHED)
4009 {
4010 /* Get here when we have finished typing a sequence of ^N and
4011 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004012 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004013 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
4015 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004016 * If any of the original typed text has been changed, eg when
4017 * ignorecase is set, we must add back-spaces to the redo
4018 * buffer. We add as few as necessary to delete just the part
4019 * of the original text that has changed.
4020 * When using the longest match, edited the match or used
4021 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004023 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4024 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004025 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004026 ptr = NULL;
4027 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029
4030#ifdef FEAT_CINDENT
4031 want_cindent = (can_cindent && cindent_on());
4032#endif
4033 /*
4034 * When completing whole lines: fix indent for 'cindent'.
4035 * Otherwise, break line if it's too long.
4036 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004037 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
4039#ifdef FEAT_CINDENT
4040 /* re-indent the current line */
4041 if (want_cindent)
4042 {
4043 do_c_expr_indent();
4044 want_cindent = FALSE; /* don't do it again */
4045 }
4046#endif
4047 }
4048 else
4049 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004050 int prev_col = curwin->w_cursor.col;
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004053 if (prev_col > 0)
4054 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004055 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004056 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004058 if (prev_col > 0
4059 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4060 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004063 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004064 * the selection without inserting anything. When
4065 * compl_enter_selects is set the Enter key does the same. */
4066 if ((c == Ctrl_Y || (compl_enter_selects
4067 && (c == CAR || c == K_KENTER || c == NL)))
4068 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004069 retval = TRUE;
4070
Bram Moolenaar47247282016-08-02 22:36:02 +02004071 /* CTRL-E means completion is Ended, go back to the typed text.
4072 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004073 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004074 {
4075 ins_compl_delete();
4076 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004077 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004078 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004079 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004080 retval = TRUE;
4081 }
4082
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004083 auto_format(FALSE, TRUE);
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086 compl_started = FALSE;
4087 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004088 if (!shortmess(SHM_COMPLETIONMENU))
4089 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004090 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004091 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (edit_submode != NULL)
4093 {
4094 edit_submode = NULL;
4095 showmode();
4096 }
4097
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004098#ifdef FEAT_CMDWIN
4099 if (c == Ctrl_C && cmdwin_type != 0)
4100 /* Avoid the popup menu remains displayed when leaving the
4101 * command line window. */
4102 update_screen(0);
4103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#ifdef FEAT_CINDENT
4105 /*
4106 * Indent now if a key was typed that is in 'cinkeys'.
4107 */
4108 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4109 do_c_expr_indent();
4110#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004111 /* Trigger the CompleteDone event to give scripts a chance to act
4112 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004113 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004116 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4117 /* Trigger the CompleteDone event to give scripts a chance to act
4118 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004119 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 /* reset continue_* if we left expansion-mode, if we stay they'll be
4122 * (re)set properly in ins_complete() */
4123 if (!vim_is_ctrl_x_key(c))
4124 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004125 compl_cont_status = 0;
4126 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004128
4129 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130}
4131
4132/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004133 * Fix the redo buffer for the completion leader replacing some of the typed
4134 * text. This inserts backspaces and appends the changed text.
4135 * "ptr" is the known leader text or NUL.
4136 */
4137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004138ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004139{
4140 int len;
4141 char_u *p;
4142 char_u *ptr = ptr_arg;
4143
4144 if (ptr == NULL)
4145 {
4146 if (compl_leader != NULL)
4147 ptr = compl_leader;
4148 else
4149 return; /* nothing to do */
4150 }
4151 if (compl_orig_text != NULL)
4152 {
4153 p = compl_orig_text;
4154 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4155 ;
4156#ifdef FEAT_MBYTE
4157 if (len > 0)
4158 len -= (*mb_head_off)(p, p + len);
4159#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004160 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004161 AppendCharToRedobuff(K_BS);
4162 }
4163 else
4164 len = 0;
4165 if (ptr != NULL)
4166 AppendToRedobuffLit(ptr + len, -1);
4167}
4168
4169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4171 * (depending on flag) starting from buf and looking for a non-scanned
4172 * buffer (other than curbuf). curbuf is special, if it is called with
4173 * buf=curbuf then it has to be the first call for a given flag/expansion.
4174 *
4175 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4176 */
4177 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004178ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 if (flag == 'w') /* just windows */
4183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 if (buf == curbuf) /* first call for this flag/expansion */
4185 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004186 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 && wp->w_buffer->b_scanned)
4188 ;
4189 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 else
4192 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4193 * (unlisted buffers)
4194 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004195 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 && ((flag == 'U'
4197 ? buf->b_p_bl
4198 : (!buf->b_p_bl
4199 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004200 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 ;
4202 return buf;
4203}
4204
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004205#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004206/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004207 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004208 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004209 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004211expand_by_function(
4212 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4213 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004214{
Bram Moolenaar82139082011-09-14 16:52:09 +02004215 list_T *matchlist = NULL;
4216 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004217 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004218 char_u *funcname;
4219 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004220 win_T *curwin_save;
4221 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004222 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004223 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004224
Bram Moolenaare344bea2005-09-01 20:46:49 +00004225 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4226 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004227 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004228
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004229 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004230 args[0].v_type = VAR_NUMBER;
4231 args[0].vval.v_number = 0;
4232 args[1].v_type = VAR_STRING;
4233 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4234 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004235
Bram Moolenaare344bea2005-09-01 20:46:49 +00004236 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004237 curwin_save = curwin;
4238 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004239
4240 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004241 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004242 {
4243 switch (rettv.v_type)
4244 {
4245 case VAR_LIST:
4246 matchlist = rettv.vval.v_list;
4247 break;
4248 case VAR_DICT:
4249 matchdict = rettv.vval.v_dict;
4250 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004251 case VAR_SPECIAL:
4252 if (rettv.vval.v_number == VVAL_NONE)
4253 compl_opt_suppress_empty = TRUE;
4254 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004255 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004256 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004257 clear_tv(&rettv);
4258 break;
4259 }
4260 }
4261
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004262 if (curwin_save != curwin || curbuf_save != curbuf)
4263 {
4264 EMSG(_(e_complwin));
4265 goto theend;
4266 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004267 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004268 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004269 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004270 {
4271 EMSG(_(e_compldel));
4272 goto theend;
4273 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004274
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004275 if (matchlist != NULL)
4276 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004277 else if (matchdict != NULL)
4278 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004279
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004280theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004281 // Restore State, it might have been changed.
4282 State = save_State;
4283
Bram Moolenaar82139082011-09-14 16:52:09 +02004284 if (matchdict != NULL)
4285 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004286 if (matchlist != NULL)
4287 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004288}
4289#endif /* FEAT_COMPL_FUNC */
4290
Bram Moolenaar39f05632006-03-19 22:15:26 +00004291#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004292/*
4293 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004294 */
4295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004296ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004297{
4298 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004299 int dir = compl_direction;
4300
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004301 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004302 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004303 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004304 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4305 /* if dir was BACKWARD then honor it just once */
4306 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004307 else if (did_emsg)
4308 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004309 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004311
4312/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004313 * Add completions from a dict.
4314 */
4315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004316ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004317{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004318 dictitem_T *di_refresh;
4319 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004320
4321 /* Check for optional "refresh" item. */
4322 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004323 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4324 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004325 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004326 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004327
4328 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4329 compl_opt_refresh_always = TRUE;
4330 }
4331
4332 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004333 di_words = dict_find(dict, (char_u *)"words", 5);
4334 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4335 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004336}
4337
4338/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004339 * Add a match to the list of matches from a typeval_T.
4340 * If the given string is already in the list of completions, then return
4341 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4342 * maybe because alloc() returns NULL, then FAIL is returned.
4343 */
4344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004345ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004346{
4347 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004348 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004349 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004350 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004351 char_u *(cptext[CPT_COUNT]);
4352
4353 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4354 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004355 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4356 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004357 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004358 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004359 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004360 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004361 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004362 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004363 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004364 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004365 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004366 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4367 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4368 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4369 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4370 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4371 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004372 }
4373 else
4374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004375 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004376 vim_memset(cptext, 0, sizeof(cptext));
4377 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004378 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004379 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004380 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004381}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004382#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004383
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384/*
4385 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004386 * The search starts at position "ini" in curbuf and in the direction
4387 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004388 * When "compl_started" is FALSE start at that position, otherwise continue
4389 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 * This may return before finding all the matches.
4391 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 */
4393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004394ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396 static pos_T first_match_pos;
4397 static pos_T last_match_pos;
4398 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 static int found_all = FALSE; /* Found all matches of a
4400 certain type. */
4401 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaar572cb562005-08-05 21:35:02 +00004403 pos_T *pos;
4404 char_u **matches;
4405 int save_p_scs;
4406 int save_p_ws;
4407 int save_p_ic;
4408 int i;
4409 int num_matches;
4410 int len;
4411 int found_new_match;
4412 int type = ctrl_x_mode;
4413 char_u *ptr;
4414 char_u *dict = NULL;
4415 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004416 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004420 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 ins_buf->b_scanned = 0;
4422 found_all = FALSE;
4423 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004425 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 last_match_pos = first_match_pos = *ini;
4427 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004428 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4429 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar4475b622017-05-01 20:46:52 +02004431 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004432 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004433
4434 /*
4435 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 for (;;)
4438 {
4439 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004440 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 * or if found_all says this entry is done. For ^X^L only use the
4444 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004445 if ((ctrl_x_mode == CTRL_X_NORMAL
4446 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 found_all = FALSE;
4450 while (*e_cpt == ',' || *e_cpt == ' ')
4451 e_cpt++;
4452 if (*e_cpt == '.' && !curbuf->b_scanned)
4453 {
4454 ins_buf = curbuf;
4455 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004456 /* Move the cursor back one character so that ^N can match the
4457 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004458 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004459 {
4460 /* Move the cursor to after the last character in the
4461 * buffer, so that word at start of buffer is found
4462 * correctly. */
4463 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4464 first_match_pos.col =
4465 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 last_match_pos = first_match_pos;
4468 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004469
4470 /* Remember the first match so that the loop stops when we
4471 * wrap and come back there a second time. */
4472 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4475 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4476 {
4477 /* Scan a buffer, but not the current one. */
4478 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4479 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 first_match_pos.col = last_match_pos.col = 0;
4482 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4483 last_match_pos.lnum = 0;
4484 type = 0;
4485 }
4486 else /* unloaded buffer, scan like dictionary */
4487 {
4488 found_all = TRUE;
4489 if (ins_buf->b_fname == NULL)
4490 continue;
4491 type = CTRL_X_DICTIONARY;
4492 dict = ins_buf->b_fname;
4493 dict_f = DICT_EXACT;
4494 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004495 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 ins_buf->b_fname == NULL
4497 ? buf_spname(ins_buf)
4498 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004499 ? ins_buf->b_fname
4500 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004501 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503 else if (*e_cpt == NUL)
4504 break;
4505 else
4506 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004507 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 type = -1;
4509 else if (*e_cpt == 'k' || *e_cpt == 's')
4510 {
4511 if (*e_cpt == 'k')
4512 type = CTRL_X_DICTIONARY;
4513 else
4514 type = CTRL_X_THESAURUS;
4515 if (*++e_cpt != ',' && *e_cpt != NUL)
4516 {
4517 dict = e_cpt;
4518 dict_f = DICT_FIRST;
4519 }
4520 }
4521#ifdef FEAT_FIND_ID
4522 else if (*e_cpt == 'i')
4523 type = CTRL_X_PATH_PATTERNS;
4524 else if (*e_cpt == 'd')
4525 type = CTRL_X_PATH_DEFINES;
4526#endif
4527 else if (*e_cpt == ']' || *e_cpt == 't')
4528 {
4529 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004530 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004531 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 else
4534 type = -1;
4535
4536 /* in any case e_cpt is advanced to the next entry */
4537 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4538
4539 found_all = TRUE;
4540 if (type == -1)
4541 continue;
4542 }
4543 }
4544
Bram Moolenaar4475b622017-05-01 20:46:52 +02004545 /* If complete() was called then compl_pattern has been reset. The
4546 * following won't work then, bail out. */
4547 if (compl_pattern == NULL)
4548 break;
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 switch (type)
4551 {
4552 case -1:
4553 break;
4554#ifdef FEAT_FIND_ID
4555 case CTRL_X_PATH_PATTERNS:
4556 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004557 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4562 (linenr_T)1, (linenr_T)MAXLNUM);
4563 break;
4564#endif
4565
4566 case CTRL_X_DICTIONARY:
4567 case CTRL_X_THESAURUS:
4568 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004569 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 : (type == CTRL_X_THESAURUS
4571 ? (*curbuf->b_p_tsr == NUL
4572 ? p_tsr
4573 : curbuf->b_p_tsr)
4574 : (*curbuf->b_p_dict == NUL
4575 ? p_dict
4576 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004577 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004578 dict != NULL ? dict_f
4579 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 dict = NULL;
4581 break;
4582
4583 case CTRL_X_TAGS:
4584 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4585 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004588 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 * of matches is found when compl_pattern is empty */
4590 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004591 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4592 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4594 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004595 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 p_ic = save_p_ic;
4598 break;
4599
4600 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4603 {
4604
4605 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004607 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 break;
4610
4611 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 if (expand_cmdline(&compl_xp, compl_pattern,
4613 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004615 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 break;
4617
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004618#ifdef FEAT_COMPL_FUNC
4619 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004620 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004621 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004622 break;
4623#endif
4624
Bram Moolenaar488c6512005-08-11 20:09:58 +00004625 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004626#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004627 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004628 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004629 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004630 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004631#endif
4632 break;
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 default: /* normal ^P/^N and ^X^L */
4635 /*
4636 * If 'infercase' is set, don't use 'smartcase' here
4637 */
4638 save_p_scs = p_scs;
4639 if (ins_buf->b_p_inf)
4640 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641
Bram Moolenaar361aa502014-01-23 22:45:58 +01004642 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 * end but never from the middle, thus setting nowrapscan in this
4644 * buffers is a good idea, on the other hand, we always set
4645 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4646 save_p_ws = p_ws;
4647 if (ins_buf != curbuf)
4648 p_ws = FALSE;
4649 else if (*e_cpt == '.')
4650 p_ws = TRUE;
4651 for (;;)
4652 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004653 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004655 ++msg_silent; /* Don't want messages for wrapscan. */
4656
Bram Moolenaare4214502015-03-05 18:08:43 +01004657 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4658 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004659 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004660 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004663 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004665 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004666 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004668 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004669 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004670 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004672 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 first_match_pos = *pos;
4675 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004676 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004679 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 found_new_match = FAIL;
4681 if (found_new_match == FAIL)
4682 {
4683 if (ins_buf == curbuf)
4684 found_all = TRUE;
4685 break;
4686 }
4687
4688 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004689 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 && ini->lnum == pos->lnum
4691 && ini->col == pos->col)
4692 continue;
4693 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004694 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004696 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4699 continue;
4700 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4701 if (!p_paste)
4702 ptr = skipwhite(ptr);
4703 }
4704 len = (int)STRLEN(ptr);
4705 }
4706 else
4707 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004708 char_u *tmp_ptr = ptr;
4709
4710 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 /* Skip if already inside a word. */
4714 if (vim_iswordp(tmp_ptr))
4715 continue;
4716 /* Find start of next word. */
4717 tmp_ptr = find_word_start(tmp_ptr);
4718 }
4719 /* Find end of this word. */
4720 tmp_ptr = find_word_end(tmp_ptr);
4721 len = (int)(tmp_ptr - ptr);
4722
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004723 if ((compl_cont_status & CONT_ADDING)
4724 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4727 {
4728 /* Try next line, if any. the new word will be
4729 * "join" as if the normal command "J" was used.
4730 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004731 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 * works -- Acevedo */
4733 STRNCPY(IObuff, ptr, len);
4734 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4735 tmp_ptr = ptr = skipwhite(ptr);
4736 /* Find start of next word. */
4737 tmp_ptr = find_word_start(tmp_ptr);
4738 /* Find end of next word. */
4739 tmp_ptr = find_word_end(tmp_ptr);
4740 if (tmp_ptr > ptr)
4741 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004742 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004744 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 IObuff[len++] = ' ';
4746 /* IObuf =~ "\k.* ", thus len >= 2 */
4747 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004748 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 || (vim_strchr(p_cpo, CPO_JOINSP)
4750 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004751 && (IObuff[len - 2] == '?'
4752 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 IObuff[len++] = ' ';
4754 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004755 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if (tmp_ptr - ptr >= IOSIZE - len)
4757 tmp_ptr = ptr + IOSIZE - len - 1;
4758 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4759 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004760 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 IObuff[len] = NUL;
4763 ptr = IObuff;
4764 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 continue;
4767 }
4768 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004769 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004770 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004771 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
4773 found_new_match = OK;
4774 break;
4775 }
4776 }
4777 p_scs = save_p_scs;
4778 p_ws = save_p_ws;
4779 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004780
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004782 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004783 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 found_new_match = OK;
4785
4786 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004787 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4788 * match */
4789 if ((ctrl_x_mode != CTRL_X_NORMAL
4790 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004792 {
4793 if (got_int)
4794 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004795 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004796 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004797 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004798
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004799 if ((ctrl_x_mode != CTRL_X_NORMAL
4800 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004801 || compl_interrupted)
4802 break;
4803 compl_started = TRUE;
4804 }
4805 else
4806 {
4807 /* Mark a buffer scanned when it has been scanned completely */
4808 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4809 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004811 compl_started = FALSE;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004816 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 && *e_cpt == NUL) /* Got to end of 'complete' */
4818 found_new_match = FAIL;
4819
4820 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004821 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4822 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 i = ins_compl_make_cyclic();
4824
Bram Moolenaar4475b622017-05-01 20:46:52 +02004825 if (compl_old_match != NULL)
4826 {
4827 /* If several matches were added (FORWARD) or the search failed and has
4828 * just been made cyclic then we have to move compl_curr_match to the
4829 * next or previous entry (if any) -- Acevedo */
4830 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4831 : compl_old_match->cp_prev;
4832 if (compl_curr_match == NULL)
4833 compl_curr_match = compl_old_match;
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 return i;
4836}
4837
4838/* Delete the old text being completed. */
4839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004840ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004842 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 /*
4845 * In insert mode: Delete the typed part.
4846 * In replace mode: Put the old characters back, if any.
4847 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004848 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4849 if ((int)curwin->w_cursor.col > col)
4850 {
4851 if (stop_arrow() == FAIL)
4852 return;
4853 backspace_until_column(col);
4854 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004855
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004856 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4857 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004859 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004860 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861}
4862
Bram Moolenaar472e8592016-10-15 17:06:47 +02004863/*
4864 * Insert the new text being completed.
4865 * "in_compl_func" is TRUE when called from complete_check().
4866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004868ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004870 dict_T *dict;
4871
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004872 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004873 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4874 compl_used_match = FALSE;
4875 else
4876 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004877
4878 /* Set completed item. */
4879 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004880 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004881 if (dict != NULL)
4882 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004883 dict_add_string(dict, "word", compl_shown_match->cp_str);
4884 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4885 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4886 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4887 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4888 dict_add_string(dict, "user_data",
4889 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004890 }
4891 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004892 if (!in_compl_func)
4893 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894}
4895
4896/*
4897 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4899 * get more completions. If it is FALSE, then we just do nothing when there
4900 * are no more completions in a given direction. The latter case is used when
4901 * we are still in the middle of finding completions, to allow browsing
4902 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 * Return the total number of matches, or -1 if still unknown -- webb.
4904 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004905 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4906 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 *
4908 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004909 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4910 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 */
4912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004913ins_compl_next(
4914 int allow_get_expansion,
4915 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004916 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004917 int insert_match, /* Insert the newly selected match */
4918 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004921 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004922 compl_T *found_compl = NULL;
4923 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004924 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004925 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004927 /* When user complete function return -1 for findstart which is next
4928 * time of 'always', compl_shown_match become NULL. */
4929 if (compl_shown_match == NULL)
4930 return -1;
4931
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004932 if (compl_leader != NULL
4933 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004935 /* Set "compl_shown_match" to the actually shown match, it may differ
4936 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004937 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004938 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004939 && compl_shown_match->cp_next != NULL
4940 && compl_shown_match->cp_next != compl_first_match)
4941 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004942
4943 /* If we didn't find it searching forward, and compl_shows_dir is
4944 * backward, find the last match. */
4945 if (compl_shows_dir == BACKWARD
4946 && !ins_compl_equal(compl_shown_match,
4947 compl_leader, (int)STRLEN(compl_leader))
4948 && (compl_shown_match->cp_next == NULL
4949 || compl_shown_match->cp_next == compl_first_match))
4950 {
4951 while (!ins_compl_equal(compl_shown_match,
4952 compl_leader, (int)STRLEN(compl_leader))
4953 && compl_shown_match->cp_prev != NULL
4954 && compl_shown_match->cp_prev != compl_first_match)
4955 compl_shown_match = compl_shown_match->cp_prev;
4956 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004957 }
4958
4959 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004960 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 /* Delete old text to be replaced */
4962 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004963
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004964 /* When finding the longest common text we stick at the original text,
4965 * don't let CTRL-N or CTRL-P move to the first match. */
4966 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4967
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004968 /* When restarting the search don't insert the first match either. */
4969 if (compl_restarting)
4970 {
4971 advance = FALSE;
4972 compl_restarting = FALSE;
4973 }
4974
Bram Moolenaare3226be2005-12-18 22:10:00 +00004975 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4976 * around. */
4977 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004979 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004982 found_end = (compl_first_match != NULL
4983 && (compl_shown_match->cp_next == compl_first_match
4984 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004985 }
4986 else if (compl_shows_dir == BACKWARD
4987 && compl_shown_match->cp_prev != NULL)
4988 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004989 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004990 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004991 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004994 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004995 if (!allow_get_expansion)
4996 {
4997 if (advance)
4998 {
4999 if (compl_shows_dir == BACKWARD)
5000 compl_pending -= todo + 1;
5001 else
5002 compl_pending += todo + 1;
5003 }
5004 return -1;
5005 }
5006
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005007 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005008 {
5009 if (compl_shows_dir == BACKWARD)
5010 --compl_pending;
5011 else
5012 ++compl_pending;
5013 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005014
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005015 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005016 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005017
5018 /* handle any pending completions */
5019 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005020 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005021 {
5022 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5023 {
5024 compl_shown_match = compl_shown_match->cp_next;
5025 --compl_pending;
5026 }
5027 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5028 {
5029 compl_shown_match = compl_shown_match->cp_prev;
5030 ++compl_pending;
5031 }
5032 else
5033 break;
5034 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005035 found_end = FALSE;
5036 }
5037 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5038 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005039 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005040 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005041 ++todo;
5042 else
5043 /* Remember a matching item. */
5044 found_compl = compl_shown_match;
5045
5046 /* Stop at the end of the list when we found a usable match. */
5047 if (found_end)
5048 {
5049 if (found_compl != NULL)
5050 {
5051 compl_shown_match = found_compl;
5052 break;
5053 }
5054 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005058 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005059 if (compl_no_insert && !started)
5060 {
5061 ins_bytes(compl_orig_text + ins_compl_len());
5062 compl_used_match = FALSE;
5063 }
5064 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005065 {
5066 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005067 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005068 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005069 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005070 }
5071 else
5072 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 if (!allow_get_expansion)
5075 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005076 /* may undisplay the popup menu first */
5077 ins_compl_upd_pum();
5078
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005079 /* redraw to show the user what was inserted */
5080 update_screen(0);
5081
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005082 /* display the updated popup menu */
5083 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005084#ifdef FEAT_GUI
5085 if (gui.in_use)
5086 {
5087 /* Show the cursor after the match, not after the redrawn text. */
5088 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005089 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005090 }
5091#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005092
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /* Delete old text to be replaced, since we're still searching and
5094 * don't want to match ourselves! */
5095 ins_compl_delete();
5096 }
5097
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005098 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005099 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005100 if (compl_no_insert && !started)
5101 compl_enter_selects = TRUE;
5102 else
5103 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 /*
5106 * Show the file name for the match (if any)
5107 * Truncate the file name to avoid a wait for return.
5108 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005109 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005111 char *lead = _("match in file");
5112 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5113 char_u *s;
5114 char_u *e;
5115
5116 if (space > 0)
5117 {
5118 /* We need the tail that fits. With double-byte encoding going
5119 * back from the end is very slow, thus go from the start and keep
5120 * the text that fits in "space" between "s" and "e". */
5121 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5122 {
5123 space -= ptr2cells(e);
5124 while (space < 0)
5125 {
5126 space += ptr2cells(s);
5127 MB_PTR_ADV(s);
5128 }
5129 }
5130 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5131 s > compl_shown_match->cp_fname ? "<" : "", s);
5132 msg(IObuff);
5133 redraw_cmdline = FALSE; /* don't overwrite! */
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136
5137 return num_matches;
5138}
5139
5140/*
5141 * Call this while finding completions, to check whether the user has hit a key
5142 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005143 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005145 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005146 * "in_compl_func" is TRUE when called from complete_check(), don't set
5147 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 */
5149 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005150ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005153 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005155 /* Don't check when reading keys from a script, :normal or feedkeys().
5156 * That would break the test scripts. But do check for keys when called
5157 * from complete_check(). */
5158 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 return;
5160
5161 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005162 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 return;
5164 count = 0;
5165
Bram Moolenaara260a972006-06-23 19:36:29 +00005166 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5167 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (c != NUL)
5170 {
5171 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5172 {
5173 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005174 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005175 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005176 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005178 else
5179 {
5180 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005181 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005182 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005183 if (c != K_IGNORE)
5184 {
5185 /* Don't interrupt completion when the character wasn't typed,
5186 * e.g., when doing @q to replay keys. */
5187 if (c != Ctrl_R && KeyTyped)
5188 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005189
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005190 vungetc(c);
5191 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005194 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005195 {
5196 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5197
5198 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005199 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005200 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005201}
5202
5203/*
5204 * Decide the direction of Insert mode complete from the key typed.
5205 * Returns BACKWARD or FORWARD.
5206 */
5207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005208ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005209{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005210 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005211 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005212 return BACKWARD;
5213 return FORWARD;
5214}
5215
5216/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005217 * Return TRUE for keys that are used for completion only when the popup menu
5218 * is visible.
5219 */
5220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005221ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005222{
5223 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005224 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5225 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005226}
5227
5228/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005229 * Decide the number of completions to move forward.
5230 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5231 */
5232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005233ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005234{
5235 int h;
5236
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005237 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005238 {
5239 h = pum_get_height();
5240 if (h > 3)
5241 h -= 2; /* keep some context */
5242 return h;
5243 }
5244 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245}
5246
5247/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005248 * Return TRUE if completion with "c" should insert the match, FALSE if only
5249 * to change the currently selected completion.
5250 */
5251 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005252ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005253{
5254 switch (c)
5255 {
5256 case K_UP:
5257 case K_DOWN:
5258 case K_PAGEDOWN:
5259 case K_KPAGEDOWN:
5260 case K_S_DOWN:
5261 case K_PAGEUP:
5262 case K_KPAGEUP:
5263 case K_S_UP:
5264 return FALSE;
5265 }
5266 return TRUE;
5267}
5268
5269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 * Do Insert mode completion.
5271 * Called when character "c" was typed, which has a meaning for completion.
5272 * Returns OK if completion was done, FAIL if something failed (out of mem).
5273 */
5274 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005275ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 char_u *line;
5278 int startcol = 0; /* column where searched text starts */
5279 colnr_T curs_col; /* cursor column */
5280 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005281 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005282 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005283 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005284 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaare3226be2005-12-18 22:10:00 +00005286 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005287 insert_match = ins_compl_use_match(c);
5288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
5291 /* First time we hit ^N or ^P (in a row, I mean) */
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 did_ai = FALSE;
5294#ifdef FEAT_SMARTINDENT
5295 did_si = FALSE;
5296 can_si = FALSE;
5297 can_si_back = FALSE;
5298#endif
5299 if (stop_arrow() == FAIL)
5300 return FAIL;
5301
5302 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005304 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005306 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 * "compl_startpos" to the cursor as a pattern to add a new word
5308 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005309 * "compl_startpos" is not in the same line as the cursor then fix it
5310 * (the line has been split because it was longer than 'tw'). if SOL
5311 * is set then skip the previous pattern, a word at the beginning of
5312 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005313 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5314 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
5316 /*
5317 * it is a continued search
5318 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005320 if (ctrl_x_mode == CTRL_X_NORMAL
5321 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5322 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 /* line (probably) wrapped, set compl_startpos to the
5327 * first non_blank in the line, if it is not a wordchar
5328 * include it to get a better pattern, but then we don't
5329 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005330 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 compl_startpos.col = compl_col;
5332 compl_startpos.lnum = curwin->w_cursor.lnum;
5333 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 else
5336 {
5337 /* S_IPOS was set when we inserted a word that was at the
5338 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 * mode but first we need to redefine compl_startpos */
5340 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 compl_cont_status |= CONT_SOL;
5343 compl_startpos.col = (colnr_T)(skipwhite(
5344 line + compl_length
5345 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005350 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005351 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_cont_status &= ~CONT_SOL;
5356 compl_length = (IOSIZE - MIN_SPACE);
5357 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5360 if (compl_length < 1)
5361 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005363 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005374 if (ctrl_x_mode != CTRL_X_NORMAL)
5375 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005376 compl_cont_status = 0;
5377 compl_cont_status |= CONT_N_ADDS;
5378 compl_startpos = curwin->w_cursor;
5379 startcol = (int)curs_col;
5380 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382
5383 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005384 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005386 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5388 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 compl_col += ++startcol;
5394 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005397 compl_pattern = str_foldcase(line + compl_col,
5398 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 compl_pattern = vim_strnsave(line + compl_col,
5401 compl_length);
5402 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return FAIL;
5404 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 char_u *prefix = (char_u *)"\\<";
5408
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005409 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005410 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005411 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 if (!vim_iswordp(line + compl_col)
5415 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 && (
5417#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421#endif
5422 )))
5423 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 STRCPY((char *)compl_pattern, prefix);
5425 (void)quote_meta(compl_pattern + STRLEN(prefix),
5426 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005430 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#endif
5434 )
5435 {
5436 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005437 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5438 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 compl_col += curs_col;
5441 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
5443 else
5444 {
5445#ifdef FEAT_MBYTE
5446 /* Search the point of change class of multibyte character
5447 * or not a word single byte character backward. */
5448 if (has_mbyte)
5449 {
5450 int base_class;
5451 int head_off;
5452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005453 startcol -= (*mb_head_off)(line, line + startcol);
5454 base_class = mb_get_class(line + startcol);
5455 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 head_off = (*mb_head_off)(line, line + startcol);
5458 if (base_class != mb_get_class(line + startcol
5459 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 }
5464 else
5465#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005466 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 compl_col += ++startcol;
5469 compl_length = (int)curs_col - startcol;
5470 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
5472 /* Only match word with at least two chars -- webb
5473 * there's no need to call quote_meta,
5474 * alloc(7) is enough -- Acevedo
5475 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 compl_pattern = alloc(7);
5477 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 STRCPY((char *)compl_pattern, "\\<");
5480 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5481 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 else
5484 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005485 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005486 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005487 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005489 STRCPY((char *)compl_pattern, "\\<");
5490 (void)quote_meta(compl_pattern + 2, line + compl_col,
5491 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 }
5494 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005495 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005497 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 compl_length = (int)curs_col - (int)compl_col;
5499 if (compl_length < 0) /* cursor in indent: empty pattern */
5500 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 compl_pattern = str_foldcase(line + compl_col, compl_length,
5503 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005505 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5506 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return FAIL;
5508 }
5509 else if (ctrl_x_mode == CTRL_X_FILES)
5510 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005511 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005512 if (startcol > 0)
5513 {
5514 char_u *p = line + startcol;
5515
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005516 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005517 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005518 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005519 if (p == line && vim_isfilec(PTR2CHAR(p)))
5520 startcol = 0;
5521 else
5522 startcol = (int)(p - line) + 1;
5523 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005524
Bram Moolenaar0300e462013-09-08 16:03:45 +02005525 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 compl_length = (int)curs_col - startcol;
5527 compl_pattern = addstar(line + compl_col, compl_length,
5528 EXPAND_FILES);
5529 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 return FAIL;
5531 }
5532 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5533 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005534 compl_pattern = vim_strnsave(line, curs_col);
5535 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005537 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005538 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005539 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5540 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005541 /* No completion possible, use an empty pattern to get a
5542 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005543 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005544 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005545 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5546 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005548 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005549 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005550#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005551 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005552 * Call user defined function 'completefunc' with "a:findstart"
5553 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005554 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005555 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005556 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005557 char_u *funcname;
5558 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005559 win_T *curwin_save;
5560 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005561 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005562
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005563 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005564 * string */
5565 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5566 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5567 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005568 {
5569 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5570 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005571 /* restore did_ai, so that adding comment leader works */
5572 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005573 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005574 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005575
Bram Moolenaarffa96842018-06-12 22:05:14 +02005576 args[0].v_type = VAR_NUMBER;
5577 args[0].vval.v_number = 1;
5578 args[1].v_type = VAR_STRING;
5579 args[1].vval.v_string = (char_u *)"";
5580 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005581 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005582 curwin_save = curwin;
5583 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005584 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005585
5586 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005587 if (curwin_save != curwin || curbuf_save != curbuf)
5588 {
5589 EMSG(_(e_complwin));
5590 return FAIL;
5591 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005592 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005593 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005594 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005595 {
5596 EMSG(_(e_compldel));
5597 return FAIL;
5598 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005599
Bram Moolenaar6110a002012-01-26 18:58:38 +01005600 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005601 * cancel the complete without an error.
5602 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005603 if (col == -2)
5604 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005605 if (col == -3)
5606 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005607 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005608 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005609 if (!shortmess(SHM_COMPLETIONMENU))
5610 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005611 return FAIL;
5612 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005613
Bram Moolenaar82139082011-09-14 16:52:09 +02005614 /*
5615 * Reset extended parameters of completion, when start new
5616 * completion.
5617 */
5618 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005619 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005620
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005621 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005622 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005623 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005624 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005625 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005626
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005627 /* Setup variables for completion. Need to obtain "line" again,
5628 * it may have become invalid. */
5629 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005630 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5632 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005633#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 return FAIL;
5635 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005636 else if (ctrl_x_mode == CTRL_X_SPELL)
5637 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005638#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005639 if (spell_bad_len > 0)
5640 compl_col = curs_col - spell_bad_len;
5641 else
5642 compl_col = spell_word_start(startcol);
5643 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005644 {
5645 compl_length = 0;
5646 compl_col = curs_col;
5647 }
5648 else
5649 {
5650 spell_expand_check_cap(compl_col);
5651 compl_length = (int)curs_col - compl_col;
5652 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005653 /* Need to obtain "line" again, it may have become invalid. */
5654 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005655 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5656 if (compl_pattern == NULL)
5657#endif
5658 return FAIL;
5659 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005660 else
5661 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005662 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005663 return FAIL;
5664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005666 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005669 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 /* Insert a new line, keep indentation but ignore 'comments' */
5672#ifdef FEAT_COMMENTS
5673 char_u *old = curbuf->b_p_com;
5674
5675 curbuf->b_p_com = (char_u *)"";
5676#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 compl_startpos.lnum = curwin->w_cursor.lnum;
5678 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 ins_eol('\r');
5680#ifdef FEAT_COMMENTS
5681 curbuf->b_p_com = old;
5682#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005683 compl_length = 0;
5684 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 }
5687 else
5688 {
5689 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005690 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005693 if (compl_cont_status & CONT_LOCAL)
5694 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 else
5696 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5697
Bram Moolenaar62951b12011-09-21 18:23:05 +02005698 /* If any of the original typed text has been changed we need to fix
5699 * the redo buffer. */
5700 ins_compl_fixRedoBufForLeader(NULL);
5701
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005702 /* Always add completion for the original text. */
5703 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005704 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5705 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005706 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005708 VIM_CLEAR(compl_pattern);
5709 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return FAIL;
5711 }
5712
5713 /* showmode might reset the internal line pointers, so it must
5714 * be called before line = ml_get(), or when this address is no
5715 * longer needed. -- Acevedo.
5716 */
5717 edit_submode_extra = (char_u *)_("-- Searching...");
5718 edit_submode_highl = HLF_COUNT;
5719 showmode();
5720 edit_submode_extra = NULL;
5721 out_flush();
5722 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005723 else if (insert_match && stop_arrow() == FAIL)
5724 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005726 compl_shown_match = compl_curr_match;
5727 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005730 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005732 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005733 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005734 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005736 /* may undisplay the popup menu */
5737 ins_compl_upd_pum();
5738
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005739 if (n > 1) /* all matches have been found */
5740 compl_matches = n;
5741 compl_curr_match = compl_shown_match;
5742 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaard68071d2006-05-02 22:08:30 +00005744 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5745 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 if (got_int && !global_busy)
5747 {
5748 (void)vgetc();
5749 got_int = FALSE;
5750 }
5751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005752 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005753 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005755 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5756 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5758 edit_submode_highl = HLF_E;
5759 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5760 * because we couldn't expand anything at first place, but if we used
5761 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5762 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005763 if ( compl_length > 1
5764 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005765 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5767 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005768 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770
Bram Moolenaar572cb562005-08-05 21:35:02 +00005771 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005772 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005774 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776 if (edit_submode_extra == NULL)
5777 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005778 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 edit_submode_extra = (char_u *)_("Back at original");
5781 edit_submode_highl = HLF_W;
5782 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005783 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
5785 edit_submode_extra = (char_u *)_("Word from other line");
5786 edit_submode_highl = HLF_COUNT;
5787 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005788 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 edit_submode_extra = (char_u *)_("The only match");
5791 edit_submode_highl = HLF_COUNT;
5792 }
5793 else
5794 {
5795 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005798 int number = 0;
5799 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005801 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
5803 /* search backwards for the first valid (!= -1) number.
5804 * This should normally succeed already at the first loop
5805 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005806 for (match = compl_curr_match->cp_prev; match != NULL
5807 && match != compl_first_match;
5808 match = match->cp_prev)
5809 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005811 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813 }
5814 if (match != NULL)
5815 /* go up and assign all numbers which are not assigned
5816 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005817 for (match = match->cp_next;
5818 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005819 match = match->cp_next)
5820 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
5822 else /* BACKWARD */
5823 {
5824 /* search forwards (upwards) for the first valid (!= -1)
5825 * number. This should normally succeed already at the
5826 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005827 for (match = compl_curr_match->cp_next; match != NULL
5828 && match != compl_first_match;
5829 match = match->cp_next)
5830 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
5834 }
5835 if (match != NULL)
5836 /* go down and assign all numbers which are not
5837 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005838 for (match = match->cp_prev; match
5839 && match->cp_number == -1;
5840 match = match->cp_prev)
5841 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843 }
5844
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005845 /* The match should always have a sequence number now, this is
5846 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005847 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005849 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5850 * Translations may need more than twice that. */
5851 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005853 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005854 vim_snprintf((char *)match_ref, sizeof(match_ref),
5855 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005856 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005858 vim_snprintf((char *)match_ref, sizeof(match_ref),
5859 _("match %d"),
5860 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 edit_submode_extra = match_ref;
5862 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005863 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 curs_columns(FALSE);
5865 }
5866 }
5867 }
5868
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005869 // Show a message about what (completion) mode we're in.
5870 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005872 showmode();
5873 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005874 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005875 if (edit_submode_extra != NULL)
5876 {
5877 if (!p_smd)
5878 msg_attr(edit_submode_extra,
5879 edit_submode_highl < HLF_COUNT
5880 ? HL_ATTR(edit_submode_highl) : 0);
5881 }
5882 else
5883 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaard68071d2006-05-02 22:08:30 +00005887 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005888 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005889 show_pum(save_w_wrow, save_w_leftcol);
5890
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005891 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005892 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 return OK;
5895}
5896
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005897 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005898show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005899{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005900 /* RedrawingDisabled may be set when invoked through complete(). */
5901 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005902
Bram Moolenaarcb036422017-03-01 12:29:10 +01005903 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005904
Bram Moolenaarcb036422017-03-01 12:29:10 +01005905 /* If the cursor moved or the display scrolled we need to remove the pum
5906 * first. */
5907 setcursor();
5908 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5909 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005910
Bram Moolenaarcb036422017-03-01 12:29:10 +01005911 ins_compl_show_pum();
5912 setcursor();
5913 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005914}
5915
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916/*
5917 * Looks in the first "len" chars. of "src" for search-metachars.
5918 * If dest is not NULL the chars. are copied there quoting (with
5919 * a backslash) the metachars, and dest would be NUL terminated.
5920 * Returns the length (needed) of dest
5921 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005922 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005925 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005927 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
5929 switch (*src)
5930 {
5931 case '.':
5932 case '*':
5933 case '[':
5934 if (ctrl_x_mode == CTRL_X_DICTIONARY
5935 || ctrl_x_mode == CTRL_X_THESAURUS)
5936 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005937 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 case '~':
5939 if (!p_magic) /* quote these only if magic is set */
5940 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005941 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case '\\':
5943 if (ctrl_x_mode == CTRL_X_DICTIONARY
5944 || ctrl_x_mode == CTRL_X_THESAURUS)
5945 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005946 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 case '^': /* currently it's not needed. */
5948 case '$':
5949 m++;
5950 if (dest != NULL)
5951 *dest++ = '\\';
5952 break;
5953 }
5954 if (dest != NULL)
5955 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005956# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 /* Copy remaining bytes of a multibyte character. */
5958 if (has_mbyte)
5959 {
5960 int i, mb_len;
5961
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005962 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (mb_len > 0 && len >= mb_len)
5964 for (i = 0; i < mb_len; ++i)
5965 {
5966 --len;
5967 ++src;
5968 if (dest != NULL)
5969 *dest++ = *src;
5970 }
5971 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 }
5974 if (dest != NULL)
5975 *dest = NUL;
5976
5977 return m;
5978}
5979#endif /* FEAT_INS_EXPAND */
5980
5981/*
5982 * Next character is interpreted literally.
5983 * A one, two or three digit decimal number is interpreted as its byte value.
5984 * If one or two digits are entered, the next character is given to vungetc().
5985 * For Unicode a character > 255 may be returned.
5986 */
5987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 int cc;
5991 int nc;
5992 int i;
5993 int hex = FALSE;
5994 int octal = FALSE;
5995#ifdef FEAT_MBYTE
5996 int unicode = 0;
5997#endif
5998
5999 if (got_int)
6000 return Ctrl_C;
6001
6002#ifdef FEAT_GUI
6003 /*
6004 * In GUI there is no point inserting the internal code for a special key.
6005 * It is more useful to insert the string "<KEY>" instead. This would
6006 * probably be useful in a text window too, but it would not be
6007 * vi-compatible (maybe there should be an option for it?) -- webb
6008 */
6009 if (gui.in_use)
6010 ++allow_keys;
6011#endif
6012#ifdef USE_ON_FLY_SCROLL
6013 dont_scroll = TRUE; /* disallow scrolling here */
6014#endif
6015 ++no_mapping; /* don't map the next key hits */
6016 cc = 0;
6017 i = 0;
6018 for (;;)
6019 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006020 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021#ifdef FEAT_CMDL_INFO
6022 if (!(State & CMDLINE)
6023# ifdef FEAT_MBYTE
6024 && MB_BYTE2LEN_CHECK(nc) == 1
6025# endif
6026 )
6027 add_to_showcmd(nc);
6028#endif
6029 if (nc == 'x' || nc == 'X')
6030 hex = TRUE;
6031 else if (nc == 'o' || nc == 'O')
6032 octal = TRUE;
6033#ifdef FEAT_MBYTE
6034 else if (nc == 'u' || nc == 'U')
6035 unicode = nc;
6036#endif
6037 else
6038 {
6039 if (hex
6040#ifdef FEAT_MBYTE
6041 || unicode != 0
6042#endif
6043 )
6044 {
6045 if (!vim_isxdigit(nc))
6046 break;
6047 cc = cc * 16 + hex2nr(nc);
6048 }
6049 else if (octal)
6050 {
6051 if (nc < '0' || nc > '7')
6052 break;
6053 cc = cc * 8 + nc - '0';
6054 }
6055 else
6056 {
6057 if (!VIM_ISDIGIT(nc))
6058 break;
6059 cc = cc * 10 + nc - '0';
6060 }
6061
6062 ++i;
6063 }
6064
6065 if (cc > 255
6066#ifdef FEAT_MBYTE
6067 && unicode == 0
6068#endif
6069 )
6070 cc = 255; /* limit range to 0-255 */
6071 nc = 0;
6072
6073 if (hex) /* hex: up to two chars */
6074 {
6075 if (i >= 2)
6076 break;
6077 }
6078#ifdef FEAT_MBYTE
6079 else if (unicode) /* Unicode: up to four or eight chars */
6080 {
6081 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6082 break;
6083 }
6084#endif
6085 else if (i >= 3) /* decimal or octal: up to three chars */
6086 break;
6087 }
6088 if (i == 0) /* no number entered */
6089 {
6090 if (nc == K_ZERO) /* NUL is stored as NL */
6091 {
6092 cc = '\n';
6093 nc = 0;
6094 }
6095 else
6096 {
6097 cc = nc;
6098 nc = 0;
6099 }
6100 }
6101
6102 if (cc == 0) /* NUL is stored as NL */
6103 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006104#ifdef FEAT_MBYTE
6105 if (enc_dbcs && (cc & 0xff) == 0)
6106 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6107 second byte will cause trouble! */
6108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110 --no_mapping;
6111#ifdef FEAT_GUI
6112 if (gui.in_use)
6113 --allow_keys;
6114#endif
6115 if (nc)
6116 vungetc(nc);
6117 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6118 return cc;
6119}
6120
6121/*
6122 * Insert character, taking care of special keys and mod_mask
6123 */
6124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125insert_special(
6126 int c,
6127 int allow_modmask,
6128 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130 char_u *p;
6131 int len;
6132
6133 /*
6134 * Special function key, translate into "<Key>". Up to the last '>' is
6135 * inserted with ins_str(), so as not to replace characters in replace
6136 * mode.
6137 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6138 * unless 'allow_modmask' is TRUE.
6139 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006140#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 /* Command-key never produces a normal key */
6142 if (mod_mask & MOD_MASK_CMD)
6143 allow_modmask = TRUE;
6144#endif
6145 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6146 {
6147 p = get_special_key_name(c, mod_mask);
6148 len = (int)STRLEN(p);
6149 c = p[len - 1];
6150 if (len > 2)
6151 {
6152 if (stop_arrow() == FAIL)
6153 return;
6154 p[len - 1] = NUL;
6155 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006156 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 ctrlv = FALSE;
6158 }
6159 }
6160 if (stop_arrow() == OK)
6161 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6162}
6163
6164/*
6165 * Special characters in this context are those that need processing other
6166 * than the simple insertion that can be performed here. This includes ESC
6167 * which terminates the insert, and CR/NL which need special processing to
6168 * open up a new line. This routine tries to optimize insertions performed by
6169 * the "redo", "undo" or "put" commands, so it needs to know when it should
6170 * stop and defer processing to the "normal" mechanism.
6171 * '0' and '^' are special, because they can be followed by CTRL-D.
6172 */
6173#ifdef EBCDIC
6174# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6175#else
6176# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6177#endif
6178
6179#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006180# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006182# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183#endif
6184
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006185/*
6186 * "flags": INSCHAR_FORMAT - force formatting
6187 * INSCHAR_CTRLV - char typed just after CTRL-V
6188 * INSCHAR_NO_FEX - don't use 'formatexpr'
6189 *
6190 * NOTE: passes the flags value straight through to internal_format() which,
6191 * beside INSCHAR_FORMAT (above), is also looking for these:
6192 * INSCHAR_DO_COM - format comments
6193 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006196insertchar(
6197 int c, /* character to insert or NUL */
6198 int flags, /* INSCHAR_FORMAT, etc. */
6199 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 int textwidth;
6202#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006206 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006208 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211 /*
6212 * Try to break the line in two or more pieces when:
6213 * - Always do this if we have been called to do formatting only.
6214 * - Always do this when 'formatoptions' has the 'a' flag and the line
6215 * ends in white space.
6216 * - Otherwise:
6217 * - Don't do this if inserting a blank
6218 * - Don't do this if an existing character is being replaced, unless
6219 * we're in VREPLACE mode.
6220 * - Do this if the cursor is not on the line where insert started
6221 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6222 * before the insert.
6223 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6224 * before 'textwidth'
6225 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006227 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006228 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 && *ml_get_cursor() != NUL)
6232 && (curwin->w_cursor.lnum != Insstart.lnum
6233 || ((!has_format_option(FO_INS_LONG)
6234 || Insstart_textlen <= (colnr_T)textwidth)
6235 && (!fo_ins_blank
6236 || Insstart_blank_vcol <= (colnr_T)textwidth
6237 ))))))
6238 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239 /* Format with 'formatexpr' when it's set. Use internal formatting
6240 * when 'formatexpr' isn't set or it returns non-zero. */
6241#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006242 int do_internal = TRUE;
6243 colnr_T virtcol = get_nolist_virtcol()
6244 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006245
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006246 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6247 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006248 {
6249 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6250 /* It may be required to save for undo again, e.g. when setline()
6251 * was called. */
6252 ins_need_undo = TRUE;
6253 }
6254 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006256 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (c == NUL) /* only formatting was wanted */
6260 return;
6261
6262#ifdef FEAT_COMMENTS
6263 /* Check whether this character should end a comment. */
6264 if (did_ai && (int)c == end_comment_pending)
6265 {
6266 char_u *line;
6267 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6268 int middle_len, end_len;
6269 int i;
6270
6271 /*
6272 * Need to remove existing (middle) comment leader and insert end
6273 * comment leader. First, check what comment leader we can find.
6274 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006275 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6277 {
6278 /* Skip middle-comment string */
6279 while (*p && p[-1] != ':') /* find end of middle flags */
6280 ++p;
6281 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6282 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006283 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 --middle_len;
6285
6286 /* Find the end-comment string */
6287 while (*p && p[-1] != ':') /* find end of end flags */
6288 ++p;
6289 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6290
6291 /* Skip white space before the cursor */
6292 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006293 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 ;
6295 i++;
6296
6297 /* Skip to before the middle leader */
6298 i -= middle_len;
6299
6300 /* Check some expected things before we go on */
6301 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6302 {
6303 /* Backspace over all the stuff we want to replace */
6304 backspace_until_column(i);
6305
6306 /*
6307 * Insert the end-comment string, except for the last
6308 * character, which will get inserted as normal later.
6309 */
6310 ins_bytes_len(lead_end, end_len - 1);
6311 }
6312 }
6313 }
6314 end_comment_pending = NUL;
6315#endif
6316
6317 did_ai = FALSE;
6318#ifdef FEAT_SMARTINDENT
6319 did_si = FALSE;
6320 can_si = FALSE;
6321 can_si_back = FALSE;
6322#endif
6323
6324 /*
6325 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6326 * This speeds up normal text input considerably.
6327 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6328 * need to re-indent at a ':', or any other character (but not what
6329 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006330 * Don't do this when there an InsertCharPre autocommand is defined,
6331 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006332 * Do the check for InsertCharPre before the call to vpeekc() because the
6333 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 */
6335#ifdef USE_ON_FLY_SCROLL
6336 dont_scroll = FALSE; /* allow scrolling here */
6337#endif
6338
6339 if ( !ISSPECIAL(c)
6340#ifdef FEAT_MBYTE
6341 && (!has_mbyte || (*mb_char2len)(c) == 1)
6342#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006343 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 && vpeekc() != NUL
6345 && !(State & REPLACE_FLAG)
6346#ifdef FEAT_CINDENT
6347 && !cindent_on()
6348#endif
6349#ifdef FEAT_RIGHTLEFT
6350 && !p_ri
6351#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006352 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354#define INPUT_BUFLEN 100
6355 char_u buf[INPUT_BUFLEN + 1];
6356 int i;
6357 colnr_T virtcol = 0;
6358
6359 buf[0] = c;
6360 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 virtcol = get_nolist_virtcol();
6363 /*
6364 * Stop the string when:
6365 * - no more chars available
6366 * - finding a special character (command key)
6367 * - buffer is full
6368 * - running into the 'textwidth' boundary
6369 * - need to check for abbreviation: A non-word char after a word-char
6370 */
6371 while ( (c = vpeekc()) != NUL
6372 && !ISSPECIAL(c)
6373#ifdef FEAT_MBYTE
6374 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6375#endif
6376 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006377# ifdef FEAT_FKMAP
6378 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 && (textwidth == 0
6381 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6382 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6383 {
6384#ifdef FEAT_RIGHTLEFT
6385 c = vgetc();
6386 if (p_hkmap && KeyTyped)
6387 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 buf[i++] = c;
6389#else
6390 buf[i++] = vgetc();
6391#endif
6392 }
6393
6394#ifdef FEAT_DIGRAPHS
6395 do_digraph(-1); /* clear digraphs */
6396 do_digraph(buf[i-1]); /* may be the start of a digraph */
6397#endif
6398 buf[i] = NUL;
6399 ins_str(buf);
6400 if (flags & INSCHAR_CTRLV)
6401 {
6402 redo_literal(*buf);
6403 i = 1;
6404 }
6405 else
6406 i = 0;
6407 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006408 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410 else
6411 {
6412#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006413 int cc;
6414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6416 {
6417 char_u buf[MB_MAXBYTES + 1];
6418
6419 (*mb_char2bytes)(c, buf);
6420 buf[cc] = NUL;
6421 ins_char_bytes(buf, cc);
6422 AppendCharToRedobuff(c);
6423 }
6424 else
6425#endif
6426 {
6427 ins_char(c);
6428 if (flags & INSCHAR_CTRLV)
6429 redo_literal(c);
6430 else
6431 AppendCharToRedobuff(c);
6432 }
6433 }
6434}
6435
6436/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006438 *
6439 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6440 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 */
6442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006443internal_format(
6444 int textwidth,
6445 int second_indent,
6446 int flags,
6447 int format_only,
6448 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449{
6450 int cc;
6451 int save_char = NUL;
6452 int haveto_redraw = FALSE;
6453 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6454#ifdef FEAT_MBYTE
6455 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6456#endif
6457 int fo_white_par = has_format_option(FO_WHITE_PAR);
6458 int first_line = TRUE;
6459#ifdef FEAT_COMMENTS
6460 colnr_T leader_len;
6461 int no_leader = FALSE;
6462 int do_comments = (flags & INSCHAR_DO_COM);
6463#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006464#ifdef FEAT_LINEBREAK
6465 int has_lbr = curwin->w_p_lbr;
6466
6467 /* make sure win_lbr_chartabsize() counts correctly */
6468 curwin->w_p_lbr = FALSE;
6469#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470
6471 /*
6472 * When 'ai' is off we don't want a space under the cursor to be
6473 * deleted. Replace it with an 'x' temporarily.
6474 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006475 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006476 {
6477 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006478 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 {
6480 save_char = cc;
6481 pchar_cursor('x');
6482 }
6483 }
6484
6485 /*
6486 * Repeat breaking lines, until the current line is not too long.
6487 */
6488 while (!got_int)
6489 {
6490 int startcol; /* Cursor column at entry */
6491 int wantcol; /* column at textwidth border */
6492 int foundcol; /* column for start of spaces */
6493 int end_foundcol = 0; /* column for start of word */
6494 colnr_T len;
6495 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496 int orig_col = 0;
6497 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006498 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006499 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006500
Bram Moolenaar97b98102009-11-17 16:41:01 +00006501 virtcol = get_nolist_virtcol()
6502 + char2cells(c != NUL ? c : gchar_cursor());
6503 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504 break;
6505
6506#ifdef FEAT_COMMENTS
6507 if (no_leader)
6508 do_comments = FALSE;
6509 else if (!(flags & INSCHAR_FORMAT)
6510 && has_format_option(FO_WRAP_COMS))
6511 do_comments = TRUE;
6512
6513 /* Don't break until after the comment leader */
6514 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006515 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006516 else
6517 leader_len = 0;
6518
6519 /* If the line doesn't start with a comment leader, then don't
6520 * start one in a following broken line. Avoids that a %word
6521 * moved to the start of the next line causes all following lines
6522 * to start with %. */
6523 if (leader_len == 0)
6524 no_leader = TRUE;
6525#endif
6526 if (!(flags & INSCHAR_FORMAT)
6527#ifdef FEAT_COMMENTS
6528 && leader_len == 0
6529#endif
6530 && !has_format_option(FO_WRAP))
6531
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533 if ((startcol = curwin->w_cursor.col) == 0)
6534 break;
6535
6536 /* find column of textwidth border */
6537 coladvance((colnr_T)textwidth);
6538 wantcol = curwin->w_cursor.col;
6539
Bram Moolenaar97b98102009-11-17 16:41:01 +00006540 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006541 foundcol = 0;
6542
6543 /*
6544 * Find position to break at.
6545 * Stop at first entered white when 'formatoptions' has 'v'
6546 */
6547 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006548 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006549 || curwin->w_cursor.lnum != Insstart.lnum
6550 || curwin->w_cursor.col >= Insstart.col)
6551 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006552 if (curwin->w_cursor.col == startcol && c != NUL)
6553 cc = c;
6554 else
6555 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006556 if (WHITECHAR(cc))
6557 {
6558 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006559 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560
6561 /* find start of sequence of blanks */
6562 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6563 {
6564 dec_cursor();
6565 cc = gchar_cursor();
6566 }
6567 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6568 break; /* only spaces in front of text */
6569#ifdef FEAT_COMMENTS
6570 /* Don't break until after the comment leader */
6571 if (curwin->w_cursor.col < leader_len)
6572 break;
6573#endif
6574 if (has_format_option(FO_ONE_LETTER))
6575 {
6576 /* do not break after one-letter words */
6577 if (curwin->w_cursor.col == 0)
6578 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006579#ifdef FEAT_COMMENTS
6580 /* do not break "#a b" when 'tw' is 2 */
6581 if (curwin->w_cursor.col <= leader_len)
6582 break;
6583#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006584 col = curwin->w_cursor.col;
6585 dec_cursor();
6586 cc = gchar_cursor();
6587
6588 if (WHITECHAR(cc))
6589 continue; /* one-letter, continue */
6590 curwin->w_cursor.col = col;
6591 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006592
6593 inc_cursor();
6594
6595 end_foundcol = end_col + 1;
6596 foundcol = curwin->w_cursor.col;
6597 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006598 break;
6599 }
6600#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006601 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006602 {
6603 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006604 if (curwin->w_cursor.col != startcol)
6605 {
6606#ifdef FEAT_COMMENTS
6607 /* Don't break until after the comment leader */
6608 if (curwin->w_cursor.col < leader_len)
6609 break;
6610#endif
6611 col = curwin->w_cursor.col;
6612 inc_cursor();
6613 /* Don't change end_foundcol if already set. */
6614 if (foundcol != curwin->w_cursor.col)
6615 {
6616 foundcol = curwin->w_cursor.col;
6617 end_foundcol = foundcol;
6618 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6619 break;
6620 }
6621 curwin->w_cursor.col = col;
6622 }
6623
6624 if (curwin->w_cursor.col == 0)
6625 break;
6626
6627 col = curwin->w_cursor.col;
6628
6629 dec_cursor();
6630 cc = gchar_cursor();
6631
6632 if (WHITECHAR(cc))
6633 continue; /* break with space */
6634#ifdef FEAT_COMMENTS
6635 /* Don't break until after the comment leader */
6636 if (curwin->w_cursor.col < leader_len)
6637 break;
6638#endif
6639
6640 curwin->w_cursor.col = col;
6641
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006642 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006643 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006644 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6645 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006646 }
6647#endif
6648 if (curwin->w_cursor.col == 0)
6649 break;
6650 dec_cursor();
6651 }
6652
6653 if (foundcol == 0) /* no spaces, cannot break line */
6654 {
6655 curwin->w_cursor.col = startcol;
6656 break;
6657 }
6658
6659 /* Going to break the line, remove any "$" now. */
6660 undisplay_dollar();
6661
6662 /*
6663 * Offset between cursor position and line break is used by replace
6664 * stack functions. VREPLACE does not use this, and backspaces
6665 * over the text instead.
6666 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667 if (State & VREPLACE_FLAG)
6668 orig_col = startcol; /* Will start backspacing from here */
6669 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006670 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671
6672 /*
6673 * adjust startcol for spaces that will be deleted and
6674 * characters that will remain on top line
6675 */
6676 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006677 while ((cc = gchar_cursor(), WHITECHAR(cc))
6678 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006679 inc_cursor();
6680 startcol -= curwin->w_cursor.col;
6681 if (startcol < 0)
6682 startcol = 0;
6683
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684 if (State & VREPLACE_FLAG)
6685 {
6686 /*
6687 * In VREPLACE mode, we will backspace over the text to be
6688 * wrapped, so save a copy now to put on the next line.
6689 */
6690 saved_text = vim_strsave(ml_get_cursor());
6691 curwin->w_cursor.col = orig_col;
6692 if (saved_text == NULL)
6693 break; /* Can't do it, out of memory */
6694 saved_text[startcol] = NUL;
6695
6696 /* Backspace over characters that will move to the next line */
6697 if (!fo_white_par)
6698 backspace_until_column(foundcol);
6699 }
6700 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006701 {
6702 /* put cursor after pos. to break line */
6703 if (!fo_white_par)
6704 curwin->w_cursor.col = foundcol;
6705 }
6706
6707 /*
6708 * Split the line just before the margin.
6709 * Only insert/delete lines, but don't really redraw the window.
6710 */
6711 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6712 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6713#ifdef FEAT_COMMENTS
6714 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006715 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006717 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6718 if (!(flags & INSCHAR_COM_LIST))
6719 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006720
6721 replace_offset = 0;
6722 if (first_line)
6723 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006724 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006726 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006727 * This section is for auto-wrap of numeric lists. When not
6728 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6729 * flag will be set and open_line() will handle it (as seen
6730 * above). The code here (and in get_number_indent()) will
6731 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006732 */
6733 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006734 second_indent =
6735 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006736 if (second_indent >= 0)
6737 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006738 if (State & VREPLACE_FLAG)
6739 change_indent(INDENT_SET, second_indent,
6740 FALSE, NUL, TRUE);
6741 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006742#ifdef FEAT_COMMENTS
6743 if (leader_len > 0 && second_indent - leader_len > 0)
6744 {
6745 int i;
6746 int padding = second_indent - leader_len;
6747
6748 /* We started at the first_line of a numbered list
6749 * that has a comment. the open_line() function has
6750 * inserted the proper comment leader and positioned
6751 * the cursor at the end of the split line. Now we
6752 * add the additional whitespace needed after the
6753 * comment leader for the numbered list. */
6754 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006755 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006756 }
6757 else
6758 {
6759#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006760 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006761#ifdef FEAT_COMMENTS
6762 }
6763#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006764 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006765 }
6766 first_line = FALSE;
6767 }
6768
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006769 if (State & VREPLACE_FLAG)
6770 {
6771 /*
6772 * In VREPLACE mode we have backspaced over the text to be
6773 * moved, now we re-insert it into the new line.
6774 */
6775 ins_bytes(saved_text);
6776 vim_free(saved_text);
6777 }
6778 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779 {
6780 /*
6781 * Check if cursor is not past the NUL off the line, cindent
6782 * may have added or removed indent.
6783 */
6784 curwin->w_cursor.col += startcol;
6785 len = (colnr_T)STRLEN(ml_get_curline());
6786 if (curwin->w_cursor.col > len)
6787 curwin->w_cursor.col = len;
6788 }
6789
6790 haveto_redraw = TRUE;
6791#ifdef FEAT_CINDENT
6792 can_cindent = TRUE;
6793#endif
6794 /* moved the cursor, don't autoindent or cindent now */
6795 did_ai = FALSE;
6796#ifdef FEAT_SMARTINDENT
6797 did_si = FALSE;
6798 can_si = FALSE;
6799 can_si_back = FALSE;
6800#endif
6801 line_breakcheck();
6802 }
6803
6804 if (save_char != NUL) /* put back space after cursor */
6805 pchar_cursor(save_char);
6806
Bram Moolenaar0026d472014-09-09 16:32:39 +02006807#ifdef FEAT_LINEBREAK
6808 curwin->w_p_lbr = has_lbr;
6809#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006810 if (!format_only && haveto_redraw)
6811 {
6812 update_topline();
6813 redraw_curbuf_later(VALID);
6814 }
6815}
6816
6817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * Called after inserting or deleting text: When 'formatoptions' includes the
6819 * 'a' flag format from the current line until the end of the paragraph.
6820 * Keep the cursor at the same position relative to the text.
6821 * The caller must have saved the cursor line for undo, following ones will be
6822 * saved here.
6823 */
6824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006825auto_format(
6826 int trailblank, /* when TRUE also format with trailing blank */
6827 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 pos_T pos;
6830 colnr_T len;
6831 char_u *old;
6832 char_u *new, *pnew;
6833 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006834 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 if (!has_format_option(FO_AUTO))
6837 return;
6838
6839 pos = curwin->w_cursor;
6840 old = ml_get_curline();
6841
6842 /* may remove added space */
6843 check_auto_format(FALSE);
6844
6845 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6846 * user might insert normal text next. Also skip formatting when "1" is
6847 * in 'formatoptions' and there is a single character before the cursor.
6848 * Otherwise the line would be broken and when typing another non-white
6849 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006850 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (*old != NUL && !trailblank && wasatend)
6852 {
6853 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006854 cc = gchar_cursor();
6855 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6856 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006858 cc = gchar_cursor();
6859 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 curwin->w_cursor = pos;
6862 return;
6863 }
6864 curwin->w_cursor = pos;
6865 }
6866
6867#ifdef FEAT_COMMENTS
6868 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6869 * comments. */
6870 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006871 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 return;
6873#endif
6874
6875 /*
6876 * May start formatting in a previous line, so that after "x" a word is
6877 * moved to the previous line if it fits there now. Only when this is not
6878 * the start of a paragraph.
6879 */
6880 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6881 {
6882 --curwin->w_cursor.lnum;
6883 if (u_save_cursor() == FAIL)
6884 return;
6885 }
6886
6887 /*
6888 * Do the formatting and restore the cursor position. "saved_cursor" will
6889 * be adjusted for the text formatting.
6890 */
6891 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006892 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 curwin->w_cursor = saved_cursor;
6894 saved_cursor.lnum = 0;
6895
6896 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6897 {
6898 /* "cannot happen" */
6899 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6900 coladvance((colnr_T)MAXCOL);
6901 }
6902 else
6903 check_cursor_col();
6904
6905 /* Insert mode: If the cursor is now after the end of the line while it
6906 * previously wasn't, the line was broken. Because of the rule above we
6907 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6908 * formatted. */
6909 if (!wasatend && has_format_option(FO_WHITE_PAR))
6910 {
6911 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006912 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (curwin->w_cursor.col == len)
6914 {
6915 pnew = vim_strnsave(new, len + 2);
6916 pnew[len] = ' ';
6917 pnew[len + 1] = NUL;
6918 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6919 /* remove the space later */
6920 did_add_space = TRUE;
6921 }
6922 else
6923 /* may remove added space */
6924 check_auto_format(FALSE);
6925 }
6926
6927 check_cursor();
6928}
6929
6930/*
6931 * When an extra space was added to continue a paragraph for auto-formatting,
6932 * delete it now. The space must be under the cursor, just after the insert
6933 * position.
6934 */
6935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936check_auto_format(
6937 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006940 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 if (did_add_space)
6943 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006944 cc = gchar_cursor();
6945 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 /* Somehow the space was removed already. */
6947 did_add_space = FALSE;
6948 else
6949 {
6950 if (!end_insert)
6951 {
6952 inc_cursor();
6953 c = gchar_cursor();
6954 dec_cursor();
6955 }
6956 if (c != NUL)
6957 {
6958 /* The space is no longer at the end of the line, delete it. */
6959 del_char(FALSE);
6960 did_add_space = FALSE;
6961 }
6962 }
6963 }
6964}
6965
6966/*
6967 * Find out textwidth to be used for formatting:
6968 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006969 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * if invalid value, use 0.
6971 * Set default to window width (maximum 79) for "gq" operator.
6972 */
6973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006974comp_textwidth(
6975 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 int textwidth;
6978
6979 textwidth = curbuf->b_p_tw;
6980 if (textwidth == 0 && curbuf->b_p_wm)
6981 {
6982 /* The width is the window width minus 'wrapmargin' minus all the
6983 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006984 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985#ifdef FEAT_CMDWIN
6986 if (cmdwin_type != 0)
6987 textwidth -= 1;
6988#endif
6989#ifdef FEAT_FOLDING
6990 textwidth -= curwin->w_p_fdc;
6991#endif
6992#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006993 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 textwidth -= 1;
6995#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006996 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 textwidth -= 8;
6998 }
6999 if (textwidth < 0)
7000 textwidth = 0;
7001 if (ff && textwidth == 0)
7002 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007003 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (textwidth > 79)
7005 textwidth = 79;
7006 }
7007 return textwidth;
7008}
7009
7010/*
7011 * Put a character in the redo buffer, for when just after a CTRL-V.
7012 */
7013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007014redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 char_u buf[10];
7017
7018 /* Only digits need special treatment. Translate them into a string of
7019 * three digits. */
7020 if (VIM_ISDIGIT(c))
7021 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007022 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 AppendToRedobuff(buf);
7024 }
7025 else
7026 AppendCharToRedobuff(c);
7027}
7028
7029/*
7030 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007031 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 */
7033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034start_arrow(
7035 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007037 start_arrow_common(end_insert_pos, TRUE);
7038}
7039
7040/*
7041 * Like start_arrow() but with end_change argument.
7042 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7043 */
7044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045start_arrow_with_change(
7046 pos_T *end_insert_pos, /* can be NULL */
7047 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007048{
7049 start_arrow_common(end_insert_pos, end_change);
7050 if (!end_change)
7051 {
7052 AppendCharToRedobuff(Ctrl_G);
7053 AppendCharToRedobuff('U');
7054 }
7055}
7056
7057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058start_arrow_common(
7059 pos_T *end_insert_pos, /* can be NULL */
7060 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007061{
7062 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
7064 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007065 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 arrow_used = TRUE; /* this means we stopped the current insert */
7067 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007068#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007069 check_spell_redraw();
7070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007073#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007074/*
7075 * If we skipped highlighting word at cursor, do it now.
7076 * It may be skipped again, thus reset spell_redraw_lnum first.
7077 */
7078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007080{
7081 if (spell_redraw_lnum != 0)
7082 {
7083 linenr_T lnum = spell_redraw_lnum;
7084
7085 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007086 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007087 }
7088}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007089
7090/*
7091 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7092 * spelled word, if there is one.
7093 */
7094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007096{
7097 pos_T tpos = curwin->w_cursor;
7098
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007099 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007100 if (curwin->w_cursor.col != tpos.col)
7101 start_arrow(&tpos);
7102}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007103#endif
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105/*
7106 * stop_arrow() is called before a change is made in insert mode.
7107 * If an arrow key has been used, start a new insertion.
7108 * Returns FAIL if undo is impossible, shouldn't insert then.
7109 */
7110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 if (arrow_used)
7114 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007115 Insstart = curwin->w_cursor; /* new insertion starts here */
7116 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7117 /* Don't update the original insert position when moved to the
7118 * right, except when nothing was inserted yet. */
7119 update_Insstart_orig = FALSE;
7120 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7121
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 if (u_save_cursor() == OK)
7123 {
7124 arrow_used = FALSE;
7125 ins_need_undo = FALSE;
7126 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (State & VREPLACE_FLAG)
7130 {
7131 orig_line_count = curbuf->b_ml.ml_line_count;
7132 vr_lines_changed = 1;
7133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 ResetRedobuff();
7135 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007136 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138 else if (ins_need_undo)
7139 {
7140 if (u_save_cursor() == OK)
7141 ins_need_undo = FALSE;
7142 }
7143
7144#ifdef FEAT_FOLDING
7145 /* Always open fold at the cursor line when inserting something. */
7146 foldOpenCursor();
7147#endif
7148
7149 return (arrow_used || ins_need_undo ? FAIL : OK);
7150}
7151
7152/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007153 * Do a few things to stop inserting.
7154 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7155 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 */
7157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158stop_insert(
7159 pos_T *end_insert_pos,
7160 int esc, /* called by ins_esc() */
7161 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007163 int cc;
7164 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
7166 stop_redo_ins();
7167 replace_flush(); /* abandon replace stack */
7168
7169 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007170 * Save the inserted text for later redo with ^@ and CTRL-A.
7171 * Don't do it when "restart_edit" was set and nothing was inserted,
7172 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007174 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007175 if (did_restart_edit == 0 || (ptr != NULL
7176 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007177 {
7178 vim_free(last_insert);
7179 last_insert = ptr;
7180 last_insert_skip = new_insert_skip;
7181 }
7182 else
7183 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007185 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
7187 /* Auto-format now. It may seem strange to do this when stopping an
7188 * insertion (or moving the cursor), but it's required when appending
7189 * a line and having it end in a space. But only do it when something
7190 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007191 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007193 pos_T tpos = curwin->w_cursor;
7194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 /* When the cursor is at the end of the line after a space the
7196 * formatting will move it to the following word. Avoid that by
7197 * moving the cursor onto the space. */
7198 cc = 'x';
7199 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7200 {
7201 dec_cursor();
7202 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007203 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007204 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206
7207 auto_format(TRUE, FALSE);
7208
Bram Moolenaar1c465442017-03-12 20:10:05 +01007209 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007210 {
7211 if (gchar_cursor() != NUL)
7212 inc_cursor();
7213#ifdef FEAT_VIRTUALEDIT
7214 /* If the cursor is still at the same character, also keep
7215 * the "coladd". */
7216 if (gchar_cursor() == NUL
7217 && curwin->w_cursor.lnum == tpos.lnum
7218 && curwin->w_cursor.col == tpos.col)
7219 curwin->w_cursor.coladd = tpos.coladd;
7220#endif
7221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223
7224 /* If a space was inserted for auto-formatting, remove it now. */
7225 check_auto_format(TRUE);
7226
7227 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007228 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007229 * Do this when ESC was used or moving the cursor up/down.
7230 * Check for the old position still being valid, just in case the text
7231 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007232 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007233 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7234 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007236 pos_T tpos = curwin->w_cursor;
7237
7238 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007239 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007240 for (;;)
7241 {
7242 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7243 --curwin->w_cursor.col;
7244 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007245 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007246 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007247 if (del_char(TRUE) == FAIL)
7248 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007249 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007250 if (curwin->w_cursor.lnum != tpos.lnum)
7251 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007252 else
7253 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007254 /* reset tpos, could have been invalidated in the loop above */
7255 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007256 tpos.col++;
7257 if (cc != NUL && gchar_pos(&tpos) == NUL)
7258 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 /* <C-S-Right> may have started Visual mode, adjust the position for
7262 * deleted characters. */
7263 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7264 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007265 int len = (int)STRLEN(ml_get_curline());
7266
7267 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007269 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007270#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276 }
7277 did_ai = FALSE;
7278#ifdef FEAT_SMARTINDENT
7279 did_si = FALSE;
7280 can_si = FALSE;
7281 can_si_back = FALSE;
7282#endif
7283
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007284 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7285 * now in a different buffer. */
7286 if (end_insert_pos != NULL)
7287 {
7288 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007289 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007290 curbuf->b_op_end = *end_insert_pos;
7291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292}
7293
7294/*
7295 * Set the last inserted text to a single character.
7296 * Used for the replace command.
7297 */
7298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 char_u *s;
7302
7303 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (last_insert != NULL)
7306 {
7307 s = last_insert;
7308 /* Use the CTRL-V only when entering a special char */
7309 if (c < ' ' || c == DEL)
7310 *s++ = Ctrl_V;
7311 s = add_char2buf(c, s);
7312 *s++ = ESC;
7313 *s++ = NUL;
7314 last_insert_skip = 0;
7315 }
7316}
7317
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007318#if defined(EXITFREE) || defined(PROTO)
7319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007321{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007322 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007323# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007324 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007325# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007326}
7327#endif
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329/*
7330 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7331 * and CSI. Handle multi-byte characters.
7332 * Returns a pointer to after the added bytes.
7333 */
7334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007338 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 int i;
7340 int len;
7341
7342 len = (*mb_char2bytes)(c, temp);
7343 for (i = 0; i < len; ++i)
7344 {
7345 c = temp[i];
7346#endif
7347 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7348 if (c == K_SPECIAL)
7349 {
7350 *s++ = K_SPECIAL;
7351 *s++ = KS_SPECIAL;
7352 *s++ = KE_FILLER;
7353 }
7354#ifdef FEAT_GUI
7355 else if (c == CSI)
7356 {
7357 *s++ = CSI;
7358 *s++ = KS_EXTRA;
7359 *s++ = (int)KE_CSI;
7360 }
7361#endif
7362 else
7363 *s++ = c;
7364#ifdef FEAT_MBYTE
7365 }
7366#endif
7367 return s;
7368}
7369
7370/*
7371 * move cursor to start of line
7372 * if flags & BL_WHITE move to first non-white
7373 * if flags & BL_SOL move to first non-white if startofline is set,
7374 * otherwise keep "curswant" column
7375 * if flags & BL_FIX don't leave the cursor on a NUL.
7376 */
7377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007378beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 if ((flags & BL_SOL) && !p_sol)
7381 coladvance(curwin->w_curswant);
7382 else
7383 {
7384 curwin->w_cursor.col = 0;
7385#ifdef FEAT_VIRTUALEDIT
7386 curwin->w_cursor.coladd = 0;
7387#endif
7388
7389 if (flags & (BL_WHITE | BL_SOL))
7390 {
7391 char_u *ptr;
7392
Bram Moolenaar1c465442017-03-12 20:10:05 +01007393 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7395 ++curwin->w_cursor.col;
7396 }
7397 curwin->w_set_curswant = TRUE;
7398 }
7399}
7400
7401/*
7402 * oneright oneleft cursor_down cursor_up
7403 *
7404 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007405 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 * Return OK when successful, FAIL when we hit a line of file boundary.
7407 */
7408
7409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007410oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415#ifdef FEAT_VIRTUALEDIT
7416 if (virtual_active())
7417 {
7418 pos_T prevpos = curwin->w_cursor;
7419
7420 /* Adjust for multi-wide char (excluding TAB) */
7421 ptr = ml_get_cursor();
7422 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007423# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007425# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 ))
7429 ? ptr2cells(ptr) : 1));
7430 curwin->w_set_curswant = TRUE;
7431 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7432 return (prevpos.col != curwin->w_cursor.col
7433 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7434 }
7435#endif
7436
7437 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007438 if (*ptr == NUL)
7439 return FAIL; /* already at the very end */
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007442 if (has_mbyte)
7443 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 else
7445#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007446 l = 1;
7447
7448 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7449 * contains "onemore". */
7450 if (ptr[l] == NUL
7451#ifdef FEAT_VIRTUALEDIT
7452 && (ve_flags & VE_ONEMORE) == 0
7453#endif
7454 )
7455 return FAIL;
7456 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 curwin->w_set_curswant = TRUE;
7459 return OK;
7460}
7461
7462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465#ifdef FEAT_VIRTUALEDIT
7466 if (virtual_active())
7467 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007468# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int v = getviscol();
7472
7473 if (v == 0)
7474 return FAIL;
7475
7476# ifdef FEAT_LINEBREAK
7477 /* We might get stuck on 'showbreak', skip over it. */
7478 width = 1;
7479 for (;;)
7480 {
7481 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007482 /* getviscol() is slow, skip it when 'showbreak' is empty,
7483 * 'breakindent' is not set and there are no multi-byte
7484 * characters */
7485 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486# ifdef FEAT_MBYTE
7487 && !has_mbyte
7488# endif
7489 ) || getviscol() < v)
7490 break;
7491 ++width;
7492 }
7493# else
7494 coladvance(v - 1);
7495# endif
7496
7497 if (curwin->w_cursor.coladd == 1)
7498 {
7499 char_u *ptr;
7500
7501 /* Adjust for multi-wide char (not a TAB) */
7502 ptr = ml_get_cursor();
7503 if (*ptr != TAB && vim_isprintc(
7504# ifdef FEAT_MBYTE
7505 (*mb_ptr2char)(ptr)
7506# else
7507 *ptr
7508# endif
7509 ) && ptr2cells(ptr) > 1)
7510 curwin->w_cursor.coladd = 0;
7511 }
7512
7513 curwin->w_set_curswant = TRUE;
7514 return OK;
7515 }
7516#endif
7517
7518 if (curwin->w_cursor.col == 0)
7519 return FAIL;
7520
7521 curwin->w_set_curswant = TRUE;
7522 --curwin->w_cursor.col;
7523
7524#ifdef FEAT_MBYTE
7525 /* if the character on the left of the current cursor is a multi-byte
7526 * character, move to its first byte */
7527 if (has_mbyte)
7528 mb_adjust_cursor();
7529#endif
7530 return OK;
7531}
7532
7533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007534cursor_up(
7535 long n,
7536 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 linenr_T lnum;
7539
7540 if (n > 0)
7541 {
7542 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007543 /* This fails if the cursor is already in the first line or the count
7544 * is larger than the line number and '-' is in 'cpoptions' */
7545 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 return FAIL;
7547 if (n >= lnum)
7548 lnum = 1;
7549 else
7550#ifdef FEAT_FOLDING
7551 if (hasAnyFolding(curwin))
7552 {
7553 /*
7554 * Count each sequence of folded lines as one logical line.
7555 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007556 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 (void)hasFolding(lnum, &lnum, NULL);
7558
7559 while (n--)
7560 {
7561 /* move up one line */
7562 --lnum;
7563 if (lnum <= 1)
7564 break;
7565 /* If we entered a fold, move to the beginning, unless in
7566 * Insert mode or when 'foldopen' contains "all": it will open
7567 * in a moment. */
7568 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7569 (void)hasFolding(lnum, &lnum, NULL);
7570 }
7571 if (lnum < 1)
7572 lnum = 1;
7573 }
7574 else
7575#endif
7576 lnum -= n;
7577 curwin->w_cursor.lnum = lnum;
7578 }
7579
7580 /* try to advance to the column we want to be at */
7581 coladvance(curwin->w_curswant);
7582
7583 if (upd_topline)
7584 update_topline(); /* make sure curwin->w_topline is valid */
7585
7586 return OK;
7587}
7588
7589/*
7590 * Cursor down a number of logical lines.
7591 */
7592 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593cursor_down(
7594 long n,
7595 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 linenr_T lnum;
7598
7599 if (n > 0)
7600 {
7601 lnum = curwin->w_cursor.lnum;
7602#ifdef FEAT_FOLDING
7603 /* Move to last line of fold, will fail if it's the end-of-file. */
7604 (void)hasFolding(lnum, NULL, &lnum);
7605#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007606 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007607 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007608 if (lnum >= curbuf->b_ml.ml_line_count
7609 || (lnum + n > curbuf->b_ml.ml_line_count
7610 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 return FAIL;
7612 if (lnum + n >= curbuf->b_ml.ml_line_count)
7613 lnum = curbuf->b_ml.ml_line_count;
7614 else
7615#ifdef FEAT_FOLDING
7616 if (hasAnyFolding(curwin))
7617 {
7618 linenr_T last;
7619
7620 /* count each sequence of folded lines as one logical line */
7621 while (n--)
7622 {
7623 if (hasFolding(lnum, NULL, &last))
7624 lnum = last + 1;
7625 else
7626 ++lnum;
7627 if (lnum >= curbuf->b_ml.ml_line_count)
7628 break;
7629 }
7630 if (lnum > curbuf->b_ml.ml_line_count)
7631 lnum = curbuf->b_ml.ml_line_count;
7632 }
7633 else
7634#endif
7635 lnum += n;
7636 curwin->w_cursor.lnum = lnum;
7637 }
7638
7639 /* try to advance to the column we want to be at */
7640 coladvance(curwin->w_curswant);
7641
7642 if (upd_topline)
7643 update_topline(); /* make sure curwin->w_topline is valid */
7644
7645 return OK;
7646}
7647
7648/*
7649 * Stuff the last inserted text in the read buffer.
7650 * Last_insert actually is a copy of the redo buffer, so we
7651 * first have to remove the command.
7652 */
7653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654stuff_inserted(
7655 int c, /* Command character to be inserted */
7656 long count, /* Repeat this many times */
7657 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
7659 char_u *esc_ptr;
7660 char_u *ptr;
7661 char_u *last_ptr;
7662 char_u last = NUL;
7663
7664 ptr = get_last_insert();
7665 if (ptr == NULL)
7666 {
7667 EMSG(_(e_noinstext));
7668 return FAIL;
7669 }
7670
7671 /* may want to stuff the command character, to start Insert mode */
7672 if (c != NUL)
7673 stuffcharReadbuff(c);
7674 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7675 *esc_ptr = NUL; /* remove the ESC */
7676
7677 /* when the last char is either "0" or "^" it will be quoted if no ESC
7678 * comes after it OR if it will inserted more than once and "ptr"
7679 * starts with ^D. -- Acevedo
7680 */
7681 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7682 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7683 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7684 {
7685 last = *last_ptr;
7686 *last_ptr = NUL;
7687 }
7688
7689 do
7690 {
7691 stuffReadbuff(ptr);
7692 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7693 if (last)
7694 stuffReadbuff((char_u *)(last == '0'
7695 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7696 : IF_EB("\026^", CTRL_V_STR "^")));
7697 }
7698 while (--count > 0);
7699
7700 if (last)
7701 *last_ptr = last;
7702
7703 if (esc_ptr != NULL)
7704 *esc_ptr = ESC; /* put the ESC back */
7705
7706 /* may want to stuff a trailing ESC, to get out of Insert mode */
7707 if (!no_esc)
7708 stuffcharReadbuff(ESC);
7709
7710 return OK;
7711}
7712
7713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 if (last_insert == NULL)
7717 return NULL;
7718 return last_insert + last_insert_skip;
7719}
7720
7721/*
7722 * Get last inserted string, and remove trailing <Esc>.
7723 * Returns pointer to allocated memory (must be freed) or NULL.
7724 */
7725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007726get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 char_u *s;
7729 int len;
7730
7731 if (last_insert == NULL)
7732 return NULL;
7733 s = vim_strsave(last_insert + last_insert_skip);
7734 if (s != NULL)
7735 {
7736 len = (int)STRLEN(s);
7737 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7738 s[len - 1] = NUL;
7739 }
7740 return s;
7741}
7742
7743/*
7744 * Check the word in front of the cursor for an abbreviation.
7745 * Called when the non-id character "c" has been entered.
7746 * When an abbreviation is recognized it is removed from the text and
7747 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7748 */
7749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007750echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 /* Don't check for abbreviation in paste mode, when disabled and just
7753 * after moving around with cursor keys. */
7754 if (p_paste || no_abbr || arrow_used)
7755 return FALSE;
7756
7757 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7758 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7759}
7760
7761/*
7762 * replace-stack functions
7763 *
7764 * When replacing characters, the replaced characters are remembered for each
7765 * new character. This is used to re-insert the old text when backspacing.
7766 *
7767 * There is a NUL headed list of characters for each character that is
7768 * currently in the file after the insertion point. When BS is used, one NUL
7769 * headed list is put back for the deleted character.
7770 *
7771 * For a newline, there are two NUL headed lists. One contains the characters
7772 * that the NL replaced. The extra one stores the characters after the cursor
7773 * that were deleted (always white space).
7774 *
7775 * Replace_offset is normally 0, in which case replace_push will add a new
7776 * character at the end of the stack. If replace_offset is not 0, that many
7777 * characters will be left on the stack above the newly inserted character.
7778 */
7779
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007780static char_u *replace_stack = NULL;
7781static long replace_stack_nr = 0; /* next entry in replace stack */
7782static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007785replace_push(
7786 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 char_u *p;
7789
7790 if (replace_stack_nr < replace_offset) /* nothing to do */
7791 return;
7792 if (replace_stack_len <= replace_stack_nr)
7793 {
7794 replace_stack_len += 50;
7795 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7796 if (p == NULL) /* out of memory */
7797 {
7798 replace_stack_len -= 50;
7799 return;
7800 }
7801 if (replace_stack != NULL)
7802 {
7803 mch_memmove(p, replace_stack,
7804 (size_t)(replace_stack_nr * sizeof(char_u)));
7805 vim_free(replace_stack);
7806 }
7807 replace_stack = p;
7808 }
7809 p = replace_stack + replace_stack_nr - replace_offset;
7810 if (replace_offset)
7811 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7812 *p = c;
7813 ++replace_stack_nr;
7814}
7815
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007816#if defined(FEAT_MBYTE) || defined(PROTO)
7817/*
7818 * Push a character onto the replace stack. Handles a multi-byte character in
7819 * reverse byte order, so that the first byte is popped off first.
7820 * Return the number of bytes done (includes composing characters).
7821 */
7822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007823replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007824{
7825 int l = (*mb_ptr2len)(p);
7826 int j;
7827
7828 for (j = l - 1; j >= 0; --j)
7829 replace_push(p[j]);
7830 return l;
7831}
7832#endif
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834/*
7835 * Pop one item from the replace stack.
7836 * return -1 if stack empty
7837 * return replaced character or NUL otherwise
7838 */
7839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007840replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 if (replace_stack_nr == 0)
7843 return -1;
7844 return (int)replace_stack[--replace_stack_nr];
7845}
7846
7847/*
7848 * Join the top two items on the replace stack. This removes to "off"'th NUL
7849 * encountered.
7850 */
7851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007852replace_join(
7853 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 int i;
7856
7857 for (i = replace_stack_nr; --i >= 0; )
7858 if (replace_stack[i] == NUL && off-- <= 0)
7859 {
7860 --replace_stack_nr;
7861 mch_memmove(replace_stack + i, replace_stack + i + 1,
7862 (size_t)(replace_stack_nr - i));
7863 return;
7864 }
7865}
7866
7867/*
7868 * Pop bytes from the replace stack until a NUL is found, and insert them
7869 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7870 */
7871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007872replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 int cc;
7875 int oldState = State;
7876
7877 State = NORMAL; /* don't want REPLACE here */
7878 while ((cc = replace_pop()) > 0)
7879 {
7880#ifdef FEAT_MBYTE
7881 mb_replace_pop_ins(cc);
7882#else
7883 ins_char(cc);
7884#endif
7885 dec_cursor();
7886 }
7887 State = oldState;
7888}
7889
7890#ifdef FEAT_MBYTE
7891/*
7892 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7893 * indicates a multi-byte char, pop the other bytes too.
7894 */
7895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007899 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 int i;
7901 int c;
7902
7903 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7904 {
7905 buf[0] = cc;
7906 for (i = 1; i < n; ++i)
7907 buf[i] = replace_pop();
7908 ins_bytes_len(buf, n);
7909 }
7910 else
7911 ins_char(cc);
7912
7913 if (enc_utf8)
7914 /* Handle composing chars. */
7915 for (;;)
7916 {
7917 c = replace_pop();
7918 if (c == -1) /* stack empty */
7919 break;
7920 if ((n = MB_BYTE2LEN(c)) == 1)
7921 {
7922 /* Not a multi-byte char, put it back. */
7923 replace_push(c);
7924 break;
7925 }
7926 else
7927 {
7928 buf[0] = c;
7929 for (i = 1; i < n; ++i)
7930 buf[i] = replace_pop();
7931 if (utf_iscomposing(utf_ptr2char(buf)))
7932 ins_bytes_len(buf, n);
7933 else
7934 {
7935 /* Not a composing char, put it back. */
7936 for (i = n - 1; i >= 0; --i)
7937 replace_push(buf[i]);
7938 break;
7939 }
7940 }
7941 }
7942}
7943#endif
7944
7945/*
7946 * make the replace stack empty
7947 * (called when exiting replace mode)
7948 */
7949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007950replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007952 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 replace_stack_len = 0;
7954 replace_stack_nr = 0;
7955}
7956
7957/*
7958 * Handle doing a BS for one character.
7959 * cc < 0: replace stack empty, just move cursor
7960 * cc == 0: character was inserted, delete it
7961 * cc > 0: character was replaced, put cc (first byte of original char) back
7962 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007963 * When "limit_col" is >= 0, don't delete before this column. Matters when
7964 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 */
7966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007967replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 int orig_len = 0;
7971 int ins_len;
7972 int orig_vcols = 0;
7973 colnr_T start_vcol;
7974 char_u *p;
7975 int i;
7976 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 cc = replace_pop();
7979 if (cc > 0)
7980 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007981#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007982 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007983
7984 if (curbuf->b_has_textprop)
7985 {
7986 // Do not adjust text properties for individual delete and insert
7987 // operations, do it afterwards on the resulting text.
7988 len_before = STRLEN(ml_get_curline());
7989 ++text_prop_frozen;
7990 }
7991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (State & VREPLACE_FLAG)
7993 {
7994 /* Get the number of screen cells used by the character we are
7995 * going to delete. */
7996 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7997 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999#ifdef FEAT_MBYTE
8000 if (has_mbyte)
8001 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008002 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008004 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 replace_push(cc);
8006 }
8007 else
8008#endif
8009 {
8010 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008012 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 }
8014 replace_pop_ins();
8015
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (State & VREPLACE_FLAG)
8017 {
8018 /* Get the number of screen cells used by the inserted characters */
8019 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008020 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 vcol = start_vcol;
8022 for (i = 0; i < ins_len; ++i)
8023 {
8024 vcol += chartabsize(p + i, vcol);
8025#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008026 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027#endif
8028 }
8029 vcol -= start_vcol;
8030
8031 /* Delete spaces that were inserted after the cursor to keep the
8032 * text aligned. */
8033 curwin->w_cursor.col += ins_len;
8034 while (vcol > orig_vcols && gchar_cursor() == ' ')
8035 {
8036 del_char(FALSE);
8037 ++orig_vcols;
8038 }
8039 curwin->w_cursor.col -= ins_len;
8040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
Bram Moolenaar196d1572019-01-02 23:47:18 +01008042 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01008044
8045#ifdef FEAT_TEXT_PROP
8046 if (curbuf->b_has_textprop)
8047 {
8048 size_t len_now = STRLEN(ml_get_curline());
8049
8050 --text_prop_frozen;
8051 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
8052 (int)(len_now - len_before));
8053 }
8054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 }
8056 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008057 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
8059
8060#ifdef FEAT_CINDENT
8061/*
8062 * Return TRUE if C-indenting is on.
8063 */
8064 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008065cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
8067 return (!p_paste && (curbuf->b_p_cin
8068# ifdef FEAT_EVAL
8069 || *curbuf->b_p_inde != NUL
8070# endif
8071 ));
8072}
8073#endif
8074
8075#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8076/*
8077 * Re-indent the current line, based on the current contents of it and the
8078 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8079 * confused what all the part that handles Control-T is doing that I'm not.
8080 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8081 */
8082
8083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008084fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008086 int amount = get_the_indent();
8087
8088 if (amount >= 0)
8089 {
8090 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8091 if (linewhite(curwin->w_cursor.lnum))
8092 did_ai = TRUE; /* delete the indent if the line stays empty */
8093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094}
8095
8096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 if (p_paste)
8100 return;
8101# ifdef FEAT_LISP
8102 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8103 fixthisline(get_lisp_indent);
8104# endif
8105# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8106 else
8107# endif
8108# ifdef FEAT_CINDENT
8109 if (cindent_on())
8110 do_c_expr_indent();
8111# endif
8112}
8113
8114#endif
8115
8116#ifdef FEAT_CINDENT
8117/*
8118 * return TRUE if 'cinkeys' contains the key "keytyped",
8119 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008120 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8122 *
8123 * "keytyped" can have a few special values:
8124 * KEY_OPEN_FORW
8125 * KEY_OPEN_BACK
8126 * KEY_COMPLETE just finished completion.
8127 *
8128 * If line_is_empty is TRUE accept keys with '0' before them.
8129 */
8130 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008131in_cinkeys(
8132 int keytyped,
8133 int when,
8134 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 char_u *look;
8137 int try_match;
8138 int try_match_word;
8139 char_u *p;
8140 char_u *line;
8141 int icase;
8142 int i;
8143
Bram Moolenaar30848942009-12-24 14:46:12 +00008144 if (keytyped == NUL)
8145 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8146 return FALSE;
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148#ifdef FEAT_EVAL
8149 if (*curbuf->b_p_inde != NUL)
8150 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8151 else
8152#endif
8153 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8154 while (*look)
8155 {
8156 /*
8157 * Find out if we want to try a match with this key, depending on
8158 * 'when' and a '*' or '!' before the key.
8159 */
8160 switch (when)
8161 {
8162 case '*': try_match = (*look == '*'); break;
8163 case '!': try_match = (*look == '!'); break;
8164 default: try_match = (*look != '*'); break;
8165 }
8166 if (*look == '*' || *look == '!')
8167 ++look;
8168
8169 /*
8170 * If there is a '0', only accept a match if the line is empty.
8171 * But may still match when typing last char of a word.
8172 */
8173 if (*look == '0')
8174 {
8175 try_match_word = try_match;
8176 if (!line_is_empty)
8177 try_match = FALSE;
8178 ++look;
8179 }
8180 else
8181 try_match_word = FALSE;
8182
8183 /*
8184 * does it look like a control character?
8185 */
8186 if (*look == '^'
8187#ifdef EBCDIC
8188 && (Ctrl_chr(look[1]) != 0)
8189#else
8190 && look[1] >= '?' && look[1] <= '_'
8191#endif
8192 )
8193 {
8194 if (try_match && keytyped == Ctrl_chr(look[1]))
8195 return TRUE;
8196 look += 2;
8197 }
8198 /*
8199 * 'o' means "o" command, open forward.
8200 * 'O' means "O" command, open backward.
8201 */
8202 else if (*look == 'o')
8203 {
8204 if (try_match && keytyped == KEY_OPEN_FORW)
8205 return TRUE;
8206 ++look;
8207 }
8208 else if (*look == 'O')
8209 {
8210 if (try_match && keytyped == KEY_OPEN_BACK)
8211 return TRUE;
8212 ++look;
8213 }
8214
8215 /*
8216 * 'e' means to check for "else" at start of line and just before the
8217 * cursor.
8218 */
8219 else if (*look == 'e')
8220 {
8221 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8222 {
8223 p = ml_get_curline();
8224 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8225 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8226 return TRUE;
8227 }
8228 ++look;
8229 }
8230
8231 /*
8232 * ':' only causes an indent if it is at the end of a label or case
8233 * statement, or when it was before typing the ':' (to fix
8234 * class::method for C++).
8235 */
8236 else if (*look == ':')
8237 {
8238 if (try_match && keytyped == ':')
8239 {
8240 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008241 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008243 /* Need to get the line again after cin_islabel(). */
8244 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (curwin->w_cursor.col > 2
8246 && p[curwin->w_cursor.col - 1] == ':'
8247 && p[curwin->w_cursor.col - 2] == ':')
8248 {
8249 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008250 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008251 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 p = ml_get_curline();
8253 p[curwin->w_cursor.col - 1] = ':';
8254 if (i)
8255 return TRUE;
8256 }
8257 }
8258 ++look;
8259 }
8260
8261
8262 /*
8263 * Is it a key in <>, maybe?
8264 */
8265 else if (*look == '<')
8266 {
8267 if (try_match)
8268 {
8269 /*
8270 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8271 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8272 * >, *, : and ! keys if they really really want to.
8273 */
8274 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8275 && keytyped == look[1])
8276 return TRUE;
8277
8278 if (keytyped == get_special_key_code(look + 1))
8279 return TRUE;
8280 }
8281 while (*look && *look != '>')
8282 look++;
8283 while (*look == '>')
8284 look++;
8285 }
8286
8287 /*
8288 * Is it a word: "=word"?
8289 */
8290 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8291 {
8292 ++look;
8293 if (*look == '~')
8294 {
8295 icase = TRUE;
8296 ++look;
8297 }
8298 else
8299 icase = FALSE;
8300 p = vim_strchr(look, ',');
8301 if (p == NULL)
8302 p = look + STRLEN(look);
8303 if ((try_match || try_match_word)
8304 && curwin->w_cursor.col >= (colnr_T)(p - look))
8305 {
8306 int match = FALSE;
8307
8308#ifdef FEAT_INS_EXPAND
8309 if (keytyped == KEY_COMPLETE)
8310 {
8311 char_u *s;
8312
8313 /* Just completed a word, check if it starts with "look".
8314 * search back for the start of a word. */
8315 line = ml_get_curline();
8316# ifdef FEAT_MBYTE
8317 if (has_mbyte)
8318 {
8319 char_u *n;
8320
8321 for (s = line + curwin->w_cursor.col; s > line; s = n)
8322 {
8323 n = mb_prevptr(line, s);
8324 if (!vim_iswordp(n))
8325 break;
8326 }
8327 }
8328 else
8329# endif
8330 for (s = line + curwin->w_cursor.col; s > line; --s)
8331 if (!vim_iswordc(s[-1]))
8332 break;
8333 if (s + (p - look) <= line + curwin->w_cursor.col
8334 && (icase
8335 ? MB_STRNICMP(s, look, p - look)
8336 : STRNCMP(s, look, p - look)) == 0)
8337 match = TRUE;
8338 }
8339 else
8340#endif
8341 /* TODO: multi-byte */
8342 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8343 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8344 {
8345 line = ml_get_cursor();
8346 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8347 || !vim_iswordc(line[-(p - look) - 1]))
8348 && (icase
8349 ? MB_STRNICMP(line - (p - look), look, p - look)
8350 : STRNCMP(line - (p - look), look, p - look))
8351 == 0)
8352 match = TRUE;
8353 }
8354 if (match && try_match_word && !try_match)
8355 {
8356 /* "0=word": Check if there are only blanks before the
8357 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008358 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 (int)(curwin->w_cursor.col - (p - look)))
8360 match = FALSE;
8361 }
8362 if (match)
8363 return TRUE;
8364 }
8365 look = p;
8366 }
8367
8368 /*
8369 * ok, it's a boring generic character.
8370 */
8371 else
8372 {
8373 if (try_match && *look == keytyped)
8374 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008375 if (*look != NUL)
8376 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
8378
8379 /*
8380 * Skip over ", ".
8381 */
8382 look = skip_to_option_part(look);
8383 }
8384 return FALSE;
8385}
8386#endif /* FEAT_CINDENT */
8387
8388#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8389/*
8390 * Map Hebrew keyboard when in hkmap mode.
8391 */
8392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008393hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
8395 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8396 {
8397 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8398 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8399 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8400 static char_u map[26] =
8401 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8402 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8403 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8404 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8405 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8406 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8407 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8408 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8409 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8410
8411 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8412 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8413 /* '-1'='sofit' */
8414 else if (c == 'x')
8415 return 'X';
8416 else if (c == 'q')
8417 return '\''; /* {geresh}={'} */
8418 else if (c == 246)
8419 return ' '; /* \"o --> ' ' for a german keyboard */
8420 else if (c == 228)
8421 return ' '; /* \"a --> ' ' -- / -- */
8422 else if (c == 252)
8423 return ' '; /* \"u --> ' ' -- / -- */
8424#ifdef EBCDIC
8425 else if (islower(c))
8426#else
8427 /* NOTE: islower() does not do the right thing for us on Linux so we
8428 * do this the same was as 5.7 and previous, so it works correctly on
8429 * all systems. Specifically, the e.g. Delete and Arrow keys are
8430 * munged and won't work if e.g. searching for Hebrew text.
8431 */
8432 else if (c >= 'a' && c <= 'z')
8433#endif
8434 return (int)(map[CharOrdLow(c)] + p_aleph);
8435 else
8436 return c;
8437 }
8438 else
8439 {
8440 switch (c)
8441 {
8442 case '`': return ';';
8443 case '/': return '.';
8444 case '\'': return ',';
8445 case 'q': return '/';
8446 case 'w': return '\'';
8447
8448 /* Hebrew letters - set offset from 'a' */
8449 case ',': c = '{'; break;
8450 case '.': c = 'v'; break;
8451 case ';': c = 't'; break;
8452 default: {
8453 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8454
8455#ifdef EBCDIC
8456 /* see note about islower() above */
8457 if (!islower(c))
8458#else
8459 if (c < 'a' || c > 'z')
8460#endif
8461 return c;
8462 c = str[CharOrdLow(c)];
8463 break;
8464 }
8465 }
8466
8467 return (int)(CharOrdLow(c) + p_aleph);
8468 }
8469}
8470#endif
8471
8472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008473ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
8475 int need_redraw = FALSE;
8476 int regname;
8477 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008478 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479
8480 /*
8481 * If we are going to wait for a character, show a '"'.
8482 */
8483 pc_status = PC_STATUS_UNSET;
8484 if (redrawing() && !char_avail())
8485 {
8486 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008487 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
8489 edit_putchar('"', TRUE);
8490#ifdef FEAT_CMDL_INFO
8491 add_to_showcmd_c(Ctrl_R);
8492#endif
8493 }
8494
8495#ifdef USE_ON_FLY_SCROLL
8496 dont_scroll = TRUE; /* disallow scrolling here */
8497#endif
8498
8499 /*
8500 * Don't map the register name. This also prevents the mode message to be
8501 * deleted when ESC is hit.
8502 */
8503 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008504 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8507 {
8508 /* Get a third key for literal register insertion */
8509 literally = regname;
8510#ifdef FEAT_CMDL_INFO
8511 add_to_showcmd_c(literally);
8512#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008513 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516 --no_mapping;
8517
8518#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008519 /* Don't call u_sync() while typing the expression or giving an error
8520 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 ++no_u_sync;
8522 if (regname == '=')
8523 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008524# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008526# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008527 /* Sync undo when evaluating the expression calls setline() or
8528 * append(), so that it can be undone separately. */
8529 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008532# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 /* Restore the Input Method. */
8534 if (im_on)
8535 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008538 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8539 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008540 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 else
8544 {
8545#endif
8546 if (literally == Ctrl_O || literally == Ctrl_P)
8547 {
8548 /* Append the command to the redo buffer. */
8549 AppendCharToRedobuff(Ctrl_R);
8550 AppendCharToRedobuff(literally);
8551 AppendCharToRedobuff(regname);
8552
8553 do_put(regname, BACKWARD, 1L,
8554 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8555 }
8556 else if (insert_reg(regname, literally) == FAIL)
8557 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008558 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 need_redraw = TRUE; /* remove the '"' */
8560 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008561 else if (stop_insert_mode)
8562 /* When the '=' register was used and a function was invoked that
8563 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8564 * insert anything, need to remove the '"' */
8565 need_redraw = TRUE;
8566
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#ifdef FEAT_EVAL
8568 }
8569 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008570 if (u_sync_once == 1)
8571 ins_need_undo = TRUE;
8572 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#endif
8574#ifdef FEAT_CMDL_INFO
8575 clear_showcmd();
8576#endif
8577
8578 /* If the inserted register is empty, we need to remove the '"' */
8579 if (need_redraw || stuff_empty())
8580 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008581
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008582 /* Disallow starting Visual mode here, would get a weird mode. */
8583 if (!vis_active && VIsual_active)
8584 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585}
8586
8587/*
8588 * CTRL-G commands in Insert mode.
8589 */
8590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008591ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 int c;
8594
8595#ifdef FEAT_INS_EXPAND
8596 /* Right after CTRL-X the cursor will be after the ruler. */
8597 setcursor();
8598#endif
8599
8600 /*
8601 * Don't map the second key. This also prevents the mode message to be
8602 * deleted when ESC is hit.
8603 */
8604 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008605 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 --no_mapping;
8607 switch (c)
8608 {
8609 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8610 case K_UP:
8611 case Ctrl_K:
8612 case 'k': ins_up(TRUE);
8613 break;
8614
8615 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8616 case K_DOWN:
8617 case Ctrl_J:
8618 case 'j': ins_down(TRUE);
8619 break;
8620
8621 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008622 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008624
8625 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008626 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008627 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008628 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 break;
8630
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008631 /* CTRL-G U: do not break undo with the next char */
8632 case 'U':
8633 /* Allow one left/right cursor movement with the next char,
8634 * without breaking undo. */
8635 dont_sync_undo = MAYBE;
8636 break;
8637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008639 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641}
8642
8643/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008644 * CTRL-^ in Insert mode.
8645 */
8646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008647ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008648{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008649 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008650 {
8651 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8652 if (State & LANGMAP)
8653 {
8654 curbuf->b_p_iminsert = B_IMODE_NONE;
8655 State &= ~LANGMAP;
8656 }
8657 else
8658 {
8659 curbuf->b_p_iminsert = B_IMODE_LMAP;
8660 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008661#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008662 im_set_active(FALSE);
8663#endif
8664 }
8665 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008666#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008667 else
8668 {
8669 /* There are no ":lmap" mappings, toggle IM */
8670 if (im_get_status())
8671 {
8672 curbuf->b_p_iminsert = B_IMODE_NONE;
8673 im_set_active(FALSE);
8674 }
8675 else
8676 {
8677 curbuf->b_p_iminsert = B_IMODE_IM;
8678 State &= ~LANGMAP;
8679 im_set_active(TRUE);
8680 }
8681 }
8682#endif
8683 set_iminsert_global();
8684 showmode();
8685#ifdef FEAT_GUI
8686 /* may show different cursor shape or color */
8687 if (gui.in_use)
8688 gui_update_cursor(TRUE, FALSE);
8689#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008690#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008691 /* Show/unshow value of 'keymap' in status lines. */
8692 status_redraw_curbuf();
8693#endif
8694}
8695
8696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 * Handle ESC in insert mode.
8698 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8699 * insert.
8700 */
8701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008702ins_esc(
8703 long *count,
8704 int cmdchar,
8705 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 int temp;
8708 static int disabled_redraw = FALSE;
8709
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008710#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008711 check_spell_redraw();
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713#if defined(FEAT_HANGULIN)
8714# if defined(ESC_CHG_TO_ENG_MODE)
8715 hangul_input_state_set(0);
8716# endif
8717 if (composing_hangul)
8718 {
8719 push_raw_key(composing_hangul_buffer, 2);
8720 composing_hangul = 0;
8721 }
8722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 temp = curwin->w_cursor.col;
8725 if (disabled_redraw)
8726 {
8727 --RedrawingDisabled;
8728 disabled_redraw = FALSE;
8729 }
8730 if (!arrow_used)
8731 {
8732 /*
8733 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008734 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8735 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 */
8737 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008738 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
8740 /*
8741 * Repeating insert may take a long time. Check for
8742 * interrupt now and then.
8743 */
8744 if (*count > 0)
8745 {
8746 line_breakcheck();
8747 if (got_int)
8748 *count = 0;
8749 }
8750
8751 if (--*count > 0) /* repeat what was typed */
8752 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008753 /* Vi repeats the insert without replacing characters. */
8754 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8755 State &= ~REPLACE_FLAG;
8756
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 (void)start_redo_ins();
8758 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008759 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 ++RedrawingDisabled;
8761 disabled_redraw = TRUE;
8762 return FALSE; /* repeat the insert */
8763 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008764 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 undisplay_dollar();
8766 }
8767
8768 /* When an autoindent was removed, curswant stays after the
8769 * indent */
8770 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8771 curwin->w_set_curswant = TRUE;
8772
8773 /* Remember the last Insert position in the '^ mark. */
8774 if (!cmdmod.keepjumps)
8775 curbuf->b_last_insert = curwin->w_cursor;
8776
8777 /*
8778 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008779 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008781 if (!nomove
8782 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783#ifdef FEAT_VIRTUALEDIT
8784 || curwin->w_cursor.coladd > 0
8785#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008786 )
8787 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008788 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#ifdef FEAT_RIGHTLEFT
8790 && !revins_on
8791#endif
8792 )
8793 {
8794#ifdef FEAT_VIRTUALEDIT
8795 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8796 {
8797 oneleft();
8798 if (restart_edit != NUL)
8799 ++curwin->w_cursor.coladd;
8800 }
8801 else
8802#endif
8803 {
8804 --curwin->w_cursor.col;
8805#ifdef FEAT_MBYTE
8806 /* Correct cursor for multi-byte character. */
8807 if (has_mbyte)
8808 mb_adjust_cursor();
8809#endif
8810 }
8811 }
8812
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008813#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 /* Disable IM to allow typing English directly for Normal mode commands.
8815 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8816 * well). */
8817 if (!(State & LANGMAP))
8818 im_save_status(&curbuf->b_p_iminsert);
8819 im_set_active(FALSE);
8820#endif
8821
8822 State = NORMAL;
8823 /* need to position cursor again (e.g. when on a TAB ) */
8824 changed_cline_bef_curs();
8825
8826#ifdef FEAT_MOUSE
8827 setmouse();
8828#endif
8829#ifdef CURSOR_SHAPE
8830 ui_cursor_shape(); /* may show different cursor shape */
8831#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008832 if (!p_ek)
8833 /* Re-enable bracketed paste mode. */
8834 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
8836 /*
8837 * When recording or for CTRL-O, need to display the new mode.
8838 * Otherwise remove the mode message.
8839 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008840 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 showmode();
8842 else if (p_smd)
8843 MSG("");
8844
8845 return TRUE; /* exit Insert mode */
8846}
8847
8848#ifdef FEAT_RIGHTLEFT
8849/*
8850 * Toggle language: hkmap and revins_on.
8851 * Move to end of reverse inserted text.
8852 */
8853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008854ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 if (revins_on && revins_chars && revins_scol >= 0)
8857 {
8858 while (gchar_cursor() != NUL && revins_chars--)
8859 ++curwin->w_cursor.col;
8860 }
8861 p_ri = !p_ri;
8862 revins_on = (State == INSERT && p_ri);
8863 if (revins_on)
8864 {
8865 revins_scol = curwin->w_cursor.col;
8866 revins_legal++;
8867 revins_chars = 0;
8868 undisplay_dollar();
8869 }
8870 else
8871 revins_scol = -1;
8872#ifdef FEAT_FKMAP
8873 if (p_altkeymap)
8874 {
8875 /*
8876 * to be consistent also for redo command, using '.'
8877 * set arrow_used to true and stop it - causing to redo
8878 * characters entered in one mode (normal/reverse insert).
8879 */
8880 arrow_used = TRUE;
8881 (void)stop_arrow();
8882 p_fkmap = curwin->w_p_rl ^ p_ri;
8883 if (p_fkmap && p_ri)
8884 State = INSERT;
8885 }
8886 else
8887#endif
8888 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8889 showmode();
8890}
8891#endif
8892
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893/*
8894 * If 'keymodel' contains "startsel", may start selection.
8895 * Returns TRUE when a CTRL-O and other keys stuffed.
8896 */
8897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008898ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 if (km_startsel)
8901 switch (c)
8902 {
8903 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 case K_PAGEUP:
8906 case K_KPAGEUP:
8907 case K_PAGEDOWN:
8908 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008909# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 case K_LEFT:
8911 case K_RIGHT:
8912 case K_UP:
8913 case K_DOWN:
8914 case K_END:
8915 case K_HOME:
8916# endif
8917 if (!(mod_mask & MOD_MASK_SHIFT))
8918 break;
8919 /* FALLTHROUGH */
8920 case K_S_LEFT:
8921 case K_S_RIGHT:
8922 case K_S_UP:
8923 case K_S_DOWN:
8924 case K_S_END:
8925 case K_S_HOME:
8926 /* Start selection right away, the cursor can move with
8927 * CTRL-O when beyond the end of the line. */
8928 start_selection();
8929
8930 /* Execute the key in (insert) Select mode. */
8931 stuffcharReadbuff(Ctrl_O);
8932 if (mod_mask)
8933 {
8934 char_u buf[4];
8935
8936 buf[0] = K_SPECIAL;
8937 buf[1] = KS_MODIFIER;
8938 buf[2] = mod_mask;
8939 buf[3] = NUL;
8940 stuffReadbuff(buf);
8941 }
8942 stuffcharReadbuff(c);
8943 return TRUE;
8944 }
8945 return FALSE;
8946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
8948/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008949 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008950 */
8951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008952ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008953{
8954#ifdef FEAT_FKMAP
8955 if (p_fkmap && p_ri)
8956 {
8957 beep_flush();
8958 EMSG(farsi_text_3); /* encoded in Farsi */
8959 return;
8960 }
8961#endif
8962
Bram Moolenaar1e015462005-09-25 22:16:38 +00008963# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008964 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008965 (char_u *)((State & REPLACE_FLAG) ? "i"
8966 : replaceState == VREPLACE ? "v"
8967 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008968# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008969 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008970 if (State & REPLACE_FLAG)
8971 State = INSERT | (State & LANGMAP);
8972 else
8973 State = replaceState | (State & LANGMAP);
8974 AppendCharToRedobuff(K_INS);
8975 showmode();
8976#ifdef CURSOR_SHAPE
8977 ui_cursor_shape(); /* may show different cursor shape */
8978#endif
8979}
8980
8981/*
8982 * Pressed CTRL-O in Insert mode.
8983 */
8984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008985ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008986{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008987 if (State & VREPLACE_FLAG)
8988 restart_edit = 'V';
8989 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008990 if (State & REPLACE_FLAG)
8991 restart_edit = 'R';
8992 else
8993 restart_edit = 'I';
8994#ifdef FEAT_VIRTUALEDIT
8995 if (virtual_active())
8996 ins_at_eol = FALSE; /* cursor always keeps its column */
8997 else
8998#endif
8999 ins_at_eol = (gchar_cursor() == NUL);
9000}
9001
9002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 * If the cursor is on an indent, ^T/^D insert/delete one
9004 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009005 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * with vi. But vi only supports ^T and ^D after an
9007 * autoindent, we support it everywhere.
9008 */
9009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009010ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
9012 if (stop_arrow() == FAIL)
9013 return;
9014 AppendCharToRedobuff(c);
9015
9016 /*
9017 * 0^D and ^^D: remove all indent.
9018 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009019 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9020 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
9022 --curwin->w_cursor.col;
9023 (void)del_char(FALSE); /* delete the '^' or '0' */
9024 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9025 if (State & REPLACE_FLAG)
9026 replace_pop_ins();
9027 if (lastc == '^')
9028 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009029 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
9031 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009032 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
9034 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9035 did_ai = FALSE;
9036#ifdef FEAT_SMARTINDENT
9037 did_si = FALSE;
9038 can_si = FALSE;
9039 can_si_back = FALSE;
9040#endif
9041#ifdef FEAT_CINDENT
9042 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9043#endif
9044}
9045
9046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009047ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
9049 int temp;
9050
9051 if (stop_arrow() == FAIL)
9052 return;
9053 if (gchar_cursor() == NUL) /* delete newline */
9054 {
9055 temp = curwin->w_cursor.col;
9056 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009057 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009058 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009062 /* Adjust orig_line_count in case more lines have been deleted than
9063 * have been added. That makes sure, that open_line() later
9064 * can access all buffer lines correctly */
9065 if (State & VREPLACE_FLAG &&
9066 orig_line_count > curbuf->b_ml.ml_line_count)
9067 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009070 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9071 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 did_ai = FALSE;
9073#ifdef FEAT_SMARTINDENT
9074 did_si = FALSE;
9075 can_si = FALSE;
9076 can_si_back = FALSE;
9077#endif
9078 AppendCharToRedobuff(K_DEL);
9079}
9080
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009081/*
9082 * Delete one character for ins_bs().
9083 */
9084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009085ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009086{
9087 dec_cursor();
9088 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9089 if (State & REPLACE_FLAG)
9090 {
9091 /* Don't delete characters before the insert point when in
9092 * Replace mode */
9093 if (curwin->w_cursor.lnum != Insstart.lnum
9094 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009095 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009096 }
9097 else
9098 (void)del_char(FALSE);
9099}
9100
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101/*
9102 * Handle Backspace, delete-word and delete-line in Insert mode.
9103 * Return TRUE when backspace was actually used.
9104 */
9105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106ins_bs(
9107 int c,
9108 int mode,
9109 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 linenr_T lnum;
9112 int cc;
9113 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009114 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 colnr_T mincol;
9116 int did_backspace = FALSE;
9117 int in_indent;
9118 int oldState;
9119#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009120 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#endif
9122
9123 /*
9124 * can't delete anything in an empty file
9125 * can't backup past first character in buffer
9126 * can't backup past starting point unless 'backspace' > 1
9127 * can backup to a previous line if 'backspace' == 0
9128 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009129 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 || (
9131#ifdef FEAT_RIGHTLEFT
9132 !revins_on &&
9133#endif
9134 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9135 || (!can_bs(BS_START)
9136 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009137 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9138 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9140 && curwin->w_cursor.col <= ai_col)
9141 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9142 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009143 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 return FALSE;
9145 }
9146
9147 if (stop_arrow() == FAIL)
9148 return FALSE;
9149 in_indent = inindent(0);
9150#ifdef FEAT_CINDENT
9151 if (in_indent)
9152 can_cindent = FALSE;
9153#endif
9154#ifdef FEAT_COMMENTS
9155 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9156#endif
9157#ifdef FEAT_RIGHTLEFT
9158 if (revins_on) /* put cursor after last inserted char */
9159 inc_cursor();
9160#endif
9161
9162#ifdef FEAT_VIRTUALEDIT
9163 /* Virtualedit:
9164 * BACKSPACE_CHAR eats a virtual space
9165 * BACKSPACE_WORD eats all coladd
9166 * BACKSPACE_LINE eats all coladd and keeps going
9167 */
9168 if (curwin->w_cursor.coladd > 0)
9169 {
9170 if (mode == BACKSPACE_CHAR)
9171 {
9172 --curwin->w_cursor.coladd;
9173 return TRUE;
9174 }
9175 if (mode == BACKSPACE_WORD)
9176 {
9177 curwin->w_cursor.coladd = 0;
9178 return TRUE;
9179 }
9180 curwin->w_cursor.coladd = 0;
9181 }
9182#endif
9183
9184 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009185 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 */
9187 if (curwin->w_cursor.col == 0)
9188 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009189 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009190 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191#ifdef FEAT_RIGHTLEFT
9192 || revins_on
9193#endif
9194 )
9195 {
9196 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9197 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9198 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009199 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009200 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 }
9202 /*
9203 * In replace mode:
9204 * cc < 0: NL was inserted, delete it
9205 * cc >= 0: NL was replaced, put original characters back
9206 */
9207 cc = -1;
9208 if (State & REPLACE_FLAG)
9209 cc = replace_pop(); /* returns -1 if NL was inserted */
9210 /*
9211 * In replace mode, in the line we started replacing, we only move the
9212 * cursor.
9213 */
9214 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9215 {
9216 dec_cursor();
9217 }
9218 else
9219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 if (!(State & VREPLACE_FLAG)
9221 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 {
9223 temp = gchar_cursor(); /* remember current char */
9224 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009225
9226 /* When "aw" is in 'formatoptions' we must delete the space at
9227 * the end of the line, otherwise the line will be broken
9228 * again when auto-formatting. */
9229 if (has_format_option(FO_AUTO)
9230 && has_format_option(FO_WHITE_PAR))
9231 {
9232 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9233 TRUE);
9234 int len;
9235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009236 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009237 if (len > 0 && ptr[len - 1] == ' ')
9238 ptr[len - 1] = NUL;
9239 }
9240
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009241 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 if (temp == NUL && gchar_cursor() != NUL)
9243 inc_cursor();
9244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 else
9246 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247
9248 /*
9249 * In REPLACE mode we have to put back the text that was replaced
9250 * by the NL. On the replace stack is first a NUL-terminated
9251 * sequence of characters that were deleted and then the
9252 * characters that NL replaced.
9253 */
9254 if (State & REPLACE_FLAG)
9255 {
9256 /*
9257 * Do the next ins_char() in NORMAL state, to
9258 * prevent ins_char() from replacing characters and
9259 * avoiding showmatch().
9260 */
9261 oldState = State;
9262 State = NORMAL;
9263 /*
9264 * restore characters (blanks) deleted after cursor
9265 */
9266 while (cc > 0)
9267 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009268 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#ifdef FEAT_MBYTE
9270 mb_replace_pop_ins(cc);
9271#else
9272 ins_char(cc);
9273#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009274 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 cc = replace_pop();
9276 }
9277 /* restore the characters that NL replaced */
9278 replace_pop_ins();
9279 State = oldState;
9280 }
9281 }
9282 did_ai = FALSE;
9283 }
9284 else
9285 {
9286 /*
9287 * Delete character(s) before the cursor.
9288 */
9289#ifdef FEAT_RIGHTLEFT
9290 if (revins_on) /* put cursor on last inserted char */
9291 dec_cursor();
9292#endif
9293 mincol = 0;
9294 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009295 if (mode == BACKSPACE_LINE
9296 && (curbuf->b_p_ai
9297#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009298 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009299#endif
9300 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301#ifdef FEAT_RIGHTLEFT
9302 && !revins_on
9303#endif
9304 )
9305 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009306 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009308 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009310 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 }
9312
9313 /*
9314 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9315 */
9316 if ( mode == BACKSPACE_CHAR
9317 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009318 || ((get_sts_value() != 0
9319#ifdef FEAT_VARTABS
9320 || tabstop_count(curbuf->b_p_vsts_array)
9321#endif
9322 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009323 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 && (*(ml_get_cursor() - 1) == TAB
9325 || (*(ml_get_cursor() - 1) == ' '
9326 && (!*inserted_space_p
9327 || arrow_used))))))
9328 {
9329 int ts;
9330 colnr_T vcol;
9331 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009332 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
9334 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 /* Compute the virtual column where we want to be. Since
9336 * 'showbreak' may get in the way, need to get the last column of
9337 * the previous character. */
9338 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009339 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 dec_cursor();
9341 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9342 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009343#ifdef FEAT_VARTABS
9344 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009345 {
9346 ts = (int)get_sw_value(curbuf);
9347 want_vcol = (want_vcol / ts) * ts;
9348 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009349 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009350 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009351 curbuf->b_p_vsts_array);
9352#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009353 if (p_sta && in_indent)
9354 ts = (int)get_sw_value(curbuf);
9355 else
9356 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
9360 /* delete characters until we are at or before want_vcol */
9361 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009362 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009363 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
9365 /* insert extra spaces until we are at want_vcol */
9366 while (vcol < want_vcol)
9367 {
9368 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009369 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9370 && curwin->w_cursor.col < Insstart_orig.col)
9371 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 if (State & VREPLACE_FLAG)
9374 ins_char(' ');
9375 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
9377 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009378 if ((State & REPLACE_FLAG))
9379 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 }
9381 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9382 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009383
9384 /* If we are now back where we started delete one character. Can
9385 * happen when using 'sts' and 'linebreak'. */
9386 if (vcol >= start_vcol)
9387 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 }
9389
9390 /*
9391 * Delete upto starting point, start of line or previous word.
9392 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009395#ifdef FEAT_MBYTE
9396 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009398 if (has_mbyte)
9399 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009401 do
9402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009404 if (!revins_on) /* put cursor on char to be deleted */
9405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009407
9408 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009410 /* look multi-byte character class */
9411 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009413 prev_cclass = cclass;
9414 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009417
9418 /* start of word? */
9419 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9420 {
9421 mode = BACKSPACE_WORD_NOT_SPACE;
9422 temp = vim_iswordc(cc);
9423 }
9424 /* end of word? */
9425 else if (mode == BACKSPACE_WORD_NOT_SPACE
9426 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9427#ifdef FEAT_MBYTE
9428 || prev_cclass != cclass
9429#endif
9430 ))
9431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009433 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009435 inc_cursor();
9436#ifdef FEAT_RIGHTLEFT
9437 else if (State & REPLACE_FLAG)
9438 dec_cursor();
9439#endif
9440 break;
9441 }
9442 if (State & REPLACE_FLAG)
9443 replace_do_bs(-1);
9444 else
9445 {
9446#ifdef FEAT_MBYTE
9447 if (enc_utf8 && p_deco)
9448 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9449#endif
9450 (void)del_char(FALSE);
9451#ifdef FEAT_MBYTE
9452 /*
9453 * If there are combining characters and 'delcombine' is set
9454 * move the cursor back. Don't back up before the base
9455 * character.
9456 */
9457 if (enc_utf8 && p_deco && cpc[0] != NUL)
9458 inc_cursor();
9459#endif
9460#ifdef FEAT_RIGHTLEFT
9461 if (revins_chars)
9462 {
9463 revins_chars--;
9464 revins_legal++;
9465 }
9466 if (revins_on && gchar_cursor() == NUL)
9467 break;
9468#endif
9469 }
9470 /* Just a single backspace?: */
9471 if (mode == BACKSPACE_CHAR)
9472 break;
9473 } while (
9474#ifdef FEAT_RIGHTLEFT
9475 revins_on ||
9476#endif
9477 (curwin->w_cursor.col > mincol
9478 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9479 || curwin->w_cursor.col != Insstart_orig.col)));
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 did_backspace = TRUE;
9482 }
9483#ifdef FEAT_SMARTINDENT
9484 did_si = FALSE;
9485 can_si = FALSE;
9486 can_si_back = FALSE;
9487#endif
9488 if (curwin->w_cursor.col <= 1)
9489 did_ai = FALSE;
9490 /*
9491 * It's a little strange to put backspaces into the redo
9492 * buffer, but it makes auto-indent a lot easier to deal
9493 * with.
9494 */
9495 AppendCharToRedobuff(c);
9496
9497 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009498 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009499 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009500 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 /* vi behaviour: the cursor moves backward but the character that
9503 * was there remains visible
9504 * Vim behaviour: the cursor moves backward and the character that
9505 * was there is erased from the screen.
9506 * We can emulate the vi behaviour by pretending there is a dollar
9507 * displayed even when there isn't.
9508 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009509 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 dollar_vcol = curwin->w_virtcol;
9511
Bram Moolenaarce3be472008-01-14 19:12:28 +00009512#ifdef FEAT_FOLDING
9513 /* When deleting a char the cursor line must never be in a closed fold.
9514 * E.g., when 'foldmethod' is indent and deleting the first non-white
9515 * char before a Tab. */
9516 if (did_backspace)
9517 foldOpenCursor();
9518#endif
9519
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 return did_backspace;
9521}
9522
9523#ifdef FEAT_MOUSE
9524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009525ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009528 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529
9530# ifdef FEAT_GUI
9531 /* When GUI is active, also move/paste when 'mouse' is empty */
9532 if (!gui.in_use)
9533# endif
9534 if (!mouse_has(MOUSE_INSERT))
9535 return;
9536
9537 undisplay_dollar();
9538 tpos = curwin->w_cursor;
9539 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9540 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009541 win_T *new_curwin = curwin;
9542
9543 if (curwin != old_curwin && win_valid(old_curwin))
9544 {
9545 /* Mouse took us to another window. We need to go back to the
9546 * previous one to stop insert there properly. */
9547 curwin = old_curwin;
9548 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009549#ifdef FEAT_JOB_CHANNEL
9550 if (bt_prompt(curbuf))
9551 // Restart Insert mode when re-entering the prompt buffer.
9552 curbuf->b_prompt_insert = 'A';
9553#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009554 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009555 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009556 if (curwin != new_curwin && win_valid(new_curwin))
9557 {
9558 curwin = new_curwin;
9559 curbuf = curwin->w_buffer;
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561# ifdef FEAT_CINDENT
9562 can_cindent = TRUE;
9563# endif
9564 }
9565
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 /* redraw status lines (in case another window became active) */
9567 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568}
9569
9570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009571ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009574 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009575# ifdef FEAT_INS_EXPAND
9576 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577# endif
9578
9579 tpos = curwin->w_cursor;
9580
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009581 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 {
9583 int row, col;
9584
9585 row = mouse_row;
9586 col = mouse_col;
9587
9588 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009589 wp = mouse_find_win(&row, &col);
9590 if (wp == NULL)
9591 return;
9592 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 curbuf = curwin->w_buffer;
9594 }
9595 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 undisplay_dollar();
9597
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009598# ifdef FEAT_INS_EXPAND
9599 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009600 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009601# endif
9602 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009603 if (dir == MSCR_DOWN || dir == MSCR_UP)
9604 {
9605 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9606 scroll_redraw(dir,
9607 (long)(curwin->w_botline - curwin->w_topline));
9608 else
9609 scroll_redraw(dir, 3L);
9610 }
9611#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009612 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009613 {
9614 int val, step = 6;
9615
9616 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009617 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009618 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9619 if (val < 0)
9620 val = 0;
9621 gui_do_horiz_scroll(val, TRUE);
9622 }
9623#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009624# ifdef FEAT_INS_EXPAND
9625 did_scroll = TRUE;
9626# endif
9627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 curwin->w_redr_status = TRUE;
9630
9631 curwin = old_curwin;
9632 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009634# ifdef FEAT_INS_EXPAND
9635 /* The popup menu may overlay the window, need to redraw it.
9636 * TODO: Would be more efficient to only redraw the windows that are
9637 * overlapped by the popup menu. */
9638 if (pum_visible() && did_scroll)
9639 {
9640 redraw_all_later(NOT_VALID);
9641 ins_compl_show_pum();
9642 }
9643# endif
9644
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009645 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
9647 start_arrow(&tpos);
9648# ifdef FEAT_CINDENT
9649 can_cindent = TRUE;
9650# endif
9651 }
9652}
9653#endif
9654
Bram Moolenaarec2da362017-01-21 20:04:22 +01009655/*
9656 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9657 * P_PE literally.
9658 * When "drop" is TRUE then consume the text and drop it.
9659 */
9660 int
9661bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9662{
9663 int c;
9664 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9665 int idx = 0;
9666 char_u *end = find_termcode((char_u *)"PE");
9667 int ret_char = -1;
9668 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009669 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009670
9671 /* If the end code is too long we can't detect it, read everything. */
9672 if (STRLEN(end) >= NUMBUFLEN)
9673 end = NULL;
9674 ++no_mapping;
9675 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009676 if (!p_paste)
9677 // Also have the side effects of setting 'paste' to make it work much
9678 // faster.
9679 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009680
Bram Moolenaarec2da362017-01-21 20:04:22 +01009681 for (;;)
9682 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009683 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009684 if (end == NULL && vpeekc() == NUL)
9685 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009686 do
9687 {
9688 c = vgetc();
9689 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9690 if (c == NUL || got_int)
9691 // When CTRL-C was encountered the typeahead will be flushed and we
9692 // won't get the end sequence.
9693 break;
9694
Bram Moolenaarec2da362017-01-21 20:04:22 +01009695#ifdef FEAT_MBYTE
9696 if (has_mbyte)
9697 idx += (*mb_char2bytes)(c, buf + idx);
9698 else
9699#endif
9700 buf[idx++] = c;
9701 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009702 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009703 {
9704 if (end[idx] == NUL)
9705 break; /* Found the end of paste code. */
9706 continue;
9707 }
9708 if (!drop)
9709 {
9710 switch (mode)
9711 {
9712 case PASTE_CMDLINE:
9713 put_on_cmdline(buf, idx, TRUE);
9714 break;
9715
9716 case PASTE_EX:
9717 if (gap != NULL && ga_grow(gap, idx) == OK)
9718 {
9719 mch_memmove((char *)gap->ga_data + gap->ga_len,
9720 buf, (size_t)idx);
9721 gap->ga_len += idx;
9722 }
9723 break;
9724
9725 case PASTE_INSERT:
9726 if (stop_arrow() == OK)
9727 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009728 c = buf[0];
9729 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9730 ins_eol(c);
9731 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009732 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009733 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009734 AppendToRedobuffLit(buf, idx);
9735 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009736 }
9737 break;
9738
9739 case PASTE_ONE_CHAR:
9740 if (ret_char == -1)
9741 {
9742#ifdef FEAT_MBYTE
9743 if (has_mbyte)
9744 ret_char = (*mb_ptr2char)(buf);
9745 else
9746#endif
9747 ret_char = buf[0];
9748 }
9749 break;
9750 }
9751 }
9752 idx = 0;
9753 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009754
Bram Moolenaarec2da362017-01-21 20:04:22 +01009755 --no_mapping;
9756 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009757 if (!save_paste)
9758 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009759
9760 return ret_char;
9761}
9762
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009763#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009766{
9767 /* We will be leaving the current window, unless closing another tab. */
9768 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9769 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9770 {
9771 undisplay_dollar();
9772 start_arrow(&curwin->w_cursor);
9773# ifdef FEAT_CINDENT
9774 can_cindent = TRUE;
9775# endif
9776 }
9777
9778 if (c == K_TABLINE)
9779 goto_tabpage(current_tab);
9780 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009781 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009782 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009783 redraw_statuslines(); /* will redraw the tabline when needed */
9784 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009785}
9786#endif
9787
9788#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009790ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 pos_T tpos;
9793
9794 undisplay_dollar();
9795 tpos = curwin->w_cursor;
9796 if (gui_do_scroll())
9797 {
9798 start_arrow(&tpos);
9799# ifdef FEAT_CINDENT
9800 can_cindent = TRUE;
9801# endif
9802 }
9803}
9804
9805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009806ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808 pos_T tpos;
9809
9810 undisplay_dollar();
9811 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009812 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 {
9814 start_arrow(&tpos);
9815# ifdef FEAT_CINDENT
9816 can_cindent = TRUE;
9817# endif
9818 }
9819}
9820#endif
9821
9822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009823ins_left(
9824 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825{
9826 pos_T tpos;
9827
9828#ifdef FEAT_FOLDING
9829 if ((fdo_flags & FDO_HOR) && KeyTyped)
9830 foldOpenCursor();
9831#endif
9832 undisplay_dollar();
9833 tpos = curwin->w_cursor;
9834 if (oneleft() == OK)
9835 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009836#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9837 /* Only call start_arrow() when not busy with preediting, it will
9838 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009839 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009840#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009841 {
9842 start_arrow_with_change(&tpos, end_change);
9843 if (!end_change)
9844 AppendCharToRedobuff(K_LEFT);
9845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846#ifdef FEAT_RIGHTLEFT
9847 /* If exit reversed string, position is fixed */
9848 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9849 revins_legal++;
9850 revins_chars++;
9851#endif
9852 }
9853
9854 /*
9855 * if 'whichwrap' set for cursor in insert mode may go to
9856 * previous line
9857 */
9858 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9859 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009860 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 start_arrow(&tpos);
9862 --(curwin->w_cursor.lnum);
9863 coladvance((colnr_T)MAXCOL);
9864 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9865 }
9866 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009867 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009868 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869}
9870
9871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009872ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 pos_T tpos;
9875
9876#ifdef FEAT_FOLDING
9877 if ((fdo_flags & FDO_HOR) && KeyTyped)
9878 foldOpenCursor();
9879#endif
9880 undisplay_dollar();
9881 tpos = curwin->w_cursor;
9882 if (c == K_C_HOME)
9883 curwin->w_cursor.lnum = 1;
9884 curwin->w_cursor.col = 0;
9885#ifdef FEAT_VIRTUALEDIT
9886 curwin->w_cursor.coladd = 0;
9887#endif
9888 curwin->w_curswant = 0;
9889 start_arrow(&tpos);
9890}
9891
9892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895 pos_T tpos;
9896
9897#ifdef FEAT_FOLDING
9898 if ((fdo_flags & FDO_HOR) && KeyTyped)
9899 foldOpenCursor();
9900#endif
9901 undisplay_dollar();
9902 tpos = curwin->w_cursor;
9903 if (c == K_C_END)
9904 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9905 coladvance((colnr_T)MAXCOL);
9906 curwin->w_curswant = MAXCOL;
9907
9908 start_arrow(&tpos);
9909}
9910
9911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009912ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913{
9914#ifdef FEAT_FOLDING
9915 if ((fdo_flags & FDO_HOR) && KeyTyped)
9916 foldOpenCursor();
9917#endif
9918 undisplay_dollar();
9919 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9920 {
9921 start_arrow(&curwin->w_cursor);
9922 (void)bck_word(1L, FALSE, FALSE);
9923 curwin->w_set_curswant = TRUE;
9924 }
9925 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009926 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927}
9928
9929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009930ins_right(
9931 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933#ifdef FEAT_FOLDING
9934 if ((fdo_flags & FDO_HOR) && KeyTyped)
9935 foldOpenCursor();
9936#endif
9937 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009938 if (gchar_cursor() != NUL
9939#ifdef FEAT_VIRTUALEDIT
9940 || virtual_active()
9941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 )
9943 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009944 start_arrow_with_change(&curwin->w_cursor, end_change);
9945 if (!end_change)
9946 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 curwin->w_set_curswant = TRUE;
9948#ifdef FEAT_VIRTUALEDIT
9949 if (virtual_active())
9950 oneright();
9951 else
9952#endif
9953 {
9954#ifdef FEAT_MBYTE
9955 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009956 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 else
9958#endif
9959 ++curwin->w_cursor.col;
9960 }
9961
9962#ifdef FEAT_RIGHTLEFT
9963 revins_legal++;
9964 if (revins_chars)
9965 revins_chars--;
9966#endif
9967 }
9968 /* if 'whichwrap' set for cursor in insert mode, may move the
9969 * cursor to the next line */
9970 else if (vim_strchr(p_ww, ']') != NULL
9971 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9972 {
9973 start_arrow(&curwin->w_cursor);
9974 curwin->w_set_curswant = TRUE;
9975 ++curwin->w_cursor.lnum;
9976 curwin->w_cursor.col = 0;
9977 }
9978 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009979 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009980 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981}
9982
9983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009984ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
9986#ifdef FEAT_FOLDING
9987 if ((fdo_flags & FDO_HOR) && KeyTyped)
9988 foldOpenCursor();
9989#endif
9990 undisplay_dollar();
9991 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9992 || gchar_cursor() != NUL)
9993 {
9994 start_arrow(&curwin->w_cursor);
9995 (void)fwd_word(1L, FALSE, 0);
9996 curwin->w_set_curswant = TRUE;
9997 }
9998 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009999 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000}
10001
10002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003ins_up(
10004 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 pos_T tpos;
10007 linenr_T old_topline = curwin->w_topline;
10008#ifdef FEAT_DIFF
10009 int old_topfill = curwin->w_topfill;
10010#endif
10011
10012 undisplay_dollar();
10013 tpos = curwin->w_cursor;
10014 if (cursor_up(1L, TRUE) == OK)
10015 {
10016 if (startcol)
10017 coladvance(getvcol_nolist(&Insstart));
10018 if (old_topline != curwin->w_topline
10019#ifdef FEAT_DIFF
10020 || old_topfill != curwin->w_topfill
10021#endif
10022 )
10023 redraw_later(VALID);
10024 start_arrow(&tpos);
10025#ifdef FEAT_CINDENT
10026 can_cindent = TRUE;
10027#endif
10028 }
10029 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010030 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031}
10032
10033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010034ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
10036 pos_T tpos;
10037
10038 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010039
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010040 if (mod_mask & MOD_MASK_CTRL)
10041 {
10042 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010043 if (first_tabpage->tp_next != NULL)
10044 {
10045 start_arrow(&curwin->w_cursor);
10046 goto_tabpage(-1);
10047 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010048 return;
10049 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010050
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 tpos = curwin->w_cursor;
10052 if (onepage(BACKWARD, 1L) == OK)
10053 {
10054 start_arrow(&tpos);
10055#ifdef FEAT_CINDENT
10056 can_cindent = TRUE;
10057#endif
10058 }
10059 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010060 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010064ins_down(
10065 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
10067 pos_T tpos;
10068 linenr_T old_topline = curwin->w_topline;
10069#ifdef FEAT_DIFF
10070 int old_topfill = curwin->w_topfill;
10071#endif
10072
10073 undisplay_dollar();
10074 tpos = curwin->w_cursor;
10075 if (cursor_down(1L, TRUE) == OK)
10076 {
10077 if (startcol)
10078 coladvance(getvcol_nolist(&Insstart));
10079 if (old_topline != curwin->w_topline
10080#ifdef FEAT_DIFF
10081 || old_topfill != curwin->w_topfill
10082#endif
10083 )
10084 redraw_later(VALID);
10085 start_arrow(&tpos);
10086#ifdef FEAT_CINDENT
10087 can_cindent = TRUE;
10088#endif
10089 }
10090 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010091 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092}
10093
10094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010095ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 pos_T tpos;
10098
10099 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010100
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010101 if (mod_mask & MOD_MASK_CTRL)
10102 {
10103 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010104 if (first_tabpage->tp_next != NULL)
10105 {
10106 start_arrow(&curwin->w_cursor);
10107 goto_tabpage(0);
10108 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010109 return;
10110 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010111
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 tpos = curwin->w_cursor;
10113 if (onepage(FORWARD, 1L) == OK)
10114 {
10115 start_arrow(&tpos);
10116#ifdef FEAT_CINDENT
10117 can_cindent = TRUE;
10118#endif
10119 }
10120 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010121 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122}
10123
10124#ifdef FEAT_DND
10125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10129}
10130#endif
10131
10132/*
10133 * Handle TAB in Insert or Replace mode.
10134 * Return TRUE when the TAB needs to be inserted like a normal character.
10135 */
10136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010137ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138{
10139 int ind;
10140 int i;
10141 int temp;
10142
10143 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10144 Insstart_blank_vcol = get_nolist_virtcol();
10145 if (echeck_abbr(TAB + ABBR_OFF))
10146 return FALSE;
10147
10148 ind = inindent(0);
10149#ifdef FEAT_CINDENT
10150 if (ind)
10151 can_cindent = FALSE;
10152#endif
10153
10154 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010155 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 */
10157 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010158#ifdef FEAT_VARTABS
10159 && !(p_sta && ind
10160 /* These five lines mean 'tabstop' != 'shiftwidth' */
10161 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10162 || (tabstop_count(curbuf->b_p_vts_array) == 1
10163 && tabstop_first(curbuf->b_p_vts_array)
10164 != get_sw_value(curbuf))
10165 || (tabstop_count(curbuf->b_p_vts_array) == 0
10166 && curbuf->b_p_ts != get_sw_value(curbuf))))
10167 && tabstop_count(curbuf->b_p_vsts_array) == 0
10168#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010169 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010170#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010171 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 return TRUE;
10173
10174 if (stop_arrow() == FAIL)
10175 return TRUE;
10176
10177 did_ai = FALSE;
10178#ifdef FEAT_SMARTINDENT
10179 did_si = FALSE;
10180 can_si = FALSE;
10181 can_si_back = FALSE;
10182#endif
10183 AppendToRedobuff((char_u *)"\t");
10184
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010185#ifdef FEAT_VARTABS
10186 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10187 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010188 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010189 temp -= get_nolist_virtcol() % temp;
10190 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010191 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010192 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010193 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010194 curbuf->b_p_vsts_array);
10195 else /* otherwise use 'tabstop' */
10196 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10197 curbuf->b_p_vts_array);
10198#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010200 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010201 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10202 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 else /* otherwise use 'tabstop' */
10204 temp = (int)curbuf->b_p_ts;
10205 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207
10208 /*
10209 * Insert the first space with ins_char(). It will delete one char in
10210 * replace mode. Insert the rest with ins_str(); it will not delete any
10211 * chars. For VREPLACE mode, we use ins_char() for all characters.
10212 */
10213 ins_char(' ');
10214 while (--temp > 0)
10215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 if (State & VREPLACE_FLAG)
10217 ins_char(' ');
10218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 {
10220 ins_str((char_u *)" ");
10221 if (State & REPLACE_FLAG) /* no char replaced */
10222 replace_push(NUL);
10223 }
10224 }
10225
10226 /*
10227 * When 'expandtab' not set: Replace spaces by TABs where possible.
10228 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010229#ifdef FEAT_VARTABS
10230 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10231 || get_sts_value() > 0
10232 || (p_sta && ind)))
10233#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010234 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 {
10237 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 char_u *saved_line = NULL; /* init for GCC */
10239 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 pos_T fpos;
10241 pos_T *cursor;
10242 colnr_T want_vcol, vcol;
10243 int change_col = -1;
10244 int save_list = curwin->w_p_list;
10245
10246 /*
10247 * Get the current line. For VREPLACE mode, don't make real changes
10248 * yet, just work on a copy of the line.
10249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 if (State & VREPLACE_FLAG)
10251 {
10252 pos = curwin->w_cursor;
10253 cursor = &pos;
10254 saved_line = vim_strsave(ml_get_curline());
10255 if (saved_line == NULL)
10256 return FALSE;
10257 ptr = saved_line + pos.col;
10258 }
10259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 {
10261 ptr = ml_get_cursor();
10262 cursor = &curwin->w_cursor;
10263 }
10264
10265 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10266 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10267 curwin->w_p_list = FALSE;
10268
10269 /* Find first white before the cursor */
10270 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010271 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 {
10273 --fpos.col;
10274 --ptr;
10275 }
10276
10277 /* In Replace mode, don't change characters before the insert point. */
10278 if ((State & REPLACE_FLAG)
10279 && fpos.lnum == Insstart.lnum
10280 && fpos.col < Insstart.col)
10281 {
10282 ptr += Insstart.col - fpos.col;
10283 fpos.col = Insstart.col;
10284 }
10285
10286 /* compute virtual column numbers of first white and cursor */
10287 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10288 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10289
Bram Moolenaar597a4222014-06-25 14:39:50 +020010290 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10291 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010292 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010294 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 if (vcol + i > want_vcol)
10296 break;
10297 if (*ptr != TAB)
10298 {
10299 *ptr = TAB;
10300 if (change_col < 0)
10301 {
10302 change_col = fpos.col; /* Column of first change */
10303 /* May have to adjust Insstart */
10304 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10305 Insstart.col = fpos.col;
10306 }
10307 }
10308 ++fpos.col;
10309 ++ptr;
10310 vcol += i;
10311 }
10312
10313 if (change_col >= 0)
10314 {
10315 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010316 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317
10318 /* Skip over the spaces we need. */
10319 while (vcol < want_vcol && *ptr == ' ')
10320 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010321 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 ++ptr;
10323 ++repl_off;
10324 }
10325 if (vcol > want_vcol)
10326 {
10327 /* Must have a char with 'showbreak' just before it. */
10328 --ptr;
10329 --repl_off;
10330 }
10331 fpos.col += repl_off;
10332
10333 /* Delete following spaces. */
10334 i = cursor->col - fpos.col;
10335 if (i > 0)
10336 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010337 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010339 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 for (temp = i; --temp >= 0; )
10341 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010342#ifdef FEAT_TEXT_PROP
10343 curbuf->b_ml.ml_line_len -= i;
10344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010346#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010347 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010348 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010349 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010350 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10351 (char_u *)"\t", 1);
10352 }
10353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 cursor->col -= i;
10355
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 /*
10357 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10358 * backspacing over the changed spacing and then inserting the new
10359 * spacing.
10360 */
10361 if (State & VREPLACE_FLAG)
10362 {
10363 /* Backspace from real cursor to change_col */
10364 backspace_until_column(change_col);
10365
10366 /* Insert each char in saved_line from changed_col to
10367 * ptr-cursor */
10368 ins_bytes_len(saved_line + change_col,
10369 cursor->col - change_col);
10370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 }
10372
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 if (State & VREPLACE_FLAG)
10374 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 curwin->w_p_list = save_list;
10376 }
10377
10378 return FALSE;
10379}
10380
10381/*
10382 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010383 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 */
10385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
10388 int i;
10389
10390 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010391 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010393 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 undisplay_dollar();
10395
10396 /*
10397 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10398 * character under the cursor. Only push a NUL on the replace stack,
10399 * nothing to put back when the NL is deleted.
10400 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010401 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 replace_push(NUL);
10403
10404 /*
10405 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10406 * replacing the next line, so we push all of the characters left on the
10407 * line onto the replace stack. This is not done here though, it is done
10408 * in open_line().
10409 */
10410
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010411#ifdef FEAT_VIRTUALEDIT
10412 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10413 * CTRL-O). */
10414 if (virtual_active() && curwin->w_cursor.coladd > 0)
10415 coladvance(getviscol());
10416#endif
10417
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418#ifdef FEAT_RIGHTLEFT
10419# ifdef FEAT_FKMAP
10420 if (p_altkeymap && p_fkmap)
10421 fkmap(NL);
10422# endif
10423 /* NL in reverse insert will always start in the end of
10424 * current line. */
10425 if (revins_on)
10426 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10427#endif
10428
10429 AppendToRedobuff(NL_STR);
10430 i = open_line(FORWARD,
10431#ifdef FEAT_COMMENTS
10432 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10433#endif
10434 0, old_indent);
10435 old_indent = 0;
10436#ifdef FEAT_CINDENT
10437 can_cindent = TRUE;
10438#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010439#ifdef FEAT_FOLDING
10440 /* When inserting a line the cursor line must never be in a closed fold. */
10441 foldOpenCursor();
10442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010444 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445}
10446
10447#ifdef FEAT_DIGRAPHS
10448/*
10449 * Handle digraph in insert mode.
10450 * Returns character still to be inserted, or NUL when nothing remaining to be
10451 * done.
10452 */
10453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010454ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
10456 int c;
10457 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010458 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
10460 pc_status = PC_STATUS_UNSET;
10461 if (redrawing() && !char_avail())
10462 {
10463 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010464 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465
10466 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010467 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468#ifdef FEAT_CMDL_INFO
10469 add_to_showcmd_c(Ctrl_K);
10470#endif
10471 }
10472
10473#ifdef USE_ON_FLY_SCROLL
10474 dont_scroll = TRUE; /* disallow scrolling here */
10475#endif
10476
10477 /* don't map the digraph chars. This also prevents the
10478 * mode message to be deleted when ESC is hit */
10479 ++no_mapping;
10480 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010481 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 --no_mapping;
10483 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010484 if (did_putchar)
10485 /* when the line fits in 'columns' the '?' is at the start of the next
10486 * line and will not be removed by the redraw */
10487 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010488
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 if (IS_SPECIAL(c) || mod_mask) /* special key */
10490 {
10491#ifdef FEAT_CMDL_INFO
10492 clear_showcmd();
10493#endif
10494 insert_special(c, TRUE, FALSE);
10495 return NUL;
10496 }
10497 if (c != ESC)
10498 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010499 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 if (redrawing() && !char_avail())
10501 {
10502 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010503 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
10505 if (char2cells(c) == 1)
10506 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010507 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010509 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511#ifdef FEAT_CMDL_INFO
10512 add_to_showcmd_c(c);
10513#endif
10514 }
10515 ++no_mapping;
10516 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010517 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 --no_mapping;
10519 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010520 if (did_putchar)
10521 /* when the line fits in 'columns' the '?' is at the start of the
10522 * next line and will not be removed by a redraw */
10523 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (cc != ESC)
10525 {
10526 AppendToRedobuff((char_u *)CTRL_V_STR);
10527 c = getdigraph(c, cc, TRUE);
10528#ifdef FEAT_CMDL_INFO
10529 clear_showcmd();
10530#endif
10531 return c;
10532 }
10533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534#ifdef FEAT_CMDL_INFO
10535 clear_showcmd();
10536#endif
10537 return NUL;
10538}
10539#endif
10540
10541/*
10542 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10543 * Returns the char to be inserted, or NUL if none found.
10544 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548 int c;
10549 int temp;
10550 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010551 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
10553 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10554 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010555 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 return NUL;
10557 }
10558
10559 /* try to advance to the cursor column */
10560 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010561 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 prev_ptr = ptr;
10563 validate_virtcol();
10564 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10565 {
10566 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010567 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569 if ((colnr_T)temp > curwin->w_virtcol)
10570 ptr = prev_ptr;
10571
10572#ifdef FEAT_MBYTE
10573 c = (*mb_ptr2char)(ptr);
10574#else
10575 c = *ptr;
10576#endif
10577 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010578 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 return c;
10580}
10581
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010582/*
10583 * CTRL-Y or CTRL-E typed in Insert mode.
10584 */
10585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010586ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010587{
10588 int c = tc;
10589
10590#ifdef FEAT_INS_EXPAND
10591 if (ctrl_x_mode == CTRL_X_SCROLL)
10592 {
10593 if (c == Ctrl_Y)
10594 scrolldown_clamp();
10595 else
10596 scrollup_clamp();
10597 redraw_later(VALID);
10598 }
10599 else
10600#endif
10601 {
10602 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10603 if (c != NUL)
10604 {
10605 long tw_save;
10606
10607 /* The character must be taken literally, insert like it
10608 * was typed after a CTRL-V, and pretend 'textwidth'
10609 * wasn't set. Digits, 'o' and 'x' are special after a
10610 * CTRL-V, don't use it for these. */
10611 if (c < 256 && !isalnum(c))
10612 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10613 tw_save = curbuf->b_p_tw;
10614 curbuf->b_p_tw = -1;
10615 insert_special(c, TRUE, FALSE);
10616 curbuf->b_p_tw = tw_save;
10617#ifdef FEAT_RIGHTLEFT
10618 revins_chars++;
10619 revins_legal++;
10620#endif
10621 c = Ctrl_V; /* pretend CTRL-V is last character */
10622 auto_format(FALSE, TRUE);
10623 }
10624 }
10625 return c;
10626}
10627
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628#ifdef FEAT_SMARTINDENT
10629/*
10630 * Try to do some very smart auto-indenting.
10631 * Used when inserting a "normal" character.
10632 */
10633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010634ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635{
10636 pos_T *pos, old_pos;
10637 char_u *ptr;
10638 int i;
10639 int temp;
10640
10641 /*
10642 * do some very smart indenting when entering '{' or '}'
10643 */
10644 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10645 {
10646 /*
10647 * for '}' set indent equal to indent of line containing matching '{'
10648 */
10649 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10650 {
10651 old_pos = curwin->w_cursor;
10652 /*
10653 * If the matching '{' has a ')' immediately before it (ignoring
10654 * white-space), then line up with the start of the line
10655 * containing the matching '(' if there is one. This handles the
10656 * case where an "if (..\n..) {" statement continues over multiple
10657 * lines -- webb
10658 */
10659 ptr = ml_get(pos->lnum);
10660 i = pos->col;
10661 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010662 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 ;
10664 curwin->w_cursor.lnum = pos->lnum;
10665 curwin->w_cursor.col = i;
10666 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10667 curwin->w_cursor = *pos;
10668 i = get_indent();
10669 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010671 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 (void)set_indent(i, SIN_CHANGED);
10674 }
10675 else if (curwin->w_cursor.col > 0)
10676 {
10677 /*
10678 * when inserting '{' after "O" reduce indent, but not
10679 * more than indent of previous line
10680 */
10681 temp = TRUE;
10682 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10683 {
10684 old_pos = curwin->w_cursor;
10685 i = get_indent();
10686 while (curwin->w_cursor.lnum > 1)
10687 {
10688 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10689
10690 /* ignore empty lines and lines starting with '#'. */
10691 if (*ptr != '#' && *ptr != NUL)
10692 break;
10693 }
10694 if (get_indent() >= i)
10695 temp = FALSE;
10696 curwin->w_cursor = old_pos;
10697 }
10698 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010699 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 }
10701 }
10702
10703 /*
10704 * set indent of '#' always to 0
10705 */
10706 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10707 {
10708 /* remember current indent for next line */
10709 old_indent = get_indent();
10710 (void)set_indent(0, SIN_CHANGED);
10711 }
10712
10713 /* Adjust ai_col, the char at this position can be deleted. */
10714 if (ai_col > curwin->w_cursor.col)
10715 ai_col = curwin->w_cursor.col;
10716}
10717#endif
10718
10719/*
10720 * Get the value that w_virtcol would have when 'list' is off.
10721 * Unless 'cpo' contains the 'L' flag.
10722 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010723 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010724get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010726 // check validity of cursor in current buffer
10727 if (curwin->w_buffer == NULL
10728 || curwin->w_buffer->b_ml.ml_mfp == NULL
10729 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10730 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10732 return getvcol_nolist(&curwin->w_cursor);
10733 validate_virtcol();
10734 return curwin->w_virtcol;
10735}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010736
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010737#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010738/*
10739 * Handle the InsertCharPre autocommand.
10740 * "c" is the character that was typed.
10741 * Return a pointer to allocated memory with the replacement string.
10742 * Return NULL to continue inserting "c".
10743 */
10744 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010745do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010746{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010747 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010748 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010749 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010750
10751 /* Return quickly when there is nothing to do. */
10752 if (!has_insertcharpre())
10753 return NULL;
10754
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010755# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010756 if (has_mbyte)
10757 buf[(*mb_char2bytes)(c, buf)] = NUL;
10758 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010759# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010760 {
10761 buf[0] = c;
10762 buf[1] = NUL;
10763 }
10764
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010765 /* Lock the text to avoid weird things from happening. */
10766 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010767 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010768
Bram Moolenaar704984a2012-06-01 14:57:51 +020010769 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010770 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010771 {
10772 /* Get the value of v:char. It may be empty or more than one
10773 * character. Only use it when changed, otherwise continue with the
10774 * original character to avoid breaking autoindent. */
10775 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10776 res = vim_strsave(get_vim_var_str(VV_CHAR));
10777 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010778
10779 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10780 --textlock;
10781
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010782 // Restore the State, it may have been changed.
10783 State = save_State;
10784
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010785 return res;
10786}
10787#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010788
10789/*
10790 * Trigger "event" and take care of fixing undo.
10791 */
10792 static int
10793ins_apply_autocmds(event_T event)
10794{
10795 varnumber_T tick = CHANGEDTICK(curbuf);
10796 int r;
10797
10798 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10799
10800 // If u_savesub() was called then we are not prepared to start
10801 // a new line. Call u_save() with no contents to fix that.
10802 if (tick != CHANGEDTICK(curbuf))
10803 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10804
10805 return r;
10806}