blob: 852060bd26b6e1d8f5b62de6cd133068e3a5b06c [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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100363 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100371 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000372 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100379 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000380 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();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100479 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 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
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001748#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001749 if ((conceal_update_lines
1750 && (conceal_old_cursor_line != conceal_new_cursor_line
1751 || conceal_cursor_line(curwin)))
1752 || need_cursor_line_redraw)
1753 {
1754 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001755 redrawWinline(curwin, conceal_old_cursor_line);
1756 redrawWinline(curwin, conceal_new_cursor_line == 0
1757 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001758 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001759 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001760 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001761#endif
1762 if (must_redraw)
1763 update_screen(0);
1764 else if (clear_cmdline || redraw_cmdline)
1765 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001766 showruler(FALSE);
1767 setcursor();
1768 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1773 */
1774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001778 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001781 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001786 did_putchar = TRUE;
1787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1789
1790#ifdef FEAT_CMDL_INFO
1791 add_to_showcmd_c(Ctrl_V);
1792#endif
1793
1794 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001795 if (did_putchar)
1796 /* when the line fits in 'columns' the '^' is at the start of the next
1797 * line and will not removed by the redraw */
1798 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef FEAT_CMDL_INFO
1800 clear_showcmd();
1801#endif
1802 insert_special(c, FALSE, TRUE);
1803#ifdef FEAT_RIGHTLEFT
1804 revins_chars++;
1805 revins_legal++;
1806#endif
1807}
1808
1809/*
1810 * Put a character directly onto the screen. It's not stored in a buffer.
1811 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1812 */
1813static int pc_status;
1814#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1815#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1816#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1817#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819static int pc_attr;
1820static int pc_row;
1821static int pc_col;
1822
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 int attr;
1827
1828 if (ScreenLines != NULL)
1829 {
1830 update_topline(); /* just in case w_topline isn't valid */
1831 validate_cursor();
1832 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001833 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 attr = 0;
1836 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001837 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1839 pc_status = PC_STATUS_UNSET;
1840#endif
1841#ifdef FEAT_RIGHTLEFT
1842 if (curwin->w_p_rl)
1843 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001844 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845# ifdef FEAT_MBYTE
1846 if (has_mbyte)
1847 {
1848 int fix_col = mb_fix_col(pc_col, pc_row);
1849
1850 if (fix_col != pc_col)
1851 {
1852 screen_putchar(' ', pc_row, fix_col, attr);
1853 --curwin->w_wcol;
1854 pc_status = PC_STATUS_RIGHT;
1855 }
1856 }
1857# endif
1858 }
1859 else
1860#endif
1861 {
1862 pc_col += curwin->w_wcol;
1863#ifdef FEAT_MBYTE
1864 if (mb_lefthalve(pc_row, pc_col))
1865 pc_status = PC_STATUS_LEFT;
1866#endif
1867 }
1868
1869 /* save the character to be able to put it back */
1870#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1871 if (pc_status == PC_STATUS_UNSET)
1872#endif
1873 {
1874 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1875 pc_status = PC_STATUS_SET;
1876 }
1877 screen_putchar(c, pc_row, pc_col, attr);
1878 }
1879}
1880
Bram Moolenaarf2732452018-06-03 14:47:35 +02001881#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1882/*
1883 * Return the effective prompt for the current buffer.
1884 */
1885 char_u *
1886prompt_text(void)
1887{
1888 if (curbuf->b_prompt_text == NULL)
1889 return (char_u *)"% ";
1890 return curbuf->b_prompt_text;
1891}
1892
1893/*
1894 * Prepare for prompt mode: Make sure the last line has the prompt text.
1895 * Move the cursor to this line.
1896 */
1897 static void
1898init_prompt(int cmdchar_todo)
1899{
1900 char_u *prompt = prompt_text();
1901 char_u *text;
1902
1903 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1904 text = ml_get_curline();
1905 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1906 {
1907 // prompt is missing, insert it or append a line with it
1908 if (*text == NUL)
1909 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1910 else
1911 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1912 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1913 coladvance((colnr_T)MAXCOL);
1914 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1915 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001916
1917 // Insert always starts after the prompt, allow editing text after it.
1918 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1919 || Insstart_orig.col != (int)STRLEN(prompt))
1920 {
1921 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001922 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001923 Insstart_orig = Insstart;
1924 Insstart_textlen = Insstart.col;
1925 Insstart_blank_vcol = MAXCOL;
1926 arrow_used = FALSE;
1927 }
1928
Bram Moolenaarf2732452018-06-03 14:47:35 +02001929 if (cmdchar_todo == 'A')
1930 coladvance((colnr_T)MAXCOL);
1931 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001932 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001933 /* Make sure the cursor is in a valid position. */
1934 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001935}
1936
1937/*
1938 * Return TRUE if the cursor is in the editable position of the prompt line.
1939 */
1940 int
1941prompt_curpos_editable()
1942{
1943 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1944 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1945}
1946#endif
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948/*
1949 * Undo the previous edit_putchar().
1950 */
1951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001952edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1955 {
1956#if defined(FEAT_MBYTE)
1957 if (pc_status == PC_STATUS_RIGHT)
1958 ++curwin->w_wcol;
1959 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001960 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 else
1962#endif
1963 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1964 }
1965}
1966
1967/*
1968 * Called when p_dollar is set: display a '$' at the end of the changed text
1969 * Only works when cursor is in the line that changes.
1970 */
1971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 colnr_T save_col;
1975
1976 if (!redrawing())
1977 return;
1978
1979 cursor_off();
1980 save_col = curwin->w_cursor.col;
1981 curwin->w_cursor.col = col;
1982#ifdef FEAT_MBYTE
1983 if (has_mbyte)
1984 {
1985 char_u *p;
1986
1987 /* If on the last byte of a multi-byte move to the first byte. */
1988 p = ml_get_curline();
1989 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1990 }
1991#endif
1992 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001993 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
1995 edit_putchar('$', FALSE);
1996 dollar_vcol = curwin->w_virtcol;
1997 }
1998 curwin->w_cursor.col = save_col;
1999}
2000
2001/*
2002 * Call this function before moving the cursor from the normal insert position
2003 * in insert mode.
2004 */
2005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002006undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002008 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002010 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002011 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013}
2014
2015/*
2016 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2017 * Keep the cursor on the same character.
2018 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2019 * type == INDENT_DEC decrease indent (for CTRL-D)
2020 * type == INDENT_SET set indent to "amount"
2021 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2022 */
2023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024change_indent(
2025 int type,
2026 int amount,
2027 int round,
2028 int replaced, /* replaced character, put on replace stack */
2029 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030{
2031 int vcol;
2032 int last_vcol;
2033 int insstart_less; /* reduction for Insstart.col */
2034 int new_cursor_col;
2035 int i;
2036 char_u *ptr;
2037 int save_p_list;
2038 int start_col;
2039 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 colnr_T orig_col = 0; /* init for GCC */
2041 char_u *new_line, *orig_line = NULL; /* init for GCC */
2042
2043 /* VREPLACE mode needs to know what the line was like before changing */
2044 if (State & VREPLACE_FLAG)
2045 {
2046 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2047 orig_col = curwin->w_cursor.col;
2048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /* for the following tricks we don't want list mode */
2051 save_p_list = curwin->w_p_list;
2052 curwin->w_p_list = FALSE;
2053 vc = getvcol_nolist(&curwin->w_cursor);
2054 vcol = vc;
2055
2056 /*
2057 * For Replace mode we need to fix the replace stack later, which is only
2058 * possible when the cursor is in the indent. Remember the number of
2059 * characters before the cursor if it's possible.
2060 */
2061 start_col = curwin->w_cursor.col;
2062
2063 /* determine offset from first non-blank */
2064 new_cursor_col = curwin->w_cursor.col;
2065 beginline(BL_WHITE);
2066 new_cursor_col -= curwin->w_cursor.col;
2067
2068 insstart_less = curwin->w_cursor.col;
2069
2070 /*
2071 * If the cursor is in the indent, compute how many screen columns the
2072 * cursor is to the left of the first non-blank.
2073 */
2074 if (new_cursor_col < 0)
2075 vcol = get_indent() - vcol;
2076
2077 if (new_cursor_col > 0) /* can't fix replace stack */
2078 start_col = -1;
2079
2080 /*
2081 * Set the new indent. The cursor will be put on the first non-blank.
2082 */
2083 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002084 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 else
2086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 int save_State = State;
2088
2089 /* Avoid being called recursively. */
2090 if (State & VREPLACE_FLAG)
2091 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002092 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 insstart_less -= curwin->w_cursor.col;
2096
2097 /*
2098 * Try to put cursor on same character.
2099 * If the cursor is at or after the first non-blank in the line,
2100 * compute the cursor column relative to the column of the first
2101 * non-blank character.
2102 * If we are not in insert mode, leave the cursor on the first non-blank.
2103 * If the cursor is before the first non-blank, position it relative
2104 * to the first non-blank, counted in screen columns.
2105 */
2106 if (new_cursor_col >= 0)
2107 {
2108 /*
2109 * When changing the indent while the cursor is touching it, reset
2110 * Insstart_col to 0.
2111 */
2112 if (new_cursor_col == 0)
2113 insstart_less = MAXCOL;
2114 new_cursor_col += curwin->w_cursor.col;
2115 }
2116 else if (!(State & INSERT))
2117 new_cursor_col = curwin->w_cursor.col;
2118 else
2119 {
2120 /*
2121 * Compute the screen column where the cursor should be.
2122 */
2123 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002124 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
2126 /*
2127 * Advance the cursor until we reach the right screen column.
2128 */
2129 vcol = last_vcol = 0;
2130 new_cursor_col = -1;
2131 ptr = ml_get_curline();
2132 while (vcol <= (int)curwin->w_virtcol)
2133 {
2134 last_vcol = vcol;
2135#ifdef FEAT_MBYTE
2136 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
2139#endif
2140 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002141 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 vcol = last_vcol;
2144
2145 /*
2146 * May need to insert spaces to be able to position the cursor on
2147 * the right screen column.
2148 */
2149 if (vcol != (int)curwin->w_virtcol)
2150 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002151 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002153 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (ptr != NULL)
2155 {
2156 new_cursor_col += i;
2157 ptr[i] = NUL;
2158 while (--i >= 0)
2159 ptr[i] = ' ';
2160 ins_str(ptr);
2161 vim_free(ptr);
2162 }
2163 }
2164
2165 /*
2166 * When changing the indent while the cursor is in it, reset
2167 * Insstart_col to 0.
2168 */
2169 insstart_less = MAXCOL;
2170 }
2171
2172 curwin->w_p_list = save_p_list;
2173
2174 if (new_cursor_col <= 0)
2175 curwin->w_cursor.col = 0;
2176 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002177 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 curwin->w_set_curswant = TRUE;
2179 changed_cline_bef_curs();
2180
2181 /*
2182 * May have to adjust the start of the insert.
2183 */
2184 if (State & INSERT)
2185 {
2186 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2187 {
2188 if ((int)Insstart.col <= insstart_less)
2189 Insstart.col = 0;
2190 else
2191 Insstart.col -= insstart_less;
2192 }
2193 if ((int)ai_col <= insstart_less)
2194 ai_col = 0;
2195 else
2196 ai_col -= insstart_less;
2197 }
2198
2199 /*
2200 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2201 * If the number of characters before the cursor decreased, need to pop a
2202 * few characters from the replace stack.
2203 * If the number of characters before the cursor increased, need to push a
2204 * few NULs onto the replace stack.
2205 */
2206 if (REPLACE_NORMAL(State) && start_col >= 0)
2207 {
2208 while (start_col > (int)curwin->w_cursor.col)
2209 {
2210 replace_join(0); /* remove a NUL from the replace stack */
2211 --start_col;
2212 }
2213 while (start_col < (int)curwin->w_cursor.col || replaced)
2214 {
2215 replace_push(NUL);
2216 if (replaced)
2217 {
2218 replace_push(replaced);
2219 replaced = NUL;
2220 }
2221 ++start_col;
2222 }
2223 }
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /*
2226 * For VREPLACE mode, we also have to fix the replace stack. In this case
2227 * it is always possible because we backspace over the whole line and then
2228 * put it back again the way we wanted it.
2229 */
2230 if (State & VREPLACE_FLAG)
2231 {
2232 /* If orig_line didn't allocate, just return. At least we did the job,
2233 * even if you can't backspace. */
2234 if (orig_line == NULL)
2235 return;
2236
2237 /* Save new line */
2238 new_line = vim_strsave(ml_get_curline());
2239 if (new_line == NULL)
2240 return;
2241
2242 /* We only put back the new line up to the cursor */
2243 new_line[curwin->w_cursor.col] = NUL;
2244
2245 /* Put back original line */
2246 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2247 curwin->w_cursor.col = orig_col;
2248
2249 /* Backspace from cursor to start of line */
2250 backspace_until_column(0);
2251
2252 /* Insert new stuff into line again */
2253 ins_bytes(new_line);
2254
2255 vim_free(new_line);
2256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257}
2258
2259/*
2260 * Truncate the space at the end of a line. This is to be used only in an
2261 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2262 * modes.
2263 */
2264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 int i;
2268
2269 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002270 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 if (State & REPLACE_FLAG)
2273 replace_join(0); /* remove a NUL from the replace stack */
2274 }
2275 line[i + 1] = NUL;
2276}
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278/*
2279 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2280 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002281 * Will attempt not to go before "col" even when there is a composing
2282 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
2284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002285backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 while ((int)curwin->w_cursor.col > col)
2288 {
2289 curwin->w_cursor.col--;
2290 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002291 replace_do_bs(col);
2292 else if (!del_char_after_col(col))
2293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002297/*
2298 * Like del_char(), but make sure not to go before column "limit_col".
2299 * Only matters when there are composing characters.
2300 * Return TRUE when something was deleted.
2301 */
2302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002303del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002304{
2305#ifdef FEAT_MBYTE
2306 if (enc_utf8 && limit_col >= 0)
2307 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002308 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002309
2310 /* Make sure the cursor is at the start of a character, but
2311 * skip forward again when going too far back because of a
2312 * composing character. */
2313 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002314 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002315 {
2316 int l = utf_ptr2len(ml_get_cursor());
2317
2318 if (l == 0) /* end of line */
2319 break;
2320 curwin->w_cursor.col += l;
2321 }
2322 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2323 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002324 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002325 }
2326 else
2327#endif
2328 (void)del_char(FALSE);
2329 return TRUE;
2330}
2331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2333/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002334 * CTRL-X pressed in Insert mode.
2335 */
2336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002337ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002338{
2339 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2340 * CTRL-V works like CTRL-N */
2341 if (ctrl_x_mode != CTRL_X_CMDLINE)
2342 {
2343 /* if the next ^X<> won't ADD nothing, then reset
2344 * compl_cont_status */
2345 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002346 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002347 else
2348 compl_cont_status = 0;
2349 /* We're not sure which CTRL-X mode it will be yet */
2350 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2351 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2352 edit_submode_pre = NULL;
2353 showmode();
2354 }
2355}
2356
2357/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002358 * Whether other than default completion has been selected.
2359 */
2360 int
2361ctrl_x_mode_not_default(void)
2362{
2363 return ctrl_x_mode != CTRL_X_NORMAL;
2364}
2365
2366/*
2367 * Whether CTRL-X was typed without a following character.
2368 */
2369 int
2370ctrl_x_mode_not_defined_yet(void)
2371{
2372 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2373}
2374
2375/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002376 * Return TRUE if the 'dict' or 'tsr' option can be used.
2377 */
2378 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002379has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002380{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002381 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002382# ifdef FEAT_SPELL
2383 && !curwin->w_p_spell
2384# endif
2385 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002386 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2387 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002388 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002389 edit_submode = NULL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002390 msg_attr(dict_opt ? _("'dictionary' option is empty")
2391 : _("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002392 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002393 if (emsg_silent == 0)
2394 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002395 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002396 setcursor();
2397 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002398#ifdef FEAT_EVAL
2399 if (!get_vim_var_nr(VV_TESTING))
2400#endif
2401 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002402 }
2403 return FALSE;
2404 }
2405 return TRUE;
2406}
2407
2408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2410 * This depends on the current mode.
2411 */
2412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002413vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002415 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (c == Ctrl_R)
2417 return TRUE;
2418
Bram Moolenaare3226be2005-12-18 22:10:00 +00002419 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002420 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002421 return TRUE;
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 switch (ctrl_x_mode)
2424 {
2425 case 0: /* Not in any CTRL-X mode */
2426 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2427 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002428 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2430 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2431 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002432 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002433 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CTRL_X_SCROLL:
2435 return (c == Ctrl_Y || c == Ctrl_E);
2436 case CTRL_X_WHOLE_LINE:
2437 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2438 case CTRL_X_FILES:
2439 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2440 case CTRL_X_DICTIONARY:
2441 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2442 case CTRL_X_THESAURUS:
2443 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2444 case CTRL_X_TAGS:
2445 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2446#ifdef FEAT_FIND_ID
2447 case CTRL_X_PATH_PATTERNS:
2448 return (c == Ctrl_P || c == Ctrl_N);
2449 case CTRL_X_PATH_DEFINES:
2450 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2451#endif
2452 case CTRL_X_CMDLINE:
2453 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2454 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002455#ifdef FEAT_COMPL_FUNC
2456 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002457 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002458 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002459 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002460#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002461 case CTRL_X_SPELL:
2462 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002463 case CTRL_X_EVAL:
2464 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002466 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 return FALSE;
2468}
2469
2470/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002471 * Return TRUE when character "c" is part of the item currently being
2472 * completed. Used to decide whether to abandon complete mode when the menu
2473 * is visible.
2474 */
2475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002476ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002477{
2478 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2479 /* When expanding an identifier only accept identifier chars. */
2480 return vim_isIDc(c);
2481
2482 switch (ctrl_x_mode)
2483 {
2484 case CTRL_X_FILES:
2485 /* When expanding file name only accept file name chars. But not
2486 * path separators, so that "proto/<Tab>" expands files in
2487 * "proto", not "proto/" as a whole */
2488 return vim_isfilec(c) && !vim_ispathsep(c);
2489
2490 case CTRL_X_CMDLINE:
2491 case CTRL_X_OMNI:
2492 /* Command line and Omni completion can work with just about any
2493 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002494 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002495
2496 case CTRL_X_WHOLE_LINE:
2497 /* For while line completion a space can be part of the line. */
2498 return vim_isprintc(c);
2499 }
2500 return vim_iswordc(c);
2501}
2502
2503/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002504 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002506 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 * the rest of the word to be in -- webb
2508 */
2509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510ins_compl_add_infercase(
2511 char_u *str,
2512 int len,
2513 int icase,
2514 char_u *fname,
2515 int dir,
2516 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 char_u *p;
2519 int i, c;
2520 int actual_len; /* Take multi-byte characters */
2521 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002522 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002523 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int has_lower = FALSE;
2525 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002527 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaara2993e12007-08-12 14:38:46 +00002531 /* Find actual length of completion. */
2532#ifdef FEAT_MBYTE
2533 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002535 p = str;
2536 actual_len = 0;
2537 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002539 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002543 else
2544#endif
2545 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaara2993e12007-08-12 14:38:46 +00002547 /* Find actual length of original text. */
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 p = compl_orig_text;
2552 actual_compl_length = 0;
2553 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002555 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002559 else
2560#endif
2561 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002563 /* "actual_len" may be smaller than "actual_compl_length" when using
2564 * thesaurus, only use the minimum when comparing. */
2565 min_len = actual_len < actual_compl_length
2566 ? actual_len : actual_compl_length;
2567
Bram Moolenaara2993e12007-08-12 14:38:46 +00002568 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002569 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002570 if (wca != NULL)
2571 {
2572 p = str;
2573 for (i = 0; i < actual_len; ++i)
2574#ifdef FEAT_MBYTE
2575 if (has_mbyte)
2576 wca[i] = mb_ptr2char_adv(&p);
2577 else
2578#endif
2579 wca[i] = *(p++);
2580
2581 /* Rule 1: Were any chars converted to lower? */
2582 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002583 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002584 {
2585#ifdef FEAT_MBYTE
2586 if (has_mbyte)
2587 c = mb_ptr2char_adv(&p);
2588 else
2589#endif
2590 c = *(p++);
2591 if (MB_ISLOWER(c))
2592 {
2593 has_lower = TRUE;
2594 if (MB_ISUPPER(wca[i]))
2595 {
2596 /* Rule 1 is satisfied. */
2597 for (i = actual_compl_length; i < actual_len; ++i)
2598 wca[i] = MB_TOLOWER(wca[i]);
2599 break;
2600 }
2601 }
2602 }
2603
2604 /*
2605 * Rule 2: No lower case, 2nd consecutive letter converted to
2606 * upper case.
2607 */
2608 if (!has_lower)
2609 {
2610 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002611 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002612 {
2613#ifdef FEAT_MBYTE
2614 if (has_mbyte)
2615 c = mb_ptr2char_adv(&p);
2616 else
2617#endif
2618 c = *(p++);
2619 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2620 {
2621 /* Rule 2 is satisfied. */
2622 for (i = actual_compl_length; i < actual_len; ++i)
2623 wca[i] = MB_TOUPPER(wca[i]);
2624 break;
2625 }
2626 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2627 }
2628 }
2629
2630 /* Copy the original case of the part we typed. */
2631 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002632 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002633 {
2634#ifdef FEAT_MBYTE
2635 if (has_mbyte)
2636 c = mb_ptr2char_adv(&p);
2637 else
2638#endif
2639 c = *(p++);
2640 if (MB_ISLOWER(c))
2641 wca[i] = MB_TOLOWER(wca[i]);
2642 else if (MB_ISUPPER(c))
2643 wca[i] = MB_TOUPPER(wca[i]);
2644 }
2645
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002646 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002647 * Generate encoding specific output from wide character array.
2648 * Multi-byte characters can occupy up to five bytes more than
2649 * ASCII characters, and we also need one byte for NUL, so stay
2650 * six bytes away from the edge of IObuff.
2651 */
2652 p = IObuff;
2653 i = 0;
2654 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2655#ifdef FEAT_MBYTE
2656 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002657 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002658 else
2659#endif
2660 *(p++) = wca[i++];
2661 *p = NUL;
2662
2663 vim_free(wca);
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002666 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2667 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002669 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670}
2671
2672/*
2673 * Add a match to the list of matches.
2674 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002675 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002676 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002679ins_compl_add(
2680 char_u *str,
2681 int len,
2682 int icase,
2683 char_u *fname,
2684 char_u **cptext, /* extra text for popup menu or NULL */
2685 int cdir,
2686 int flags,
2687 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002689 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002690 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 ui_breakcheck();
2693 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (len < 0)
2696 len = (int)STRLEN(str);
2697
2698 /*
2699 * If the same match is already present, don't add it.
2700 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002701 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002703 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 do
2705 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002706 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002707 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002708 && match->cp_str[len] == NUL)
2709 return NOTDONE;
2710 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002711 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002714 /* Remove any popup menu before changing the list of matches. */
2715 ins_compl_del_pum();
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /*
2718 * Allocate a new match structure.
2719 * Copy the values to the new match structure.
2720 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002721 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002723 return FAIL;
2724 match->cp_number = -1;
2725 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002726 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002727 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002732 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2737 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002738 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002739 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002740 && compl_curr_match->cp_fname != NULL
2741 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002743 else if (fname != NULL)
2744 {
2745 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002746 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002749 match->cp_fname = NULL;
2750 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002751
2752 if (cptext != NULL)
2753 {
2754 int i;
2755
2756 for (i = 0; i < CPT_COUNT; ++i)
2757 if (cptext[i] != NULL && *cptext[i] != NUL)
2758 match->cp_text[i] = vim_strsave(cptext[i]);
2759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * Link the new match structure in the list of matches.
2763 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002764 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002765 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else if (dir == FORWARD)
2767 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002768 match->cp_next = compl_curr_match->cp_next;
2769 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else /* BACKWARD */
2772 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002773 match->cp_next = compl_curr_match;
2774 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002776 if (match->cp_next)
2777 match->cp_next->cp_prev = match;
2778 if (match->cp_prev)
2779 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002781 compl_first_match = match;
2782 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002784 /*
2785 * Find the longest common string if still doing that.
2786 */
2787 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2788 ins_compl_longest_match(match);
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 return OK;
2791}
2792
2793/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002794 * Return TRUE if "str[len]" matches with match->cp_str, considering
2795 * match->cp_icase.
2796 */
2797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002798ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002799{
2800 if (match->cp_icase)
2801 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2802 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2803}
2804
2805/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002806 * Reduce the longest common string for match "match".
2807 */
2808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002809ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002810{
2811 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002813 int had_match;
2814
2815 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002816 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002817 /* First match, use it as a whole. */
2818 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (compl_leader != NULL)
2820 {
2821 had_match = (curwin->w_cursor.col > compl_col);
2822 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002823 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002824 ins_redraw(FALSE);
2825
2826 /* When the match isn't there (to avoid matching itself) remove it
2827 * again after redrawing. */
2828 if (!had_match)
2829 ins_compl_delete();
2830 compl_used_match = FALSE;
2831 }
2832 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002833 else
2834 {
2835 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002836 p = compl_leader;
2837 s = match->cp_str;
2838 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002839 {
2840#ifdef FEAT_MBYTE
2841 if (has_mbyte)
2842 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002843 c1 = mb_ptr2char(p);
2844 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002845 }
2846 else
2847#endif
2848 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002849 c1 = *p;
2850 c2 = *s;
2851 }
2852 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2853 : (c1 != c2))
2854 break;
2855#ifdef FEAT_MBYTE
2856 if (has_mbyte)
2857 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002858 MB_PTR_ADV(p);
2859 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002860 }
2861 else
2862#endif
2863 {
2864 ++p;
2865 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002866 }
2867 }
2868
2869 if (*p != NUL)
2870 {
2871 /* Leader was shortened, need to change the inserted text. */
2872 *p = NUL;
2873 had_match = (curwin->w_cursor.col > compl_col);
2874 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002875 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002876 ins_redraw(FALSE);
2877
2878 /* When the match isn't there (to avoid matching itself) remove it
2879 * again after redrawing. */
2880 if (!had_match)
2881 ins_compl_delete();
2882 }
2883
2884 compl_used_match = FALSE;
2885 }
2886}
2887
2888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * Add an array of matches to the list of matches.
2890 * Frees matches[].
2891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893ins_compl_add_matches(
2894 int num_matches,
2895 char_u **matches,
2896 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 int i;
2899 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002900 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
Bram Moolenaar572cb562005-08-05 21:35:02 +00002902 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002903 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002904 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002906 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 FreeWild(num_matches, matches);
2908}
2909
2910/* Make the completion list cyclic.
2911 * Return the number of matches (excluding the original).
2912 */
2913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002916 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 int count = 0;
2918
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002919 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 /*
2922 * Find the end of the list.
2923 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002924 match = compl_first_match;
2925 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002926 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002928 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 ++count;
2930 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002931 match->cp_next = compl_first_match;
2932 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 return count;
2935}
2936
Bram Moolenaara94bc432006-03-10 21:42:59 +00002937/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002938 * Set variables that store noselect and noinsert behavior from the
2939 * 'completeopt' value.
2940 */
2941 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002942completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002943{
2944 compl_no_insert = FALSE;
2945 compl_no_select = FALSE;
2946 if (strstr((char *)p_cot, "noselect") != NULL)
2947 compl_no_select = TRUE;
2948 if (strstr((char *)p_cot, "noinsert") != NULL)
2949 compl_no_insert = TRUE;
2950}
2951
2952/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002953 * Start completion for the complete() function.
2954 * "startcol" is where the matched text starts (1 is first column).
2955 * "list" is the list of matches.
2956 */
2957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002959{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002960 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002961 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002962
Bram Moolenaara94bc432006-03-10 21:42:59 +00002963 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002964 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002965 ins_compl_prep(' ');
2966 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002967 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002968
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002969 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002970 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002971 startcol = curwin->w_cursor.col;
2972 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002973 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002974 /* compl_pattern doesn't need to be set */
2975 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2976 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002977 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002978 return;
2979
Bram Moolenaare4214502015-03-05 18:08:43 +01002980 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002981
2982 ins_compl_add_list(list);
2983 compl_matches = ins_compl_make_cyclic();
2984 compl_started = TRUE;
2985 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002986 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002987
2988 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002989 if (compl_no_insert || compl_no_select)
2990 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002991 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002992 if (compl_no_select)
2993 /* Down/Up has no real effect. */
2994 ins_complete(K_UP, FALSE);
2995 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002996 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002997 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002998 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002999
3000 /* Lazily show the popup menu, unless we got interrupted. */
3001 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003002 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003003 out_flush();
3004}
3005
3006
Bram Moolenaar9372a112005-12-06 19:59:18 +00003007/* "compl_match_array" points the currently displayed list of entries in the
3008 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003009static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003010static int compl_match_arraysize;
3011
3012/*
3013 * Update the screen and when there is any scrolling remove the popup menu.
3014 */
3015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003016ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003017{
3018 int h;
3019
3020 if (compl_match_array != NULL)
3021 {
3022 h = curwin->w_cline_height;
Bram Moolenaarae654382019-01-17 21:09:05 +01003023 // Update the screen later, before drawing the popup menu over it.
3024 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003025 if (h != curwin->w_cline_height)
3026 ins_compl_del_pum();
3027 }
3028}
3029
3030/*
3031 * Remove any popup menu.
3032 */
3033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035{
3036 if (compl_match_array != NULL)
3037 {
3038 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003039 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003040 }
3041}
3042
3043/*
3044 * Return TRUE if the popup menu should be displayed.
3045 */
3046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003047pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003049 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003050 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003051 return FALSE;
3052
3053 /* The display looks bad on a B&W display. */
3054 if (t_colors < 8
3055#ifdef FEAT_GUI
3056 && !gui.in_use
3057#endif
3058 )
3059 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003060 return TRUE;
3061}
3062
3063/*
3064 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003065 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003066 */
3067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003069{
3070 compl_T *compl;
3071 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003072
3073 /* Don't display the popup menu if there are no matches or there is only
3074 * one (ignoring the original text). */
3075 compl = compl_first_match;
3076 i = 0;
3077 do
3078 {
3079 if (compl == NULL
3080 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3081 break;
3082 compl = compl->cp_next;
3083 } while (compl != compl_first_match);
3084
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003085 if (strstr((char *)p_cot, "menuone") != NULL)
3086 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 return (i >= 2);
3088}
3089
3090/*
3091 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003092 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003093 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096{
3097 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003098 compl_T *shown_compl = NULL;
3099 int did_find_shown_match = FALSE;
3100 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003101 int i;
3102 int cur = -1;
3103 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003104 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003105
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003106 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003107 return;
3108
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003109#if defined(FEAT_EVAL)
3110 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3111 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3112#endif
3113
Bram Moolenaarae654382019-01-17 21:09:05 +01003114 // Update the screen later, before drawing the popup menu over it.
3115 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003116
3117 if (compl_match_array == NULL)
3118 {
3119 /* Need to build the popup menu list. */
3120 compl_match_arraysize = 0;
3121 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003122 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003123 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003124 do
3125 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003126 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3127 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003128 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 ++compl_match_arraysize;
3130 compl = compl->cp_next;
3131 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003132 if (compl_match_arraysize == 0)
3133 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003134 compl_match_array = (pumitem_T *)alloc_clear(
3135 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003136 * compl_match_arraysize));
3137 if (compl_match_array != NULL)
3138 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003139 /* If the current match is the original text don't find the first
3140 * match after it, don't highlight anything. */
3141 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3142 shown_match_ok = TRUE;
3143
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003144 i = 0;
3145 compl = compl_first_match;
3146 do
3147 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003148 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3149 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003150 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 if (!shown_match_ok)
3153 {
3154 if (compl == compl_shown_match || did_find_shown_match)
3155 {
3156 /* This item is the shown match or this is the
3157 * first displayed item after the shown match. */
3158 compl_shown_match = compl;
3159 did_find_shown_match = TRUE;
3160 shown_match_ok = TRUE;
3161 }
3162 else
3163 /* Remember this displayed match for when the
3164 * shown match is just below it. */
3165 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003166 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003167 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003168
3169 if (compl->cp_text[CPT_ABBR] != NULL)
3170 compl_match_array[i].pum_text =
3171 compl->cp_text[CPT_ABBR];
3172 else
3173 compl_match_array[i].pum_text = compl->cp_str;
3174 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3175 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3176 if (compl->cp_text[CPT_MENU] != NULL)
3177 compl_match_array[i++].pum_extra =
3178 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003179 else
3180 compl_match_array[i++].pum_extra = compl->cp_fname;
3181 }
3182
3183 if (compl == compl_shown_match)
3184 {
3185 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003186
3187 /* When the original text is the shown match don't set
3188 * compl_shown_match. */
3189 if (compl->cp_flags & ORIGINAL_TEXT)
3190 shown_match_ok = TRUE;
3191
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003192 if (!shown_match_ok && shown_compl != NULL)
3193 {
3194 /* The shown match isn't displayed, set it to the
3195 * previously displayed match. */
3196 compl_shown_match = shown_compl;
3197 shown_match_ok = TRUE;
3198 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003199 }
3200 compl = compl->cp_next;
3201 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003202
3203 if (!shown_match_ok) /* no displayed match at all */
3204 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003205 }
3206 }
3207 else
3208 {
3209 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003210 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003211 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3212 || compl_match_array[i].pum_text
3213 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003214 {
3215 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003216 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003217 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 }
3219
3220 if (compl_match_array != NULL)
3221 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003222 /* In Replace mode when a $ is displayed at the end of the line only
3223 * part of the screen would be updated. We do need to redraw here. */
3224 dollar_vcol = -1;
3225
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 /* Compute the screen column of the start of the completed text.
3227 * Use the cursor to get all wrapping and other settings right. */
3228 col = curwin->w_cursor.col;
3229 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003230 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003231 curwin->w_cursor.col = col;
3232 }
3233}
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235#define DICT_FIRST (1) /* use just first element in "dict" */
3236#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239 * Add any identifiers that match the given pattern in the list of dictionary
3240 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 */
3242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243ins_compl_dictionaries(
3244 char_u *dict_start,
3245 char_u *pat,
3246 int flags, /* DICT_FIRST and/or DICT_EXACT */
3247 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003249 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 char_u *ptr;
3251 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u **files;
3254 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003256 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 if (*dict == NUL)
3259 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003260#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003261 /* When 'dictionary' is empty and spell checking is enabled use
3262 * "spell". */
3263 if (!thesaurus && curwin->w_p_spell)
3264 dict = (char_u *)"spell";
3265 else
3266#endif
3267 return;
3268 }
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 if (buf == NULL)
3272 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003273 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /* If 'infercase' is set, don't use 'smartcase' here */
3276 save_p_scs = p_scs;
3277 if (curbuf->b_p_inf)
3278 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279
3280 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3281 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003283 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003285 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003286 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003287
3288 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003289 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003290 len = STRLEN(pat_esc) + 10;
3291 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 {
3294 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003295 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003296 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003297 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003298 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3299 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003300 vim_free(ptr);
3301 }
3302 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003304 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003305 if (regmatch.regprog == NULL)
3306 goto theend;
3307 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3310 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 /* copy one dictionary file name into buf */
3314 if (flags == DICT_EXACT)
3315 {
3316 count = 1;
3317 files = &dict;
3318 }
3319 else
3320 {
3321 /* Expand wildcards in the dictionary name, but do not allow
3322 * backticks (for security, the 'dict' option may have been set in
3323 * a modeline). */
3324 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003325# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 if (!thesaurus && STRCMP(buf, "spell") == 0)
3327 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003328 else
3329# endif
3330 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 || expand_wildcards(1, &buf, &count, &files,
3332 EW_FILE|EW_SILENT) != OK)
3333 count = 0;
3334 }
3335
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003336# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003337 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003339 /* Complete from active spelling. Skip "\<" in the pattern, we
3340 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003341 if (pat[0] == '\\' && pat[1] == '<')
3342 ptr = pat + 2;
3343 else
3344 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003345 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003347 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003348# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003349 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003350 {
3351 ins_compl_files(count, files, thesaurus, flags,
3352 &regmatch, buf, &dir);
3353 if (flags != DICT_EXACT)
3354 FreeWild(count, files);
3355 }
3356 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 break;
3358 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003359
3360theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003362 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 vim_free(buf);
3364}
3365
Bram Moolenaar0b238792006-03-02 22:49:12 +00003366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367ins_compl_files(
3368 int count,
3369 char_u **files,
3370 int thesaurus,
3371 int flags,
3372 regmatch_T *regmatch,
3373 char_u *buf,
3374 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003375{
3376 char_u *ptr;
3377 int i;
3378 FILE *fp;
3379 int add_r;
3380
3381 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3382 {
3383 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3384 if (flags != DICT_EXACT)
3385 {
3386 vim_snprintf((char *)IObuff, IOSIZE,
3387 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003388 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003389 }
3390
3391 if (fp != NULL)
3392 {
3393 /*
3394 * Read dictionary file line by line.
3395 * Check each line for a match.
3396 */
3397 while (!got_int && !compl_interrupted
3398 && !vim_fgets(buf, LSIZE, fp))
3399 {
3400 ptr = buf;
3401 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3402 {
3403 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003404 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003405 ptr = find_line_end(ptr);
3406 else
3407 ptr = find_word_end(ptr);
3408 add_r = ins_compl_add_infercase(regmatch->startp[0],
3409 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003410 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003411 if (thesaurus)
3412 {
3413 char_u *wstart;
3414
3415 /*
3416 * Add the other matches on the line
3417 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003418 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003419 while (!got_int)
3420 {
3421 /* Find start of the next word. Skip white
3422 * space and punctuation. */
3423 ptr = find_word_start(ptr);
3424 if (*ptr == NUL || *ptr == NL)
3425 break;
3426 wstart = ptr;
3427
Bram Moolenaara2993e12007-08-12 14:38:46 +00003428 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
3431 /* Japanese words may have characters in
3432 * different classes, only separate words
3433 * with single-byte non-word characters. */
3434 while (*ptr != NUL)
3435 {
3436 int l = (*mb_ptr2len)(ptr);
3437
3438 if (l < 2 && !vim_iswordc(*ptr))
3439 break;
3440 ptr += l;
3441 }
3442 else
3443#endif
3444 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003445
3446 /* Add the word. Skip the regexp match. */
3447 if (wstart != regmatch->startp[0])
3448 add_r = ins_compl_add_infercase(wstart,
3449 (int)(ptr - wstart),
3450 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003451 }
3452 }
3453 if (add_r == OK)
3454 /* if dir was BACKWARD then honor it just once */
3455 *dir = FORWARD;
3456 else if (add_r == FAIL)
3457 break;
3458 /* avoid expensive call to vim_regexec() when at end
3459 * of line */
3460 if (*ptr == '\n' || got_int)
3461 break;
3462 }
3463 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003464 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003465 }
3466 fclose(fp);
3467 }
3468 }
3469}
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471/*
3472 * Find the start of the next word.
3473 * Returns a pointer to the first char of the word. Also stops at a NUL.
3474 */
3475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478#ifdef FEAT_MBYTE
3479 if (has_mbyte)
3480 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003481 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else
3483#endif
3484 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3485 ++ptr;
3486 return ptr;
3487}
3488
3489/*
3490 * Find the end of the word. Assumes it starts inside a word.
3491 * Returns a pointer to just after the word.
3492 */
3493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496#ifdef FEAT_MBYTE
3497 int start_class;
3498
3499 if (has_mbyte)
3500 {
3501 start_class = mb_get_class(ptr);
3502 if (start_class > 1)
3503 while (*ptr != NUL)
3504 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003505 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (mb_get_class(ptr) != start_class)
3507 break;
3508 }
3509 }
3510 else
3511#endif
3512 while (vim_iswordc(*ptr))
3513 ++ptr;
3514 return ptr;
3515}
3516
3517/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518 * Find the end of the line, omitting CR and NL at the end.
3519 * Returns a pointer to just after the line.
3520 */
3521 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003522find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003523{
3524 char_u *s;
3525
3526 s = ptr + STRLEN(ptr);
3527 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3528 --s;
3529 return s;
3530}
3531
3532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Free the list of completions
3534 */
3535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003538 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003539 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaard23a8232018-02-10 18:45:26 +01003541 VIM_CLEAR(compl_pattern);
3542 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003546
3547 ins_compl_del_pum();
3548 pum_clear();
3549
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 do
3552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003554 compl_curr_match = compl_curr_match->cp_next;
3555 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003557 if (match->cp_flags & FREE_FNAME)
3558 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003559 for (i = 0; i < CPT_COUNT; ++i)
3560 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3563 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003564 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003565 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003569ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 compl_cont_status = 0;
3572 compl_started = FALSE;
3573 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003574 VIM_CLEAR(compl_pattern);
3575 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003577 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003578 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003579 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003580 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003584 * Return TRUE when Insert completion is active.
3585 */
3586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003588{
3589 return compl_started;
3590}
3591
3592/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003593 * Delete one character before the cursor and show the subset of the matches
3594 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003595 * Returns the character to be used, NUL if the work is done and another char
3596 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003597 */
3598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003600{
3601 char_u *line;
3602 char_u *p;
3603
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003604 line = ml_get_curline();
3605 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003606 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003607
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003608 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003609 * allow the word to be deleted, we won't match everything.
3610 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003611 if ((int)(p - line) - (int)compl_col < 0
3612 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003613 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3614 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3615 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003616 return K_BS;
3617
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003618 /* Deleted more than what was used to find matches or didn't finish
3619 * finding all matches: need to look for matches all over again. */
3620 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003621 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003622 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003623
Bram Moolenaara6557602006-02-04 22:43:20 +00003624 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003625 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003626 if (compl_leader != NULL)
3627 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003628 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003629 if (compl_shown_match != NULL)
3630 /* Make sure current match is not a hidden item. */
3631 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003632 return NUL;
3633 }
3634 return K_BS;
3635}
Bram Moolenaara6557602006-02-04 22:43:20 +00003636
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003637/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003638 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3639 * be called.
3640 */
3641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003643{
3644 /* Return TRUE if we didn't complete finding matches or when the
3645 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3646 return compl_was_interrupted
3647 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3648 && compl_opt_refresh_always);
3649}
3650
3651/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003652 * Called after changing "compl_leader".
3653 * Show the popup menu with a different set of matches.
3654 * May also search for matches again if the previous search was interrupted.
3655 */
3656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003658{
3659 ins_compl_del_pum();
3660 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003661 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003662 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003663
3664 if (compl_started)
3665 ins_compl_set_original_text(compl_leader);
3666 else
3667 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003668#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003669 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003670#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003671 /*
Bram Moolenaarae654382019-01-17 21:09:05 +01003672 * Matches were cleared, need to search for them now. Befor drawing
3673 * the popup menu display the changed text before the cursor. Set
3674 * "compl_restarting" to avoid that the first match is inserted.
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003675 */
Bram Moolenaarae654382019-01-17 21:09:05 +01003676 pum_call_update_screen();
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003677#ifdef FEAT_GUI
3678 if (gui.in_use)
3679 {
3680 /* Show the cursor after the match, not after the redrawn text. */
3681 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003682 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003683 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684#endif
3685 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003686 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003687 compl_cont_status = 0;
3688 compl_restarting = FALSE;
3689 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003690
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003691 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003692
3693 /* Show the popup menu with a different set of matches. */
3694 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003695
3696 /* Don't let Enter select the original text when there is no popup menu. */
3697 if (compl_match_array == NULL)
3698 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003699}
3700
3701/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003702 * Return the length of the completion, from the completion start column to
3703 * the cursor column. Making sure it never goes below zero.
3704 */
3705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003707{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003708 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003709
3710 if (off < 0)
3711 return 0;
3712 return off;
3713}
3714
3715/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003716 * Append one character to the match leader. May reduce the number of
3717 * matches.
3718 */
3719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003721{
3722#ifdef FEAT_MBYTE
3723 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003724#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003725
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003726 if (stop_arrow() == FAIL)
3727 return;
3728#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003729 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3730 {
3731 char_u buf[MB_MAXBYTES + 1];
3732
3733 (*mb_char2bytes)(c, buf);
3734 buf[cc] = NUL;
3735 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003736 if (compl_opt_refresh_always)
3737 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003738 }
3739 else
3740#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003741 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003742 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003743 if (compl_opt_refresh_always)
3744 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003745 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003746
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003747 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003748 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003749 ins_compl_restart();
3750
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003751 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003752 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003753 * break redo. */
3754 if (!compl_opt_refresh_always)
3755 {
3756 vim_free(compl_leader);
3757 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003758 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003759 if (compl_leader != NULL)
3760 ins_compl_new_leader();
3761 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003762}
3763
3764/*
3765 * Setup for finding completions again without leaving CTRL-X mode. Used when
3766 * BS or a key was typed while still searching for matches.
3767 */
3768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003770{
3771 ins_compl_free();
3772 compl_started = FALSE;
3773 compl_matches = 0;
3774 compl_cont_status = 0;
3775 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003776}
3777
3778/*
3779 * Set the first match, the original text.
3780 */
3781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003783{
3784 char_u *p;
3785
Bram Moolenaare87edf32018-04-17 22:14:32 +02003786 /* Replace the original text entry.
3787 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3788 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003789 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3790 {
3791 p = vim_strsave(str);
3792 if (p != NULL)
3793 {
3794 vim_free(compl_first_match->cp_str);
3795 compl_first_match->cp_str = p;
3796 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003797 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003798 else if (compl_first_match->cp_prev != NULL
3799 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3800 {
3801 p = vim_strsave(str);
3802 if (p != NULL)
3803 {
3804 vim_free(compl_first_match->cp_prev->cp_str);
3805 compl_first_match->cp_prev->cp_str = p;
3806 }
3807 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003808}
3809
3810/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811 * Append one character to the match leader. May reduce the number of
3812 * matches.
3813 */
3814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816{
3817 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003818 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003819 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003820 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003821
3822 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003823 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003824 {
3825 /* When still at the original match use the first entry that matches
3826 * the leader. */
3827 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3828 {
3829 p = NULL;
3830 for (cp = compl_shown_match->cp_next; cp != NULL
3831 && cp != compl_first_match; cp = cp->cp_next)
3832 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003833 if (compl_leader == NULL
3834 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003835 (int)STRLEN(compl_leader)))
3836 {
3837 p = cp->cp_str;
3838 break;
3839 }
3840 }
3841 if (p == NULL || (int)STRLEN(p) <= len)
3842 return;
3843 }
3844 else
3845 return;
3846 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003847 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003848 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003849 ins_compl_addleader(c);
3850}
3851
3852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003854 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003855 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003862 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /* Forget any previous 'special' messages if this is actually
3865 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3866 */
3867 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3868 edit_submode_extra = NULL;
3869
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003870 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003871 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3872 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003873 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003875 /* Set "compl_get_longest" when finding the first matches. */
3876 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003877 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003878 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003879 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003880 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003881
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003882 }
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3885 {
3886 /*
3887 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3888 * it will be yet. Now we decide.
3889 */
3890 switch (c)
3891 {
3892 case Ctrl_E:
3893 case Ctrl_Y:
3894 ctrl_x_mode = CTRL_X_SCROLL;
3895 if (!(State & REPLACE_FLAG))
3896 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3897 else
3898 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3899 edit_submode_pre = NULL;
3900 showmode();
3901 break;
3902 case Ctrl_L:
3903 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3904 break;
3905 case Ctrl_F:
3906 ctrl_x_mode = CTRL_X_FILES;
3907 break;
3908 case Ctrl_K:
3909 ctrl_x_mode = CTRL_X_DICTIONARY;
3910 break;
3911 case Ctrl_R:
3912 /* Simply allow ^R to happen without affecting ^X mode */
3913 break;
3914 case Ctrl_T:
3915 ctrl_x_mode = CTRL_X_THESAURUS;
3916 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003917#ifdef FEAT_COMPL_FUNC
3918 case Ctrl_U:
3919 ctrl_x_mode = CTRL_X_FUNCTION;
3920 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003922 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003924#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003925 case 's':
3926 case Ctrl_S:
3927 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003928#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003930 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003932#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 case Ctrl_RSB:
3935 ctrl_x_mode = CTRL_X_TAGS;
3936 break;
3937#ifdef FEAT_FIND_ID
3938 case Ctrl_I:
3939 case K_S_TAB:
3940 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3941 break;
3942 case Ctrl_D:
3943 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3944 break;
3945#endif
3946 case Ctrl_V:
3947 case Ctrl_Q:
3948 ctrl_x_mode = CTRL_X_CMDLINE;
3949 break;
3950 case Ctrl_P:
3951 case Ctrl_N:
3952 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3953 * just started ^X mode, or there were enough ^X's to cancel
3954 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3955 * do normal expansion when interrupting a different mode (say
3956 * ^X^F^X^P or ^P^X^X^P, see below)
3957 * nothing changes if interrupting mode 0, (eg, the flag
3958 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 if (!(compl_cont_status & CONT_INTRPT))
3960 compl_cont_status |= CONT_LOCAL;
3961 else if (compl_cont_mode != 0)
3962 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 /* FALLTHROUGH */
3964 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 /* If we have typed at least 2 ^X's... for modes != 0, we set
3966 * compl_cont_status = 0 (eg, as if we had just started ^X
3967 * mode).
3968 * For mode 0, we set "compl_cont_mode" to an impossible
3969 * value, in both cases ^X^X can be used to restart the same
3970 * mode (avoiding ADDING mode).
3971 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3972 * 'complete' and local ^P expansions respectively.
3973 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3974 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (c == Ctrl_X)
3976 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 if (compl_cont_mode != 0)
3978 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003980 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003982 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 edit_submode = NULL;
3984 showmode();
3985 break;
3986 }
3987 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003988 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 /* We're already in CTRL-X mode, do we stay in it? */
3991 if (!vim_is_ctrl_x_key(c))
3992 {
3993 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003994 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
3996 ctrl_x_mode = CTRL_X_FINISHED;
3997 edit_submode = NULL;
3998 }
3999 showmode();
4000 }
4001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 /* Show error message from attempted keyword completion (probably
4005 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004006 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004008 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4009 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 || ctrl_x_mode == CTRL_X_FINISHED)
4011 {
4012 /* Get here when we have finished typing a sequence of ^N and
4013 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004015 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004018 * If any of the original typed text has been changed, eg when
4019 * ignorecase is set, we must add back-spaces to the redo
4020 * buffer. We add as few as necessary to delete just the part
4021 * of the original text that has changed.
4022 * When using the longest match, edited the match or used
4023 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004025 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4026 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004027 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004028 ptr = NULL;
4029 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031
4032#ifdef FEAT_CINDENT
4033 want_cindent = (can_cindent && cindent_on());
4034#endif
4035 /*
4036 * When completing whole lines: fix indent for 'cindent'.
4037 * Otherwise, break line if it's too long.
4038 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
4041#ifdef FEAT_CINDENT
4042 /* re-indent the current line */
4043 if (want_cindent)
4044 {
4045 do_c_expr_indent();
4046 want_cindent = FALSE; /* don't do it again */
4047 }
4048#endif
4049 }
4050 else
4051 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004052 int prev_col = curwin->w_cursor.col;
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004055 if (prev_col > 0)
4056 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004057 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004058 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004060 if (prev_col > 0
4061 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4062 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004065 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004066 * the selection without inserting anything. When
4067 * compl_enter_selects is set the Enter key does the same. */
4068 if ((c == Ctrl_Y || (compl_enter_selects
4069 && (c == CAR || c == K_KENTER || c == NL)))
4070 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004071 retval = TRUE;
4072
Bram Moolenaar47247282016-08-02 22:36:02 +02004073 /* CTRL-E means completion is Ended, go back to the typed text.
4074 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004075 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004076 {
4077 ins_compl_delete();
4078 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004079 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004080 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004081 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004082 retval = TRUE;
4083 }
4084
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004085 auto_format(FALSE, TRUE);
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 compl_started = FALSE;
4089 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004090 if (!shortmess(SHM_COMPLETIONMENU))
4091 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004092 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004093 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (edit_submode != NULL)
4095 {
4096 edit_submode = NULL;
4097 showmode();
4098 }
4099
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004100#ifdef FEAT_CMDWIN
4101 if (c == Ctrl_C && cmdwin_type != 0)
4102 /* Avoid the popup menu remains displayed when leaving the
4103 * command line window. */
4104 update_screen(0);
4105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#ifdef FEAT_CINDENT
4107 /*
4108 * Indent now if a key was typed that is in 'cinkeys'.
4109 */
4110 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4111 do_c_expr_indent();
4112#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004113 /* Trigger the CompleteDone event to give scripts a chance to act
4114 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004115 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004118 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4119 /* Trigger the CompleteDone event to give scripts a chance to act
4120 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004121 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 /* reset continue_* if we left expansion-mode, if we stay they'll be
4124 * (re)set properly in ins_complete() */
4125 if (!vim_is_ctrl_x_key(c))
4126 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 compl_cont_status = 0;
4128 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004130
4131 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132}
4133
4134/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004135 * Fix the redo buffer for the completion leader replacing some of the typed
4136 * text. This inserts backspaces and appends the changed text.
4137 * "ptr" is the known leader text or NUL.
4138 */
4139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004140ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004141{
4142 int len;
4143 char_u *p;
4144 char_u *ptr = ptr_arg;
4145
4146 if (ptr == NULL)
4147 {
4148 if (compl_leader != NULL)
4149 ptr = compl_leader;
4150 else
4151 return; /* nothing to do */
4152 }
4153 if (compl_orig_text != NULL)
4154 {
4155 p = compl_orig_text;
4156 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4157 ;
4158#ifdef FEAT_MBYTE
4159 if (len > 0)
4160 len -= (*mb_head_off)(p, p + len);
4161#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004162 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004163 AppendCharToRedobuff(K_BS);
4164 }
4165 else
4166 len = 0;
4167 if (ptr != NULL)
4168 AppendToRedobuffLit(ptr + len, -1);
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4173 * (depending on flag) starting from buf and looking for a non-scanned
4174 * buffer (other than curbuf). curbuf is special, if it is called with
4175 * buf=curbuf then it has to be the first call for a given flag/expansion.
4176 *
4177 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4178 */
4179 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004180ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 if (flag == 'w') /* just windows */
4185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (buf == curbuf) /* first call for this flag/expansion */
4187 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004188 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 && wp->w_buffer->b_scanned)
4190 ;
4191 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 else
4194 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4195 * (unlisted buffers)
4196 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004197 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 && ((flag == 'U'
4199 ? buf->b_p_bl
4200 : (!buf->b_p_bl
4201 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004202 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 ;
4204 return buf;
4205}
4206
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004207#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004208/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004209 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004210 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004211 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004213expand_by_function(
4214 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4215 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004216{
Bram Moolenaar82139082011-09-14 16:52:09 +02004217 list_T *matchlist = NULL;
4218 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004219 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004220 char_u *funcname;
4221 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004222 win_T *curwin_save;
4223 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004224 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004225 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004226
Bram Moolenaare344bea2005-09-01 20:46:49 +00004227 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4228 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004229 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004230
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004231 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004232 args[0].v_type = VAR_NUMBER;
4233 args[0].vval.v_number = 0;
4234 args[1].v_type = VAR_STRING;
4235 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4236 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004237
Bram Moolenaare344bea2005-09-01 20:46:49 +00004238 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004239 curwin_save = curwin;
4240 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004241
4242 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004243 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004244 {
4245 switch (rettv.v_type)
4246 {
4247 case VAR_LIST:
4248 matchlist = rettv.vval.v_list;
4249 break;
4250 case VAR_DICT:
4251 matchdict = rettv.vval.v_dict;
4252 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004253 case VAR_SPECIAL:
4254 if (rettv.vval.v_number == VVAL_NONE)
4255 compl_opt_suppress_empty = TRUE;
4256 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004257 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004258 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004259 clear_tv(&rettv);
4260 break;
4261 }
4262 }
4263
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004264 if (curwin_save != curwin || curbuf_save != curbuf)
4265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004266 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004267 goto theend;
4268 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004269 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004270 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004271 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004274 goto theend;
4275 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004276
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004277 if (matchlist != NULL)
4278 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004279 else if (matchdict != NULL)
4280 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004281
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004282theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004283 // Restore State, it might have been changed.
4284 State = save_State;
4285
Bram Moolenaar82139082011-09-14 16:52:09 +02004286 if (matchdict != NULL)
4287 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004288 if (matchlist != NULL)
4289 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004290}
4291#endif /* FEAT_COMPL_FUNC */
4292
Bram Moolenaar39f05632006-03-19 22:15:26 +00004293#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004294/*
4295 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004296 */
4297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004298ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004299{
4300 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004301 int dir = compl_direction;
4302
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004303 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004304 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004305 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004306 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4307 /* if dir was BACKWARD then honor it just once */
4308 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004309 else if (did_emsg)
4310 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004311 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004312}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004313
4314/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004315 * Add completions from a dict.
4316 */
4317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004318ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004319{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004320 dictitem_T *di_refresh;
4321 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004322
4323 /* Check for optional "refresh" item. */
4324 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004325 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4326 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004327 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004328 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004329
4330 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4331 compl_opt_refresh_always = TRUE;
4332 }
4333
4334 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004335 di_words = dict_find(dict, (char_u *)"words", 5);
4336 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4337 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004338}
4339
4340/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004341 * Add a match to the list of matches from a typeval_T.
4342 * If the given string is already in the list of completions, then return
4343 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4344 * maybe because alloc() returns NULL, then FAIL is returned.
4345 */
4346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004347ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004348{
4349 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004350 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004351 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004352 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004353 char_u *(cptext[CPT_COUNT]);
4354
4355 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4356 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004357 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4358 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004359 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004360 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004361 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004362 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004363 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004364 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004365 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004366 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004367 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004368 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4369 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4370 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4371 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4372 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4373 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004374 }
4375 else
4376 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004377 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004378 vim_memset(cptext, 0, sizeof(cptext));
4379 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004380 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004381 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004382 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004383}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004384#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386/*
4387 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004388 * The search starts at position "ini" in curbuf and in the direction
4389 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004390 * When "compl_started" is FALSE start at that position, otherwise continue
4391 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 * This may return before finding all the matches.
4393 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 */
4395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004396ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
4398 static pos_T first_match_pos;
4399 static pos_T last_match_pos;
4400 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 static int found_all = FALSE; /* Found all matches of a
4402 certain type. */
4403 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar572cb562005-08-05 21:35:02 +00004405 pos_T *pos;
4406 char_u **matches;
4407 int save_p_scs;
4408 int save_p_ws;
4409 int save_p_ic;
4410 int i;
4411 int num_matches;
4412 int len;
4413 int found_new_match;
4414 int type = ctrl_x_mode;
4415 char_u *ptr;
4416 char_u *dict = NULL;
4417 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004418 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004422 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 ins_buf->b_scanned = 0;
4424 found_all = FALSE;
4425 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004427 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 last_match_pos = first_match_pos = *ini;
4429 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004430 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4431 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaar4475b622017-05-01 20:46:52 +02004433 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004434 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004435
4436 /*
4437 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 for (;;)
4440 {
4441 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004442 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 * or if found_all says this entry is done. For ^X^L only use the
4446 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004447 if ((ctrl_x_mode == CTRL_X_NORMAL
4448 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 found_all = FALSE;
4452 while (*e_cpt == ',' || *e_cpt == ' ')
4453 e_cpt++;
4454 if (*e_cpt == '.' && !curbuf->b_scanned)
4455 {
4456 ins_buf = curbuf;
4457 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004458 /* Move the cursor back one character so that ^N can match the
4459 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004460 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004461 {
4462 /* Move the cursor to after the last character in the
4463 * buffer, so that word at start of buffer is found
4464 * correctly. */
4465 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4466 first_match_pos.col =
4467 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 last_match_pos = first_match_pos;
4470 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004471
4472 /* Remember the first match so that the loop stops when we
4473 * wrap and come back there a second time. */
4474 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4477 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4478 {
4479 /* Scan a buffer, but not the current one. */
4480 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4481 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 first_match_pos.col = last_match_pos.col = 0;
4484 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4485 last_match_pos.lnum = 0;
4486 type = 0;
4487 }
4488 else /* unloaded buffer, scan like dictionary */
4489 {
4490 found_all = TRUE;
4491 if (ins_buf->b_fname == NULL)
4492 continue;
4493 type = CTRL_X_DICTIONARY;
4494 dict = ins_buf->b_fname;
4495 dict_f = DICT_EXACT;
4496 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004497 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 ins_buf->b_fname == NULL
4499 ? buf_spname(ins_buf)
4500 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004501 ? ins_buf->b_fname
4502 : ins_buf->b_sfname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004503 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505 else if (*e_cpt == NUL)
4506 break;
4507 else
4508 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004509 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 type = -1;
4511 else if (*e_cpt == 'k' || *e_cpt == 's')
4512 {
4513 if (*e_cpt == 'k')
4514 type = CTRL_X_DICTIONARY;
4515 else
4516 type = CTRL_X_THESAURUS;
4517 if (*++e_cpt != ',' && *e_cpt != NUL)
4518 {
4519 dict = e_cpt;
4520 dict_f = DICT_FIRST;
4521 }
4522 }
4523#ifdef FEAT_FIND_ID
4524 else if (*e_cpt == 'i')
4525 type = CTRL_X_PATH_PATTERNS;
4526 else if (*e_cpt == 'd')
4527 type = CTRL_X_PATH_DEFINES;
4528#endif
4529 else if (*e_cpt == ']' || *e_cpt == 't')
4530 {
4531 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004532 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004533 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
4535 else
4536 type = -1;
4537
4538 /* in any case e_cpt is advanced to the next entry */
4539 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4540
4541 found_all = TRUE;
4542 if (type == -1)
4543 continue;
4544 }
4545 }
4546
Bram Moolenaar4475b622017-05-01 20:46:52 +02004547 /* If complete() was called then compl_pattern has been reset. The
4548 * following won't work then, bail out. */
4549 if (compl_pattern == NULL)
4550 break;
4551
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 switch (type)
4553 {
4554 case -1:
4555 break;
4556#ifdef FEAT_FIND_ID
4557 case CTRL_X_PATH_PATTERNS:
4558 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004559 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4564 (linenr_T)1, (linenr_T)MAXLNUM);
4565 break;
4566#endif
4567
4568 case CTRL_X_DICTIONARY:
4569 case CTRL_X_THESAURUS:
4570 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004571 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 : (type == CTRL_X_THESAURUS
4573 ? (*curbuf->b_p_tsr == NUL
4574 ? p_tsr
4575 : curbuf->b_p_tsr)
4576 : (*curbuf->b_p_dict == NUL
4577 ? p_dict
4578 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004579 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004580 dict != NULL ? dict_f
4581 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 dict = NULL;
4583 break;
4584
4585 case CTRL_X_TAGS:
4586 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4587 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004590 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 * of matches is found when compl_pattern is empty */
4592 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004593 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4594 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4596 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004597 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599 p_ic = save_p_ic;
4600 break;
4601
4602 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4605 {
4606
4607 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004609 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 break;
4612
4613 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 if (expand_cmdline(&compl_xp, compl_pattern,
4615 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004617 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004620#ifdef FEAT_COMPL_FUNC
4621 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004622 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004623 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004624 break;
4625#endif
4626
Bram Moolenaar488c6512005-08-11 20:09:58 +00004627 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004628#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004629 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004630 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004631 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004632 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004633#endif
4634 break;
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 default: /* normal ^P/^N and ^X^L */
4637 /*
4638 * If 'infercase' is set, don't use 'smartcase' here
4639 */
4640 save_p_scs = p_scs;
4641 if (ins_buf->b_p_inf)
4642 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643
Bram Moolenaar361aa502014-01-23 22:45:58 +01004644 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 * end but never from the middle, thus setting nowrapscan in this
4646 * buffers is a good idea, on the other hand, we always set
4647 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4648 save_p_ws = p_ws;
4649 if (ins_buf != curbuf)
4650 p_ws = FALSE;
4651 else if (*e_cpt == '.')
4652 p_ws = TRUE;
4653 for (;;)
4654 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004655 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004657 ++msg_silent; /* Don't want messages for wrapscan. */
4658
Bram Moolenaare4214502015-03-05 18:08:43 +01004659 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4660 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004661 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004662 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004663 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004665 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004667 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004668 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004669 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004670 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004671 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004672 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004674 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 first_match_pos = *pos;
4677 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004678 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004681 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 found_new_match = FAIL;
4683 if (found_new_match == FAIL)
4684 {
4685 if (ins_buf == curbuf)
4686 found_all = TRUE;
4687 break;
4688 }
4689
4690 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004691 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 && ini->lnum == pos->lnum
4693 && ini->col == pos->col)
4694 continue;
4695 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004696 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4701 continue;
4702 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4703 if (!p_paste)
4704 ptr = skipwhite(ptr);
4705 }
4706 len = (int)STRLEN(ptr);
4707 }
4708 else
4709 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004710 char_u *tmp_ptr = ptr;
4711
4712 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 /* Skip if already inside a word. */
4716 if (vim_iswordp(tmp_ptr))
4717 continue;
4718 /* Find start of next word. */
4719 tmp_ptr = find_word_start(tmp_ptr);
4720 }
4721 /* Find end of this word. */
4722 tmp_ptr = find_word_end(tmp_ptr);
4723 len = (int)(tmp_ptr - ptr);
4724
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 if ((compl_cont_status & CONT_ADDING)
4726 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
4728 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4729 {
4730 /* Try next line, if any. the new word will be
4731 * "join" as if the normal command "J" was used.
4732 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004733 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 * works -- Acevedo */
4735 STRNCPY(IObuff, ptr, len);
4736 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4737 tmp_ptr = ptr = skipwhite(ptr);
4738 /* Find start of next word. */
4739 tmp_ptr = find_word_start(tmp_ptr);
4740 /* Find end of next word. */
4741 tmp_ptr = find_word_end(tmp_ptr);
4742 if (tmp_ptr > ptr)
4743 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004744 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004746 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 IObuff[len++] = ' ';
4748 /* IObuf =~ "\k.* ", thus len >= 2 */
4749 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004750 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 || (vim_strchr(p_cpo, CPO_JOINSP)
4752 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004753 && (IObuff[len - 2] == '?'
4754 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 IObuff[len++] = ' ';
4756 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004757 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (tmp_ptr - ptr >= IOSIZE - len)
4759 tmp_ptr = ptr + IOSIZE - len - 1;
4760 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4761 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004762 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 IObuff[len] = NUL;
4765 ptr = IObuff;
4766 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 continue;
4769 }
4770 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004771 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004772 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004773 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 found_new_match = OK;
4776 break;
4777 }
4778 }
4779 p_scs = save_p_scs;
4780 p_ws = save_p_ws;
4781 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004782
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004783 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004784 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004785 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 found_new_match = OK;
4787
4788 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004789 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4790 * match */
4791 if ((ctrl_x_mode != CTRL_X_NORMAL
4792 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004794 {
4795 if (got_int)
4796 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004797 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004798 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004799 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004800
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004801 if ((ctrl_x_mode != CTRL_X_NORMAL
4802 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004803 || compl_interrupted)
4804 break;
4805 compl_started = TRUE;
4806 }
4807 else
4808 {
4809 /* Mark a buffer scanned when it has been scanned completely */
4810 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4811 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004813 compl_started = FALSE;
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004816 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004818 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 && *e_cpt == NUL) /* Got to end of 'complete' */
4820 found_new_match = FAIL;
4821
4822 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004823 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4824 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 i = ins_compl_make_cyclic();
4826
Bram Moolenaar4475b622017-05-01 20:46:52 +02004827 if (compl_old_match != NULL)
4828 {
4829 /* If several matches were added (FORWARD) or the search failed and has
4830 * just been made cyclic then we have to move compl_curr_match to the
4831 * next or previous entry (if any) -- Acevedo */
4832 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4833 : compl_old_match->cp_prev;
4834 if (compl_curr_match == NULL)
4835 compl_curr_match = compl_old_match;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 return i;
4838}
4839
4840/* Delete the old text being completed. */
4841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004842ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004844 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 /*
4847 * In insert mode: Delete the typed part.
4848 * In replace mode: Put the old characters back, if any.
4849 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004850 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4851 if ((int)curwin->w_cursor.col > col)
4852 {
4853 if (stop_arrow() == FAIL)
4854 return;
4855 backspace_until_column(col);
4856 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004857
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004858 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4859 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004861 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004862 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863}
4864
Bram Moolenaar472e8592016-10-15 17:06:47 +02004865/*
4866 * Insert the new text being completed.
4867 * "in_compl_func" is TRUE when called from complete_check().
4868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004870ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004872 dict_T *dict;
4873
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004874 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004875 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4876 compl_used_match = FALSE;
4877 else
4878 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004879
4880 /* Set completed item. */
4881 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004882 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004883 if (dict != NULL)
4884 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004885 dict_add_string(dict, "word", compl_shown_match->cp_str);
4886 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4887 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4888 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4889 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4890 dict_add_string(dict, "user_data",
4891 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004892 }
4893 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004894 if (!in_compl_func)
4895 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896}
4897
4898/*
4899 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004900 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4901 * get more completions. If it is FALSE, then we just do nothing when there
4902 * are no more completions in a given direction. The latter case is used when
4903 * we are still in the middle of finding completions, to allow browsing
4904 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 * Return the total number of matches, or -1 if still unknown -- webb.
4906 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4908 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 *
4910 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004911 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4912 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 */
4914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004915ins_compl_next(
4916 int allow_get_expansion,
4917 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004918 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004919 int insert_match, /* Insert the newly selected match */
4920 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004923 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004924 compl_T *found_compl = NULL;
4925 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004926 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004927 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004929 /* When user complete function return -1 for findstart which is next
4930 * time of 'always', compl_shown_match become NULL. */
4931 if (compl_shown_match == NULL)
4932 return -1;
4933
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004934 if (compl_leader != NULL
4935 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004937 /* Set "compl_shown_match" to the actually shown match, it may differ
4938 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004939 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004940 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004941 && compl_shown_match->cp_next != NULL
4942 && compl_shown_match->cp_next != compl_first_match)
4943 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004944
4945 /* If we didn't find it searching forward, and compl_shows_dir is
4946 * backward, find the last match. */
4947 if (compl_shows_dir == BACKWARD
4948 && !ins_compl_equal(compl_shown_match,
4949 compl_leader, (int)STRLEN(compl_leader))
4950 && (compl_shown_match->cp_next == NULL
4951 || compl_shown_match->cp_next == compl_first_match))
4952 {
4953 while (!ins_compl_equal(compl_shown_match,
4954 compl_leader, (int)STRLEN(compl_leader))
4955 && compl_shown_match->cp_prev != NULL
4956 && compl_shown_match->cp_prev != compl_first_match)
4957 compl_shown_match = compl_shown_match->cp_prev;
4958 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004959 }
4960
4961 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004962 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 /* Delete old text to be replaced */
4964 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004965
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004966 /* When finding the longest common text we stick at the original text,
4967 * don't let CTRL-N or CTRL-P move to the first match. */
4968 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4969
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004970 /* When restarting the search don't insert the first match either. */
4971 if (compl_restarting)
4972 {
4973 advance = FALSE;
4974 compl_restarting = FALSE;
4975 }
4976
Bram Moolenaare3226be2005-12-18 22:10:00 +00004977 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4978 * around. */
4979 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004984 found_end = (compl_first_match != NULL
4985 && (compl_shown_match->cp_next == compl_first_match
4986 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004987 }
4988 else if (compl_shows_dir == BACKWARD
4989 && compl_shown_match->cp_prev != NULL)
4990 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004991 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004992 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004993 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004996 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004997 if (!allow_get_expansion)
4998 {
4999 if (advance)
5000 {
5001 if (compl_shows_dir == BACKWARD)
5002 compl_pending -= todo + 1;
5003 else
5004 compl_pending += todo + 1;
5005 }
5006 return -1;
5007 }
5008
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005009 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005010 {
5011 if (compl_shows_dir == BACKWARD)
5012 --compl_pending;
5013 else
5014 ++compl_pending;
5015 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005016
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005017 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005018 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005019
5020 /* handle any pending completions */
5021 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005022 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005023 {
5024 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5025 {
5026 compl_shown_match = compl_shown_match->cp_next;
5027 --compl_pending;
5028 }
5029 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5030 {
5031 compl_shown_match = compl_shown_match->cp_prev;
5032 ++compl_pending;
5033 }
5034 else
5035 break;
5036 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005037 found_end = FALSE;
5038 }
5039 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5040 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005041 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005042 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005043 ++todo;
5044 else
5045 /* Remember a matching item. */
5046 found_compl = compl_shown_match;
5047
5048 /* Stop at the end of the list when we found a usable match. */
5049 if (found_end)
5050 {
5051 if (found_compl != NULL)
5052 {
5053 compl_shown_match = found_compl;
5054 break;
5055 }
5056 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005060 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005061 if (compl_no_insert && !started)
5062 {
5063 ins_bytes(compl_orig_text + ins_compl_len());
5064 compl_used_match = FALSE;
5065 }
5066 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005067 {
5068 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005069 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005070 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005071 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005072 }
5073 else
5074 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
5076 if (!allow_get_expansion)
5077 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005078 /* may undisplay the popup menu first */
5079 ins_compl_upd_pum();
5080
Bram Moolenaarae654382019-01-17 21:09:05 +01005081 // Redraw before showing the popup menu to show the user what was
5082 // inserted.
5083 pum_call_update_screen();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005084
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005085 /* display the updated popup menu */
5086 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005087#ifdef FEAT_GUI
5088 if (gui.in_use)
5089 {
5090 /* Show the cursor after the match, not after the redrawn text. */
5091 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005092 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005093 }
5094#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /* Delete old text to be replaced, since we're still searching and
5097 * don't want to match ourselves! */
5098 ins_compl_delete();
5099 }
5100
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005101 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005102 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005103 if (compl_no_insert && !started)
5104 compl_enter_selects = TRUE;
5105 else
5106 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 /*
5109 * Show the file name for the match (if any)
5110 * Truncate the file name to avoid a wait for return.
5111 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005112 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005114 char *lead = _("match in file");
5115 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5116 char_u *s;
5117 char_u *e;
5118
5119 if (space > 0)
5120 {
5121 /* We need the tail that fits. With double-byte encoding going
5122 * back from the end is very slow, thus go from the start and keep
5123 * the text that fits in "space" between "s" and "e". */
5124 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5125 {
5126 space -= ptr2cells(e);
5127 while (space < 0)
5128 {
5129 space += ptr2cells(s);
5130 MB_PTR_ADV(s);
5131 }
5132 }
5133 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5134 s > compl_shown_match->cp_fname ? "<" : "", s);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005135 msg((char *)IObuff);
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005136 redraw_cmdline = FALSE; /* don't overwrite! */
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139
5140 return num_matches;
5141}
5142
5143/*
5144 * Call this while finding completions, to check whether the user has hit a key
5145 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005146 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005148 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005149 * "in_compl_func" is TRUE when called from complete_check(), don't set
5150 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 */
5152 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005153ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154{
5155 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005156 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005158 /* Don't check when reading keys from a script, :normal or feedkeys().
5159 * That would break the test scripts. But do check for keys when called
5160 * from complete_check(). */
5161 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 return;
5163
5164 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005165 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return;
5167 count = 0;
5168
Bram Moolenaara260a972006-06-23 19:36:29 +00005169 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5170 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (c != NUL)
5173 {
5174 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5175 {
5176 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005177 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005178 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005179 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005181 else
5182 {
5183 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005184 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005185 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005186 if (c != K_IGNORE)
5187 {
5188 /* Don't interrupt completion when the character wasn't typed,
5189 * e.g., when doing @q to replay keys. */
5190 if (c != Ctrl_R && KeyTyped)
5191 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005192
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005193 vungetc(c);
5194 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005197 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005198 {
5199 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5200
5201 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005202 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005203 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005204}
5205
5206/*
5207 * Decide the direction of Insert mode complete from the key typed.
5208 * Returns BACKWARD or FORWARD.
5209 */
5210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005211ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005212{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005213 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005214 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005215 return BACKWARD;
5216 return FORWARD;
5217}
5218
5219/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005220 * Return TRUE for keys that are used for completion only when the popup menu
5221 * is visible.
5222 */
5223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005224ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005225{
5226 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005227 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5228 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005229}
5230
5231/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005232 * Decide the number of completions to move forward.
5233 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5234 */
5235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005236ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005237{
5238 int h;
5239
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005240 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005241 {
5242 h = pum_get_height();
5243 if (h > 3)
5244 h -= 2; /* keep some context */
5245 return h;
5246 }
5247 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248}
5249
5250/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005251 * Return TRUE if completion with "c" should insert the match, FALSE if only
5252 * to change the currently selected completion.
5253 */
5254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005255ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005256{
5257 switch (c)
5258 {
5259 case K_UP:
5260 case K_DOWN:
5261 case K_PAGEDOWN:
5262 case K_KPAGEDOWN:
5263 case K_S_DOWN:
5264 case K_PAGEUP:
5265 case K_KPAGEUP:
5266 case K_S_UP:
5267 return FALSE;
5268 }
5269 return TRUE;
5270}
5271
5272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 * Do Insert mode completion.
5274 * Called when character "c" was typed, which has a meaning for completion.
5275 * Returns OK if completion was done, FAIL if something failed (out of mem).
5276 */
5277 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005278ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 char_u *line;
5281 int startcol = 0; /* column where searched text starts */
5282 colnr_T curs_col; /* cursor column */
5283 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005284 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005285 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005286 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005287 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
Bram Moolenaare3226be2005-12-18 22:10:00 +00005289 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005290 insert_match = ins_compl_use_match(c);
5291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 /* First time we hit ^N or ^P (in a row, I mean) */
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 did_ai = FALSE;
5297#ifdef FEAT_SMARTINDENT
5298 did_si = FALSE;
5299 can_si = FALSE;
5300 can_si_back = FALSE;
5301#endif
5302 if (stop_arrow() == FAIL)
5303 return FAIL;
5304
5305 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005307 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005309 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 * "compl_startpos" to the cursor as a pattern to add a new word
5311 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005312 * "compl_startpos" is not in the same line as the cursor then fix it
5313 * (the line has been split because it was longer than 'tw'). if SOL
5314 * is set then skip the previous pattern, a word at the beginning of
5315 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005316 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5317 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 /*
5320 * it is a continued search
5321 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005323 if (ctrl_x_mode == CTRL_X_NORMAL
5324 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5325 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005329 /* line (probably) wrapped, set compl_startpos to the
5330 * first non_blank in the line, if it is not a wordchar
5331 * include it to get a better pattern, but then we don't
5332 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005333 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 compl_startpos.col = compl_col;
5335 compl_startpos.lnum = curwin->w_cursor.lnum;
5336 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 else
5339 {
5340 /* S_IPOS was set when we inserted a word that was at the
5341 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 * mode but first we need to redefine compl_startpos */
5343 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 compl_cont_status |= CONT_SOL;
5346 compl_startpos.col = (colnr_T)(skipwhite(
5347 line + compl_length
5348 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005353 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005354 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_cont_status &= ~CONT_SOL;
5359 compl_length = (IOSIZE - MIN_SPACE);
5360 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5363 if (compl_length < 1)
5364 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005366 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005376 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005377 if (ctrl_x_mode != CTRL_X_NORMAL)
5378 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_cont_status = 0;
5380 compl_cont_status |= CONT_N_ADDS;
5381 compl_startpos = curwin->w_cursor;
5382 startcol = (int)curs_col;
5383 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385
5386 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005387 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5391 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 compl_col += ++startcol;
5397 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 compl_pattern = str_foldcase(line + compl_col,
5401 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 compl_pattern = vim_strnsave(line + compl_col,
5404 compl_length);
5405 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return FAIL;
5407 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 char_u *prefix = (char_u *)"\\<";
5411
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005412 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005414 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005417 if (!vim_iswordp(line + compl_col)
5418 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 && (
5420#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#endif
5425 )))
5426 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 STRCPY((char *)compl_pattern, prefix);
5428 (void)quote_meta(compl_pattern + STRLEN(prefix),
5429 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
5437 )
5438 {
5439 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5441 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 compl_col += curs_col;
5444 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446 else
5447 {
5448#ifdef FEAT_MBYTE
5449 /* Search the point of change class of multibyte character
5450 * or not a word single byte character backward. */
5451 if (has_mbyte)
5452 {
5453 int base_class;
5454 int head_off;
5455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 startcol -= (*mb_head_off)(line, line + startcol);
5457 base_class = mb_get_class(line + startcol);
5458 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005460 head_off = (*mb_head_off)(line, line + startcol);
5461 if (base_class != mb_get_class(line + startcol
5462 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005464 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466 }
5467 else
5468#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005469 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 compl_col += ++startcol;
5472 compl_length = (int)curs_col - startcol;
5473 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {
5475 /* Only match word with at least two chars -- webb
5476 * there's no need to call quote_meta,
5477 * alloc(7) is enough -- Acevedo
5478 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 compl_pattern = alloc(7);
5480 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005482 STRCPY((char *)compl_pattern, "\\<");
5483 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5484 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486 else
5487 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005489 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005490 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 STRCPY((char *)compl_pattern, "\\<");
5493 (void)quote_meta(compl_pattern + 2, line + compl_col,
5494 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496 }
5497 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005498 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005500 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005501 compl_length = (int)curs_col - (int)compl_col;
5502 if (compl_length < 0) /* cursor in indent: empty pattern */
5503 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005505 compl_pattern = str_foldcase(line + compl_col, compl_length,
5506 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005508 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5509 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 return FAIL;
5511 }
5512 else if (ctrl_x_mode == CTRL_X_FILES)
5513 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005514 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005515 if (startcol > 0)
5516 {
5517 char_u *p = line + startcol;
5518
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005519 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005520 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005521 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005522 if (p == line && vim_isfilec(PTR2CHAR(p)))
5523 startcol = 0;
5524 else
5525 startcol = (int)(p - line) + 1;
5526 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005527
Bram Moolenaar0300e462013-09-08 16:03:45 +02005528 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 compl_length = (int)curs_col - startcol;
5530 compl_pattern = addstar(line + compl_col, compl_length,
5531 EXPAND_FILES);
5532 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 return FAIL;
5534 }
5535 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5536 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005537 compl_pattern = vim_strnsave(line, curs_col);
5538 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005541 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005542 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5543 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005544 /* No completion possible, use an empty pattern to get a
5545 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005546 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005547 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005548 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5549 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005551 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005552 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005553#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005554 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005555 * Call user defined function 'completefunc' with "a:findstart"
5556 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005557 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005558 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005559 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005560 char_u *funcname;
5561 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005562 win_T *curwin_save;
5563 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005564 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005565
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005566 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005567 * string */
5568 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5569 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5570 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005572 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005573 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005574 /* restore did_ai, so that adding comment leader works */
5575 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005576 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005577 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005578
Bram Moolenaarffa96842018-06-12 22:05:14 +02005579 args[0].v_type = VAR_NUMBER;
5580 args[0].vval.v_number = 1;
5581 args[1].v_type = VAR_STRING;
5582 args[1].vval.v_string = (char_u *)"";
5583 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005584 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005585 curwin_save = curwin;
5586 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005587 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005588
5589 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005590 if (curwin_save != curwin || curbuf_save != curbuf)
5591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005592 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005593 return FAIL;
5594 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005595 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005596 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005597 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005599 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005600 return FAIL;
5601 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005602
Bram Moolenaar6110a002012-01-26 18:58:38 +01005603 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005604 * cancel the complete without an error.
5605 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005606 if (col == -2)
5607 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005608 if (col == -3)
5609 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005610 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005611 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005612 if (!shortmess(SHM_COMPLETIONMENU))
5613 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005614 return FAIL;
5615 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005616
Bram Moolenaar82139082011-09-14 16:52:09 +02005617 /*
5618 * Reset extended parameters of completion, when start new
5619 * completion.
5620 */
5621 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005622 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005623
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005624 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005625 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005626 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005627 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005628 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005629
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005630 /* Setup variables for completion. Need to obtain "line" again,
5631 * it may have become invalid. */
5632 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005633 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5635 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005636#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005637 return FAIL;
5638 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005639 else if (ctrl_x_mode == CTRL_X_SPELL)
5640 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005641#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005642 if (spell_bad_len > 0)
5643 compl_col = curs_col - spell_bad_len;
5644 else
5645 compl_col = spell_word_start(startcol);
5646 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005647 {
5648 compl_length = 0;
5649 compl_col = curs_col;
5650 }
5651 else
5652 {
5653 spell_expand_check_cap(compl_col);
5654 compl_length = (int)curs_col - compl_col;
5655 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005656 /* Need to obtain "line" again, it may have become invalid. */
5657 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005658 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5659 if (compl_pattern == NULL)
5660#endif
5661 return FAIL;
5662 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005663 else
5664 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005665 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005666 return FAIL;
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005669 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005672 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 /* Insert a new line, keep indentation but ignore 'comments' */
5675#ifdef FEAT_COMMENTS
5676 char_u *old = curbuf->b_p_com;
5677
5678 curbuf->b_p_com = (char_u *)"";
5679#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005680 compl_startpos.lnum = curwin->w_cursor.lnum;
5681 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 ins_eol('\r');
5683#ifdef FEAT_COMMENTS
5684 curbuf->b_p_com = old;
5685#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005686 compl_length = 0;
5687 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else
5691 {
5692 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005693 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005696 if (compl_cont_status & CONT_LOCAL)
5697 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 else
5699 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5700
Bram Moolenaar62951b12011-09-21 18:23:05 +02005701 /* If any of the original typed text has been changed we need to fix
5702 * the redo buffer. */
5703 ins_compl_fixRedoBufForLeader(NULL);
5704
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005705 /* Always add completion for the original text. */
5706 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005707 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5708 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005709 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005711 VIM_CLEAR(compl_pattern);
5712 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 return FAIL;
5714 }
5715
5716 /* showmode might reset the internal line pointers, so it must
5717 * be called before line = ml_get(), or when this address is no
5718 * longer needed. -- Acevedo.
5719 */
5720 edit_submode_extra = (char_u *)_("-- Searching...");
5721 edit_submode_highl = HLF_COUNT;
5722 showmode();
5723 edit_submode_extra = NULL;
5724 out_flush();
5725 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005726 else if (insert_match && stop_arrow() == FAIL)
5727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005729 compl_shown_match = compl_curr_match;
5730 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005733 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005735 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005736 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005737 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005739 /* may undisplay the popup menu */
5740 ins_compl_upd_pum();
5741
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005742 if (n > 1) /* all matches have been found */
5743 compl_matches = n;
5744 compl_curr_match = compl_shown_match;
5745 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaard68071d2006-05-02 22:08:30 +00005747 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5748 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (got_int && !global_busy)
5750 {
5751 (void)vgetc();
5752 got_int = FALSE;
5753 }
5754
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005755 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005756 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005758 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5759 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5761 edit_submode_highl = HLF_E;
5762 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5763 * because we couldn't expand anything at first place, but if we used
5764 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5765 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005766 if ( compl_length > 1
5767 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005768 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5770 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005771 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 }
5773
Bram Moolenaar572cb562005-08-05 21:35:02 +00005774 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005775 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005777 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 if (edit_submode_extra == NULL)
5780 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005781 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783 edit_submode_extra = (char_u *)_("Back at original");
5784 edit_submode_highl = HLF_W;
5785 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005786 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 edit_submode_extra = (char_u *)_("Word from other line");
5789 edit_submode_highl = HLF_COUNT;
5790 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005791 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 edit_submode_extra = (char_u *)_("The only match");
5794 edit_submode_highl = HLF_COUNT;
5795 }
5796 else
5797 {
5798 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005799 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005801 int number = 0;
5802 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005804 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
5806 /* search backwards for the first valid (!= -1) number.
5807 * This should normally succeed already at the first loop
5808 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005809 for (match = compl_curr_match->cp_prev; match != NULL
5810 && match != compl_first_match;
5811 match = match->cp_prev)
5812 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005814 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816 }
5817 if (match != NULL)
5818 /* go up and assign all numbers which are not assigned
5819 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005820 for (match = match->cp_next;
5821 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005822 match = match->cp_next)
5823 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
5825 else /* BACKWARD */
5826 {
5827 /* search forwards (upwards) for the first valid (!= -1)
5828 * number. This should normally succeed already at the
5829 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005830 for (match = compl_curr_match->cp_next; match != NULL
5831 && match != compl_first_match;
5832 match = match->cp_next)
5833 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005835 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 break;
5837 }
5838 if (match != NULL)
5839 /* go down and assign all numbers which are not
5840 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005841 for (match = match->cp_prev; match
5842 && match->cp_number == -1;
5843 match = match->cp_prev)
5844 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
5846 }
5847
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005848 /* The match should always have a sequence number now, this is
5849 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005850 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005852 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5853 * Translations may need more than twice that. */
5854 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005856 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005857 vim_snprintf((char *)match_ref, sizeof(match_ref),
5858 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005859 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005861 vim_snprintf((char *)match_ref, sizeof(match_ref),
5862 _("match %d"),
5863 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 edit_submode_extra = match_ref;
5865 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005866 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 curs_columns(FALSE);
5868 }
5869 }
5870 }
5871
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005872 // Show a message about what (completion) mode we're in.
5873 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005875 showmode();
5876 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005877 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005878 if (edit_submode_extra != NULL)
5879 {
5880 if (!p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005881 msg_attr((char *)edit_submode_extra,
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005882 edit_submode_highl < HLF_COUNT
5883 ? HL_ATTR(edit_submode_highl) : 0);
5884 }
5885 else
5886 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
Bram Moolenaard68071d2006-05-02 22:08:30 +00005890 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005891 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005892 show_pum(save_w_wrow, save_w_leftcol);
5893
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005894 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005895 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005896
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 return OK;
5898}
5899
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005900 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005901show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005902{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005903 /* RedrawingDisabled may be set when invoked through complete(). */
5904 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005905
Bram Moolenaarcb036422017-03-01 12:29:10 +01005906 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005907
Bram Moolenaarcb036422017-03-01 12:29:10 +01005908 /* If the cursor moved or the display scrolled we need to remove the pum
5909 * first. */
5910 setcursor();
5911 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5912 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005913
Bram Moolenaarcb036422017-03-01 12:29:10 +01005914 ins_compl_show_pum();
5915 setcursor();
5916 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005917}
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919/*
5920 * Looks in the first "len" chars. of "src" for search-metachars.
5921 * If dest is not NULL the chars. are copied there quoting (with
5922 * a backslash) the metachars, and dest would be NUL terminated.
5923 * Returns the length (needed) of dest
5924 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005925 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005926quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005928 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005930 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
5932 switch (*src)
5933 {
5934 case '.':
5935 case '*':
5936 case '[':
5937 if (ctrl_x_mode == CTRL_X_DICTIONARY
5938 || ctrl_x_mode == CTRL_X_THESAURUS)
5939 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005940 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 case '~':
5942 if (!p_magic) /* quote these only if magic is set */
5943 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005944 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 case '\\':
5946 if (ctrl_x_mode == CTRL_X_DICTIONARY
5947 || ctrl_x_mode == CTRL_X_THESAURUS)
5948 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005949 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 case '^': /* currently it's not needed. */
5951 case '$':
5952 m++;
5953 if (dest != NULL)
5954 *dest++ = '\\';
5955 break;
5956 }
5957 if (dest != NULL)
5958 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005959# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 /* Copy remaining bytes of a multibyte character. */
5961 if (has_mbyte)
5962 {
5963 int i, mb_len;
5964
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005965 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 if (mb_len > 0 && len >= mb_len)
5967 for (i = 0; i < mb_len; ++i)
5968 {
5969 --len;
5970 ++src;
5971 if (dest != NULL)
5972 *dest++ = *src;
5973 }
5974 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 }
5977 if (dest != NULL)
5978 *dest = NUL;
5979
5980 return m;
5981}
5982#endif /* FEAT_INS_EXPAND */
5983
5984/*
5985 * Next character is interpreted literally.
5986 * A one, two or three digit decimal number is interpreted as its byte value.
5987 * If one or two digits are entered, the next character is given to vungetc().
5988 * For Unicode a character > 255 may be returned.
5989 */
5990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 int cc;
5994 int nc;
5995 int i;
5996 int hex = FALSE;
5997 int octal = FALSE;
5998#ifdef FEAT_MBYTE
5999 int unicode = 0;
6000#endif
6001
6002 if (got_int)
6003 return Ctrl_C;
6004
6005#ifdef FEAT_GUI
6006 /*
6007 * In GUI there is no point inserting the internal code for a special key.
6008 * It is more useful to insert the string "<KEY>" instead. This would
6009 * probably be useful in a text window too, but it would not be
6010 * vi-compatible (maybe there should be an option for it?) -- webb
6011 */
6012 if (gui.in_use)
6013 ++allow_keys;
6014#endif
6015#ifdef USE_ON_FLY_SCROLL
6016 dont_scroll = TRUE; /* disallow scrolling here */
6017#endif
6018 ++no_mapping; /* don't map the next key hits */
6019 cc = 0;
6020 i = 0;
6021 for (;;)
6022 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006023 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#ifdef FEAT_CMDL_INFO
6025 if (!(State & CMDLINE)
6026# ifdef FEAT_MBYTE
6027 && MB_BYTE2LEN_CHECK(nc) == 1
6028# endif
6029 )
6030 add_to_showcmd(nc);
6031#endif
6032 if (nc == 'x' || nc == 'X')
6033 hex = TRUE;
6034 else if (nc == 'o' || nc == 'O')
6035 octal = TRUE;
6036#ifdef FEAT_MBYTE
6037 else if (nc == 'u' || nc == 'U')
6038 unicode = nc;
6039#endif
6040 else
6041 {
6042 if (hex
6043#ifdef FEAT_MBYTE
6044 || unicode != 0
6045#endif
6046 )
6047 {
6048 if (!vim_isxdigit(nc))
6049 break;
6050 cc = cc * 16 + hex2nr(nc);
6051 }
6052 else if (octal)
6053 {
6054 if (nc < '0' || nc > '7')
6055 break;
6056 cc = cc * 8 + nc - '0';
6057 }
6058 else
6059 {
6060 if (!VIM_ISDIGIT(nc))
6061 break;
6062 cc = cc * 10 + nc - '0';
6063 }
6064
6065 ++i;
6066 }
6067
6068 if (cc > 255
6069#ifdef FEAT_MBYTE
6070 && unicode == 0
6071#endif
6072 )
6073 cc = 255; /* limit range to 0-255 */
6074 nc = 0;
6075
6076 if (hex) /* hex: up to two chars */
6077 {
6078 if (i >= 2)
6079 break;
6080 }
6081#ifdef FEAT_MBYTE
6082 else if (unicode) /* Unicode: up to four or eight chars */
6083 {
6084 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6085 break;
6086 }
6087#endif
6088 else if (i >= 3) /* decimal or octal: up to three chars */
6089 break;
6090 }
6091 if (i == 0) /* no number entered */
6092 {
6093 if (nc == K_ZERO) /* NUL is stored as NL */
6094 {
6095 cc = '\n';
6096 nc = 0;
6097 }
6098 else
6099 {
6100 cc = nc;
6101 nc = 0;
6102 }
6103 }
6104
6105 if (cc == 0) /* NUL is stored as NL */
6106 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006107#ifdef FEAT_MBYTE
6108 if (enc_dbcs && (cc & 0xff) == 0)
6109 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6110 second byte will cause trouble! */
6111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113 --no_mapping;
6114#ifdef FEAT_GUI
6115 if (gui.in_use)
6116 --allow_keys;
6117#endif
6118 if (nc)
6119 vungetc(nc);
6120 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6121 return cc;
6122}
6123
6124/*
6125 * Insert character, taking care of special keys and mod_mask
6126 */
6127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006128insert_special(
6129 int c,
6130 int allow_modmask,
6131 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 char_u *p;
6134 int len;
6135
6136 /*
6137 * Special function key, translate into "<Key>". Up to the last '>' is
6138 * inserted with ins_str(), so as not to replace characters in replace
6139 * mode.
6140 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6141 * unless 'allow_modmask' is TRUE.
6142 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006143#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 /* Command-key never produces a normal key */
6145 if (mod_mask & MOD_MASK_CMD)
6146 allow_modmask = TRUE;
6147#endif
6148 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6149 {
6150 p = get_special_key_name(c, mod_mask);
6151 len = (int)STRLEN(p);
6152 c = p[len - 1];
6153 if (len > 2)
6154 {
6155 if (stop_arrow() == FAIL)
6156 return;
6157 p[len - 1] = NUL;
6158 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006159 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 ctrlv = FALSE;
6161 }
6162 }
6163 if (stop_arrow() == OK)
6164 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6165}
6166
6167/*
6168 * Special characters in this context are those that need processing other
6169 * than the simple insertion that can be performed here. This includes ESC
6170 * which terminates the insert, and CR/NL which need special processing to
6171 * open up a new line. This routine tries to optimize insertions performed by
6172 * the "redo", "undo" or "put" commands, so it needs to know when it should
6173 * stop and defer processing to the "normal" mechanism.
6174 * '0' and '^' are special, because they can be followed by CTRL-D.
6175 */
6176#ifdef EBCDIC
6177# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6178#else
6179# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6180#endif
6181
6182#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006183# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006185# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186#endif
6187
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006188/*
6189 * "flags": INSCHAR_FORMAT - force formatting
6190 * INSCHAR_CTRLV - char typed just after CTRL-V
6191 * INSCHAR_NO_FEX - don't use 'formatexpr'
6192 *
6193 * NOTE: passes the flags value straight through to internal_format() which,
6194 * beside INSCHAR_FORMAT (above), is also looking for these:
6195 * INSCHAR_DO_COM - format comments
6196 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199insertchar(
6200 int c, /* character to insert or NUL */
6201 int flags, /* INSCHAR_FORMAT, etc. */
6202 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int textwidth;
6205#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006209 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006211 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 /*
6215 * Try to break the line in two or more pieces when:
6216 * - Always do this if we have been called to do formatting only.
6217 * - Always do this when 'formatoptions' has the 'a' flag and the line
6218 * ends in white space.
6219 * - Otherwise:
6220 * - Don't do this if inserting a blank
6221 * - Don't do this if an existing character is being replaced, unless
6222 * we're in VREPLACE mode.
6223 * - Do this if the cursor is not on the line where insert started
6224 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6225 * before the insert.
6226 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6227 * before 'textwidth'
6228 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006230 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006231 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 && *ml_get_cursor() != NUL)
6235 && (curwin->w_cursor.lnum != Insstart.lnum
6236 || ((!has_format_option(FO_INS_LONG)
6237 || Insstart_textlen <= (colnr_T)textwidth)
6238 && (!fo_ins_blank
6239 || Insstart_blank_vcol <= (colnr_T)textwidth
6240 ))))))
6241 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242 /* Format with 'formatexpr' when it's set. Use internal formatting
6243 * when 'formatexpr' isn't set or it returns non-zero. */
6244#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006245 int do_internal = TRUE;
6246 colnr_T virtcol = get_nolist_virtcol()
6247 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006248
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006249 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6250 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006251 {
6252 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6253 /* It may be required to save for undo again, e.g. when setline()
6254 * was called. */
6255 ins_need_undo = TRUE;
6256 }
6257 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006259 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (c == NUL) /* only formatting was wanted */
6263 return;
6264
6265#ifdef FEAT_COMMENTS
6266 /* Check whether this character should end a comment. */
6267 if (did_ai && (int)c == end_comment_pending)
6268 {
6269 char_u *line;
6270 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6271 int middle_len, end_len;
6272 int i;
6273
6274 /*
6275 * Need to remove existing (middle) comment leader and insert end
6276 * comment leader. First, check what comment leader we can find.
6277 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006278 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6280 {
6281 /* Skip middle-comment string */
6282 while (*p && p[-1] != ':') /* find end of middle flags */
6283 ++p;
6284 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6285 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006286 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 --middle_len;
6288
6289 /* Find the end-comment string */
6290 while (*p && p[-1] != ':') /* find end of end flags */
6291 ++p;
6292 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6293
6294 /* Skip white space before the cursor */
6295 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006296 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 ;
6298 i++;
6299
6300 /* Skip to before the middle leader */
6301 i -= middle_len;
6302
6303 /* Check some expected things before we go on */
6304 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6305 {
6306 /* Backspace over all the stuff we want to replace */
6307 backspace_until_column(i);
6308
6309 /*
6310 * Insert the end-comment string, except for the last
6311 * character, which will get inserted as normal later.
6312 */
6313 ins_bytes_len(lead_end, end_len - 1);
6314 }
6315 }
6316 }
6317 end_comment_pending = NUL;
6318#endif
6319
6320 did_ai = FALSE;
6321#ifdef FEAT_SMARTINDENT
6322 did_si = FALSE;
6323 can_si = FALSE;
6324 can_si_back = FALSE;
6325#endif
6326
6327 /*
6328 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6329 * This speeds up normal text input considerably.
6330 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6331 * need to re-indent at a ':', or any other character (but not what
6332 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006333 * Don't do this when there an InsertCharPre autocommand is defined,
6334 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006335 * Do the check for InsertCharPre before the call to vpeekc() because the
6336 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 */
6338#ifdef USE_ON_FLY_SCROLL
6339 dont_scroll = FALSE; /* allow scrolling here */
6340#endif
6341
6342 if ( !ISSPECIAL(c)
6343#ifdef FEAT_MBYTE
6344 && (!has_mbyte || (*mb_char2len)(c) == 1)
6345#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006346 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 && vpeekc() != NUL
6348 && !(State & REPLACE_FLAG)
6349#ifdef FEAT_CINDENT
6350 && !cindent_on()
6351#endif
6352#ifdef FEAT_RIGHTLEFT
6353 && !p_ri
6354#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006355 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 {
6357#define INPUT_BUFLEN 100
6358 char_u buf[INPUT_BUFLEN + 1];
6359 int i;
6360 colnr_T virtcol = 0;
6361
6362 buf[0] = c;
6363 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006364 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 virtcol = get_nolist_virtcol();
6366 /*
6367 * Stop the string when:
6368 * - no more chars available
6369 * - finding a special character (command key)
6370 * - buffer is full
6371 * - running into the 'textwidth' boundary
6372 * - need to check for abbreviation: A non-word char after a word-char
6373 */
6374 while ( (c = vpeekc()) != NUL
6375 && !ISSPECIAL(c)
6376#ifdef FEAT_MBYTE
6377 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6378#endif
6379 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006380# ifdef FEAT_FKMAP
6381 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 && (textwidth == 0
6384 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6385 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6386 {
6387#ifdef FEAT_RIGHTLEFT
6388 c = vgetc();
6389 if (p_hkmap && KeyTyped)
6390 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 buf[i++] = c;
6392#else
6393 buf[i++] = vgetc();
6394#endif
6395 }
6396
6397#ifdef FEAT_DIGRAPHS
6398 do_digraph(-1); /* clear digraphs */
6399 do_digraph(buf[i-1]); /* may be the start of a digraph */
6400#endif
6401 buf[i] = NUL;
6402 ins_str(buf);
6403 if (flags & INSCHAR_CTRLV)
6404 {
6405 redo_literal(*buf);
6406 i = 1;
6407 }
6408 else
6409 i = 0;
6410 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006411 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413 else
6414 {
6415#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006416 int cc;
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6419 {
6420 char_u buf[MB_MAXBYTES + 1];
6421
6422 (*mb_char2bytes)(c, buf);
6423 buf[cc] = NUL;
6424 ins_char_bytes(buf, cc);
6425 AppendCharToRedobuff(c);
6426 }
6427 else
6428#endif
6429 {
6430 ins_char(c);
6431 if (flags & INSCHAR_CTRLV)
6432 redo_literal(c);
6433 else
6434 AppendCharToRedobuff(c);
6435 }
6436 }
6437}
6438
6439/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006441 *
6442 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6443 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 */
6445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006446internal_format(
6447 int textwidth,
6448 int second_indent,
6449 int flags,
6450 int format_only,
6451 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452{
6453 int cc;
6454 int save_char = NUL;
6455 int haveto_redraw = FALSE;
6456 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6457#ifdef FEAT_MBYTE
6458 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6459#endif
6460 int fo_white_par = has_format_option(FO_WHITE_PAR);
6461 int first_line = TRUE;
6462#ifdef FEAT_COMMENTS
6463 colnr_T leader_len;
6464 int no_leader = FALSE;
6465 int do_comments = (flags & INSCHAR_DO_COM);
6466#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006467#ifdef FEAT_LINEBREAK
6468 int has_lbr = curwin->w_p_lbr;
6469
6470 /* make sure win_lbr_chartabsize() counts correctly */
6471 curwin->w_p_lbr = FALSE;
6472#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473
6474 /*
6475 * When 'ai' is off we don't want a space under the cursor to be
6476 * deleted. Replace it with an 'x' temporarily.
6477 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006478 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 {
6480 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006481 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482 {
6483 save_char = cc;
6484 pchar_cursor('x');
6485 }
6486 }
6487
6488 /*
6489 * Repeat breaking lines, until the current line is not too long.
6490 */
6491 while (!got_int)
6492 {
6493 int startcol; /* Cursor column at entry */
6494 int wantcol; /* column at textwidth border */
6495 int foundcol; /* column for start of spaces */
6496 int end_foundcol = 0; /* column for start of word */
6497 colnr_T len;
6498 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499 int orig_col = 0;
6500 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006501 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006502 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006503 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504
Bram Moolenaar97b98102009-11-17 16:41:01 +00006505 virtcol = get_nolist_virtcol()
6506 + char2cells(c != NUL ? c : gchar_cursor());
6507 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006508 break;
6509
6510#ifdef FEAT_COMMENTS
6511 if (no_leader)
6512 do_comments = FALSE;
6513 else if (!(flags & INSCHAR_FORMAT)
6514 && has_format_option(FO_WRAP_COMS))
6515 do_comments = TRUE;
6516
6517 /* Don't break until after the comment leader */
6518 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006519 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520 else
6521 leader_len = 0;
6522
6523 /* If the line doesn't start with a comment leader, then don't
6524 * start one in a following broken line. Avoids that a %word
6525 * moved to the start of the next line causes all following lines
6526 * to start with %. */
6527 if (leader_len == 0)
6528 no_leader = TRUE;
6529#endif
6530 if (!(flags & INSCHAR_FORMAT)
6531#ifdef FEAT_COMMENTS
6532 && leader_len == 0
6533#endif
6534 && !has_format_option(FO_WRAP))
6535
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006536 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537 if ((startcol = curwin->w_cursor.col) == 0)
6538 break;
6539
6540 /* find column of textwidth border */
6541 coladvance((colnr_T)textwidth);
6542 wantcol = curwin->w_cursor.col;
6543
Bram Moolenaar97b98102009-11-17 16:41:01 +00006544 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006545 foundcol = 0;
6546
6547 /*
6548 * Find position to break at.
6549 * Stop at first entered white when 'formatoptions' has 'v'
6550 */
6551 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006552 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006553 || curwin->w_cursor.lnum != Insstart.lnum
6554 || curwin->w_cursor.col >= Insstart.col)
6555 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006556 if (curwin->w_cursor.col == startcol && c != NUL)
6557 cc = c;
6558 else
6559 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560 if (WHITECHAR(cc))
6561 {
6562 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006563 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006564
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006565 // find start of sequence of blanks
6566 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006567 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6568 {
6569 dec_cursor();
6570 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006571
6572 // Increment count of how many whitespace chars in this
6573 // group; we only need to know if it's more than one.
6574 if (wcc < 2)
6575 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006576 }
6577 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6578 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006579
6580 // Don't break after a period when 'formatoptions' has 'p' and
6581 // there are less than two spaces.
6582 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6583 continue;
6584
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585#ifdef FEAT_COMMENTS
6586 /* Don't break until after the comment leader */
6587 if (curwin->w_cursor.col < leader_len)
6588 break;
6589#endif
6590 if (has_format_option(FO_ONE_LETTER))
6591 {
6592 /* do not break after one-letter words */
6593 if (curwin->w_cursor.col == 0)
6594 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006595#ifdef FEAT_COMMENTS
6596 /* do not break "#a b" when 'tw' is 2 */
6597 if (curwin->w_cursor.col <= leader_len)
6598 break;
6599#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006600 col = curwin->w_cursor.col;
6601 dec_cursor();
6602 cc = gchar_cursor();
6603
6604 if (WHITECHAR(cc))
6605 continue; /* one-letter, continue */
6606 curwin->w_cursor.col = col;
6607 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006608
6609 inc_cursor();
6610
6611 end_foundcol = end_col + 1;
6612 foundcol = curwin->w_cursor.col;
6613 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006614 break;
6615 }
6616#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006617 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006618 {
6619 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006620 if (curwin->w_cursor.col != startcol)
6621 {
6622#ifdef FEAT_COMMENTS
6623 /* Don't break until after the comment leader */
6624 if (curwin->w_cursor.col < leader_len)
6625 break;
6626#endif
6627 col = curwin->w_cursor.col;
6628 inc_cursor();
6629 /* Don't change end_foundcol if already set. */
6630 if (foundcol != curwin->w_cursor.col)
6631 {
6632 foundcol = curwin->w_cursor.col;
6633 end_foundcol = foundcol;
6634 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6635 break;
6636 }
6637 curwin->w_cursor.col = col;
6638 }
6639
6640 if (curwin->w_cursor.col == 0)
6641 break;
6642
6643 col = curwin->w_cursor.col;
6644
6645 dec_cursor();
6646 cc = gchar_cursor();
6647
6648 if (WHITECHAR(cc))
6649 continue; /* break with space */
6650#ifdef FEAT_COMMENTS
6651 /* Don't break until after the comment leader */
6652 if (curwin->w_cursor.col < leader_len)
6653 break;
6654#endif
6655
6656 curwin->w_cursor.col = col;
6657
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006658 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006659 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006660 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6661 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006662 }
6663#endif
6664 if (curwin->w_cursor.col == 0)
6665 break;
6666 dec_cursor();
6667 }
6668
6669 if (foundcol == 0) /* no spaces, cannot break line */
6670 {
6671 curwin->w_cursor.col = startcol;
6672 break;
6673 }
6674
6675 /* Going to break the line, remove any "$" now. */
6676 undisplay_dollar();
6677
6678 /*
6679 * Offset between cursor position and line break is used by replace
6680 * stack functions. VREPLACE does not use this, and backspaces
6681 * over the text instead.
6682 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683 if (State & VREPLACE_FLAG)
6684 orig_col = startcol; /* Will start backspacing from here */
6685 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006686 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687
6688 /*
6689 * adjust startcol for spaces that will be deleted and
6690 * characters that will remain on top line
6691 */
6692 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006693 while ((cc = gchar_cursor(), WHITECHAR(cc))
6694 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006695 inc_cursor();
6696 startcol -= curwin->w_cursor.col;
6697 if (startcol < 0)
6698 startcol = 0;
6699
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700 if (State & VREPLACE_FLAG)
6701 {
6702 /*
6703 * In VREPLACE mode, we will backspace over the text to be
6704 * wrapped, so save a copy now to put on the next line.
6705 */
6706 saved_text = vim_strsave(ml_get_cursor());
6707 curwin->w_cursor.col = orig_col;
6708 if (saved_text == NULL)
6709 break; /* Can't do it, out of memory */
6710 saved_text[startcol] = NUL;
6711
6712 /* Backspace over characters that will move to the next line */
6713 if (!fo_white_par)
6714 backspace_until_column(foundcol);
6715 }
6716 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006717 {
6718 /* put cursor after pos. to break line */
6719 if (!fo_white_par)
6720 curwin->w_cursor.col = foundcol;
6721 }
6722
6723 /*
6724 * Split the line just before the margin.
6725 * Only insert/delete lines, but don't really redraw the window.
6726 */
6727 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6728 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6729#ifdef FEAT_COMMENTS
6730 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006731 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006733 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6734 if (!(flags & INSCHAR_COM_LIST))
6735 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006736
6737 replace_offset = 0;
6738 if (first_line)
6739 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006740 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006741 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006742 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006743 * This section is for auto-wrap of numeric lists. When not
6744 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6745 * flag will be set and open_line() will handle it (as seen
6746 * above). The code here (and in get_number_indent()) will
6747 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006748 */
6749 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006750 second_indent =
6751 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006752 if (second_indent >= 0)
6753 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006754 if (State & VREPLACE_FLAG)
6755 change_indent(INDENT_SET, second_indent,
6756 FALSE, NUL, TRUE);
6757 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006758#ifdef FEAT_COMMENTS
6759 if (leader_len > 0 && second_indent - leader_len > 0)
6760 {
6761 int i;
6762 int padding = second_indent - leader_len;
6763
6764 /* We started at the first_line of a numbered list
6765 * that has a comment. the open_line() function has
6766 * inserted the proper comment leader and positioned
6767 * the cursor at the end of the split line. Now we
6768 * add the additional whitespace needed after the
6769 * comment leader for the numbered list. */
6770 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006771 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006772 }
6773 else
6774 {
6775#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006776 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006777#ifdef FEAT_COMMENTS
6778 }
6779#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006780 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006781 }
6782 first_line = FALSE;
6783 }
6784
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006785 if (State & VREPLACE_FLAG)
6786 {
6787 /*
6788 * In VREPLACE mode we have backspaced over the text to be
6789 * moved, now we re-insert it into the new line.
6790 */
6791 ins_bytes(saved_text);
6792 vim_free(saved_text);
6793 }
6794 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006795 {
6796 /*
6797 * Check if cursor is not past the NUL off the line, cindent
6798 * may have added or removed indent.
6799 */
6800 curwin->w_cursor.col += startcol;
6801 len = (colnr_T)STRLEN(ml_get_curline());
6802 if (curwin->w_cursor.col > len)
6803 curwin->w_cursor.col = len;
6804 }
6805
6806 haveto_redraw = TRUE;
6807#ifdef FEAT_CINDENT
6808 can_cindent = TRUE;
6809#endif
6810 /* moved the cursor, don't autoindent or cindent now */
6811 did_ai = FALSE;
6812#ifdef FEAT_SMARTINDENT
6813 did_si = FALSE;
6814 can_si = FALSE;
6815 can_si_back = FALSE;
6816#endif
6817 line_breakcheck();
6818 }
6819
6820 if (save_char != NUL) /* put back space after cursor */
6821 pchar_cursor(save_char);
6822
Bram Moolenaar0026d472014-09-09 16:32:39 +02006823#ifdef FEAT_LINEBREAK
6824 curwin->w_p_lbr = has_lbr;
6825#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006826 if (!format_only && haveto_redraw)
6827 {
6828 update_topline();
6829 redraw_curbuf_later(VALID);
6830 }
6831}
6832
6833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 * Called after inserting or deleting text: When 'formatoptions' includes the
6835 * 'a' flag format from the current line until the end of the paragraph.
6836 * Keep the cursor at the same position relative to the text.
6837 * The caller must have saved the cursor line for undo, following ones will be
6838 * saved here.
6839 */
6840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841auto_format(
6842 int trailblank, /* when TRUE also format with trailing blank */
6843 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 pos_T pos;
6846 colnr_T len;
6847 char_u *old;
6848 char_u *new, *pnew;
6849 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006850 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
6852 if (!has_format_option(FO_AUTO))
6853 return;
6854
6855 pos = curwin->w_cursor;
6856 old = ml_get_curline();
6857
6858 /* may remove added space */
6859 check_auto_format(FALSE);
6860
6861 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6862 * user might insert normal text next. Also skip formatting when "1" is
6863 * in 'formatoptions' and there is a single character before the cursor.
6864 * Otherwise the line would be broken and when typing another non-white
6865 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006866 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (*old != NUL && !trailblank && wasatend)
6868 {
6869 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006870 cc = gchar_cursor();
6871 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6872 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006874 cc = gchar_cursor();
6875 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {
6877 curwin->w_cursor = pos;
6878 return;
6879 }
6880 curwin->w_cursor = pos;
6881 }
6882
6883#ifdef FEAT_COMMENTS
6884 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6885 * comments. */
6886 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006887 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 return;
6889#endif
6890
6891 /*
6892 * May start formatting in a previous line, so that after "x" a word is
6893 * moved to the previous line if it fits there now. Only when this is not
6894 * the start of a paragraph.
6895 */
6896 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6897 {
6898 --curwin->w_cursor.lnum;
6899 if (u_save_cursor() == FAIL)
6900 return;
6901 }
6902
6903 /*
6904 * Do the formatting and restore the cursor position. "saved_cursor" will
6905 * be adjusted for the text formatting.
6906 */
6907 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006908 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 curwin->w_cursor = saved_cursor;
6910 saved_cursor.lnum = 0;
6911
6912 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6913 {
6914 /* "cannot happen" */
6915 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6916 coladvance((colnr_T)MAXCOL);
6917 }
6918 else
6919 check_cursor_col();
6920
6921 /* Insert mode: If the cursor is now after the end of the line while it
6922 * previously wasn't, the line was broken. Because of the rule above we
6923 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6924 * formatted. */
6925 if (!wasatend && has_format_option(FO_WHITE_PAR))
6926 {
6927 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006928 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (curwin->w_cursor.col == len)
6930 {
6931 pnew = vim_strnsave(new, len + 2);
6932 pnew[len] = ' ';
6933 pnew[len + 1] = NUL;
6934 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6935 /* remove the space later */
6936 did_add_space = TRUE;
6937 }
6938 else
6939 /* may remove added space */
6940 check_auto_format(FALSE);
6941 }
6942
6943 check_cursor();
6944}
6945
6946/*
6947 * When an extra space was added to continue a paragraph for auto-formatting,
6948 * delete it now. The space must be under the cursor, just after the insert
6949 * position.
6950 */
6951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006952check_auto_format(
6953 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006956 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 if (did_add_space)
6959 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006960 cc = gchar_cursor();
6961 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 /* Somehow the space was removed already. */
6963 did_add_space = FALSE;
6964 else
6965 {
6966 if (!end_insert)
6967 {
6968 inc_cursor();
6969 c = gchar_cursor();
6970 dec_cursor();
6971 }
6972 if (c != NUL)
6973 {
6974 /* The space is no longer at the end of the line, delete it. */
6975 del_char(FALSE);
6976 did_add_space = FALSE;
6977 }
6978 }
6979 }
6980}
6981
6982/*
6983 * Find out textwidth to be used for formatting:
6984 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006985 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 * if invalid value, use 0.
6987 * Set default to window width (maximum 79) for "gq" operator.
6988 */
6989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006990comp_textwidth(
6991 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 int textwidth;
6994
6995 textwidth = curbuf->b_p_tw;
6996 if (textwidth == 0 && curbuf->b_p_wm)
6997 {
6998 /* The width is the window width minus 'wrapmargin' minus all the
6999 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02007000 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#ifdef FEAT_CMDWIN
7002 if (cmdwin_type != 0)
7003 textwidth -= 1;
7004#endif
7005#ifdef FEAT_FOLDING
7006 textwidth -= curwin->w_p_fdc;
7007#endif
7008#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007009 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 textwidth -= 1;
7011#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02007012 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 textwidth -= 8;
7014 }
7015 if (textwidth < 0)
7016 textwidth = 0;
7017 if (ff && textwidth == 0)
7018 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007019 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (textwidth > 79)
7021 textwidth = 79;
7022 }
7023 return textwidth;
7024}
7025
7026/*
7027 * Put a character in the redo buffer, for when just after a CTRL-V.
7028 */
7029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007030redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 char_u buf[10];
7033
7034 /* Only digits need special treatment. Translate them into a string of
7035 * three digits. */
7036 if (VIM_ISDIGIT(c))
7037 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007038 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 AppendToRedobuff(buf);
7040 }
7041 else
7042 AppendCharToRedobuff(c);
7043}
7044
7045/*
7046 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007047 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 */
7049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007050start_arrow(
7051 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007053 start_arrow_common(end_insert_pos, TRUE);
7054}
7055
7056/*
7057 * Like start_arrow() but with end_change argument.
7058 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7059 */
7060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007061start_arrow_with_change(
7062 pos_T *end_insert_pos, /* can be NULL */
7063 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007064{
7065 start_arrow_common(end_insert_pos, end_change);
7066 if (!end_change)
7067 {
7068 AppendCharToRedobuff(Ctrl_G);
7069 AppendCharToRedobuff('U');
7070 }
7071}
7072
7073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007074start_arrow_common(
7075 pos_T *end_insert_pos, /* can be NULL */
7076 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007077{
7078 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {
7080 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007081 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 arrow_used = TRUE; /* this means we stopped the current insert */
7083 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007084#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007085 check_spell_redraw();
7086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007089#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007090/*
7091 * If we skipped highlighting word at cursor, do it now.
7092 * It may be skipped again, thus reset spell_redraw_lnum first.
7093 */
7094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007096{
7097 if (spell_redraw_lnum != 0)
7098 {
7099 linenr_T lnum = spell_redraw_lnum;
7100
7101 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007102 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007103 }
7104}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007105
7106/*
7107 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7108 * spelled word, if there is one.
7109 */
7110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007112{
7113 pos_T tpos = curwin->w_cursor;
7114
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007115 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007116 if (curwin->w_cursor.col != tpos.col)
7117 start_arrow(&tpos);
7118}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007119#endif
7120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121/*
7122 * stop_arrow() is called before a change is made in insert mode.
7123 * If an arrow key has been used, start a new insertion.
7124 * Returns FAIL if undo is impossible, shouldn't insert then.
7125 */
7126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007127stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 if (arrow_used)
7130 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007131 Insstart = curwin->w_cursor; /* new insertion starts here */
7132 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7133 /* Don't update the original insert position when moved to the
7134 * right, except when nothing was inserted yet. */
7135 update_Insstart_orig = FALSE;
7136 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (u_save_cursor() == OK)
7139 {
7140 arrow_used = FALSE;
7141 ins_need_undo = FALSE;
7142 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (State & VREPLACE_FLAG)
7146 {
7147 orig_line_count = curbuf->b_ml.ml_line_count;
7148 vr_lines_changed = 1;
7149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 ResetRedobuff();
7151 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007152 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
7154 else if (ins_need_undo)
7155 {
7156 if (u_save_cursor() == OK)
7157 ins_need_undo = FALSE;
7158 }
7159
7160#ifdef FEAT_FOLDING
7161 /* Always open fold at the cursor line when inserting something. */
7162 foldOpenCursor();
7163#endif
7164
7165 return (arrow_used || ins_need_undo ? FAIL : OK);
7166}
7167
7168/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 * Do a few things to stop inserting.
7170 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7171 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 */
7173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007174stop_insert(
7175 pos_T *end_insert_pos,
7176 int esc, /* called by ins_esc() */
7177 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007179 int cc;
7180 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
7182 stop_redo_ins();
7183 replace_flush(); /* abandon replace stack */
7184
7185 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007186 * Save the inserted text for later redo with ^@ and CTRL-A.
7187 * Don't do it when "restart_edit" was set and nothing was inserted,
7188 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007190 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007191 if (did_restart_edit == 0 || (ptr != NULL
7192 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007193 {
7194 vim_free(last_insert);
7195 last_insert = ptr;
7196 last_insert_skip = new_insert_skip;
7197 }
7198 else
7199 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007201 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 /* Auto-format now. It may seem strange to do this when stopping an
7204 * insertion (or moving the cursor), but it's required when appending
7205 * a line and having it end in a space. But only do it when something
7206 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007207 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007209 pos_T tpos = curwin->w_cursor;
7210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 /* When the cursor is at the end of the line after a space the
7212 * formatting will move it to the following word. Avoid that by
7213 * moving the cursor onto the space. */
7214 cc = 'x';
7215 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7216 {
7217 dec_cursor();
7218 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007219 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007220 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222
7223 auto_format(TRUE, FALSE);
7224
Bram Moolenaar1c465442017-03-12 20:10:05 +01007225 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007226 {
7227 if (gchar_cursor() != NUL)
7228 inc_cursor();
7229#ifdef FEAT_VIRTUALEDIT
7230 /* If the cursor is still at the same character, also keep
7231 * the "coladd". */
7232 if (gchar_cursor() == NUL
7233 && curwin->w_cursor.lnum == tpos.lnum
7234 && curwin->w_cursor.col == tpos.col)
7235 curwin->w_cursor.coladd = tpos.coladd;
7236#endif
7237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239
7240 /* If a space was inserted for auto-formatting, remove it now. */
7241 check_auto_format(TRUE);
7242
7243 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007244 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007245 * Do this when ESC was used or moving the cursor up/down.
7246 * Check for the old position still being valid, just in case the text
7247 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007248 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007249 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7250 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007252 pos_T tpos = curwin->w_cursor;
7253
7254 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007255 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007256 for (;;)
7257 {
7258 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7259 --curwin->w_cursor.col;
7260 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007261 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007262 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007263 if (del_char(TRUE) == FAIL)
7264 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007265 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007266 if (curwin->w_cursor.lnum != tpos.lnum)
7267 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007268 else
7269 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007270 /* reset tpos, could have been invalidated in the loop above */
7271 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007272 tpos.col++;
7273 if (cc != NUL && gchar_pos(&tpos) == NUL)
7274 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 /* <C-S-Right> may have started Visual mode, adjust the position for
7278 * deleted characters. */
7279 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7280 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007281 int len = (int)STRLEN(ml_get_curline());
7282
7283 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007285 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007286#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292 }
7293 did_ai = FALSE;
7294#ifdef FEAT_SMARTINDENT
7295 did_si = FALSE;
7296 can_si = FALSE;
7297 can_si_back = FALSE;
7298#endif
7299
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007300 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7301 * now in a different buffer. */
7302 if (end_insert_pos != NULL)
7303 {
7304 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007305 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007306 curbuf->b_op_end = *end_insert_pos;
7307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
7309
7310/*
7311 * Set the last inserted text to a single character.
7312 * Used for the replace command.
7313 */
7314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 char_u *s;
7318
7319 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (last_insert != NULL)
7322 {
7323 s = last_insert;
7324 /* Use the CTRL-V only when entering a special char */
7325 if (c < ' ' || c == DEL)
7326 *s++ = Ctrl_V;
7327 s = add_char2buf(c, s);
7328 *s++ = ESC;
7329 *s++ = NUL;
7330 last_insert_skip = 0;
7331 }
7332}
7333
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007334#if defined(EXITFREE) || defined(PROTO)
7335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007337{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007338 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007339# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007340 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007341# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007342}
7343#endif
7344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345/*
7346 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7347 * and CSI. Handle multi-byte characters.
7348 * Returns a pointer to after the added bytes.
7349 */
7350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007351add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
7353#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007354 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 int i;
7356 int len;
7357
7358 len = (*mb_char2bytes)(c, temp);
7359 for (i = 0; i < len; ++i)
7360 {
7361 c = temp[i];
7362#endif
7363 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7364 if (c == K_SPECIAL)
7365 {
7366 *s++ = K_SPECIAL;
7367 *s++ = KS_SPECIAL;
7368 *s++ = KE_FILLER;
7369 }
7370#ifdef FEAT_GUI
7371 else if (c == CSI)
7372 {
7373 *s++ = CSI;
7374 *s++ = KS_EXTRA;
7375 *s++ = (int)KE_CSI;
7376 }
7377#endif
7378 else
7379 *s++ = c;
7380#ifdef FEAT_MBYTE
7381 }
7382#endif
7383 return s;
7384}
7385
7386/*
7387 * move cursor to start of line
7388 * if flags & BL_WHITE move to first non-white
7389 * if flags & BL_SOL move to first non-white if startofline is set,
7390 * otherwise keep "curswant" column
7391 * if flags & BL_FIX don't leave the cursor on a NUL.
7392 */
7393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 if ((flags & BL_SOL) && !p_sol)
7397 coladvance(curwin->w_curswant);
7398 else
7399 {
7400 curwin->w_cursor.col = 0;
7401#ifdef FEAT_VIRTUALEDIT
7402 curwin->w_cursor.coladd = 0;
7403#endif
7404
7405 if (flags & (BL_WHITE | BL_SOL))
7406 {
7407 char_u *ptr;
7408
Bram Moolenaar1c465442017-03-12 20:10:05 +01007409 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7411 ++curwin->w_cursor.col;
7412 }
7413 curwin->w_set_curswant = TRUE;
7414 }
7415}
7416
7417/*
7418 * oneright oneleft cursor_down cursor_up
7419 *
7420 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007421 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 * Return OK when successful, FAIL when we hit a line of file boundary.
7423 */
7424
7425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007426oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431#ifdef FEAT_VIRTUALEDIT
7432 if (virtual_active())
7433 {
7434 pos_T prevpos = curwin->w_cursor;
7435
7436 /* Adjust for multi-wide char (excluding TAB) */
7437 ptr = ml_get_cursor();
7438 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007439# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007441# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 ))
7445 ? ptr2cells(ptr) : 1));
7446 curwin->w_set_curswant = TRUE;
7447 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7448 return (prevpos.col != curwin->w_cursor.col
7449 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7450 }
7451#endif
7452
7453 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007454 if (*ptr == NUL)
7455 return FAIL; /* already at the very end */
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007458 if (has_mbyte)
7459 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else
7461#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007462 l = 1;
7463
7464 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7465 * contains "onemore". */
7466 if (ptr[l] == NUL
7467#ifdef FEAT_VIRTUALEDIT
7468 && (ve_flags & VE_ONEMORE) == 0
7469#endif
7470 )
7471 return FAIL;
7472 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
7474 curwin->w_set_curswant = TRUE;
7475 return OK;
7476}
7477
7478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481#ifdef FEAT_VIRTUALEDIT
7482 if (virtual_active())
7483 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007484# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 int v = getviscol();
7488
7489 if (v == 0)
7490 return FAIL;
7491
7492# ifdef FEAT_LINEBREAK
7493 /* We might get stuck on 'showbreak', skip over it. */
7494 width = 1;
7495 for (;;)
7496 {
7497 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007498 /* getviscol() is slow, skip it when 'showbreak' is empty,
7499 * 'breakindent' is not set and there are no multi-byte
7500 * characters */
7501 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502# ifdef FEAT_MBYTE
7503 && !has_mbyte
7504# endif
7505 ) || getviscol() < v)
7506 break;
7507 ++width;
7508 }
7509# else
7510 coladvance(v - 1);
7511# endif
7512
7513 if (curwin->w_cursor.coladd == 1)
7514 {
7515 char_u *ptr;
7516
7517 /* Adjust for multi-wide char (not a TAB) */
7518 ptr = ml_get_cursor();
7519 if (*ptr != TAB && vim_isprintc(
7520# ifdef FEAT_MBYTE
7521 (*mb_ptr2char)(ptr)
7522# else
7523 *ptr
7524# endif
7525 ) && ptr2cells(ptr) > 1)
7526 curwin->w_cursor.coladd = 0;
7527 }
7528
7529 curwin->w_set_curswant = TRUE;
7530 return OK;
7531 }
7532#endif
7533
7534 if (curwin->w_cursor.col == 0)
7535 return FAIL;
7536
7537 curwin->w_set_curswant = TRUE;
7538 --curwin->w_cursor.col;
7539
7540#ifdef FEAT_MBYTE
7541 /* if the character on the left of the current cursor is a multi-byte
7542 * character, move to its first byte */
7543 if (has_mbyte)
7544 mb_adjust_cursor();
7545#endif
7546 return OK;
7547}
7548
7549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550cursor_up(
7551 long n,
7552 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 linenr_T lnum;
7555
7556 if (n > 0)
7557 {
7558 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007559 /* This fails if the cursor is already in the first line or the count
7560 * is larger than the line number and '-' is in 'cpoptions' */
7561 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 return FAIL;
7563 if (n >= lnum)
7564 lnum = 1;
7565 else
7566#ifdef FEAT_FOLDING
7567 if (hasAnyFolding(curwin))
7568 {
7569 /*
7570 * Count each sequence of folded lines as one logical line.
7571 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007572 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 (void)hasFolding(lnum, &lnum, NULL);
7574
7575 while (n--)
7576 {
7577 /* move up one line */
7578 --lnum;
7579 if (lnum <= 1)
7580 break;
7581 /* If we entered a fold, move to the beginning, unless in
7582 * Insert mode or when 'foldopen' contains "all": it will open
7583 * in a moment. */
7584 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7585 (void)hasFolding(lnum, &lnum, NULL);
7586 }
7587 if (lnum < 1)
7588 lnum = 1;
7589 }
7590 else
7591#endif
7592 lnum -= n;
7593 curwin->w_cursor.lnum = lnum;
7594 }
7595
7596 /* try to advance to the column we want to be at */
7597 coladvance(curwin->w_curswant);
7598
7599 if (upd_topline)
7600 update_topline(); /* make sure curwin->w_topline is valid */
7601
7602 return OK;
7603}
7604
7605/*
7606 * Cursor down a number of logical lines.
7607 */
7608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609cursor_down(
7610 long n,
7611 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 linenr_T lnum;
7614
7615 if (n > 0)
7616 {
7617 lnum = curwin->w_cursor.lnum;
7618#ifdef FEAT_FOLDING
7619 /* Move to last line of fold, will fail if it's the end-of-file. */
7620 (void)hasFolding(lnum, NULL, &lnum);
7621#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007622 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007623 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007624 if (lnum >= curbuf->b_ml.ml_line_count
7625 || (lnum + n > curbuf->b_ml.ml_line_count
7626 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 return FAIL;
7628 if (lnum + n >= curbuf->b_ml.ml_line_count)
7629 lnum = curbuf->b_ml.ml_line_count;
7630 else
7631#ifdef FEAT_FOLDING
7632 if (hasAnyFolding(curwin))
7633 {
7634 linenr_T last;
7635
7636 /* count each sequence of folded lines as one logical line */
7637 while (n--)
7638 {
7639 if (hasFolding(lnum, NULL, &last))
7640 lnum = last + 1;
7641 else
7642 ++lnum;
7643 if (lnum >= curbuf->b_ml.ml_line_count)
7644 break;
7645 }
7646 if (lnum > curbuf->b_ml.ml_line_count)
7647 lnum = curbuf->b_ml.ml_line_count;
7648 }
7649 else
7650#endif
7651 lnum += n;
7652 curwin->w_cursor.lnum = lnum;
7653 }
7654
7655 /* try to advance to the column we want to be at */
7656 coladvance(curwin->w_curswant);
7657
7658 if (upd_topline)
7659 update_topline(); /* make sure curwin->w_topline is valid */
7660
7661 return OK;
7662}
7663
7664/*
7665 * Stuff the last inserted text in the read buffer.
7666 * Last_insert actually is a copy of the redo buffer, so we
7667 * first have to remove the command.
7668 */
7669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007670stuff_inserted(
7671 int c, /* Command character to be inserted */
7672 long count, /* Repeat this many times */
7673 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 char_u *esc_ptr;
7676 char_u *ptr;
7677 char_u *last_ptr;
7678 char_u last = NUL;
7679
7680 ptr = get_last_insert();
7681 if (ptr == NULL)
7682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 return FAIL;
7685 }
7686
7687 /* may want to stuff the command character, to start Insert mode */
7688 if (c != NUL)
7689 stuffcharReadbuff(c);
7690 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7691 *esc_ptr = NUL; /* remove the ESC */
7692
7693 /* when the last char is either "0" or "^" it will be quoted if no ESC
7694 * comes after it OR if it will inserted more than once and "ptr"
7695 * starts with ^D. -- Acevedo
7696 */
7697 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7698 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7699 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7700 {
7701 last = *last_ptr;
7702 *last_ptr = NUL;
7703 }
7704
7705 do
7706 {
7707 stuffReadbuff(ptr);
7708 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7709 if (last)
7710 stuffReadbuff((char_u *)(last == '0'
7711 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7712 : IF_EB("\026^", CTRL_V_STR "^")));
7713 }
7714 while (--count > 0);
7715
7716 if (last)
7717 *last_ptr = last;
7718
7719 if (esc_ptr != NULL)
7720 *esc_ptr = ESC; /* put the ESC back */
7721
7722 /* may want to stuff a trailing ESC, to get out of Insert mode */
7723 if (!no_esc)
7724 stuffcharReadbuff(ESC);
7725
7726 return OK;
7727}
7728
7729 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007730get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 if (last_insert == NULL)
7733 return NULL;
7734 return last_insert + last_insert_skip;
7735}
7736
7737/*
7738 * Get last inserted string, and remove trailing <Esc>.
7739 * Returns pointer to allocated memory (must be freed) or NULL.
7740 */
7741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007742get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 char_u *s;
7745 int len;
7746
7747 if (last_insert == NULL)
7748 return NULL;
7749 s = vim_strsave(last_insert + last_insert_skip);
7750 if (s != NULL)
7751 {
7752 len = (int)STRLEN(s);
7753 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7754 s[len - 1] = NUL;
7755 }
7756 return s;
7757}
7758
7759/*
7760 * Check the word in front of the cursor for an abbreviation.
7761 * Called when the non-id character "c" has been entered.
7762 * When an abbreviation is recognized it is removed from the text and
7763 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7764 */
7765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007766echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 /* Don't check for abbreviation in paste mode, when disabled and just
7769 * after moving around with cursor keys. */
7770 if (p_paste || no_abbr || arrow_used)
7771 return FALSE;
7772
7773 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7774 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7775}
7776
7777/*
7778 * replace-stack functions
7779 *
7780 * When replacing characters, the replaced characters are remembered for each
7781 * new character. This is used to re-insert the old text when backspacing.
7782 *
7783 * There is a NUL headed list of characters for each character that is
7784 * currently in the file after the insertion point. When BS is used, one NUL
7785 * headed list is put back for the deleted character.
7786 *
7787 * For a newline, there are two NUL headed lists. One contains the characters
7788 * that the NL replaced. The extra one stores the characters after the cursor
7789 * that were deleted (always white space).
7790 *
7791 * Replace_offset is normally 0, in which case replace_push will add a new
7792 * character at the end of the stack. If replace_offset is not 0, that many
7793 * characters will be left on the stack above the newly inserted character.
7794 */
7795
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007796static char_u *replace_stack = NULL;
7797static long replace_stack_nr = 0; /* next entry in replace stack */
7798static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
7800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801replace_push(
7802 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 char_u *p;
7805
7806 if (replace_stack_nr < replace_offset) /* nothing to do */
7807 return;
7808 if (replace_stack_len <= replace_stack_nr)
7809 {
7810 replace_stack_len += 50;
7811 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7812 if (p == NULL) /* out of memory */
7813 {
7814 replace_stack_len -= 50;
7815 return;
7816 }
7817 if (replace_stack != NULL)
7818 {
7819 mch_memmove(p, replace_stack,
7820 (size_t)(replace_stack_nr * sizeof(char_u)));
7821 vim_free(replace_stack);
7822 }
7823 replace_stack = p;
7824 }
7825 p = replace_stack + replace_stack_nr - replace_offset;
7826 if (replace_offset)
7827 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7828 *p = c;
7829 ++replace_stack_nr;
7830}
7831
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007832#if defined(FEAT_MBYTE) || defined(PROTO)
7833/*
7834 * Push a character onto the replace stack. Handles a multi-byte character in
7835 * reverse byte order, so that the first byte is popped off first.
7836 * Return the number of bytes done (includes composing characters).
7837 */
7838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007840{
7841 int l = (*mb_ptr2len)(p);
7842 int j;
7843
7844 for (j = l - 1; j >= 0; --j)
7845 replace_push(p[j]);
7846 return l;
7847}
7848#endif
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850/*
7851 * Pop one item from the replace stack.
7852 * return -1 if stack empty
7853 * return replaced character or NUL otherwise
7854 */
7855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 if (replace_stack_nr == 0)
7859 return -1;
7860 return (int)replace_stack[--replace_stack_nr];
7861}
7862
7863/*
7864 * Join the top two items on the replace stack. This removes to "off"'th NUL
7865 * encountered.
7866 */
7867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007868replace_join(
7869 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 int i;
7872
7873 for (i = replace_stack_nr; --i >= 0; )
7874 if (replace_stack[i] == NUL && off-- <= 0)
7875 {
7876 --replace_stack_nr;
7877 mch_memmove(replace_stack + i, replace_stack + i + 1,
7878 (size_t)(replace_stack_nr - i));
7879 return;
7880 }
7881}
7882
7883/*
7884 * Pop bytes from the replace stack until a NUL is found, and insert them
7885 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7886 */
7887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007888replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 int cc;
7891 int oldState = State;
7892
7893 State = NORMAL; /* don't want REPLACE here */
7894 while ((cc = replace_pop()) > 0)
7895 {
7896#ifdef FEAT_MBYTE
7897 mb_replace_pop_ins(cc);
7898#else
7899 ins_char(cc);
7900#endif
7901 dec_cursor();
7902 }
7903 State = oldState;
7904}
7905
7906#ifdef FEAT_MBYTE
7907/*
7908 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7909 * indicates a multi-byte char, pop the other bytes too.
7910 */
7911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007912mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007915 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 int i;
7917 int c;
7918
7919 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7920 {
7921 buf[0] = cc;
7922 for (i = 1; i < n; ++i)
7923 buf[i] = replace_pop();
7924 ins_bytes_len(buf, n);
7925 }
7926 else
7927 ins_char(cc);
7928
7929 if (enc_utf8)
7930 /* Handle composing chars. */
7931 for (;;)
7932 {
7933 c = replace_pop();
7934 if (c == -1) /* stack empty */
7935 break;
7936 if ((n = MB_BYTE2LEN(c)) == 1)
7937 {
7938 /* Not a multi-byte char, put it back. */
7939 replace_push(c);
7940 break;
7941 }
7942 else
7943 {
7944 buf[0] = c;
7945 for (i = 1; i < n; ++i)
7946 buf[i] = replace_pop();
7947 if (utf_iscomposing(utf_ptr2char(buf)))
7948 ins_bytes_len(buf, n);
7949 else
7950 {
7951 /* Not a composing char, put it back. */
7952 for (i = n - 1; i >= 0; --i)
7953 replace_push(buf[i]);
7954 break;
7955 }
7956 }
7957 }
7958}
7959#endif
7960
7961/*
7962 * make the replace stack empty
7963 * (called when exiting replace mode)
7964 */
7965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007966replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007968 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 replace_stack_len = 0;
7970 replace_stack_nr = 0;
7971}
7972
7973/*
7974 * Handle doing a BS for one character.
7975 * cc < 0: replace stack empty, just move cursor
7976 * cc == 0: character was inserted, delete it
7977 * cc > 0: character was replaced, put cc (first byte of original char) back
7978 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007979 * When "limit_col" is >= 0, don't delete before this column. Matters when
7980 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 */
7982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007983replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 int orig_len = 0;
7987 int ins_len;
7988 int orig_vcols = 0;
7989 colnr_T start_vcol;
7990 char_u *p;
7991 int i;
7992 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994 cc = replace_pop();
7995 if (cc > 0)
7996 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007997#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007998 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007999
8000 if (curbuf->b_has_textprop)
8001 {
8002 // Do not adjust text properties for individual delete and insert
8003 // operations, do it afterwards on the resulting text.
8004 len_before = STRLEN(ml_get_curline());
8005 ++text_prop_frozen;
8006 }
8007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (State & VREPLACE_FLAG)
8009 {
8010 /* Get the number of screen cells used by the character we are
8011 * going to delete. */
8012 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
8013 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
8014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015#ifdef FEAT_MBYTE
8016 if (has_mbyte)
8017 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008018 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008020 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 replace_push(cc);
8022 }
8023 else
8024#endif
8025 {
8026 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008028 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030 replace_pop_ins();
8031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (State & VREPLACE_FLAG)
8033 {
8034 /* Get the number of screen cells used by the inserted characters */
8035 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008036 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 vcol = start_vcol;
8038 for (i = 0; i < ins_len; ++i)
8039 {
8040 vcol += chartabsize(p + i, vcol);
8041#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008042 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043#endif
8044 }
8045 vcol -= start_vcol;
8046
8047 /* Delete spaces that were inserted after the cursor to keep the
8048 * text aligned. */
8049 curwin->w_cursor.col += ins_len;
8050 while (vcol > orig_vcols && gchar_cursor() == ' ')
8051 {
8052 del_char(FALSE);
8053 ++orig_vcols;
8054 }
8055 curwin->w_cursor.col -= ins_len;
8056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057
Bram Moolenaar196d1572019-01-02 23:47:18 +01008058 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01008060
8061#ifdef FEAT_TEXT_PROP
8062 if (curbuf->b_has_textprop)
8063 {
8064 size_t len_now = STRLEN(ml_get_curline());
8065
8066 --text_prop_frozen;
8067 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
8068 (int)(len_now - len_before));
8069 }
8070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 }
8072 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008073 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074}
8075
8076#ifdef FEAT_CINDENT
8077/*
8078 * Return TRUE if C-indenting is on.
8079 */
8080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008081cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083 return (!p_paste && (curbuf->b_p_cin
8084# ifdef FEAT_EVAL
8085 || *curbuf->b_p_inde != NUL
8086# endif
8087 ));
8088}
8089#endif
8090
8091#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8092/*
8093 * Re-indent the current line, based on the current contents of it and the
8094 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8095 * confused what all the part that handles Control-T is doing that I'm not.
8096 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8097 */
8098
8099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008100fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008102 int amount = get_the_indent();
8103
8104 if (amount >= 0)
8105 {
8106 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8107 if (linewhite(curwin->w_cursor.lnum))
8108 did_ai = TRUE; /* delete the indent if the line stays empty */
8109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110}
8111
8112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008113fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 if (p_paste)
8116 return;
8117# ifdef FEAT_LISP
8118 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8119 fixthisline(get_lisp_indent);
8120# endif
8121# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8122 else
8123# endif
8124# ifdef FEAT_CINDENT
8125 if (cindent_on())
8126 do_c_expr_indent();
8127# endif
8128}
8129
8130#endif
8131
8132#ifdef FEAT_CINDENT
8133/*
8134 * return TRUE if 'cinkeys' contains the key "keytyped",
8135 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008136 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8138 *
8139 * "keytyped" can have a few special values:
8140 * KEY_OPEN_FORW
8141 * KEY_OPEN_BACK
8142 * KEY_COMPLETE just finished completion.
8143 *
8144 * If line_is_empty is TRUE accept keys with '0' before them.
8145 */
8146 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008147in_cinkeys(
8148 int keytyped,
8149 int when,
8150 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 char_u *look;
8153 int try_match;
8154 int try_match_word;
8155 char_u *p;
8156 char_u *line;
8157 int icase;
8158 int i;
8159
Bram Moolenaar30848942009-12-24 14:46:12 +00008160 if (keytyped == NUL)
8161 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8162 return FALSE;
8163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164#ifdef FEAT_EVAL
8165 if (*curbuf->b_p_inde != NUL)
8166 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8167 else
8168#endif
8169 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8170 while (*look)
8171 {
8172 /*
8173 * Find out if we want to try a match with this key, depending on
8174 * 'when' and a '*' or '!' before the key.
8175 */
8176 switch (when)
8177 {
8178 case '*': try_match = (*look == '*'); break;
8179 case '!': try_match = (*look == '!'); break;
8180 default: try_match = (*look != '*'); break;
8181 }
8182 if (*look == '*' || *look == '!')
8183 ++look;
8184
8185 /*
8186 * If there is a '0', only accept a match if the line is empty.
8187 * But may still match when typing last char of a word.
8188 */
8189 if (*look == '0')
8190 {
8191 try_match_word = try_match;
8192 if (!line_is_empty)
8193 try_match = FALSE;
8194 ++look;
8195 }
8196 else
8197 try_match_word = FALSE;
8198
8199 /*
8200 * does it look like a control character?
8201 */
8202 if (*look == '^'
8203#ifdef EBCDIC
8204 && (Ctrl_chr(look[1]) != 0)
8205#else
8206 && look[1] >= '?' && look[1] <= '_'
8207#endif
8208 )
8209 {
8210 if (try_match && keytyped == Ctrl_chr(look[1]))
8211 return TRUE;
8212 look += 2;
8213 }
8214 /*
8215 * 'o' means "o" command, open forward.
8216 * 'O' means "O" command, open backward.
8217 */
8218 else if (*look == 'o')
8219 {
8220 if (try_match && keytyped == KEY_OPEN_FORW)
8221 return TRUE;
8222 ++look;
8223 }
8224 else if (*look == 'O')
8225 {
8226 if (try_match && keytyped == KEY_OPEN_BACK)
8227 return TRUE;
8228 ++look;
8229 }
8230
8231 /*
8232 * 'e' means to check for "else" at start of line and just before the
8233 * cursor.
8234 */
8235 else if (*look == 'e')
8236 {
8237 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8238 {
8239 p = ml_get_curline();
8240 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8241 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8242 return TRUE;
8243 }
8244 ++look;
8245 }
8246
8247 /*
8248 * ':' only causes an indent if it is at the end of a label or case
8249 * statement, or when it was before typing the ':' (to fix
8250 * class::method for C++).
8251 */
8252 else if (*look == ':')
8253 {
8254 if (try_match && keytyped == ':')
8255 {
8256 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008257 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008259 /* Need to get the line again after cin_islabel(). */
8260 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 if (curwin->w_cursor.col > 2
8262 && p[curwin->w_cursor.col - 1] == ':'
8263 && p[curwin->w_cursor.col - 2] == ':')
8264 {
8265 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008266 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008267 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 p = ml_get_curline();
8269 p[curwin->w_cursor.col - 1] = ':';
8270 if (i)
8271 return TRUE;
8272 }
8273 }
8274 ++look;
8275 }
8276
8277
8278 /*
8279 * Is it a key in <>, maybe?
8280 */
8281 else if (*look == '<')
8282 {
8283 if (try_match)
8284 {
8285 /*
8286 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8287 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8288 * >, *, : and ! keys if they really really want to.
8289 */
8290 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8291 && keytyped == look[1])
8292 return TRUE;
8293
8294 if (keytyped == get_special_key_code(look + 1))
8295 return TRUE;
8296 }
8297 while (*look && *look != '>')
8298 look++;
8299 while (*look == '>')
8300 look++;
8301 }
8302
8303 /*
8304 * Is it a word: "=word"?
8305 */
8306 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8307 {
8308 ++look;
8309 if (*look == '~')
8310 {
8311 icase = TRUE;
8312 ++look;
8313 }
8314 else
8315 icase = FALSE;
8316 p = vim_strchr(look, ',');
8317 if (p == NULL)
8318 p = look + STRLEN(look);
8319 if ((try_match || try_match_word)
8320 && curwin->w_cursor.col >= (colnr_T)(p - look))
8321 {
8322 int match = FALSE;
8323
8324#ifdef FEAT_INS_EXPAND
8325 if (keytyped == KEY_COMPLETE)
8326 {
8327 char_u *s;
8328
8329 /* Just completed a word, check if it starts with "look".
8330 * search back for the start of a word. */
8331 line = ml_get_curline();
8332# ifdef FEAT_MBYTE
8333 if (has_mbyte)
8334 {
8335 char_u *n;
8336
8337 for (s = line + curwin->w_cursor.col; s > line; s = n)
8338 {
8339 n = mb_prevptr(line, s);
8340 if (!vim_iswordp(n))
8341 break;
8342 }
8343 }
8344 else
8345# endif
8346 for (s = line + curwin->w_cursor.col; s > line; --s)
8347 if (!vim_iswordc(s[-1]))
8348 break;
8349 if (s + (p - look) <= line + curwin->w_cursor.col
8350 && (icase
8351 ? MB_STRNICMP(s, look, p - look)
8352 : STRNCMP(s, look, p - look)) == 0)
8353 match = TRUE;
8354 }
8355 else
8356#endif
8357 /* TODO: multi-byte */
8358 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8359 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8360 {
8361 line = ml_get_cursor();
8362 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8363 || !vim_iswordc(line[-(p - look) - 1]))
8364 && (icase
8365 ? MB_STRNICMP(line - (p - look), look, p - look)
8366 : STRNCMP(line - (p - look), look, p - look))
8367 == 0)
8368 match = TRUE;
8369 }
8370 if (match && try_match_word && !try_match)
8371 {
8372 /* "0=word": Check if there are only blanks before the
8373 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008374 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 (int)(curwin->w_cursor.col - (p - look)))
8376 match = FALSE;
8377 }
8378 if (match)
8379 return TRUE;
8380 }
8381 look = p;
8382 }
8383
8384 /*
8385 * ok, it's a boring generic character.
8386 */
8387 else
8388 {
8389 if (try_match && *look == keytyped)
8390 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008391 if (*look != NUL)
8392 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 }
8394
8395 /*
8396 * Skip over ", ".
8397 */
8398 look = skip_to_option_part(look);
8399 }
8400 return FALSE;
8401}
8402#endif /* FEAT_CINDENT */
8403
8404#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8405/*
8406 * Map Hebrew keyboard when in hkmap mode.
8407 */
8408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008409hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
8411 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8412 {
8413 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8414 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8415 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8416 static char_u map[26] =
8417 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8418 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8419 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8420 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8421 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8422 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8423 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8424 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8425 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8426
8427 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8428 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8429 /* '-1'='sofit' */
8430 else if (c == 'x')
8431 return 'X';
8432 else if (c == 'q')
8433 return '\''; /* {geresh}={'} */
8434 else if (c == 246)
8435 return ' '; /* \"o --> ' ' for a german keyboard */
8436 else if (c == 228)
8437 return ' '; /* \"a --> ' ' -- / -- */
8438 else if (c == 252)
8439 return ' '; /* \"u --> ' ' -- / -- */
8440#ifdef EBCDIC
8441 else if (islower(c))
8442#else
8443 /* NOTE: islower() does not do the right thing for us on Linux so we
8444 * do this the same was as 5.7 and previous, so it works correctly on
8445 * all systems. Specifically, the e.g. Delete and Arrow keys are
8446 * munged and won't work if e.g. searching for Hebrew text.
8447 */
8448 else if (c >= 'a' && c <= 'z')
8449#endif
8450 return (int)(map[CharOrdLow(c)] + p_aleph);
8451 else
8452 return c;
8453 }
8454 else
8455 {
8456 switch (c)
8457 {
8458 case '`': return ';';
8459 case '/': return '.';
8460 case '\'': return ',';
8461 case 'q': return '/';
8462 case 'w': return '\'';
8463
8464 /* Hebrew letters - set offset from 'a' */
8465 case ',': c = '{'; break;
8466 case '.': c = 'v'; break;
8467 case ';': c = 't'; break;
8468 default: {
8469 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8470
8471#ifdef EBCDIC
8472 /* see note about islower() above */
8473 if (!islower(c))
8474#else
8475 if (c < 'a' || c > 'z')
8476#endif
8477 return c;
8478 c = str[CharOrdLow(c)];
8479 break;
8480 }
8481 }
8482
8483 return (int)(CharOrdLow(c) + p_aleph);
8484 }
8485}
8486#endif
8487
8488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008489ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 int need_redraw = FALSE;
8492 int regname;
8493 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008494 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
8496 /*
8497 * If we are going to wait for a character, show a '"'.
8498 */
8499 pc_status = PC_STATUS_UNSET;
8500 if (redrawing() && !char_avail())
8501 {
8502 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008503 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504
8505 edit_putchar('"', TRUE);
8506#ifdef FEAT_CMDL_INFO
8507 add_to_showcmd_c(Ctrl_R);
8508#endif
8509 }
8510
8511#ifdef USE_ON_FLY_SCROLL
8512 dont_scroll = TRUE; /* disallow scrolling here */
8513#endif
8514
8515 /*
8516 * Don't map the register name. This also prevents the mode message to be
8517 * deleted when ESC is hit.
8518 */
8519 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008520 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8523 {
8524 /* Get a third key for literal register insertion */
8525 literally = regname;
8526#ifdef FEAT_CMDL_INFO
8527 add_to_showcmd_c(literally);
8528#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008529 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 }
8532 --no_mapping;
8533
8534#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008535 /* Don't call u_sync() while typing the expression or giving an error
8536 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 ++no_u_sync;
8538 if (regname == '=')
8539 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008540 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008541# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008543# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008544 /* Sync undo when evaluating the expression calls setline() or
8545 * append(), so that it can be undone separately. */
8546 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008547
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008549
8550 // Cursor may be moved back a column.
8551 curwin->w_cursor = curpos;
8552 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008553# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008554 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (im_on)
8556 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008559 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8560 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008561 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 else
8565 {
8566#endif
8567 if (literally == Ctrl_O || literally == Ctrl_P)
8568 {
8569 /* Append the command to the redo buffer. */
8570 AppendCharToRedobuff(Ctrl_R);
8571 AppendCharToRedobuff(literally);
8572 AppendCharToRedobuff(regname);
8573
8574 do_put(regname, BACKWARD, 1L,
8575 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8576 }
8577 else if (insert_reg(regname, literally) == FAIL)
8578 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008579 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 need_redraw = TRUE; /* remove the '"' */
8581 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008582 else if (stop_insert_mode)
8583 /* When the '=' register was used and a function was invoked that
8584 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8585 * insert anything, need to remove the '"' */
8586 need_redraw = TRUE;
8587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#ifdef FEAT_EVAL
8589 }
8590 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008591 if (u_sync_once == 1)
8592 ins_need_undo = TRUE;
8593 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#endif
8595#ifdef FEAT_CMDL_INFO
8596 clear_showcmd();
8597#endif
8598
8599 /* If the inserted register is empty, we need to remove the '"' */
8600 if (need_redraw || stuff_empty())
8601 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008602
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008603 /* Disallow starting Visual mode here, would get a weird mode. */
8604 if (!vis_active && VIsual_active)
8605 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606}
8607
8608/*
8609 * CTRL-G commands in Insert mode.
8610 */
8611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008612ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
8614 int c;
8615
8616#ifdef FEAT_INS_EXPAND
8617 /* Right after CTRL-X the cursor will be after the ruler. */
8618 setcursor();
8619#endif
8620
8621 /*
8622 * Don't map the second key. This also prevents the mode message to be
8623 * deleted when ESC is hit.
8624 */
8625 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008626 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 --no_mapping;
8628 switch (c)
8629 {
8630 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8631 case K_UP:
8632 case Ctrl_K:
8633 case 'k': ins_up(TRUE);
8634 break;
8635
8636 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8637 case K_DOWN:
8638 case Ctrl_J:
8639 case 'j': ins_down(TRUE);
8640 break;
8641
8642 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008643 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008645
8646 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008647 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008648 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008649 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 break;
8651
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008652 /* CTRL-G U: do not break undo with the next char */
8653 case 'U':
8654 /* Allow one left/right cursor movement with the next char,
8655 * without breaking undo. */
8656 dont_sync_undo = MAYBE;
8657 break;
8658
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008660 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662}
8663
8664/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008665 * CTRL-^ in Insert mode.
8666 */
8667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008668ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008669{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008670 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008671 {
8672 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8673 if (State & LANGMAP)
8674 {
8675 curbuf->b_p_iminsert = B_IMODE_NONE;
8676 State &= ~LANGMAP;
8677 }
8678 else
8679 {
8680 curbuf->b_p_iminsert = B_IMODE_LMAP;
8681 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008682#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008683 im_set_active(FALSE);
8684#endif
8685 }
8686 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008687#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008688 else
8689 {
8690 /* There are no ":lmap" mappings, toggle IM */
8691 if (im_get_status())
8692 {
8693 curbuf->b_p_iminsert = B_IMODE_NONE;
8694 im_set_active(FALSE);
8695 }
8696 else
8697 {
8698 curbuf->b_p_iminsert = B_IMODE_IM;
8699 State &= ~LANGMAP;
8700 im_set_active(TRUE);
8701 }
8702 }
8703#endif
8704 set_iminsert_global();
8705 showmode();
8706#ifdef FEAT_GUI
8707 /* may show different cursor shape or color */
8708 if (gui.in_use)
8709 gui_update_cursor(TRUE, FALSE);
8710#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008711#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008712 /* Show/unshow value of 'keymap' in status lines. */
8713 status_redraw_curbuf();
8714#endif
8715}
8716
8717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 * Handle ESC in insert mode.
8719 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8720 * insert.
8721 */
8722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008723ins_esc(
8724 long *count,
8725 int cmdchar,
8726 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 int temp;
8729 static int disabled_redraw = FALSE;
8730
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008731#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008732 check_spell_redraw();
8733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#if defined(FEAT_HANGULIN)
8735# if defined(ESC_CHG_TO_ENG_MODE)
8736 hangul_input_state_set(0);
8737# endif
8738 if (composing_hangul)
8739 {
8740 push_raw_key(composing_hangul_buffer, 2);
8741 composing_hangul = 0;
8742 }
8743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
8745 temp = curwin->w_cursor.col;
8746 if (disabled_redraw)
8747 {
8748 --RedrawingDisabled;
8749 disabled_redraw = FALSE;
8750 }
8751 if (!arrow_used)
8752 {
8753 /*
8754 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008755 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8756 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 */
8758 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008759 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
8761 /*
8762 * Repeating insert may take a long time. Check for
8763 * interrupt now and then.
8764 */
8765 if (*count > 0)
8766 {
8767 line_breakcheck();
8768 if (got_int)
8769 *count = 0;
8770 }
8771
8772 if (--*count > 0) /* repeat what was typed */
8773 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008774 /* Vi repeats the insert without replacing characters. */
8775 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8776 State &= ~REPLACE_FLAG;
8777
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 (void)start_redo_ins();
8779 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008780 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 ++RedrawingDisabled;
8782 disabled_redraw = TRUE;
8783 return FALSE; /* repeat the insert */
8784 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008785 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 undisplay_dollar();
8787 }
8788
8789 /* When an autoindent was removed, curswant stays after the
8790 * indent */
8791 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8792 curwin->w_set_curswant = TRUE;
8793
8794 /* Remember the last Insert position in the '^ mark. */
8795 if (!cmdmod.keepjumps)
8796 curbuf->b_last_insert = curwin->w_cursor;
8797
8798 /*
8799 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008800 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008802 if (!nomove
8803 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#ifdef FEAT_VIRTUALEDIT
8805 || curwin->w_cursor.coladd > 0
8806#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008807 )
8808 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008809 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810#ifdef FEAT_RIGHTLEFT
8811 && !revins_on
8812#endif
8813 )
8814 {
8815#ifdef FEAT_VIRTUALEDIT
8816 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8817 {
8818 oneleft();
8819 if (restart_edit != NUL)
8820 ++curwin->w_cursor.coladd;
8821 }
8822 else
8823#endif
8824 {
8825 --curwin->w_cursor.col;
8826#ifdef FEAT_MBYTE
8827 /* Correct cursor for multi-byte character. */
8828 if (has_mbyte)
8829 mb_adjust_cursor();
8830#endif
8831 }
8832 }
8833
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008834#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 /* Disable IM to allow typing English directly for Normal mode commands.
8836 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8837 * well). */
8838 if (!(State & LANGMAP))
8839 im_save_status(&curbuf->b_p_iminsert);
8840 im_set_active(FALSE);
8841#endif
8842
8843 State = NORMAL;
8844 /* need to position cursor again (e.g. when on a TAB ) */
8845 changed_cline_bef_curs();
8846
8847#ifdef FEAT_MOUSE
8848 setmouse();
8849#endif
8850#ifdef CURSOR_SHAPE
8851 ui_cursor_shape(); /* may show different cursor shape */
8852#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008853 if (!p_ek)
8854 /* Re-enable bracketed paste mode. */
8855 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 /*
8858 * When recording or for CTRL-O, need to display the new mode.
8859 * Otherwise remove the mode message.
8860 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008861 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 showmode();
8863 else if (p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008864 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
8866 return TRUE; /* exit Insert mode */
8867}
8868
8869#ifdef FEAT_RIGHTLEFT
8870/*
8871 * Toggle language: hkmap and revins_on.
8872 * Move to end of reverse inserted text.
8873 */
8874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008875ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 if (revins_on && revins_chars && revins_scol >= 0)
8878 {
8879 while (gchar_cursor() != NUL && revins_chars--)
8880 ++curwin->w_cursor.col;
8881 }
8882 p_ri = !p_ri;
8883 revins_on = (State == INSERT && p_ri);
8884 if (revins_on)
8885 {
8886 revins_scol = curwin->w_cursor.col;
8887 revins_legal++;
8888 revins_chars = 0;
8889 undisplay_dollar();
8890 }
8891 else
8892 revins_scol = -1;
8893#ifdef FEAT_FKMAP
8894 if (p_altkeymap)
8895 {
8896 /*
8897 * to be consistent also for redo command, using '.'
8898 * set arrow_used to true and stop it - causing to redo
8899 * characters entered in one mode (normal/reverse insert).
8900 */
8901 arrow_used = TRUE;
8902 (void)stop_arrow();
8903 p_fkmap = curwin->w_p_rl ^ p_ri;
8904 if (p_fkmap && p_ri)
8905 State = INSERT;
8906 }
8907 else
8908#endif
8909 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8910 showmode();
8911}
8912#endif
8913
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914/*
8915 * If 'keymodel' contains "startsel", may start selection.
8916 * Returns TRUE when a CTRL-O and other keys stuffed.
8917 */
8918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008919ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920{
8921 if (km_startsel)
8922 switch (c)
8923 {
8924 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 case K_PAGEUP:
8927 case K_KPAGEUP:
8928 case K_PAGEDOWN:
8929 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008930# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 case K_LEFT:
8932 case K_RIGHT:
8933 case K_UP:
8934 case K_DOWN:
8935 case K_END:
8936 case K_HOME:
8937# endif
8938 if (!(mod_mask & MOD_MASK_SHIFT))
8939 break;
8940 /* FALLTHROUGH */
8941 case K_S_LEFT:
8942 case K_S_RIGHT:
8943 case K_S_UP:
8944 case K_S_DOWN:
8945 case K_S_END:
8946 case K_S_HOME:
8947 /* Start selection right away, the cursor can move with
8948 * CTRL-O when beyond the end of the line. */
8949 start_selection();
8950
8951 /* Execute the key in (insert) Select mode. */
8952 stuffcharReadbuff(Ctrl_O);
8953 if (mod_mask)
8954 {
8955 char_u buf[4];
8956
8957 buf[0] = K_SPECIAL;
8958 buf[1] = KS_MODIFIER;
8959 buf[2] = mod_mask;
8960 buf[3] = NUL;
8961 stuffReadbuff(buf);
8962 }
8963 stuffcharReadbuff(c);
8964 return TRUE;
8965 }
8966 return FALSE;
8967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
8969/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008970 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008971 */
8972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008974{
8975#ifdef FEAT_FKMAP
8976 if (p_fkmap && p_ri)
8977 {
8978 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008979 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008980 return;
8981 }
8982#endif
8983
Bram Moolenaar1e015462005-09-25 22:16:38 +00008984# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008985 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008986 (char_u *)((State & REPLACE_FLAG) ? "i"
8987 : replaceState == VREPLACE ? "v"
8988 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008989# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008990 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008991 if (State & REPLACE_FLAG)
8992 State = INSERT | (State & LANGMAP);
8993 else
8994 State = replaceState | (State & LANGMAP);
8995 AppendCharToRedobuff(K_INS);
8996 showmode();
8997#ifdef CURSOR_SHAPE
8998 ui_cursor_shape(); /* may show different cursor shape */
8999#endif
9000}
9001
9002/*
9003 * Pressed CTRL-O in Insert mode.
9004 */
9005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009006ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009007{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009008 if (State & VREPLACE_FLAG)
9009 restart_edit = 'V';
9010 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009011 if (State & REPLACE_FLAG)
9012 restart_edit = 'R';
9013 else
9014 restart_edit = 'I';
9015#ifdef FEAT_VIRTUALEDIT
9016 if (virtual_active())
9017 ins_at_eol = FALSE; /* cursor always keeps its column */
9018 else
9019#endif
9020 ins_at_eol = (gchar_cursor() == NUL);
9021}
9022
9023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 * If the cursor is on an indent, ^T/^D insert/delete one
9025 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009026 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 * with vi. But vi only supports ^T and ^D after an
9028 * autoindent, we support it everywhere.
9029 */
9030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009031ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 if (stop_arrow() == FAIL)
9034 return;
9035 AppendCharToRedobuff(c);
9036
9037 /*
9038 * 0^D and ^^D: remove all indent.
9039 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009040 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9041 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 {
9043 --curwin->w_cursor.col;
9044 (void)del_char(FALSE); /* delete the '^' or '0' */
9045 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9046 if (State & REPLACE_FLAG)
9047 replace_pop_ins();
9048 if (lastc == '^')
9049 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009050 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 }
9052 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009053 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
9055 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9056 did_ai = FALSE;
9057#ifdef FEAT_SMARTINDENT
9058 did_si = FALSE;
9059 can_si = FALSE;
9060 can_si_back = FALSE;
9061#endif
9062#ifdef FEAT_CINDENT
9063 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9064#endif
9065}
9066
9067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070 int temp;
9071
9072 if (stop_arrow() == FAIL)
9073 return;
9074 if (gchar_cursor() == NUL) /* delete newline */
9075 {
9076 temp = curwin->w_cursor.col;
9077 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009078 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009079 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009083 /* Adjust orig_line_count in case more lines have been deleted than
9084 * have been added. That makes sure, that open_line() later
9085 * can access all buffer lines correctly */
9086 if (State & VREPLACE_FLAG &&
9087 orig_line_count > curbuf->b_ml.ml_line_count)
9088 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009091 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9092 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 did_ai = FALSE;
9094#ifdef FEAT_SMARTINDENT
9095 did_si = FALSE;
9096 can_si = FALSE;
9097 can_si_back = FALSE;
9098#endif
9099 AppendCharToRedobuff(K_DEL);
9100}
9101
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009102/*
9103 * Delete one character for ins_bs().
9104 */
9105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009107{
9108 dec_cursor();
9109 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9110 if (State & REPLACE_FLAG)
9111 {
9112 /* Don't delete characters before the insert point when in
9113 * Replace mode */
9114 if (curwin->w_cursor.lnum != Insstart.lnum
9115 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009116 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009117 }
9118 else
9119 (void)del_char(FALSE);
9120}
9121
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122/*
9123 * Handle Backspace, delete-word and delete-line in Insert mode.
9124 * Return TRUE when backspace was actually used.
9125 */
9126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009127ins_bs(
9128 int c,
9129 int mode,
9130 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 linenr_T lnum;
9133 int cc;
9134 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009135 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 colnr_T mincol;
9137 int did_backspace = FALSE;
9138 int in_indent;
9139 int oldState;
9140#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009141 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#endif
9143
9144 /*
9145 * can't delete anything in an empty file
9146 * can't backup past first character in buffer
9147 * can't backup past starting point unless 'backspace' > 1
9148 * can backup to a previous line if 'backspace' == 0
9149 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009150 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 || (
9152#ifdef FEAT_RIGHTLEFT
9153 !revins_on &&
9154#endif
9155 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9156 || (!can_bs(BS_START)
9157 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009158 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9159 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9161 && curwin->w_cursor.col <= ai_col)
9162 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9163 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009164 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 return FALSE;
9166 }
9167
9168 if (stop_arrow() == FAIL)
9169 return FALSE;
9170 in_indent = inindent(0);
9171#ifdef FEAT_CINDENT
9172 if (in_indent)
9173 can_cindent = FALSE;
9174#endif
9175#ifdef FEAT_COMMENTS
9176 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9177#endif
9178#ifdef FEAT_RIGHTLEFT
9179 if (revins_on) /* put cursor after last inserted char */
9180 inc_cursor();
9181#endif
9182
9183#ifdef FEAT_VIRTUALEDIT
9184 /* Virtualedit:
9185 * BACKSPACE_CHAR eats a virtual space
9186 * BACKSPACE_WORD eats all coladd
9187 * BACKSPACE_LINE eats all coladd and keeps going
9188 */
9189 if (curwin->w_cursor.coladd > 0)
9190 {
9191 if (mode == BACKSPACE_CHAR)
9192 {
9193 --curwin->w_cursor.coladd;
9194 return TRUE;
9195 }
9196 if (mode == BACKSPACE_WORD)
9197 {
9198 curwin->w_cursor.coladd = 0;
9199 return TRUE;
9200 }
9201 curwin->w_cursor.coladd = 0;
9202 }
9203#endif
9204
9205 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009206 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 */
9208 if (curwin->w_cursor.col == 0)
9209 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009210 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009211 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#ifdef FEAT_RIGHTLEFT
9213 || revins_on
9214#endif
9215 )
9216 {
9217 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9218 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9219 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009220 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009221 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 }
9223 /*
9224 * In replace mode:
9225 * cc < 0: NL was inserted, delete it
9226 * cc >= 0: NL was replaced, put original characters back
9227 */
9228 cc = -1;
9229 if (State & REPLACE_FLAG)
9230 cc = replace_pop(); /* returns -1 if NL was inserted */
9231 /*
9232 * In replace mode, in the line we started replacing, we only move the
9233 * cursor.
9234 */
9235 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9236 {
9237 dec_cursor();
9238 }
9239 else
9240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if (!(State & VREPLACE_FLAG)
9242 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 {
9244 temp = gchar_cursor(); /* remember current char */
9245 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009246
9247 /* When "aw" is in 'formatoptions' we must delete the space at
9248 * the end of the line, otherwise the line will be broken
9249 * again when auto-formatting. */
9250 if (has_format_option(FO_AUTO)
9251 && has_format_option(FO_WHITE_PAR))
9252 {
9253 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9254 TRUE);
9255 int len;
9256
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009257 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009258 if (len > 0 && ptr[len - 1] == ' ')
9259 ptr[len - 1] = NUL;
9260 }
9261
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009262 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 if (temp == NUL && gchar_cursor() != NUL)
9264 inc_cursor();
9265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 else
9267 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268
9269 /*
9270 * In REPLACE mode we have to put back the text that was replaced
9271 * by the NL. On the replace stack is first a NUL-terminated
9272 * sequence of characters that were deleted and then the
9273 * characters that NL replaced.
9274 */
9275 if (State & REPLACE_FLAG)
9276 {
9277 /*
9278 * Do the next ins_char() in NORMAL state, to
9279 * prevent ins_char() from replacing characters and
9280 * avoiding showmatch().
9281 */
9282 oldState = State;
9283 State = NORMAL;
9284 /*
9285 * restore characters (blanks) deleted after cursor
9286 */
9287 while (cc > 0)
9288 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009289 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#ifdef FEAT_MBYTE
9291 mb_replace_pop_ins(cc);
9292#else
9293 ins_char(cc);
9294#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009295 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 cc = replace_pop();
9297 }
9298 /* restore the characters that NL replaced */
9299 replace_pop_ins();
9300 State = oldState;
9301 }
9302 }
9303 did_ai = FALSE;
9304 }
9305 else
9306 {
9307 /*
9308 * Delete character(s) before the cursor.
9309 */
9310#ifdef FEAT_RIGHTLEFT
9311 if (revins_on) /* put cursor on last inserted char */
9312 dec_cursor();
9313#endif
9314 mincol = 0;
9315 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009316 if (mode == BACKSPACE_LINE
9317 && (curbuf->b_p_ai
9318#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009319 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009320#endif
9321 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322#ifdef FEAT_RIGHTLEFT
9323 && !revins_on
9324#endif
9325 )
9326 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009327 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009329 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009331 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 }
9333
9334 /*
9335 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9336 */
9337 if ( mode == BACKSPACE_CHAR
9338 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009339 || ((get_sts_value() != 0
9340#ifdef FEAT_VARTABS
9341 || tabstop_count(curbuf->b_p_vsts_array)
9342#endif
9343 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009344 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 && (*(ml_get_cursor() - 1) == TAB
9346 || (*(ml_get_cursor() - 1) == ' '
9347 && (!*inserted_space_p
9348 || arrow_used))))))
9349 {
9350 int ts;
9351 colnr_T vcol;
9352 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009353 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 /* Compute the virtual column where we want to be. Since
9357 * 'showbreak' may get in the way, need to get the last column of
9358 * the previous character. */
9359 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009360 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 dec_cursor();
9362 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9363 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009364#ifdef FEAT_VARTABS
9365 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009366 {
9367 ts = (int)get_sw_value(curbuf);
9368 want_vcol = (want_vcol / ts) * ts;
9369 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009370 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009371 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009372 curbuf->b_p_vsts_array);
9373#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009374 if (p_sta && in_indent)
9375 ts = (int)get_sw_value(curbuf);
9376 else
9377 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
9381 /* delete characters until we are at or before want_vcol */
9382 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009383 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009384 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
9386 /* insert extra spaces until we are at want_vcol */
9387 while (vcol < want_vcol)
9388 {
9389 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009390 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9391 && curwin->w_cursor.col < Insstart_orig.col)
9392 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (State & VREPLACE_FLAG)
9395 ins_char(' ');
9396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 {
9398 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009399 if ((State & REPLACE_FLAG))
9400 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 }
9402 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9403 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009404
9405 /* If we are now back where we started delete one character. Can
9406 * happen when using 'sts' and 'linebreak'. */
9407 if (vcol >= start_vcol)
9408 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
9410
9411 /*
9412 * Delete upto starting point, start of line or previous word.
9413 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009416#ifdef FEAT_MBYTE
9417 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009419 if (has_mbyte)
9420 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009422 do
9423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009425 if (!revins_on) /* put cursor on char to be deleted */
9426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009428
9429 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009431 /* look multi-byte character class */
9432 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009434 prev_cclass = cclass;
9435 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009438
9439 /* start of word? */
9440 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9441 {
9442 mode = BACKSPACE_WORD_NOT_SPACE;
9443 temp = vim_iswordc(cc);
9444 }
9445 /* end of word? */
9446 else if (mode == BACKSPACE_WORD_NOT_SPACE
9447 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9448#ifdef FEAT_MBYTE
9449 || prev_cclass != cclass
9450#endif
9451 ))
9452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009454 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009456 inc_cursor();
9457#ifdef FEAT_RIGHTLEFT
9458 else if (State & REPLACE_FLAG)
9459 dec_cursor();
9460#endif
9461 break;
9462 }
9463 if (State & REPLACE_FLAG)
9464 replace_do_bs(-1);
9465 else
9466 {
9467#ifdef FEAT_MBYTE
9468 if (enc_utf8 && p_deco)
9469 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9470#endif
9471 (void)del_char(FALSE);
9472#ifdef FEAT_MBYTE
9473 /*
9474 * If there are combining characters and 'delcombine' is set
9475 * move the cursor back. Don't back up before the base
9476 * character.
9477 */
9478 if (enc_utf8 && p_deco && cpc[0] != NUL)
9479 inc_cursor();
9480#endif
9481#ifdef FEAT_RIGHTLEFT
9482 if (revins_chars)
9483 {
9484 revins_chars--;
9485 revins_legal++;
9486 }
9487 if (revins_on && gchar_cursor() == NUL)
9488 break;
9489#endif
9490 }
9491 /* Just a single backspace?: */
9492 if (mode == BACKSPACE_CHAR)
9493 break;
9494 } while (
9495#ifdef FEAT_RIGHTLEFT
9496 revins_on ||
9497#endif
9498 (curwin->w_cursor.col > mincol
9499 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9500 || curwin->w_cursor.col != Insstart_orig.col)));
9501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 did_backspace = TRUE;
9503 }
9504#ifdef FEAT_SMARTINDENT
9505 did_si = FALSE;
9506 can_si = FALSE;
9507 can_si_back = FALSE;
9508#endif
9509 if (curwin->w_cursor.col <= 1)
9510 did_ai = FALSE;
9511 /*
9512 * It's a little strange to put backspaces into the redo
9513 * buffer, but it makes auto-indent a lot easier to deal
9514 * with.
9515 */
9516 AppendCharToRedobuff(c);
9517
9518 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009519 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009520 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009521 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522
9523 /* vi behaviour: the cursor moves backward but the character that
9524 * was there remains visible
9525 * Vim behaviour: the cursor moves backward and the character that
9526 * was there is erased from the screen.
9527 * We can emulate the vi behaviour by pretending there is a dollar
9528 * displayed even when there isn't.
9529 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009530 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 dollar_vcol = curwin->w_virtcol;
9532
Bram Moolenaarce3be472008-01-14 19:12:28 +00009533#ifdef FEAT_FOLDING
9534 /* When deleting a char the cursor line must never be in a closed fold.
9535 * E.g., when 'foldmethod' is indent and deleting the first non-white
9536 * char before a Tab. */
9537 if (did_backspace)
9538 foldOpenCursor();
9539#endif
9540
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 return did_backspace;
9542}
9543
9544#ifdef FEAT_MOUSE
9545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009549 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
9551# ifdef FEAT_GUI
9552 /* When GUI is active, also move/paste when 'mouse' is empty */
9553 if (!gui.in_use)
9554# endif
9555 if (!mouse_has(MOUSE_INSERT))
9556 return;
9557
9558 undisplay_dollar();
9559 tpos = curwin->w_cursor;
9560 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9561 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009562 win_T *new_curwin = curwin;
9563
9564 if (curwin != old_curwin && win_valid(old_curwin))
9565 {
9566 /* Mouse took us to another window. We need to go back to the
9567 * previous one to stop insert there properly. */
9568 curwin = old_curwin;
9569 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009570#ifdef FEAT_JOB_CHANNEL
9571 if (bt_prompt(curbuf))
9572 // Restart Insert mode when re-entering the prompt buffer.
9573 curbuf->b_prompt_insert = 'A';
9574#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009575 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009576 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009577 if (curwin != new_curwin && win_valid(new_curwin))
9578 {
9579 curwin = new_curwin;
9580 curbuf = curwin->w_buffer;
9581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582# ifdef FEAT_CINDENT
9583 can_cindent = TRUE;
9584# endif
9585 }
9586
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 /* redraw status lines (in case another window became active) */
9588 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589}
9590
9591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009595 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009596# ifdef FEAT_INS_EXPAND
9597 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598# endif
9599
9600 tpos = curwin->w_cursor;
9601
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009602 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 {
9604 int row, col;
9605
9606 row = mouse_row;
9607 col = mouse_col;
9608
9609 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009610 wp = mouse_find_win(&row, &col);
9611 if (wp == NULL)
9612 return;
9613 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 curbuf = curwin->w_buffer;
9615 }
9616 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 undisplay_dollar();
9618
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009619# ifdef FEAT_INS_EXPAND
9620 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009621 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009622# endif
9623 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009624 if (dir == MSCR_DOWN || dir == MSCR_UP)
9625 {
9626 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9627 scroll_redraw(dir,
9628 (long)(curwin->w_botline - curwin->w_topline));
9629 else
9630 scroll_redraw(dir, 3L);
9631 }
9632#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009633 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009634 {
9635 int val, step = 6;
9636
9637 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009638 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009639 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9640 if (val < 0)
9641 val = 0;
9642 gui_do_horiz_scroll(val, TRUE);
9643 }
9644#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009645# ifdef FEAT_INS_EXPAND
9646 did_scroll = TRUE;
9647# endif
9648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 curwin->w_redr_status = TRUE;
9651
9652 curwin = old_curwin;
9653 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009655# ifdef FEAT_INS_EXPAND
9656 /* The popup menu may overlay the window, need to redraw it.
9657 * TODO: Would be more efficient to only redraw the windows that are
9658 * overlapped by the popup menu. */
9659 if (pum_visible() && did_scroll)
9660 {
9661 redraw_all_later(NOT_VALID);
9662 ins_compl_show_pum();
9663 }
9664# endif
9665
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009666 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 {
9668 start_arrow(&tpos);
9669# ifdef FEAT_CINDENT
9670 can_cindent = TRUE;
9671# endif
9672 }
9673}
9674#endif
9675
Bram Moolenaarec2da362017-01-21 20:04:22 +01009676/*
9677 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9678 * P_PE literally.
9679 * When "drop" is TRUE then consume the text and drop it.
9680 */
9681 int
9682bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9683{
9684 int c;
9685 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9686 int idx = 0;
9687 char_u *end = find_termcode((char_u *)"PE");
9688 int ret_char = -1;
9689 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009690 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009691
9692 /* If the end code is too long we can't detect it, read everything. */
9693 if (STRLEN(end) >= NUMBUFLEN)
9694 end = NULL;
9695 ++no_mapping;
9696 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009697 if (!p_paste)
9698 // Also have the side effects of setting 'paste' to make it work much
9699 // faster.
9700 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009701
Bram Moolenaarec2da362017-01-21 20:04:22 +01009702 for (;;)
9703 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009704 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009705 if (end == NULL && vpeekc() == NUL)
9706 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009707 do
9708 {
9709 c = vgetc();
9710 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9711 if (c == NUL || got_int)
9712 // When CTRL-C was encountered the typeahead will be flushed and we
9713 // won't get the end sequence.
9714 break;
9715
Bram Moolenaarec2da362017-01-21 20:04:22 +01009716#ifdef FEAT_MBYTE
9717 if (has_mbyte)
9718 idx += (*mb_char2bytes)(c, buf + idx);
9719 else
9720#endif
9721 buf[idx++] = c;
9722 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009723 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009724 {
9725 if (end[idx] == NUL)
9726 break; /* Found the end of paste code. */
9727 continue;
9728 }
9729 if (!drop)
9730 {
9731 switch (mode)
9732 {
9733 case PASTE_CMDLINE:
9734 put_on_cmdline(buf, idx, TRUE);
9735 break;
9736
9737 case PASTE_EX:
9738 if (gap != NULL && ga_grow(gap, idx) == OK)
9739 {
9740 mch_memmove((char *)gap->ga_data + gap->ga_len,
9741 buf, (size_t)idx);
9742 gap->ga_len += idx;
9743 }
9744 break;
9745
9746 case PASTE_INSERT:
9747 if (stop_arrow() == OK)
9748 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009749 c = buf[0];
9750 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9751 ins_eol(c);
9752 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009753 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009754 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009755 AppendToRedobuffLit(buf, idx);
9756 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009757 }
9758 break;
9759
9760 case PASTE_ONE_CHAR:
9761 if (ret_char == -1)
9762 {
9763#ifdef FEAT_MBYTE
9764 if (has_mbyte)
9765 ret_char = (*mb_ptr2char)(buf);
9766 else
9767#endif
9768 ret_char = buf[0];
9769 }
9770 break;
9771 }
9772 }
9773 idx = 0;
9774 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009775
Bram Moolenaarec2da362017-01-21 20:04:22 +01009776 --no_mapping;
9777 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009778 if (!save_paste)
9779 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009780
9781 return ret_char;
9782}
9783
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009784#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009786ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009787{
9788 /* We will be leaving the current window, unless closing another tab. */
9789 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9790 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9791 {
9792 undisplay_dollar();
9793 start_arrow(&curwin->w_cursor);
9794# ifdef FEAT_CINDENT
9795 can_cindent = TRUE;
9796# endif
9797 }
9798
9799 if (c == K_TABLINE)
9800 goto_tabpage(current_tab);
9801 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009802 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009803 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009804 redraw_statuslines(); /* will redraw the tabline when needed */
9805 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009806}
9807#endif
9808
9809#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009811ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813 pos_T tpos;
9814
9815 undisplay_dollar();
9816 tpos = curwin->w_cursor;
9817 if (gui_do_scroll())
9818 {
9819 start_arrow(&tpos);
9820# ifdef FEAT_CINDENT
9821 can_cindent = TRUE;
9822# endif
9823 }
9824}
9825
9826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 pos_T tpos;
9830
9831 undisplay_dollar();
9832 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009833 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 {
9835 start_arrow(&tpos);
9836# ifdef FEAT_CINDENT
9837 can_cindent = TRUE;
9838# endif
9839 }
9840}
9841#endif
9842
9843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844ins_left(
9845 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 pos_T tpos;
9848
9849#ifdef FEAT_FOLDING
9850 if ((fdo_flags & FDO_HOR) && KeyTyped)
9851 foldOpenCursor();
9852#endif
9853 undisplay_dollar();
9854 tpos = curwin->w_cursor;
9855 if (oneleft() == OK)
9856 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009857#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9858 /* Only call start_arrow() when not busy with preediting, it will
9859 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009860 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009861#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009862 {
9863 start_arrow_with_change(&tpos, end_change);
9864 if (!end_change)
9865 AppendCharToRedobuff(K_LEFT);
9866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867#ifdef FEAT_RIGHTLEFT
9868 /* If exit reversed string, position is fixed */
9869 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9870 revins_legal++;
9871 revins_chars++;
9872#endif
9873 }
9874
9875 /*
9876 * if 'whichwrap' set for cursor in insert mode may go to
9877 * previous line
9878 */
9879 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9880 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009881 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 start_arrow(&tpos);
9883 --(curwin->w_cursor.lnum);
9884 coladvance((colnr_T)MAXCOL);
9885 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9886 }
9887 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009888 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009889 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893ins_home(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_HOME)
9904 curwin->w_cursor.lnum = 1;
9905 curwin->w_cursor.col = 0;
9906#ifdef FEAT_VIRTUALEDIT
9907 curwin->w_cursor.coladd = 0;
9908#endif
9909 curwin->w_curswant = 0;
9910 start_arrow(&tpos);
9911}
9912
9913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 pos_T tpos;
9917
9918#ifdef FEAT_FOLDING
9919 if ((fdo_flags & FDO_HOR) && KeyTyped)
9920 foldOpenCursor();
9921#endif
9922 undisplay_dollar();
9923 tpos = curwin->w_cursor;
9924 if (c == K_C_END)
9925 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9926 coladvance((colnr_T)MAXCOL);
9927 curwin->w_curswant = MAXCOL;
9928
9929 start_arrow(&tpos);
9930}
9931
9932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009933ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
9935#ifdef FEAT_FOLDING
9936 if ((fdo_flags & FDO_HOR) && KeyTyped)
9937 foldOpenCursor();
9938#endif
9939 undisplay_dollar();
9940 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9941 {
9942 start_arrow(&curwin->w_cursor);
9943 (void)bck_word(1L, FALSE, FALSE);
9944 curwin->w_set_curswant = TRUE;
9945 }
9946 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009947 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948}
9949
9950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009951ins_right(
9952 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954#ifdef FEAT_FOLDING
9955 if ((fdo_flags & FDO_HOR) && KeyTyped)
9956 foldOpenCursor();
9957#endif
9958 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009959 if (gchar_cursor() != NUL
9960#ifdef FEAT_VIRTUALEDIT
9961 || virtual_active()
9962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 )
9964 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009965 start_arrow_with_change(&curwin->w_cursor, end_change);
9966 if (!end_change)
9967 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 curwin->w_set_curswant = TRUE;
9969#ifdef FEAT_VIRTUALEDIT
9970 if (virtual_active())
9971 oneright();
9972 else
9973#endif
9974 {
9975#ifdef FEAT_MBYTE
9976 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009977 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 else
9979#endif
9980 ++curwin->w_cursor.col;
9981 }
9982
9983#ifdef FEAT_RIGHTLEFT
9984 revins_legal++;
9985 if (revins_chars)
9986 revins_chars--;
9987#endif
9988 }
9989 /* if 'whichwrap' set for cursor in insert mode, may move the
9990 * cursor to the next line */
9991 else if (vim_strchr(p_ww, ']') != NULL
9992 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9993 {
9994 start_arrow(&curwin->w_cursor);
9995 curwin->w_set_curswant = TRUE;
9996 ++curwin->w_cursor.lnum;
9997 curwin->w_cursor.col = 0;
9998 }
9999 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010000 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +020010001 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002}
10003
10004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010005ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007#ifdef FEAT_FOLDING
10008 if ((fdo_flags & FDO_HOR) && KeyTyped)
10009 foldOpenCursor();
10010#endif
10011 undisplay_dollar();
10012 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
10013 || gchar_cursor() != NUL)
10014 {
10015 start_arrow(&curwin->w_cursor);
10016 (void)fwd_word(1L, FALSE, 0);
10017 curwin->w_set_curswant = TRUE;
10018 }
10019 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010020 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
10023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010024ins_up(
10025 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 pos_T tpos;
10028 linenr_T old_topline = curwin->w_topline;
10029#ifdef FEAT_DIFF
10030 int old_topfill = curwin->w_topfill;
10031#endif
10032
10033 undisplay_dollar();
10034 tpos = curwin->w_cursor;
10035 if (cursor_up(1L, TRUE) == OK)
10036 {
10037 if (startcol)
10038 coladvance(getvcol_nolist(&Insstart));
10039 if (old_topline != curwin->w_topline
10040#ifdef FEAT_DIFF
10041 || old_topfill != curwin->w_topfill
10042#endif
10043 )
10044 redraw_later(VALID);
10045 start_arrow(&tpos);
10046#ifdef FEAT_CINDENT
10047 can_cindent = TRUE;
10048#endif
10049 }
10050 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010051 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
10054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010055ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
10057 pos_T tpos;
10058
10059 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010060
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010061 if (mod_mask & MOD_MASK_CTRL)
10062 {
10063 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010064 if (first_tabpage->tp_next != NULL)
10065 {
10066 start_arrow(&curwin->w_cursor);
10067 goto_tabpage(-1);
10068 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010069 return;
10070 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010071
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 tpos = curwin->w_cursor;
10073 if (onepage(BACKWARD, 1L) == OK)
10074 {
10075 start_arrow(&tpos);
10076#ifdef FEAT_CINDENT
10077 can_cindent = TRUE;
10078#endif
10079 }
10080 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010081 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082}
10083
10084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010085ins_down(
10086 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 pos_T tpos;
10089 linenr_T old_topline = curwin->w_topline;
10090#ifdef FEAT_DIFF
10091 int old_topfill = curwin->w_topfill;
10092#endif
10093
10094 undisplay_dollar();
10095 tpos = curwin->w_cursor;
10096 if (cursor_down(1L, TRUE) == OK)
10097 {
10098 if (startcol)
10099 coladvance(getvcol_nolist(&Insstart));
10100 if (old_topline != curwin->w_topline
10101#ifdef FEAT_DIFF
10102 || old_topfill != curwin->w_topfill
10103#endif
10104 )
10105 redraw_later(VALID);
10106 start_arrow(&tpos);
10107#ifdef FEAT_CINDENT
10108 can_cindent = TRUE;
10109#endif
10110 }
10111 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010112 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113}
10114
10115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010116ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118 pos_T tpos;
10119
10120 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010121
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010122 if (mod_mask & MOD_MASK_CTRL)
10123 {
10124 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010125 if (first_tabpage->tp_next != NULL)
10126 {
10127 start_arrow(&curwin->w_cursor);
10128 goto_tabpage(0);
10129 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010130 return;
10131 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010132
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 tpos = curwin->w_cursor;
10134 if (onepage(FORWARD, 1L) == OK)
10135 {
10136 start_arrow(&tpos);
10137#ifdef FEAT_CINDENT
10138 can_cindent = TRUE;
10139#endif
10140 }
10141 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010142 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143}
10144
10145#ifdef FEAT_DND
10146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010147ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
10149 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10150}
10151#endif
10152
10153/*
10154 * Handle TAB in Insert or Replace mode.
10155 * Return TRUE when the TAB needs to be inserted like a normal character.
10156 */
10157 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010158ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
10160 int ind;
10161 int i;
10162 int temp;
10163
10164 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10165 Insstart_blank_vcol = get_nolist_virtcol();
10166 if (echeck_abbr(TAB + ABBR_OFF))
10167 return FALSE;
10168
10169 ind = inindent(0);
10170#ifdef FEAT_CINDENT
10171 if (ind)
10172 can_cindent = FALSE;
10173#endif
10174
10175 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010176 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 */
10178 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010179#ifdef FEAT_VARTABS
10180 && !(p_sta && ind
10181 /* These five lines mean 'tabstop' != 'shiftwidth' */
10182 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10183 || (tabstop_count(curbuf->b_p_vts_array) == 1
10184 && tabstop_first(curbuf->b_p_vts_array)
10185 != get_sw_value(curbuf))
10186 || (tabstop_count(curbuf->b_p_vts_array) == 0
10187 && curbuf->b_p_ts != get_sw_value(curbuf))))
10188 && tabstop_count(curbuf->b_p_vsts_array) == 0
10189#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010190 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010191#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010192 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 return TRUE;
10194
10195 if (stop_arrow() == FAIL)
10196 return TRUE;
10197
10198 did_ai = FALSE;
10199#ifdef FEAT_SMARTINDENT
10200 did_si = FALSE;
10201 can_si = FALSE;
10202 can_si_back = FALSE;
10203#endif
10204 AppendToRedobuff((char_u *)"\t");
10205
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010206#ifdef FEAT_VARTABS
10207 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10208 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010209 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010210 temp -= get_nolist_virtcol() % temp;
10211 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010212 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010213 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010214 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010215 curbuf->b_p_vsts_array);
10216 else /* otherwise use 'tabstop' */
10217 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10218 curbuf->b_p_vts_array);
10219#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010221 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010222 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10223 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 else /* otherwise use 'tabstop' */
10225 temp = (int)curbuf->b_p_ts;
10226 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228
10229 /*
10230 * Insert the first space with ins_char(). It will delete one char in
10231 * replace mode. Insert the rest with ins_str(); it will not delete any
10232 * chars. For VREPLACE mode, we use ins_char() for all characters.
10233 */
10234 ins_char(' ');
10235 while (--temp > 0)
10236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 if (State & VREPLACE_FLAG)
10238 ins_char(' ');
10239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 {
10241 ins_str((char_u *)" ");
10242 if (State & REPLACE_FLAG) /* no char replaced */
10243 replace_push(NUL);
10244 }
10245 }
10246
10247 /*
10248 * When 'expandtab' not set: Replace spaces by TABs where possible.
10249 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010250#ifdef FEAT_VARTABS
10251 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10252 || get_sts_value() > 0
10253 || (p_sta && ind)))
10254#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010255 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 {
10258 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 char_u *saved_line = NULL; /* init for GCC */
10260 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 pos_T fpos;
10262 pos_T *cursor;
10263 colnr_T want_vcol, vcol;
10264 int change_col = -1;
10265 int save_list = curwin->w_p_list;
10266
10267 /*
10268 * Get the current line. For VREPLACE mode, don't make real changes
10269 * yet, just work on a copy of the line.
10270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 if (State & VREPLACE_FLAG)
10272 {
10273 pos = curwin->w_cursor;
10274 cursor = &pos;
10275 saved_line = vim_strsave(ml_get_curline());
10276 if (saved_line == NULL)
10277 return FALSE;
10278 ptr = saved_line + pos.col;
10279 }
10280 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 {
10282 ptr = ml_get_cursor();
10283 cursor = &curwin->w_cursor;
10284 }
10285
10286 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10287 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10288 curwin->w_p_list = FALSE;
10289
10290 /* Find first white before the cursor */
10291 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010292 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 {
10294 --fpos.col;
10295 --ptr;
10296 }
10297
10298 /* In Replace mode, don't change characters before the insert point. */
10299 if ((State & REPLACE_FLAG)
10300 && fpos.lnum == Insstart.lnum
10301 && fpos.col < Insstart.col)
10302 {
10303 ptr += Insstart.col - fpos.col;
10304 fpos.col = Insstart.col;
10305 }
10306
10307 /* compute virtual column numbers of first white and cursor */
10308 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10309 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10310
Bram Moolenaar597a4222014-06-25 14:39:50 +020010311 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10312 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010313 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010315 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 if (vcol + i > want_vcol)
10317 break;
10318 if (*ptr != TAB)
10319 {
10320 *ptr = TAB;
10321 if (change_col < 0)
10322 {
10323 change_col = fpos.col; /* Column of first change */
10324 /* May have to adjust Insstart */
10325 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10326 Insstart.col = fpos.col;
10327 }
10328 }
10329 ++fpos.col;
10330 ++ptr;
10331 vcol += i;
10332 }
10333
10334 if (change_col >= 0)
10335 {
10336 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010337 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338
10339 /* Skip over the spaces we need. */
10340 while (vcol < want_vcol && *ptr == ' ')
10341 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010342 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 ++ptr;
10344 ++repl_off;
10345 }
10346 if (vcol > want_vcol)
10347 {
10348 /* Must have a char with 'showbreak' just before it. */
10349 --ptr;
10350 --repl_off;
10351 }
10352 fpos.col += repl_off;
10353
10354 /* Delete following spaces. */
10355 i = cursor->col - fpos.col;
10356 if (i > 0)
10357 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010358 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010360 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 for (temp = i; --temp >= 0; )
10362 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010363#ifdef FEAT_TEXT_PROP
10364 curbuf->b_ml.ml_line_len -= i;
10365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010367#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010368 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010369 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010370 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010371 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10372 (char_u *)"\t", 1);
10373 }
10374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 cursor->col -= i;
10376
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 /*
10378 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10379 * backspacing over the changed spacing and then inserting the new
10380 * spacing.
10381 */
10382 if (State & VREPLACE_FLAG)
10383 {
10384 /* Backspace from real cursor to change_col */
10385 backspace_until_column(change_col);
10386
10387 /* Insert each char in saved_line from changed_col to
10388 * ptr-cursor */
10389 ins_bytes_len(saved_line + change_col,
10390 cursor->col - change_col);
10391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 }
10393
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 if (State & VREPLACE_FLAG)
10395 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 curwin->w_p_list = save_list;
10397 }
10398
10399 return FALSE;
10400}
10401
10402/*
10403 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010404 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 */
10406 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010407ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 int i;
10410
10411 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010412 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 undisplay_dollar();
10416
10417 /*
10418 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10419 * character under the cursor. Only push a NUL on the replace stack,
10420 * nothing to put back when the NL is deleted.
10421 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010422 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 replace_push(NUL);
10424
10425 /*
10426 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10427 * replacing the next line, so we push all of the characters left on the
10428 * line onto the replace stack. This is not done here though, it is done
10429 * in open_line().
10430 */
10431
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010432#ifdef FEAT_VIRTUALEDIT
10433 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10434 * CTRL-O). */
10435 if (virtual_active() && curwin->w_cursor.coladd > 0)
10436 coladvance(getviscol());
10437#endif
10438
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439#ifdef FEAT_RIGHTLEFT
10440# ifdef FEAT_FKMAP
10441 if (p_altkeymap && p_fkmap)
10442 fkmap(NL);
10443# endif
10444 /* NL in reverse insert will always start in the end of
10445 * current line. */
10446 if (revins_on)
10447 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10448#endif
10449
10450 AppendToRedobuff(NL_STR);
10451 i = open_line(FORWARD,
10452#ifdef FEAT_COMMENTS
10453 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10454#endif
10455 0, old_indent);
10456 old_indent = 0;
10457#ifdef FEAT_CINDENT
10458 can_cindent = TRUE;
10459#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010460#ifdef FEAT_FOLDING
10461 /* When inserting a line the cursor line must never be in a closed fold. */
10462 foldOpenCursor();
10463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010465 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
10467
10468#ifdef FEAT_DIGRAPHS
10469/*
10470 * Handle digraph in insert mode.
10471 * Returns character still to be inserted, or NUL when nothing remaining to be
10472 * done.
10473 */
10474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010475ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476{
10477 int c;
10478 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010479 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
10481 pc_status = PC_STATUS_UNSET;
10482 if (redrawing() && !char_avail())
10483 {
10484 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010485 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
10487 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010488 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489#ifdef FEAT_CMDL_INFO
10490 add_to_showcmd_c(Ctrl_K);
10491#endif
10492 }
10493
10494#ifdef USE_ON_FLY_SCROLL
10495 dont_scroll = TRUE; /* disallow scrolling here */
10496#endif
10497
10498 /* don't map the digraph chars. This also prevents the
10499 * mode message to be deleted when ESC is hit */
10500 ++no_mapping;
10501 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010502 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 --no_mapping;
10504 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010505 if (did_putchar)
10506 /* when the line fits in 'columns' the '?' is at the start of the next
10507 * line and will not be removed by the redraw */
10508 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010509
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 if (IS_SPECIAL(c) || mod_mask) /* special key */
10511 {
10512#ifdef FEAT_CMDL_INFO
10513 clear_showcmd();
10514#endif
10515 insert_special(c, TRUE, FALSE);
10516 return NUL;
10517 }
10518 if (c != ESC)
10519 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010520 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 if (redrawing() && !char_avail())
10522 {
10523 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010524 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525
10526 if (char2cells(c) == 1)
10527 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010528 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010530 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 }
10532#ifdef FEAT_CMDL_INFO
10533 add_to_showcmd_c(c);
10534#endif
10535 }
10536 ++no_mapping;
10537 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010538 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 --no_mapping;
10540 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010541 if (did_putchar)
10542 /* when the line fits in 'columns' the '?' is at the start of the
10543 * next line and will not be removed by a redraw */
10544 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 if (cc != ESC)
10546 {
10547 AppendToRedobuff((char_u *)CTRL_V_STR);
10548 c = getdigraph(c, cc, TRUE);
10549#ifdef FEAT_CMDL_INFO
10550 clear_showcmd();
10551#endif
10552 return c;
10553 }
10554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555#ifdef FEAT_CMDL_INFO
10556 clear_showcmd();
10557#endif
10558 return NUL;
10559}
10560#endif
10561
10562/*
10563 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10564 * Returns the char to be inserted, or NUL if none found.
10565 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010567ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569 int c;
10570 int temp;
10571 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010572 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573
10574 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10575 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010576 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 return NUL;
10578 }
10579
10580 /* try to advance to the cursor column */
10581 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010582 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 prev_ptr = ptr;
10584 validate_virtcol();
10585 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10586 {
10587 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010588 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 }
10590 if ((colnr_T)temp > curwin->w_virtcol)
10591 ptr = prev_ptr;
10592
10593#ifdef FEAT_MBYTE
10594 c = (*mb_ptr2char)(ptr);
10595#else
10596 c = *ptr;
10597#endif
10598 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010599 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 return c;
10601}
10602
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010603/*
10604 * CTRL-Y or CTRL-E typed in Insert mode.
10605 */
10606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010607ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010608{
10609 int c = tc;
10610
10611#ifdef FEAT_INS_EXPAND
10612 if (ctrl_x_mode == CTRL_X_SCROLL)
10613 {
10614 if (c == Ctrl_Y)
10615 scrolldown_clamp();
10616 else
10617 scrollup_clamp();
10618 redraw_later(VALID);
10619 }
10620 else
10621#endif
10622 {
10623 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10624 if (c != NUL)
10625 {
10626 long tw_save;
10627
10628 /* The character must be taken literally, insert like it
10629 * was typed after a CTRL-V, and pretend 'textwidth'
10630 * wasn't set. Digits, 'o' and 'x' are special after a
10631 * CTRL-V, don't use it for these. */
10632 if (c < 256 && !isalnum(c))
10633 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10634 tw_save = curbuf->b_p_tw;
10635 curbuf->b_p_tw = -1;
10636 insert_special(c, TRUE, FALSE);
10637 curbuf->b_p_tw = tw_save;
10638#ifdef FEAT_RIGHTLEFT
10639 revins_chars++;
10640 revins_legal++;
10641#endif
10642 c = Ctrl_V; /* pretend CTRL-V is last character */
10643 auto_format(FALSE, TRUE);
10644 }
10645 }
10646 return c;
10647}
10648
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649#ifdef FEAT_SMARTINDENT
10650/*
10651 * Try to do some very smart auto-indenting.
10652 * Used when inserting a "normal" character.
10653 */
10654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010655ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656{
10657 pos_T *pos, old_pos;
10658 char_u *ptr;
10659 int i;
10660 int temp;
10661
10662 /*
10663 * do some very smart indenting when entering '{' or '}'
10664 */
10665 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10666 {
10667 /*
10668 * for '}' set indent equal to indent of line containing matching '{'
10669 */
10670 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10671 {
10672 old_pos = curwin->w_cursor;
10673 /*
10674 * If the matching '{' has a ')' immediately before it (ignoring
10675 * white-space), then line up with the start of the line
10676 * containing the matching '(' if there is one. This handles the
10677 * case where an "if (..\n..) {" statement continues over multiple
10678 * lines -- webb
10679 */
10680 ptr = ml_get(pos->lnum);
10681 i = pos->col;
10682 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010683 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 ;
10685 curwin->w_cursor.lnum = pos->lnum;
10686 curwin->w_cursor.col = i;
10687 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10688 curwin->w_cursor = *pos;
10689 i = get_indent();
10690 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010692 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 (void)set_indent(i, SIN_CHANGED);
10695 }
10696 else if (curwin->w_cursor.col > 0)
10697 {
10698 /*
10699 * when inserting '{' after "O" reduce indent, but not
10700 * more than indent of previous line
10701 */
10702 temp = TRUE;
10703 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10704 {
10705 old_pos = curwin->w_cursor;
10706 i = get_indent();
10707 while (curwin->w_cursor.lnum > 1)
10708 {
10709 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10710
10711 /* ignore empty lines and lines starting with '#'. */
10712 if (*ptr != '#' && *ptr != NUL)
10713 break;
10714 }
10715 if (get_indent() >= i)
10716 temp = FALSE;
10717 curwin->w_cursor = old_pos;
10718 }
10719 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010720 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 }
10722 }
10723
10724 /*
10725 * set indent of '#' always to 0
10726 */
10727 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10728 {
10729 /* remember current indent for next line */
10730 old_indent = get_indent();
10731 (void)set_indent(0, SIN_CHANGED);
10732 }
10733
10734 /* Adjust ai_col, the char at this position can be deleted. */
10735 if (ai_col > curwin->w_cursor.col)
10736 ai_col = curwin->w_cursor.col;
10737}
10738#endif
10739
10740/*
10741 * Get the value that w_virtcol would have when 'list' is off.
10742 * Unless 'cpo' contains the 'L' flag.
10743 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010744 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010745get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010747 // check validity of cursor in current buffer
10748 if (curwin->w_buffer == NULL
10749 || curwin->w_buffer->b_ml.ml_mfp == NULL
10750 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10751 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10753 return getvcol_nolist(&curwin->w_cursor);
10754 validate_virtcol();
10755 return curwin->w_virtcol;
10756}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010757
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010758#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010759/*
10760 * Handle the InsertCharPre autocommand.
10761 * "c" is the character that was typed.
10762 * Return a pointer to allocated memory with the replacement string.
10763 * Return NULL to continue inserting "c".
10764 */
10765 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010766do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010767{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010768 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010769 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010770 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010771
10772 /* Return quickly when there is nothing to do. */
10773 if (!has_insertcharpre())
10774 return NULL;
10775
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010776# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010777 if (has_mbyte)
10778 buf[(*mb_char2bytes)(c, buf)] = NUL;
10779 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010780# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010781 {
10782 buf[0] = c;
10783 buf[1] = NUL;
10784 }
10785
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010786 /* Lock the text to avoid weird things from happening. */
10787 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010788 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010789
Bram Moolenaar704984a2012-06-01 14:57:51 +020010790 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010791 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010792 {
10793 /* Get the value of v:char. It may be empty or more than one
10794 * character. Only use it when changed, otherwise continue with the
10795 * original character to avoid breaking autoindent. */
10796 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10797 res = vim_strsave(get_vim_var_str(VV_CHAR));
10798 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010799
10800 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10801 --textlock;
10802
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010803 // Restore the State, it may have been changed.
10804 State = save_State;
10805
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010806 return res;
10807}
10808#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010809
10810/*
10811 * Trigger "event" and take care of fixing undo.
10812 */
10813 static int
10814ins_apply_autocmds(event_T event)
10815{
10816 varnumber_T tick = CHANGEDTICK(curbuf);
10817 int r;
10818
10819 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10820
10821 // If u_savesub() was called then we are not prepared to start
10822 // a new line. Call u_save() with no contents to fix that.
10823 if (tick != CHANGEDTICK(curbuf))
10824 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10825
10826 return r;
10827}