blob: de5adcb19743c511311a8dfdad63e05b7963af58 [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 Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void replace_flush(void);
217static void replace_do_bs(int limit_col);
218static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ins_reg(void);
223static void ins_ctrl_g(void);
224static void ins_ctrl_hat(void);
225static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static int ins_start_select(int c);
230static void ins_insert(int replaceState);
231static void ins_ctrl_o(void);
232static void ins_shift(int c, int lastc);
233static void ins_del(void);
234static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ins_mouse(int c);
237static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000239#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ins_left(int end_change);
243static void ins_home(int c);
244static void ins_end(int c);
245static void ins_s_left(void);
246static void ins_right(int end_change);
247static void ins_s_right(void);
248static void ins_up(int startcol);
249static void ins_pageup(void);
250static void ins_down(int startcol);
251static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static int ins_tab(void);
256static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100264#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100266#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200267static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269static colnr_T Insstart_textlen; /* length of line when insert started */
270static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100271static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273static char_u *last_insert = NULL; /* the text of the previous insert,
274 K_SPECIAL and CSI are escaped */
275static int last_insert_skip; /* nr of chars in front of previous insert */
276static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000277static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279#ifdef FEAT_CINDENT
280static int can_cindent; /* may do cindenting on this line */
281#endif
282
283static int old_indent = 0; /* for ^^D command in insert mode */
284
285#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000286static int revins_on; /* reverse insert mode on */
287static int revins_chars; /* how much to skip after edit */
288static int revins_legal; /* was the last char 'legal'? */
289static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#endif
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292static int ins_need_undo; /* call u_save() before inserting a
293 char. Set when edit() is called.
294 after that arrow_used is used. */
295
296static int did_add_space = FALSE; /* auto_format() added an extra space
297 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200298static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
299 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300
301/*
302 * edit(): Start inserting text.
303 *
304 * "cmdchar" can be:
305 * 'i' normal insert command
306 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100307 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 * 'R' replace command
309 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
310 * but still only one <CR> is inserted. The <Esc> is not used for redo.
311 * 'g' "gI" command.
312 * 'V' "gR" command for Virtual Replace mode.
313 * 'v' "gr" command for single character Virtual Replace mode.
314 *
315 * This function is not called recursively. For CTRL-O commands, it returns
316 * and lets the caller handle the Normal-mode command.
317 *
318 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
319 */
320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100321edit(
322 int cmdchar,
323 int startln, /* if set, insert at start of line */
324 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 int c = 0;
327 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100328 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000329 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 int i;
332 int did_backspace = TRUE; /* previous char was backspace */
333#ifdef FEAT_CINDENT
334 int line_is_white = FALSE; /* line is empty before insert */
335#endif
336 linenr_T old_topline = 0; /* topline before insertion */
337#ifdef FEAT_DIFF
338 int old_topfill = -1;
339#endif
340 int inserted_space = FALSE; /* just inserted a space */
341 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000342 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200343#ifdef FEAT_JOB_CHANNEL
344 int cmdchar_todo = cmdchar;
345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000347 /* Remember whether editing was restarted after CTRL-O. */
348 did_restart_edit = restart_edit;
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 /* sleep before redrawing, needed for "CTRL-O :" that results in an
351 * error message */
352 check_for_delay(TRUE);
353
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100354 /* set Insstart_orig to Insstart */
355 update_Insstart_orig = TRUE;
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#ifdef HAVE_SANDBOX
358 /* Don't allow inserting in the sandbox. */
359 if (sandbox != 0)
360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return FALSE;
363 }
364#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000365 /* Don't allow changes in the buffer while editing the cmdline. The
366 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000367 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100369 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 return FALSE;
371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000374 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000375 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 ins_compl_clear(); /* clear stuff for CTRL-X mode */
381#endif
382
Bram Moolenaar843ee412004-06-30 16:16:41 +0000383 /*
384 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
385 */
386 if (cmdchar != 'r' && cmdchar != 'v')
387 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100388 pos_T save_cursor = curwin->w_cursor;
389
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100390#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391 if (cmdchar == 'R')
392 ptr = (char_u *)"r";
393 else if (cmdchar == 'V')
394 ptr = (char_u *)"v";
395 else
396 ptr = (char_u *)"i";
397 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200398 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100399#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200400 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100401
Bram Moolenaar097c9922013-05-19 21:15:15 +0200402 /* Make sure the cursor didn't move. Do call check_cursor_col() in
403 * case the text was modified. Since Insert mode was not started yet
404 * a call to check_cursor_col() may move the cursor, especially with
405 * the "A" command, thus set State to avoid that. Also check that the
406 * line number is still valid (lines may have been deleted).
407 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100408 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100409#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200410 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100411#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100413 {
414 int save_state = State;
415
416 curwin->w_cursor = save_cursor;
417 State = INSERT;
418 check_cursor_col();
419 State = save_state;
420 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000421 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000422
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200423#ifdef FEAT_CONCEAL
424 /* Check if the cursor line needs redrawing before changing State. If
425 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200426 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#endif
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#ifdef FEAT_MOUSE
430 /*
431 * When doing a paste with the middle mouse button, Insstart is set to
432 * where the paste started.
433 */
434 if (where_paste_started.lnum != 0)
435 Insstart = where_paste_started;
436 else
437#endif
438 {
439 Insstart = curwin->w_cursor;
440 if (startln)
441 Insstart.col = 0;
442 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000443 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 Insstart_blank_vcol = MAXCOL;
445 if (!did_ai)
446 ai_col = 0;
447
448 if (cmdchar != NUL && restart_edit == 0)
449 {
450 ResetRedobuff();
451 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (cmdchar == 'V' || cmdchar == 'v')
453 {
454 /* "gR" or "gr" command */
455 AppendCharToRedobuff('g');
456 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
457 }
458 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100460 if (cmdchar == K_PS)
461 AppendCharToRedobuff('a');
462 else
463 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 if (cmdchar == 'g') /* "gI" command */
465 AppendCharToRedobuff('I');
466 else if (cmdchar == 'r') /* "r<CR>" command */
467 count = 1; /* insert only one <CR> */
468 }
469 }
470
471 if (cmdchar == 'R')
472 {
473#ifdef FEAT_FKMAP
474 if (p_fkmap && p_ri)
475 {
476 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100477 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 State = INSERT;
479 }
480 else
481#endif
482 State = REPLACE;
483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 else if (cmdchar == 'V' || cmdchar == 'v')
485 {
486 State = VREPLACE;
487 replaceState = VREPLACE;
488 orig_line_count = curbuf->b_ml.ml_line_count;
489 vr_lines_changed = 1;
490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 else
492 State = INSERT;
493
494 stop_insert_mode = FALSE;
495
496 /*
497 * Need to recompute the cursor position, it might move when the cursor is
498 * on a TAB or special character.
499 */
500 curs_columns(TRUE);
501
502 /*
503 * Enable langmap or IME, indicated by 'iminsert'.
504 * Note that IME may enabled/disabled without us noticing here, thus the
505 * 'iminsert' value may not reflect what is actually used. It is updated
506 * when hitting <Esc>.
507 */
508 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
509 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100510#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
512#endif
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514#ifdef FEAT_MOUSE
515 setmouse();
516#endif
517#ifdef FEAT_CMDL_INFO
518 clear_showcmd();
519#endif
520#ifdef FEAT_RIGHTLEFT
521 /* there is no reverse replace mode */
522 revins_on = (State == INSERT && p_ri);
523 if (revins_on)
524 undisplay_dollar();
525 revins_chars = 0;
526 revins_legal = 0;
527 revins_scol = -1;
528#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100529 if (!p_ek)
530 /* Disable bracketed paste mode, we won't recognize the escape
531 * sequences. */
532 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
534 /*
535 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100536 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
537 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 if (restart_edit != 0 && stuff_empty())
540 {
541#ifdef FEAT_MOUSE
542 /*
543 * After a paste we consider text typed to be part of the insert for
544 * the pasted text. You can backspace over the pasted text too.
545 */
546 if (where_paste_started.lnum)
547 arrow_used = FALSE;
548 else
549#endif
550 arrow_used = TRUE;
551 restart_edit = 0;
552
553 /*
554 * If the cursor was after the end-of-line before the CTRL-O and it is
555 * now at the end-of-line, put it after the end-of-line (this is not
556 * correct in very rare cases).
557 * Also do this if curswant is greater than the current virtual
558 * column. Eg after "^O$" or "^O80|".
559 */
560 validate_virtcol();
561 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 || curwin->w_curswant > curwin->w_virtcol)
564 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
565 {
566 if (ptr[1] == NUL)
567 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 else if (has_mbyte)
569 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000570 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (ptr[i] == NUL)
572 curwin->w_cursor.col += i;
573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000575 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577 else
578 arrow_used = FALSE;
579
580 /* we are in insert mode now, don't need to start it anymore */
581 need_start_insertmode = FALSE;
582
583 /* Need to save the line for undo before inserting the first char. */
584 ins_need_undo = TRUE;
585
586#ifdef FEAT_MOUSE
587 where_paste_started.lnum = 0;
588#endif
589#ifdef FEAT_CINDENT
590 can_cindent = TRUE;
591#endif
592#ifdef FEAT_FOLDING
593 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
594 * restarting. */
595 if (!p_im && did_restart_edit == 0)
596 foldOpenCursor();
597#endif
598
599 /*
600 * If 'showmode' is set, show the current (insert/replace/..) mode.
601 * A warning message for changing a readonly file is given here, before
602 * actually changing anything. It's put after the mode, if any.
603 */
604 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000605 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 i = showmode();
607
608 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000609 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611#ifdef CURSOR_SHAPE
612 ui_cursor_shape(); /* may show different cursor shape */
613#endif
614#ifdef FEAT_DIGRAPHS
615 do_digraph(-1); /* clear digraphs */
616#endif
617
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000618 /*
619 * Get the current length of the redo buffer, those characters have to be
620 * skipped if we want to get to the inserted characters.
621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 ptr = get_inserted();
623 if (ptr == NULL)
624 new_insert_skip = 0;
625 else
626 {
627 new_insert_skip = (int)STRLEN(ptr);
628 vim_free(ptr);
629 }
630
631 old_indent = 0;
632
633 /*
634 * Main loop in Insert mode: repeat until Insert mode is left.
635 */
636 for (;;)
637 {
638#ifdef FEAT_RIGHTLEFT
639 if (!revins_legal)
640 revins_scol = -1; /* reset on illegal motions */
641 else
642 revins_legal = 0;
643#endif
644 if (arrow_used) /* don't repeat insert when arrow key used */
645 count = 0;
646
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100647 if (update_Insstart_orig)
648 Insstart_orig = Insstart;
649
Bram Moolenaar00672e12016-06-26 18:38:13 +0200650 if (stop_insert_mode
651#ifdef FEAT_INS_EXPAND
652 && !pum_visible()
653#endif
654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
656 /* ":stopinsert" used or 'insertmode' reset */
657 count = 0;
658 goto doESCkey;
659 }
660
661 /* set curwin->w_curswant for next K_DOWN or K_UP */
662 if (!arrow_used)
663 curwin->w_set_curswant = TRUE;
664
665 /* If there is no typeahead may check for timestamps (e.g., for when a
666 * menu invoked a shell command). */
667 if (stuff_empty())
668 {
669 did_check_timestamps = FALSE;
670 if (need_check_timestamps)
671 check_timestamps(FALSE);
672 }
673
674 /*
675 * When emsg() was called msg_scroll will have been set.
676 */
677 msg_scroll = FALSE;
678
679#ifdef FEAT_GUI
680 /* When 'mousefocus' is set a mouse movement may have taken us to
681 * another window. "need_mouse_correct" may then be set because of an
682 * autocommand. */
683 if (need_mouse_correct)
684 gui_mouse_correct();
685#endif
686
687#ifdef FEAT_FOLDING
688 /* Open fold at the cursor line, according to 'foldopen'. */
689 if (fdo_flags & FDO_INSERT)
690 foldOpenCursor();
691 /* Close folds where the cursor isn't, according to 'foldclose' */
692 if (!char_avail())
693 foldCheckClose();
694#endif
695
Bram Moolenaarf2732452018-06-03 14:47:35 +0200696#ifdef FEAT_JOB_CHANNEL
697 if (bt_prompt(curbuf))
698 {
699 init_prompt(cmdchar_todo);
700 cmdchar_todo = NUL;
701 }
702#endif
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 /*
705 * If we inserted a character at the last position of the last line in
706 * the window, scroll the window one line up. This avoids an extra
707 * redraw.
708 * This is detected when the cursor column is smaller after inserting
709 * something.
710 * Don't do this when the topline changed already, it has
711 * already been adjusted (by insertchar() calling open_line())).
712 */
713 if (curbuf->b_mod_set
714 && curwin->w_p_wrap
715 && !did_backspace
716 && curwin->w_topline == old_topline
717#ifdef FEAT_DIFF
718 && curwin->w_topfill == old_topfill
719#endif
720 )
721 {
722 mincol = curwin->w_wcol;
723 validate_cursor_col();
724
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200725 if (
726#ifdef FEAT_VARTABS
727 (int)curwin->w_wcol < mincol - tabstop_at(
728 get_nolist_virtcol(), curbuf->b_p_ts,
729 curbuf->b_p_vts_array)
730#else
731 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 && curwin->w_wrow == W_WINROW(curwin)
734 + curwin->w_height - 1 - p_so
735 && (curwin->w_cursor.lnum != curwin->w_topline
736#ifdef FEAT_DIFF
737 || curwin->w_topfill > 0
738#endif
739 ))
740 {
741#ifdef FEAT_DIFF
742 if (curwin->w_topfill > 0)
743 --curwin->w_topfill;
744 else
745#endif
746#ifdef FEAT_FOLDING
747 if (hasFolding(curwin->w_topline, NULL, &old_topline))
748 set_topline(curwin, old_topline + 1);
749 else
750#endif
751 set_topline(curwin, curwin->w_topline + 1);
752 }
753 }
754
755 /* May need to adjust w_topline to show the cursor. */
756 update_topline();
757
758 did_backspace = FALSE;
759
760 validate_cursor(); /* may set must_redraw */
761
762 /*
763 * Redraw the display when no characters are waiting.
764 * Also shows mode, ruler and positions cursor.
765 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000766 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (curwin->w_p_scb)
769 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
Bram Moolenaar860cae12010-06-05 23:22:07 +0200771 if (curwin->w_p_crb)
772 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 update_curswant();
774 old_topline = curwin->w_topline;
775#ifdef FEAT_DIFF
776 old_topfill = curwin->w_topfill;
777#endif
778
779#ifdef USE_ON_FLY_SCROLL
780 dont_scroll = FALSE; /* allow scrolling here */
781#endif
782
783 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100784 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100786 if (c != K_CURSORHOLD)
787 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200788
789 /* After using CTRL-G U the next cursor key will not break undo. */
790 if (dont_sync_undo == MAYBE)
791 dont_sync_undo = TRUE;
792 else
793 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100794 if (cmdchar == K_PS)
795 /* Got here from normal mode when bracketed paste started. */
796 c = K_PS;
797 else
798 do
799 {
800 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200801
802 if (stop_insert_mode)
803 {
804 // Insert mode ended, possibly from a callback.
805 count = 0;
806 nomove = TRUE;
807 goto doESCkey;
808 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100809 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000811 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
812 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_RIGHTLEFT
815 if (p_hkmap && KeyTyped)
816 c = hkmap(c); /* Hebrew mode mapping */
817#endif
818#ifdef FEAT_FKMAP
819 if (p_fkmap && KeyTyped)
820 c = fkmap(c); /* Farsi mode mapping */
821#endif
822
823#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000824 /*
825 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000826 * and the cursor is still in the completed word. Only when there is
827 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000829 if (compl_started
830 && pum_wanted()
831 && curwin->w_cursor.col >= compl_col
832 && (compl_shown_match == NULL
833 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000834 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000835 /* BS: Delete one character from "compl_leader". */
836 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000837 && curwin->w_cursor.col > compl_col
838 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000839 continue;
840
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000841 /* When no match was selected or it was edited. */
842 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000843 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000845 * "compl_leader". Except when at the original match and
846 * there is nothing to add, CTRL-L works like CTRL-P then. */
847 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100848 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000849 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000850 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 {
852 ins_compl_addfrommatch();
853 continue;
854 }
855
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000856 /* A non-white character that fits in with the current
857 * completion: Add to "compl_leader". */
858 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000859 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100860#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100861 /* Trigger InsertCharPre. */
862 char_u *str = do_insert_char_pre(c);
863 char_u *p;
864
865 if (str != NULL)
866 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100867 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100868 ins_compl_addleader(PTR2CHAR(p));
869 vim_free(str);
870 }
871 else
872#endif
873 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000874 continue;
875 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000876
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000877 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000878 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200879 if ((c == Ctrl_Y || (compl_enter_selects
880 && (c == CAR || c == K_KENTER || c == NL)))
881 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000882 {
883 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200884 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000885 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 }
887 }
888
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
890 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000891 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000892 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000893 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
895
Bram Moolenaar488c6512005-08-11 20:09:58 +0000896 /* CTRL-\ CTRL-N goes to Normal mode,
897 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
898 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (c == Ctrl_BSL)
900 {
901 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000902 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 ++no_mapping;
904 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000905 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 --no_mapping;
907 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000908 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000910 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 vungetc(c);
912 c = Ctrl_BSL;
913 }
914 else if (c == Ctrl_G && p_im)
915 continue;
916 else
917 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000918 if (c == Ctrl_O)
919 {
920 ins_ctrl_o();
921 ins_at_eol = FALSE; /* cursor keeps its column */
922 nomove = TRUE;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 count = 0;
925 goto doESCkey;
926 }
927 }
928
929#ifdef FEAT_DIGRAPHS
930 c = do_digraph(c);
931#endif
932
933#ifdef FEAT_INS_EXPAND
934 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
935 goto docomplete;
936#endif
937 if (c == Ctrl_V || c == Ctrl_Q)
938 {
939 ins_ctrl_v();
940 c = Ctrl_V; /* pretend CTRL-V is last typed character */
941 continue;
942 }
943
944#ifdef FEAT_CINDENT
945 if (cindent_on()
946# ifdef FEAT_INS_EXPAND
947 && ctrl_x_mode == 0
948# endif
949 )
950 {
951 /* A key name preceded by a bang means this key is not to be
952 * inserted. Skip ahead to the re-indenting below.
953 * A key name preceded by a star means that indenting has to be
954 * done before inserting the key. */
955 line_is_white = inindent(0);
956 if (in_cinkeys(c, '!', line_is_white))
957 goto force_cindent;
958 if (can_cindent && in_cinkeys(c, '*', line_is_white)
959 && stop_arrow() == OK)
960 do_c_expr_indent();
961 }
962#endif
963
964#ifdef FEAT_RIGHTLEFT
965 if (curwin->w_p_rl)
966 switch (c)
967 {
968 case K_LEFT: c = K_RIGHT; break;
969 case K_S_LEFT: c = K_S_RIGHT; break;
970 case K_C_LEFT: c = K_C_RIGHT; break;
971 case K_RIGHT: c = K_LEFT; break;
972 case K_S_RIGHT: c = K_S_LEFT; break;
973 case K_C_RIGHT: c = K_C_LEFT; break;
974 }
975#endif
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 /*
978 * If 'keymodel' contains "startsel", may start selection. If it
979 * does, a CTRL-O and c will be stuffed, we need to get these
980 * characters.
981 */
982 if (ins_start_select(c))
983 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 /*
986 * The big switch to handle a character in insert mode.
987 */
988 switch (c)
989 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (echeck_abbr(ESC + ABBR_OFF))
992 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200993 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#ifdef FEAT_CMDWIN
997 if (c == Ctrl_C && cmdwin_type != 0)
998 {
999 /* Close the cmdline window. */
1000 cmdwin_result = K_IGNORE;
1001 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001002 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 goto doESCkey;
1004 }
1005#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001006#ifdef FEAT_JOB_CHANNEL
1007 if (c == Ctrl_C && bt_prompt(curbuf))
1008 {
1009 if (invoke_prompt_interrupt())
1010 {
1011 if (!bt_prompt(curbuf))
1012 // buffer changed to a non-prompt buffer, get out of
1013 // Insert mode
1014 goto doESCkey;
1015 break;
1016 }
1017 }
1018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020#ifdef UNIX
1021do_intr:
1022#endif
1023 /* when 'insertmode' set, and not halfway a mapping, don't leave
1024 * Insert mode */
1025 if (goto_im())
1026 {
1027 if (got_int)
1028 {
1029 (void)vgetc(); /* flush all buffers */
1030 got_int = FALSE;
1031 }
1032 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001033 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035 }
1036doESCkey:
1037 /*
1038 * This is the ONLY return from edit()!
1039 */
1040 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1041 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 o_lnum = curwin->w_cursor.lnum;
1044
Bram Moolenaar488c6512005-08-11 20:09:58 +00001045 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001046 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001047 // When CTRL-C was typed got_int will be set, with the result
1048 // that the autocommands won't be executed. When mapped got_int
1049 // is not set, but let's keep the behavior the same.
1050 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001051 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001052 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 continue;
1056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case Ctrl_Z: /* suspend when 'insertmode' set */
1058 if (!p_im)
1059 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001060 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001061#ifdef CURSOR_SHAPE
1062 ui_cursor_shape(); /* may need to update cursor shape */
1063#endif
1064 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065
1066 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001067#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001068 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 goto docomplete;
1070#endif
1071 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1072 break;
1073 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001074
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001075 /* don't move the cursor left when 'virtualedit' has "onemore". */
1076 if (ve_flags & VE_ONEMORE)
1077 {
1078 ins_at_eol = FALSE;
1079 nomove = TRUE;
1080 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 count = 0;
1082 goto doESCkey;
1083
Bram Moolenaar572cb562005-08-05 21:35:02 +00001084 case K_INS: /* toggle insert/replace mode */
1085 case K_KINS:
1086 ins_insert(replaceState);
1087 break;
1088
1089 case K_SELECT: /* end of Select mode mapping - ignore */
1090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_HELP: /* Help key works like <ESC> <Help> */
1093 case K_F1:
1094 case K_XF1:
1095 stuffcharReadbuff(K_HELP);
1096 if (p_im)
1097 need_start_insertmode = TRUE;
1098 goto doESCkey;
1099
1100#ifdef FEAT_NETBEANS_INTG
1101 case K_F21: /* NetBeans command */
1102 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001103 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 --no_mapping;
1105 netbeans_keycommand(i);
1106 break;
1107#endif
1108
1109 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case NUL:
1111 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 /* For ^@ the trailing ESC will end the insert, unless there is an
1113 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1115 && c != Ctrl_A && !p_im)
1116 goto doESCkey; /* quit insert mode */
1117 inserted_space = FALSE;
1118 break;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 ins_reg();
1122 auto_format(FALSE, TRUE);
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_ctrl_g();
1128 break;
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_HAT: /* switch input mode and/or langmap */
1131 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 break;
1133
1134#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (!p_ari)
1137 goto normalchar;
1138 ins_ctrl_();
1139 break;
1140#endif
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1144 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1145 goto docomplete;
1146#endif
1147 /* FALLTHROUGH */
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150# ifdef FEAT_INS_EXPAND
1151 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 if (has_compl_option(FALSE))
1154 goto docomplete;
1155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157# endif
1158 ins_shift(c, lastc);
1159 auto_format(FALSE, TRUE);
1160 inserted_space = FALSE;
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 case K_KDEL:
1165 ins_del();
1166 auto_format(FALSE, TRUE);
1167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case Ctrl_H:
1171 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1172 auto_format(FALSE, TRUE);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001176#ifdef FEAT_JOB_CHANNEL
1177 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1178 {
1179 // In a prompt window CTRL-W is used for window commands.
1180 // Use Shift-CTRL-W to delete a word.
1181 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001182 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001183 nomove = TRUE;
1184 count = 0;
1185 goto doESCkey;
1186 }
1187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1189 auto_format(FALSE, TRUE);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001193# ifdef FEAT_COMPL_FUNC
1194 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001196 goto docomplete;
1197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1199 auto_format(FALSE, TRUE);
1200 inserted_space = FALSE;
1201 break;
1202
1203#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 case K_LEFTMOUSE_NM:
1206 case K_LEFTDRAG:
1207 case K_LEFTRELEASE:
1208 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001209 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_MIDDLEMOUSE:
1211 case K_MIDDLEDRAG:
1212 case K_MIDDLERELEASE:
1213 case K_RIGHTMOUSE:
1214 case K_RIGHTDRAG:
1215 case K_RIGHTRELEASE:
1216 case K_X1MOUSE:
1217 case K_X1DRAG:
1218 case K_X1RELEASE:
1219 case K_X2MOUSE:
1220 case K_X2DRAG:
1221 case K_X2RELEASE:
1222 ins_mouse(c);
1223 break;
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001226 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 break;
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001230 ins_mousescroll(MSCR_UP);
1231 break;
1232
1233 case K_MOUSELEFT: /* Scroll wheel left */
1234 ins_mousescroll(MSCR_LEFT);
1235 break;
1236
1237 case K_MOUSERIGHT: /* Scroll wheel right */
1238 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 break;
1240#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001241 case K_PS:
1242 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1243 if (cmdchar == K_PS)
1244 /* invoked from normal mode, bail out */
1245 goto doESCkey;
1246 break;
1247 case K_PE:
1248 /* Got K_PE without K_PS, ignore. */
1249 break;
1250
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001251#ifdef FEAT_GUI_TABLINE
1252 case K_TABLINE:
1253 case K_TABMENU:
1254 ins_tabline(c);
1255 break;
1256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260
Bram Moolenaar754b5602006-02-09 23:53:20 +00001261 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001262 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001263 did_cursorhold = TRUE;
1264 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001265
Bram Moolenaar4770d092006-01-12 23:22:24 +00001266#ifdef FEAT_GUI_W32
1267 /* On Win32 ignore <M-F4>, we get it when closing the window was
1268 * cancelled. */
1269 case K_F4:
1270 if (mod_mask != MOD_MASK_ALT)
1271 goto normalchar;
1272 break;
1273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_GUI
1276 case K_VER_SCROLLBAR:
1277 ins_scroll();
1278 break;
1279
1280 case K_HOR_SCROLLBAR:
1281 ins_horscroll();
1282 break;
1283#endif
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 case K_S_HOME:
1288 case K_C_HOME:
1289 ins_home(c);
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 case K_S_END:
1295 case K_C_END:
1296 ins_end(c);
1297 break;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001300 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1301 ins_s_left();
1302 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001303 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 break;
1305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001306 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 case K_C_LEFT:
1308 ins_s_left();
1309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001312 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1313 ins_s_right();
1314 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001315 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 break;
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 case K_C_RIGHT:
1320 ins_s_right();
1321 break;
1322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001323 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001324#ifdef FEAT_INS_EXPAND
1325 if (pum_visible())
1326 goto docomplete;
1327#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001328 if (mod_mask & MOD_MASK_SHIFT)
1329 ins_pageup();
1330 else
1331 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 case K_PAGEUP:
1336 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001337#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001338 if (pum_visible())
1339 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ins_pageup();
1342 break;
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001345#ifdef FEAT_INS_EXPAND
1346 if (pum_visible())
1347 goto docomplete;
1348#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001349 if (mod_mask & MOD_MASK_SHIFT)
1350 ins_pagedown();
1351 else
1352 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 break;
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 case K_PAGEDOWN:
1357 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001358#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001359 if (pum_visible())
1360 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 ins_pagedown();
1363 break;
1364
1365#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001366 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ins_drop();
1368 break;
1369#endif
1370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 c = TAB;
1373 /* FALLTHROUGH */
1374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1377 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1378 goto docomplete;
1379#endif
1380 inserted_space = FALSE;
1381 if (ins_tab())
1382 goto normalchar; /* insert TAB as a normal char */
1383 auto_format(FALSE, TRUE);
1384 break;
1385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001386 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 c = CAR;
1388 /* FALLTHROUGH */
1389 case CAR:
1390 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001391#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /* In a quickfix window a <CR> jumps to the error under the
1393 * cursor. */
1394 if (bt_quickfix(curbuf) && c == CAR)
1395 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001396 if (curwin->w_llist_ref == NULL) /* quickfix window */
1397 do_cmdline_cmd((char_u *)".cc");
1398 else /* location list window */
1399 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 break;
1401 }
1402#endif
1403#ifdef FEAT_CMDWIN
1404 if (cmdwin_type != 0)
1405 {
1406 /* Execute the command in the cmdline window. */
1407 cmdwin_result = CAR;
1408 goto doESCkey;
1409 }
1410#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001411#ifdef FEAT_JOB_CHANNEL
1412 if (bt_prompt(curbuf))
1413 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001414 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001415 if (!bt_prompt(curbuf))
1416 // buffer changed to a non-prompt buffer, get out of
1417 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001418 goto doESCkey;
1419 break;
1420 }
1421#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001422 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto doESCkey; /* out of memory */
1424 auto_format(FALSE, FALSE);
1425 inserted_space = FALSE;
1426 break;
1427
Bram Moolenaar860cae12010-06-05 23:22:07 +02001428#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001429 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430# ifdef FEAT_INS_EXPAND
1431 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1432 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001433 if (has_compl_option(TRUE))
1434 goto docomplete;
1435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437# endif
1438# ifdef FEAT_DIGRAPHS
1439 c = ins_digraph();
1440 if (c == NUL)
1441 break;
1442# endif
1443 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001447 case Ctrl_X: /* Enter CTRL-X mode */
1448 ins_ctrl_x();
1449 break;
1450
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (ctrl_x_mode != CTRL_X_TAGS)
1453 goto normalchar;
1454 goto docomplete;
1455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (ctrl_x_mode != CTRL_X_FILES)
1458 goto normalchar;
1459 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001460
1461 case 's': /* Spelling completion after ^X */
1462 case Ctrl_S:
1463 if (ctrl_x_mode != CTRL_X_SPELL)
1464 goto normalchar;
1465 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
1467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#ifdef FEAT_INS_EXPAND
1470 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1471#endif
1472 {
1473 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1474 if (p_im)
1475 {
1476 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1477 break;
1478 goto doESCkey;
1479 }
1480 goto normalchar;
1481 }
1482#ifdef FEAT_INS_EXPAND
1483 /* FALLTHROUGH */
1484
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001485 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 case Ctrl_N:
1487 /* if 'complete' is empty then plain ^P is no longer special,
1488 * but it is under other ^X modes */
1489 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001490 && (ctrl_x_mode == CTRL_X_NORMAL
1491 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001492 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 goto normalchar;
1494
1495docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001496 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001497#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001498 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001499#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001500 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001501 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001502#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001503 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001504#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001505 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 break;
1507#endif /* FEAT_INS_EXPAND */
1508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001509 case Ctrl_Y: /* copy from previous line or scroll down */
1510 case Ctrl_E: /* copy from next line or scroll up */
1511 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 break;
1513
1514 default:
1515#ifdef UNIX
1516 if (c == intr_char) /* special interrupt char */
1517 goto do_intr;
1518#endif
1519
Bram Moolenaare659c952011-05-19 17:25:41 +02001520normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001522 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001524#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001525 if (!p_paste)
1526 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001527 /* Trigger InsertCharPre. */
1528 char_u *str = do_insert_char_pre(c);
1529 char_u *p;
1530
1531 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001532 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001533 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001534 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001535 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001536 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001537 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001538 c = PTR2CHAR(p);
1539 if (c == CAR || c == K_KENTER || c == NL)
1540 ins_eol(c);
1541 else
1542 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001543 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001544 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001546 vim_free(str);
1547 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 }
1549
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001550 /* If the new value is already inserted or an empty string
1551 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001552 if (c == NUL)
1553 break;
1554 }
1555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#ifdef FEAT_SMARTINDENT
1557 /* Try to perform smart-indenting. */
1558 ins_try_si(c);
1559#endif
1560
1561 if (c == ' ')
1562 {
1563 inserted_space = TRUE;
1564#ifdef FEAT_CINDENT
1565 if (inindent(0))
1566 can_cindent = FALSE;
1567#endif
1568 if (Insstart_blank_vcol == MAXCOL
1569 && curwin->w_cursor.lnum == Insstart.lnum)
1570 Insstart_blank_vcol = get_nolist_virtcol();
1571 }
1572
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001573 /* Insert a normal character and check for abbreviations on a
1574 * special character. Let CTRL-] expand abbreviations without
1575 * inserting it. */
1576 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001577 // Add ABBR_OFF for characters above 0x100, this is
1578 // what check_abbr() expects.
1579 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1580 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 insert_special(c, FALSE, FALSE);
1583#ifdef FEAT_RIGHTLEFT
1584 revins_legal++;
1585 revins_chars++;
1586#endif
1587 }
1588
1589 auto_format(FALSE, TRUE);
1590
1591#ifdef FEAT_FOLDING
1592 /* When inserting a character the cursor line must never be in a
1593 * closed fold. */
1594 foldOpenCursor();
1595#endif
1596 break;
1597 } /* end of switch (c) */
1598
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001599 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001600 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001601#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001602 /* but not in CTRL-X mode, a script can't restore the state */
1603 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001604#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001605 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001606 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /* If the cursor was moved we didn't just insert a space */
1609 if (arrow_used)
1610 inserted_space = FALSE;
1611
1612#ifdef FEAT_CINDENT
1613 if (can_cindent && cindent_on()
1614# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001615 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616# endif
1617 )
1618 {
1619force_cindent:
1620 /*
1621 * Indent now if a key was typed that is in 'cinkeys'.
1622 */
1623 if (in_cinkeys(c, ' ', line_is_white))
1624 {
1625 if (stop_arrow() == OK)
1626 /* re-indent the current line */
1627 do_c_expr_indent();
1628 }
1629 }
1630#endif /* FEAT_CINDENT */
1631
1632 } /* for (;;) */
1633 /* NOTREACHED */
1634}
1635
1636/*
1637 * Redraw for Insert mode.
1638 * This is postponed until getting the next character to make '$' in the 'cpo'
1639 * option work correctly.
1640 * Only redraw when there are no characters available. This speeds up
1641 * inserting sequences of characters (e.g., for CTRL-R).
1642 */
1643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001644ins_redraw(
1645 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001647#ifdef FEAT_CONCEAL
1648 linenr_T conceal_old_cursor_line = 0;
1649 linenr_T conceal_new_cursor_line = 0;
1650 int conceal_update_lines = FALSE;
1651#endif
1652
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001653 if (char_avail())
1654 return;
1655
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001656#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001657 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1658 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001659 if (ready && (has_cursormovedI()
1660# if defined(FEAT_CONCEAL)
1661 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001662# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001664 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001667# endif
1668 )
1669 {
1670# ifdef FEAT_SYN_HL
1671 /* Need to update the screen first, to make sure syntax
1672 * highlighting is correct after making a change (e.g., inserting
1673 * a "(". The autocommand may also require a redraw, so it's done
1674 * again below, unfortunately. */
1675 if (syntax_present(curwin) && must_redraw)
1676 update_screen(0);
1677# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001678 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001679 {
1680 /* Make sure curswant is correct, an autocommand may call
1681 * getcurpos(). */
1682 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001683 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001684 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685# ifdef FEAT_CONCEAL
1686 if (curwin->w_p_cole > 0)
1687 {
1688 conceal_old_cursor_line = last_cursormoved.lnum;
1689 conceal_new_cursor_line = curwin->w_cursor.lnum;
1690 conceal_update_lines = TRUE;
1691 }
1692# endif
1693 last_cursormoved = curwin->w_cursor;
1694 }
1695#endif
1696
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001697 /* Trigger TextChangedI if b_changedtick differs. */
1698 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001699 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001700#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001701 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001703 )
1704 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001705 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001706 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001707
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001708 // save and restore curwin and curbuf, in case the autocmd changes them
1709 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001710 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001711 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001712 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001713 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1714 u_save(curwin->w_cursor.lnum,
1715 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001718#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001719 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1720 * TextChangedI will need to trigger for backwards compatibility, thus use
1721 * different b_last_changedtick* variables. */
1722 if (ready && has_textchangedP()
1723 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1724 && pum_visible())
1725 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001726 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001727 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001728
1729 // save and restore curwin and curbuf, in case the autocmd changes them
1730 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001731 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001732 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001733 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001734 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1735 u_save(curwin->w_cursor.lnum,
1736 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001737 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001738#endif
1739
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001740#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001741 if ((conceal_update_lines
1742 && (conceal_old_cursor_line != conceal_new_cursor_line
1743 || conceal_cursor_line(curwin)))
1744 || need_cursor_line_redraw)
1745 {
1746 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001747 redrawWinline(curwin, conceal_old_cursor_line);
1748 redrawWinline(curwin, conceal_new_cursor_line == 0
1749 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001750 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001751 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001752 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001753#endif
1754 if (must_redraw)
1755 update_screen(0);
1756 else if (clear_cmdline || redraw_cmdline)
1757 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001758 showruler(FALSE);
1759 setcursor();
1760 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761}
1762
1763/*
1764 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1765 */
1766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001770 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001773 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001778 did_putchar = TRUE;
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1781
1782#ifdef FEAT_CMDL_INFO
1783 add_to_showcmd_c(Ctrl_V);
1784#endif
1785
1786 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001787 if (did_putchar)
1788 /* when the line fits in 'columns' the '^' is at the start of the next
1789 * line and will not removed by the redraw */
1790 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#ifdef FEAT_CMDL_INFO
1792 clear_showcmd();
1793#endif
1794 insert_special(c, FALSE, TRUE);
1795#ifdef FEAT_RIGHTLEFT
1796 revins_chars++;
1797 revins_legal++;
1798#endif
1799}
1800
1801/*
1802 * Put a character directly onto the screen. It's not stored in a buffer.
1803 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1804 */
1805static int pc_status;
1806#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1807#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1808#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1809#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811static int pc_attr;
1812static int pc_row;
1813static int pc_col;
1814
1815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 int attr;
1819
1820 if (ScreenLines != NULL)
1821 {
1822 update_topline(); /* just in case w_topline isn't valid */
1823 validate_cursor();
1824 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001825 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 else
1827 attr = 0;
1828 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001829 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#ifdef FEAT_RIGHTLEFT
1832 if (curwin->w_p_rl)
1833 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001834 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (has_mbyte)
1836 {
1837 int fix_col = mb_fix_col(pc_col, pc_row);
1838
1839 if (fix_col != pc_col)
1840 {
1841 screen_putchar(' ', pc_row, fix_col, attr);
1842 --curwin->w_wcol;
1843 pc_status = PC_STATUS_RIGHT;
1844 }
1845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 else
1848#endif
1849 {
1850 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (mb_lefthalve(pc_row, pc_col))
1852 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
1855 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1859 pc_status = PC_STATUS_SET;
1860 }
1861 screen_putchar(c, pc_row, pc_col, attr);
1862 }
1863}
1864
Bram Moolenaarf2732452018-06-03 14:47:35 +02001865#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1866/*
1867 * Return the effective prompt for the current buffer.
1868 */
1869 char_u *
1870prompt_text(void)
1871{
1872 if (curbuf->b_prompt_text == NULL)
1873 return (char_u *)"% ";
1874 return curbuf->b_prompt_text;
1875}
1876
1877/*
1878 * Prepare for prompt mode: Make sure the last line has the prompt text.
1879 * Move the cursor to this line.
1880 */
1881 static void
1882init_prompt(int cmdchar_todo)
1883{
1884 char_u *prompt = prompt_text();
1885 char_u *text;
1886
1887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1888 text = ml_get_curline();
1889 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1890 {
1891 // prompt is missing, insert it or append a line with it
1892 if (*text == NUL)
1893 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1894 else
1895 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1896 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1897 coladvance((colnr_T)MAXCOL);
1898 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1899 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001900
1901 // Insert always starts after the prompt, allow editing text after it.
1902 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1903 || Insstart_orig.col != (int)STRLEN(prompt))
1904 {
1905 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001906 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001907 Insstart_orig = Insstart;
1908 Insstart_textlen = Insstart.col;
1909 Insstart_blank_vcol = MAXCOL;
1910 arrow_used = FALSE;
1911 }
1912
Bram Moolenaarf2732452018-06-03 14:47:35 +02001913 if (cmdchar_todo == 'A')
1914 coladvance((colnr_T)MAXCOL);
1915 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001916 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001917 /* Make sure the cursor is in a valid position. */
1918 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001919}
1920
1921/*
1922 * Return TRUE if the cursor is in the editable position of the prompt line.
1923 */
1924 int
1925prompt_curpos_editable()
1926{
1927 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1928 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1929}
1930#endif
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932/*
1933 * Undo the previous edit_putchar().
1934 */
1935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001936edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
1938 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (pc_status == PC_STATUS_RIGHT)
1941 ++curwin->w_wcol;
1942 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001943 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1946 }
1947}
1948
1949/*
1950 * Called when p_dollar is set: display a '$' at the end of the changed text
1951 * Only works when cursor is in the line that changes.
1952 */
1953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001954display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
1956 colnr_T save_col;
1957
1958 if (!redrawing())
1959 return;
1960
1961 cursor_off();
1962 save_col = curwin->w_cursor.col;
1963 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (has_mbyte)
1965 {
1966 char_u *p;
1967
1968 /* If on the last byte of a multi-byte move to the first byte. */
1969 p = ml_get_curline();
1970 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001973 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 edit_putchar('$', FALSE);
1976 dollar_vcol = curwin->w_virtcol;
1977 }
1978 curwin->w_cursor.col = save_col;
1979}
1980
1981/*
1982 * Call this function before moving the cursor from the normal insert position
1983 * in insert mode.
1984 */
1985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001986undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001988 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001990 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001991 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993}
1994
1995/*
1996 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1997 * Keep the cursor on the same character.
1998 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1999 * type == INDENT_DEC decrease indent (for CTRL-D)
2000 * type == INDENT_SET set indent to "amount"
2001 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2002 */
2003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002004change_indent(
2005 int type,
2006 int amount,
2007 int round,
2008 int replaced, /* replaced character, put on replace stack */
2009 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 int vcol;
2012 int last_vcol;
2013 int insstart_less; /* reduction for Insstart.col */
2014 int new_cursor_col;
2015 int i;
2016 char_u *ptr;
2017 int save_p_list;
2018 int start_col;
2019 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 colnr_T orig_col = 0; /* init for GCC */
2021 char_u *new_line, *orig_line = NULL; /* init for GCC */
2022
2023 /* VREPLACE mode needs to know what the line was like before changing */
2024 if (State & VREPLACE_FLAG)
2025 {
2026 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2027 orig_col = curwin->w_cursor.col;
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
2030 /* for the following tricks we don't want list mode */
2031 save_p_list = curwin->w_p_list;
2032 curwin->w_p_list = FALSE;
2033 vc = getvcol_nolist(&curwin->w_cursor);
2034 vcol = vc;
2035
2036 /*
2037 * For Replace mode we need to fix the replace stack later, which is only
2038 * possible when the cursor is in the indent. Remember the number of
2039 * characters before the cursor if it's possible.
2040 */
2041 start_col = curwin->w_cursor.col;
2042
2043 /* determine offset from first non-blank */
2044 new_cursor_col = curwin->w_cursor.col;
2045 beginline(BL_WHITE);
2046 new_cursor_col -= curwin->w_cursor.col;
2047
2048 insstart_less = curwin->w_cursor.col;
2049
2050 /*
2051 * If the cursor is in the indent, compute how many screen columns the
2052 * cursor is to the left of the first non-blank.
2053 */
2054 if (new_cursor_col < 0)
2055 vcol = get_indent() - vcol;
2056
2057 if (new_cursor_col > 0) /* can't fix replace stack */
2058 start_col = -1;
2059
2060 /*
2061 * Set the new indent. The cursor will be put on the first non-blank.
2062 */
2063 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002064 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 else
2066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 int save_State = State;
2068
2069 /* Avoid being called recursively. */
2070 if (State & VREPLACE_FLAG)
2071 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002072 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 insstart_less -= curwin->w_cursor.col;
2076
2077 /*
2078 * Try to put cursor on same character.
2079 * If the cursor is at or after the first non-blank in the line,
2080 * compute the cursor column relative to the column of the first
2081 * non-blank character.
2082 * If we are not in insert mode, leave the cursor on the first non-blank.
2083 * If the cursor is before the first non-blank, position it relative
2084 * to the first non-blank, counted in screen columns.
2085 */
2086 if (new_cursor_col >= 0)
2087 {
2088 /*
2089 * When changing the indent while the cursor is touching it, reset
2090 * Insstart_col to 0.
2091 */
2092 if (new_cursor_col == 0)
2093 insstart_less = MAXCOL;
2094 new_cursor_col += curwin->w_cursor.col;
2095 }
2096 else if (!(State & INSERT))
2097 new_cursor_col = curwin->w_cursor.col;
2098 else
2099 {
2100 /*
2101 * Compute the screen column where the cursor should be.
2102 */
2103 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002104 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 /*
2107 * Advance the cursor until we reach the right screen column.
2108 */
2109 vcol = last_vcol = 0;
2110 new_cursor_col = -1;
2111 ptr = ml_get_curline();
2112 while (vcol <= (int)curwin->w_virtcol)
2113 {
2114 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002116 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002119 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121 vcol = last_vcol;
2122
2123 /*
2124 * May need to insert spaces to be able to position the cursor on
2125 * the right screen column.
2126 */
2127 if (vcol != (int)curwin->w_virtcol)
2128 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002129 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002131 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (ptr != NULL)
2133 {
2134 new_cursor_col += i;
2135 ptr[i] = NUL;
2136 while (--i >= 0)
2137 ptr[i] = ' ';
2138 ins_str(ptr);
2139 vim_free(ptr);
2140 }
2141 }
2142
2143 /*
2144 * When changing the indent while the cursor is in it, reset
2145 * Insstart_col to 0.
2146 */
2147 insstart_less = MAXCOL;
2148 }
2149
2150 curwin->w_p_list = save_p_list;
2151
2152 if (new_cursor_col <= 0)
2153 curwin->w_cursor.col = 0;
2154 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002155 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 curwin->w_set_curswant = TRUE;
2157 changed_cline_bef_curs();
2158
2159 /*
2160 * May have to adjust the start of the insert.
2161 */
2162 if (State & INSERT)
2163 {
2164 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2165 {
2166 if ((int)Insstart.col <= insstart_less)
2167 Insstart.col = 0;
2168 else
2169 Insstart.col -= insstart_less;
2170 }
2171 if ((int)ai_col <= insstart_less)
2172 ai_col = 0;
2173 else
2174 ai_col -= insstart_less;
2175 }
2176
2177 /*
2178 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2179 * If the number of characters before the cursor decreased, need to pop a
2180 * few characters from the replace stack.
2181 * If the number of characters before the cursor increased, need to push a
2182 * few NULs onto the replace stack.
2183 */
2184 if (REPLACE_NORMAL(State) && start_col >= 0)
2185 {
2186 while (start_col > (int)curwin->w_cursor.col)
2187 {
2188 replace_join(0); /* remove a NUL from the replace stack */
2189 --start_col;
2190 }
2191 while (start_col < (int)curwin->w_cursor.col || replaced)
2192 {
2193 replace_push(NUL);
2194 if (replaced)
2195 {
2196 replace_push(replaced);
2197 replaced = NUL;
2198 }
2199 ++start_col;
2200 }
2201 }
2202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 /*
2204 * For VREPLACE mode, we also have to fix the replace stack. In this case
2205 * it is always possible because we backspace over the whole line and then
2206 * put it back again the way we wanted it.
2207 */
2208 if (State & VREPLACE_FLAG)
2209 {
2210 /* If orig_line didn't allocate, just return. At least we did the job,
2211 * even if you can't backspace. */
2212 if (orig_line == NULL)
2213 return;
2214
2215 /* Save new line */
2216 new_line = vim_strsave(ml_get_curline());
2217 if (new_line == NULL)
2218 return;
2219
2220 /* We only put back the new line up to the cursor */
2221 new_line[curwin->w_cursor.col] = NUL;
2222
2223 /* Put back original line */
2224 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2225 curwin->w_cursor.col = orig_col;
2226
2227 /* Backspace from cursor to start of line */
2228 backspace_until_column(0);
2229
2230 /* Insert new stuff into line again */
2231 ins_bytes(new_line);
2232
2233 vim_free(new_line);
2234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235}
2236
2237/*
2238 * Truncate the space at the end of a line. This is to be used only in an
2239 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2240 * modes.
2241 */
2242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
2245 int i;
2246
2247 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002248 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 if (State & REPLACE_FLAG)
2251 replace_join(0); /* remove a NUL from the replace stack */
2252 }
2253 line[i + 1] = NUL;
2254}
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256/*
2257 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2258 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002259 * Will attempt not to go before "col" even when there is a composing
2260 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
2262 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002263backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 while ((int)curwin->w_cursor.col > col)
2266 {
2267 curwin->w_cursor.col--;
2268 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002269 replace_do_bs(col);
2270 else if (!del_char_after_col(col))
2271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002275/*
2276 * Like del_char(), but make sure not to go before column "limit_col".
2277 * Only matters when there are composing characters.
2278 * Return TRUE when something was deleted.
2279 */
2280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002282{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002283 if (enc_utf8 && limit_col >= 0)
2284 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002285 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002286
2287 /* Make sure the cursor is at the start of a character, but
2288 * skip forward again when going too far back because of a
2289 * composing character. */
2290 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002291 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002292 {
2293 int l = utf_ptr2len(ml_get_cursor());
2294
2295 if (l == 0) /* end of line */
2296 break;
2297 curwin->w_cursor.col += l;
2298 }
2299 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2300 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002301 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002302 }
2303 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002304 (void)del_char(FALSE);
2305 return TRUE;
2306}
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2309/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002310 * CTRL-X pressed in Insert mode.
2311 */
2312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002313ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002314{
2315 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2316 * CTRL-V works like CTRL-N */
2317 if (ctrl_x_mode != CTRL_X_CMDLINE)
2318 {
2319 /* if the next ^X<> won't ADD nothing, then reset
2320 * compl_cont_status */
2321 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002322 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002323 else
2324 compl_cont_status = 0;
2325 /* We're not sure which CTRL-X mode it will be yet */
2326 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2327 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2328 edit_submode_pre = NULL;
2329 showmode();
2330 }
2331}
2332
2333/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002334 * Whether other than default completion has been selected.
2335 */
2336 int
2337ctrl_x_mode_not_default(void)
2338{
2339 return ctrl_x_mode != CTRL_X_NORMAL;
2340}
2341
2342/*
2343 * Whether CTRL-X was typed without a following character.
2344 */
2345 int
2346ctrl_x_mode_not_defined_yet(void)
2347{
2348 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2349}
2350
2351/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002352 * Return TRUE if the 'dict' or 'tsr' option can be used.
2353 */
2354 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002355has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002356{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002357 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002358# ifdef FEAT_SPELL
2359 && !curwin->w_p_spell
2360# endif
2361 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002362 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2363 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002364 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002365 edit_submode = NULL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002366 msg_attr(dict_opt ? _("'dictionary' option is empty")
2367 : _("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002368 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002369 if (emsg_silent == 0)
2370 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002371 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002372 setcursor();
2373 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002374#ifdef FEAT_EVAL
2375 if (!get_vim_var_nr(VV_TESTING))
2376#endif
2377 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002378 }
2379 return FALSE;
2380 }
2381 return TRUE;
2382}
2383
2384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2386 * This depends on the current mode.
2387 */
2388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002389vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002391 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (c == Ctrl_R)
2393 return TRUE;
2394
Bram Moolenaare3226be2005-12-18 22:10:00 +00002395 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002396 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002397 return TRUE;
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 switch (ctrl_x_mode)
2400 {
2401 case 0: /* Not in any CTRL-X mode */
2402 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2403 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002404 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2406 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2407 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002408 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002409 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CTRL_X_SCROLL:
2411 return (c == Ctrl_Y || c == Ctrl_E);
2412 case CTRL_X_WHOLE_LINE:
2413 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2414 case CTRL_X_FILES:
2415 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2416 case CTRL_X_DICTIONARY:
2417 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2418 case CTRL_X_THESAURUS:
2419 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2420 case CTRL_X_TAGS:
2421 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2422#ifdef FEAT_FIND_ID
2423 case CTRL_X_PATH_PATTERNS:
2424 return (c == Ctrl_P || c == Ctrl_N);
2425 case CTRL_X_PATH_DEFINES:
2426 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2427#endif
2428 case CTRL_X_CMDLINE:
2429 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2430 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002431#ifdef FEAT_COMPL_FUNC
2432 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002433 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002434 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002435 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002436#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002437 case CTRL_X_SPELL:
2438 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002439 case CTRL_X_EVAL:
2440 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002442 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return FALSE;
2444}
2445
2446/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002447 * Return TRUE when character "c" is part of the item currently being
2448 * completed. Used to decide whether to abandon complete mode when the menu
2449 * is visible.
2450 */
2451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002452ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002453{
2454 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2455 /* When expanding an identifier only accept identifier chars. */
2456 return vim_isIDc(c);
2457
2458 switch (ctrl_x_mode)
2459 {
2460 case CTRL_X_FILES:
2461 /* When expanding file name only accept file name chars. But not
2462 * path separators, so that "proto/<Tab>" expands files in
2463 * "proto", not "proto/" as a whole */
2464 return vim_isfilec(c) && !vim_ispathsep(c);
2465
2466 case CTRL_X_CMDLINE:
2467 case CTRL_X_OMNI:
2468 /* Command line and Omni completion can work with just about any
2469 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002470 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002471
2472 case CTRL_X_WHOLE_LINE:
2473 /* For while line completion a space can be part of the line. */
2474 return vim_isprintc(c);
2475 }
2476 return vim_iswordc(c);
2477}
2478
2479/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002480 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002482 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * the rest of the word to be in -- webb
2484 */
2485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002486ins_compl_add_infercase(
2487 char_u *str,
2488 int len,
2489 int icase,
2490 char_u *fname,
2491 int dir,
2492 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002494 char_u *p;
2495 int i, c;
2496 int actual_len; /* Take multi-byte characters */
2497 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002498 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002499 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int has_lower = FALSE;
2501 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002503 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaara2993e12007-08-12 14:38:46 +00002507 /* Find actual length of completion. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002508 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002510 p = str;
2511 actual_len = 0;
2512 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002514 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002515 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002519 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
Bram Moolenaara2993e12007-08-12 14:38:46 +00002521 /* Find actual length of original text. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002522 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002524 p = compl_orig_text;
2525 actual_compl_length = 0;
2526 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002528 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
2531 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002532 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002533 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002535 /* "actual_len" may be smaller than "actual_compl_length" when using
2536 * thesaurus, only use the minimum when comparing. */
2537 min_len = actual_len < actual_compl_length
2538 ? actual_len : actual_compl_length;
2539
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002541 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002542 if (wca != NULL)
2543 {
2544 p = str;
2545 for (i = 0; i < actual_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002546 if (has_mbyte)
2547 wca[i] = mb_ptr2char_adv(&p);
2548 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002549 wca[i] = *(p++);
2550
2551 /* Rule 1: Were any chars converted to lower? */
2552 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002553 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002554 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002555 if (has_mbyte)
2556 c = mb_ptr2char_adv(&p);
2557 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002558 c = *(p++);
2559 if (MB_ISLOWER(c))
2560 {
2561 has_lower = TRUE;
2562 if (MB_ISUPPER(wca[i]))
2563 {
2564 /* Rule 1 is satisfied. */
2565 for (i = actual_compl_length; i < actual_len; ++i)
2566 wca[i] = MB_TOLOWER(wca[i]);
2567 break;
2568 }
2569 }
2570 }
2571
2572 /*
2573 * Rule 2: No lower case, 2nd consecutive letter converted to
2574 * upper case.
2575 */
2576 if (!has_lower)
2577 {
2578 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002579 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002580 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002581 if (has_mbyte)
2582 c = mb_ptr2char_adv(&p);
2583 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002584 c = *(p++);
2585 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2586 {
2587 /* Rule 2 is satisfied. */
2588 for (i = actual_compl_length; i < actual_len; ++i)
2589 wca[i] = MB_TOUPPER(wca[i]);
2590 break;
2591 }
2592 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2593 }
2594 }
2595
2596 /* Copy the original case of the part we typed. */
2597 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002598 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002599 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002600 if (has_mbyte)
2601 c = mb_ptr2char_adv(&p);
2602 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002603 c = *(p++);
2604 if (MB_ISLOWER(c))
2605 wca[i] = MB_TOLOWER(wca[i]);
2606 else if (MB_ISUPPER(c))
2607 wca[i] = MB_TOUPPER(wca[i]);
2608 }
2609
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002610 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002611 * Generate encoding specific output from wide character array.
2612 * Multi-byte characters can occupy up to five bytes more than
2613 * ASCII characters, and we also need one byte for NUL, so stay
2614 * six bytes away from the edge of IObuff.
2615 */
2616 p = IObuff;
2617 i = 0;
2618 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002619 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002620 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002621 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002622 *(p++) = wca[i++];
2623 *p = NUL;
2624
2625 vim_free(wca);
2626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002628 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2629 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002631 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632}
2633
2634/*
2635 * Add a match to the list of matches.
2636 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002637 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002638 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002641ins_compl_add(
2642 char_u *str,
2643 int len,
2644 int icase,
2645 char_u *fname,
2646 char_u **cptext, /* extra text for popup menu or NULL */
2647 int cdir,
2648 int flags,
2649 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002651 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002652 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 ui_breakcheck();
2655 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (len < 0)
2658 len = (int)STRLEN(str);
2659
2660 /*
2661 * If the same match is already present, don't add it.
2662 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002665 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 do
2667 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002668 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002669 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002670 && match->cp_str[len] == NUL)
2671 return NOTDONE;
2672 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002673 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002676 /* Remove any popup menu before changing the list of matches. */
2677 ins_compl_del_pum();
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 /*
2680 * Allocate a new match structure.
2681 * Copy the values to the new match structure.
2682 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002683 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002685 return FAIL;
2686 match->cp_number = -1;
2687 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002688 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002689 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002694 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002697 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2699 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002700 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002701 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002702 && compl_curr_match->cp_fname != NULL
2703 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002704 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002705 else if (fname != NULL)
2706 {
2707 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002708 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002711 match->cp_fname = NULL;
2712 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002713
2714 if (cptext != NULL)
2715 {
2716 int i;
2717
2718 for (i = 0; i < CPT_COUNT; ++i)
2719 if (cptext[i] != NULL && *cptext[i] != NUL)
2720 match->cp_text[i] = vim_strsave(cptext[i]);
2721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 /*
2724 * Link the new match structure in the list of matches.
2725 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002726 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002727 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 else if (dir == FORWARD)
2729 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002730 match->cp_next = compl_curr_match->cp_next;
2731 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 else /* BACKWARD */
2734 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 match->cp_next = compl_curr_match;
2736 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002738 if (match->cp_next)
2739 match->cp_next->cp_prev = match;
2740 if (match->cp_prev)
2741 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002743 compl_first_match = match;
2744 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002746 /*
2747 * Find the longest common string if still doing that.
2748 */
2749 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2750 ins_compl_longest_match(match);
2751
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 return OK;
2753}
2754
2755/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002756 * Return TRUE if "str[len]" matches with match->cp_str, considering
2757 * match->cp_icase.
2758 */
2759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002760ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002761{
2762 if (match->cp_icase)
2763 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2764 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2765}
2766
2767/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002768 * Reduce the longest common string for match "match".
2769 */
2770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002771ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002772{
2773 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002774 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002775 int had_match;
2776
2777 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002778 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002779 /* First match, use it as a whole. */
2780 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002781 if (compl_leader != NULL)
2782 {
2783 had_match = (curwin->w_cursor.col > compl_col);
2784 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002785 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002786 ins_redraw(FALSE);
2787
2788 /* When the match isn't there (to avoid matching itself) remove it
2789 * again after redrawing. */
2790 if (!had_match)
2791 ins_compl_delete();
2792 compl_used_match = FALSE;
2793 }
2794 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002795 else
2796 {
2797 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002798 p = compl_leader;
2799 s = match->cp_str;
2800 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002801 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002802 if (has_mbyte)
2803 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002804 c1 = mb_ptr2char(p);
2805 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002806 }
2807 else
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002808 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002809 c1 = *p;
2810 c2 = *s;
2811 }
2812 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2813 : (c1 != c2))
2814 break;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002815 if (has_mbyte)
2816 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002817 MB_PTR_ADV(p);
2818 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002819 }
2820 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002821 {
2822 ++p;
2823 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002824 }
2825 }
2826
2827 if (*p != NUL)
2828 {
2829 /* Leader was shortened, need to change the inserted text. */
2830 *p = NUL;
2831 had_match = (curwin->w_cursor.col > compl_col);
2832 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002833 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002834 ins_redraw(FALSE);
2835
2836 /* When the match isn't there (to avoid matching itself) remove it
2837 * again after redrawing. */
2838 if (!had_match)
2839 ins_compl_delete();
2840 }
2841
2842 compl_used_match = FALSE;
2843 }
2844}
2845
2846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 * Add an array of matches to the list of matches.
2848 * Frees matches[].
2849 */
2850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002851ins_compl_add_matches(
2852 int num_matches,
2853 char_u **matches,
2854 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 int i;
2857 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002858 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar572cb562005-08-05 21:35:02 +00002860 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002861 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002862 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002864 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 FreeWild(num_matches, matches);
2866}
2867
2868/* Make the completion list cyclic.
2869 * Return the number of matches (excluding the original).
2870 */
2871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002872ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002874 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 int count = 0;
2876
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002877 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
2879 /*
2880 * Find the end of the list.
2881 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002882 match = compl_first_match;
2883 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002884 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002886 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 ++count;
2888 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002889 match->cp_next = compl_first_match;
2890 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892 return count;
2893}
2894
Bram Moolenaara94bc432006-03-10 21:42:59 +00002895/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002896 * Set variables that store noselect and noinsert behavior from the
2897 * 'completeopt' value.
2898 */
2899 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002900completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002901{
2902 compl_no_insert = FALSE;
2903 compl_no_select = FALSE;
2904 if (strstr((char *)p_cot, "noselect") != NULL)
2905 compl_no_select = TRUE;
2906 if (strstr((char *)p_cot, "noinsert") != NULL)
2907 compl_no_insert = TRUE;
2908}
2909
2910/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002911 * Start completion for the complete() function.
2912 * "startcol" is where the matched text starts (1 is first column).
2913 * "list" is the list of matches.
2914 */
2915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002916set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002917{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002918 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002919 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002920
Bram Moolenaara94bc432006-03-10 21:42:59 +00002921 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002922 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002923 ins_compl_prep(' ');
2924 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002925 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002926
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002927 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002928 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002929 startcol = curwin->w_cursor.col;
2930 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002931 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002932 /* compl_pattern doesn't need to be set */
2933 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2934 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002935 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002936 return;
2937
Bram Moolenaare4214502015-03-05 18:08:43 +01002938 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002939
2940 ins_compl_add_list(list);
2941 compl_matches = ins_compl_make_cyclic();
2942 compl_started = TRUE;
2943 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002944 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002945
2946 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002947 if (compl_no_insert || compl_no_select)
2948 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002949 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002950 if (compl_no_select)
2951 /* Down/Up has no real effect. */
2952 ins_complete(K_UP, FALSE);
2953 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002954 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002955 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002956 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002957
2958 /* Lazily show the popup menu, unless we got interrupted. */
2959 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002960 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002961 out_flush();
2962}
2963
2964
Bram Moolenaar9372a112005-12-06 19:59:18 +00002965/* "compl_match_array" points the currently displayed list of entries in the
2966 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002967static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002968static int compl_match_arraysize;
2969
2970/*
2971 * Update the screen and when there is any scrolling remove the popup menu.
2972 */
2973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002974ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002975{
2976 int h;
2977
2978 if (compl_match_array != NULL)
2979 {
2980 h = curwin->w_cline_height;
Bram Moolenaarae654382019-01-17 21:09:05 +01002981 // Update the screen later, before drawing the popup menu over it.
2982 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983 if (h != curwin->w_cline_height)
2984 ins_compl_del_pum();
2985 }
2986}
2987
2988/*
2989 * Remove any popup menu.
2990 */
2991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002992ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993{
2994 if (compl_match_array != NULL)
2995 {
2996 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01002997 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002998 }
2999}
3000
3001/*
3002 * Return TRUE if the popup menu should be displayed.
3003 */
3004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003005pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003007 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003008 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003009 return FALSE;
3010
3011 /* The display looks bad on a B&W display. */
3012 if (t_colors < 8
3013#ifdef FEAT_GUI
3014 && !gui.in_use
3015#endif
3016 )
3017 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003018 return TRUE;
3019}
3020
3021/*
3022 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003023 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003024 */
3025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003026pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003027{
3028 compl_T *compl;
3029 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030
3031 /* Don't display the popup menu if there are no matches or there is only
3032 * one (ignoring the original text). */
3033 compl = compl_first_match;
3034 i = 0;
3035 do
3036 {
3037 if (compl == NULL
3038 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3039 break;
3040 compl = compl->cp_next;
3041 } while (compl != compl_first_match);
3042
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003043 if (strstr((char *)p_cot, "menuone") != NULL)
3044 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003045 return (i >= 2);
3046}
3047
3048/*
3049 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003050 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003051 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003054{
3055 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003056 compl_T *shown_compl = NULL;
3057 int did_find_shown_match = FALSE;
3058 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003059 int i;
3060 int cur = -1;
3061 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003062 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003063
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003064 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003065 return;
3066
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003067#if defined(FEAT_EVAL)
3068 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3069 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3070#endif
3071
Bram Moolenaarae654382019-01-17 21:09:05 +01003072 // Update the screen later, before drawing the popup menu over it.
3073 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003074
3075 if (compl_match_array == NULL)
3076 {
3077 /* Need to build the popup menu list. */
3078 compl_match_arraysize = 0;
3079 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003080 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003081 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082 do
3083 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003084 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3085 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003086 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 ++compl_match_arraysize;
3088 compl = compl->cp_next;
3089 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003090 if (compl_match_arraysize == 0)
3091 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003092 compl_match_array = (pumitem_T *)alloc_clear(
3093 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003094 * compl_match_arraysize));
3095 if (compl_match_array != NULL)
3096 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003097 /* If the current match is the original text don't find the first
3098 * match after it, don't highlight anything. */
3099 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3100 shown_match_ok = TRUE;
3101
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 i = 0;
3103 compl = compl_first_match;
3104 do
3105 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003106 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3107 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003108 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003110 if (!shown_match_ok)
3111 {
3112 if (compl == compl_shown_match || did_find_shown_match)
3113 {
3114 /* This item is the shown match or this is the
3115 * first displayed item after the shown match. */
3116 compl_shown_match = compl;
3117 did_find_shown_match = TRUE;
3118 shown_match_ok = TRUE;
3119 }
3120 else
3121 /* Remember this displayed match for when the
3122 * shown match is just below it. */
3123 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003124 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003125 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003126
3127 if (compl->cp_text[CPT_ABBR] != NULL)
3128 compl_match_array[i].pum_text =
3129 compl->cp_text[CPT_ABBR];
3130 else
3131 compl_match_array[i].pum_text = compl->cp_str;
3132 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3133 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3134 if (compl->cp_text[CPT_MENU] != NULL)
3135 compl_match_array[i++].pum_extra =
3136 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003137 else
3138 compl_match_array[i++].pum_extra = compl->cp_fname;
3139 }
3140
3141 if (compl == compl_shown_match)
3142 {
3143 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003144
3145 /* When the original text is the shown match don't set
3146 * compl_shown_match. */
3147 if (compl->cp_flags & ORIGINAL_TEXT)
3148 shown_match_ok = TRUE;
3149
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003150 if (!shown_match_ok && shown_compl != NULL)
3151 {
3152 /* The shown match isn't displayed, set it to the
3153 * previously displayed match. */
3154 compl_shown_match = shown_compl;
3155 shown_match_ok = TRUE;
3156 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003157 }
3158 compl = compl->cp_next;
3159 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003160
3161 if (!shown_match_ok) /* no displayed match at all */
3162 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003163 }
3164 }
3165 else
3166 {
3167 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003168 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003169 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3170 || compl_match_array[i].pum_text
3171 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003172 {
3173 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003174 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003175 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003176 }
3177
3178 if (compl_match_array != NULL)
3179 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003180 /* In Replace mode when a $ is displayed at the end of the line only
3181 * part of the screen would be updated. We do need to redraw here. */
3182 dollar_vcol = -1;
3183
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003184 /* Compute the screen column of the start of the completed text.
3185 * Use the cursor to get all wrapping and other settings right. */
3186 col = curwin->w_cursor.col;
3187 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003188 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003189 curwin->w_cursor.col = col;
3190 }
3191}
3192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#define DICT_FIRST (1) /* use just first element in "dict" */
3194#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 * Add any identifiers that match the given pattern in the list of dictionary
3198 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
3200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201ins_compl_dictionaries(
3202 char_u *dict_start,
3203 char_u *pat,
3204 int flags, /* DICT_FIRST and/or DICT_EXACT */
3205 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003207 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 char_u *ptr;
3209 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 char_u **files;
3212 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003214 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar0b238792006-03-02 22:49:12 +00003216 if (*dict == NUL)
3217 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003218#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003219 /* When 'dictionary' is empty and spell checking is enabled use
3220 * "spell". */
3221 if (!thesaurus && curwin->w_p_spell)
3222 dict = (char_u *)"spell";
3223 else
3224#endif
3225 return;
3226 }
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003229 if (buf == NULL)
3230 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003231 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* If 'infercase' is set, don't use 'smartcase' here */
3234 save_p_scs = p_scs;
3235 if (curbuf->b_p_inf)
3236 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003237
3238 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3239 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003240 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003241 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003242 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003243 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003244 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003245
3246 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003247 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003248 len = STRLEN(pat_esc) + 10;
3249 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003250 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003251 {
3252 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003253 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003254 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003255 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003256 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3257 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003258 vim_free(ptr);
3259 }
3260 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003261 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003262 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003263 if (regmatch.regprog == NULL)
3264 goto theend;
3265 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3268 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 /* copy one dictionary file name into buf */
3272 if (flags == DICT_EXACT)
3273 {
3274 count = 1;
3275 files = &dict;
3276 }
3277 else
3278 {
3279 /* Expand wildcards in the dictionary name, but do not allow
3280 * backticks (for security, the 'dict' option may have been set in
3281 * a modeline). */
3282 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003283# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003284 if (!thesaurus && STRCMP(buf, "spell") == 0)
3285 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003286 else
3287# endif
3288 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 || expand_wildcards(1, &buf, &count, &files,
3290 EW_FILE|EW_SILENT) != OK)
3291 count = 0;
3292 }
3293
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003294# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003295 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003297 /* Complete from active spelling. Skip "\<" in the pattern, we
3298 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003299 if (pat[0] == '\\' && pat[1] == '<')
3300 ptr = pat + 2;
3301 else
3302 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003303 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003305 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003306# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003307 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 {
3309 ins_compl_files(count, files, thesaurus, flags,
3310 &regmatch, buf, &dir);
3311 if (flags != DICT_EXACT)
3312 FreeWild(count, files);
3313 }
3314 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 break;
3316 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003317
3318theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003320 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 vim_free(buf);
3322}
3323
Bram Moolenaar0b238792006-03-02 22:49:12 +00003324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003325ins_compl_files(
3326 int count,
3327 char_u **files,
3328 int thesaurus,
3329 int flags,
3330 regmatch_T *regmatch,
3331 char_u *buf,
3332 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003333{
3334 char_u *ptr;
3335 int i;
3336 FILE *fp;
3337 int add_r;
3338
3339 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3340 {
3341 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3342 if (flags != DICT_EXACT)
3343 {
3344 vim_snprintf((char *)IObuff, IOSIZE,
3345 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003346 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003347 }
3348
3349 if (fp != NULL)
3350 {
3351 /*
3352 * Read dictionary file line by line.
3353 * Check each line for a match.
3354 */
3355 while (!got_int && !compl_interrupted
3356 && !vim_fgets(buf, LSIZE, fp))
3357 {
3358 ptr = buf;
3359 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3360 {
3361 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003362 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003363 ptr = find_line_end(ptr);
3364 else
3365 ptr = find_word_end(ptr);
3366 add_r = ins_compl_add_infercase(regmatch->startp[0],
3367 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003368 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003369 if (thesaurus)
3370 {
3371 char_u *wstart;
3372
3373 /*
3374 * Add the other matches on the line
3375 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003376 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003377 while (!got_int)
3378 {
3379 /* Find start of the next word. Skip white
3380 * space and punctuation. */
3381 ptr = find_word_start(ptr);
3382 if (*ptr == NUL || *ptr == NL)
3383 break;
3384 wstart = ptr;
3385
Bram Moolenaara2993e12007-08-12 14:38:46 +00003386 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003387 if (has_mbyte)
3388 /* Japanese words may have characters in
3389 * different classes, only separate words
3390 * with single-byte non-word characters. */
3391 while (*ptr != NUL)
3392 {
3393 int l = (*mb_ptr2len)(ptr);
3394
3395 if (l < 2 && !vim_iswordc(*ptr))
3396 break;
3397 ptr += l;
3398 }
3399 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003400 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003401
3402 /* Add the word. Skip the regexp match. */
3403 if (wstart != regmatch->startp[0])
3404 add_r = ins_compl_add_infercase(wstart,
3405 (int)(ptr - wstart),
3406 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003407 }
3408 }
3409 if (add_r == OK)
3410 /* if dir was BACKWARD then honor it just once */
3411 *dir = FORWARD;
3412 else if (add_r == FAIL)
3413 break;
3414 /* avoid expensive call to vim_regexec() when at end
3415 * of line */
3416 if (*ptr == '\n' || got_int)
3417 break;
3418 }
3419 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003420 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003421 }
3422 fclose(fp);
3423 }
3424 }
3425}
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427/*
3428 * Find the start of the next word.
3429 * Returns a pointer to the first char of the word. Also stops at a NUL.
3430 */
3431 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003432find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (has_mbyte)
3435 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003436 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3439 ++ptr;
3440 return ptr;
3441}
3442
3443/*
3444 * Find the end of the word. Assumes it starts inside a word.
3445 * Returns a pointer to just after the word.
3446 */
3447 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 int start_class;
3451
3452 if (has_mbyte)
3453 {
3454 start_class = mb_get_class(ptr);
3455 if (start_class > 1)
3456 while (*ptr != NUL)
3457 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003458 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (mb_get_class(ptr) != start_class)
3460 break;
3461 }
3462 }
3463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 while (vim_iswordc(*ptr))
3465 ++ptr;
3466 return ptr;
3467}
3468
3469/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003470 * Find the end of the line, omitting CR and NL at the end.
3471 * Returns a pointer to just after the line.
3472 */
3473 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003475{
3476 char_u *s;
3477
3478 s = ptr + STRLEN(ptr);
3479 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3480 --s;
3481 return s;
3482}
3483
3484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 * Free the list of completions
3486 */
3487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003488ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003490 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003491 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaard23a8232018-02-10 18:45:26 +01003493 VIM_CLEAR(compl_pattern);
3494 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003496 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003498
3499 ins_compl_del_pum();
3500 pum_clear();
3501
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 do
3504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003506 compl_curr_match = compl_curr_match->cp_next;
3507 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003509 if (match->cp_flags & FREE_FNAME)
3510 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003511 for (i = 0; i < CPT_COUNT; ++i)
3512 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003514 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3515 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003516 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003517 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518}
3519
3520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003523 compl_cont_status = 0;
3524 compl_started = FALSE;
3525 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003526 VIM_CLEAR(compl_pattern);
3527 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003529 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003530 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003531 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003532 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533}
3534
3535/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003536 * Return TRUE when Insert completion is active.
3537 */
3538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003539ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003540{
3541 return compl_started;
3542}
3543
3544/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003545 * Delete one character before the cursor and show the subset of the matches
3546 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003547 * Returns the character to be used, NUL if the work is done and another char
3548 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003549 */
3550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003551ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003552{
3553 char_u *line;
3554 char_u *p;
3555
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003556 line = ml_get_curline();
3557 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003558 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003559
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003560 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003561 * allow the word to be deleted, we won't match everything.
3562 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003563 if ((int)(p - line) - (int)compl_col < 0
3564 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003565 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3566 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3567 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003568 return K_BS;
3569
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003570 /* Deleted more than what was used to find matches or didn't finish
3571 * finding all matches: need to look for matches all over again. */
3572 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003573 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003574 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003575
Bram Moolenaara6557602006-02-04 22:43:20 +00003576 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003577 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003578 if (compl_leader != NULL)
3579 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003580 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003581 if (compl_shown_match != NULL)
3582 /* Make sure current match is not a hidden item. */
3583 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003584 return NUL;
3585 }
3586 return K_BS;
3587}
Bram Moolenaara6557602006-02-04 22:43:20 +00003588
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003589/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003590 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3591 * be called.
3592 */
3593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003594ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003595{
3596 /* Return TRUE if we didn't complete finding matches or when the
3597 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3598 return compl_was_interrupted
3599 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3600 && compl_opt_refresh_always);
3601}
3602
3603/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003604 * Called after changing "compl_leader".
3605 * Show the popup menu with a different set of matches.
3606 * May also search for matches again if the previous search was interrupted.
3607 */
3608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003610{
3611 ins_compl_del_pum();
3612 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003613 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003614 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003615
3616 if (compl_started)
3617 ins_compl_set_original_text(compl_leader);
3618 else
3619 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003620#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003621 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003622#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003623 /*
Bram Moolenaarae654382019-01-17 21:09:05 +01003624 * Matches were cleared, need to search for them now. Befor drawing
3625 * the popup menu display the changed text before the cursor. Set
3626 * "compl_restarting" to avoid that the first match is inserted.
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003627 */
Bram Moolenaarae654382019-01-17 21:09:05 +01003628 pum_call_update_screen();
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003629#ifdef FEAT_GUI
3630 if (gui.in_use)
3631 {
3632 /* Show the cursor after the match, not after the redrawn text. */
3633 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003634 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003635 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003636#endif
3637 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003638 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003639 compl_cont_status = 0;
3640 compl_restarting = FALSE;
3641 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003642
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003643 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003644
3645 /* Show the popup menu with a different set of matches. */
3646 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003647
3648 /* Don't let Enter select the original text when there is no popup menu. */
3649 if (compl_match_array == NULL)
3650 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003651}
3652
3653/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003654 * Return the length of the completion, from the completion start column to
3655 * the cursor column. Making sure it never goes below zero.
3656 */
3657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003659{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003660 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003661
3662 if (off < 0)
3663 return 0;
3664 return off;
3665}
3666
3667/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003668 * Append one character to the match leader. May reduce the number of
3669 * matches.
3670 */
3671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003673{
Bram Moolenaara6557602006-02-04 22:43:20 +00003674 int cc;
3675
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003676 if (stop_arrow() == FAIL)
3677 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003678 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3679 {
3680 char_u buf[MB_MAXBYTES + 1];
3681
3682 (*mb_char2bytes)(c, buf);
3683 buf[cc] = NUL;
3684 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003685 if (compl_opt_refresh_always)
3686 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003687 }
3688 else
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003689 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003690 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003691 if (compl_opt_refresh_always)
3692 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003693 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003694
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003695 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003696 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003697 ins_compl_restart();
3698
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003699 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003700 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003701 * break redo. */
3702 if (!compl_opt_refresh_always)
3703 {
3704 vim_free(compl_leader);
3705 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003706 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003707 if (compl_leader != NULL)
3708 ins_compl_new_leader();
3709 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003710}
3711
3712/*
3713 * Setup for finding completions again without leaving CTRL-X mode. Used when
3714 * BS or a key was typed while still searching for matches.
3715 */
3716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003718{
3719 ins_compl_free();
3720 compl_started = FALSE;
3721 compl_matches = 0;
3722 compl_cont_status = 0;
3723 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003724}
3725
3726/*
3727 * Set the first match, the original text.
3728 */
3729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003730ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003731{
3732 char_u *p;
3733
Bram Moolenaare87edf32018-04-17 22:14:32 +02003734 /* Replace the original text entry.
3735 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3736 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003737 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3738 {
3739 p = vim_strsave(str);
3740 if (p != NULL)
3741 {
3742 vim_free(compl_first_match->cp_str);
3743 compl_first_match->cp_str = p;
3744 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003745 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003746 else if (compl_first_match->cp_prev != NULL
3747 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3748 {
3749 p = vim_strsave(str);
3750 if (p != NULL)
3751 {
3752 vim_free(compl_first_match->cp_prev->cp_str);
3753 compl_first_match->cp_prev->cp_str = p;
3754 }
3755 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003756}
3757
3758/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003759 * Append one character to the match leader. May reduce the number of
3760 * matches.
3761 */
3762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003764{
3765 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003766 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003767 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003768 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003769
3770 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003771 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003772 {
3773 /* When still at the original match use the first entry that matches
3774 * the leader. */
3775 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3776 {
3777 p = NULL;
3778 for (cp = compl_shown_match->cp_next; cp != NULL
3779 && cp != compl_first_match; cp = cp->cp_next)
3780 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003781 if (compl_leader == NULL
3782 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003783 (int)STRLEN(compl_leader)))
3784 {
3785 p = cp->cp_str;
3786 break;
3787 }
3788 }
3789 if (p == NULL || (int)STRLEN(p) <= len)
3790 return;
3791 }
3792 else
3793 return;
3794 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003795 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003796 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003797 ins_compl_addleader(c);
3798}
3799
3800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003802 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003803 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003806ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003810 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 /* Forget any previous 'special' messages if this is actually
3813 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3814 */
3815 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3816 edit_submode_extra = NULL;
3817
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003818 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003819 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3820 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003821 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003823 /* Set "compl_get_longest" when finding the first matches. */
3824 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003825 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003826 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003827 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003828 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003829
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003830 }
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3833 {
3834 /*
3835 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3836 * it will be yet. Now we decide.
3837 */
3838 switch (c)
3839 {
3840 case Ctrl_E:
3841 case Ctrl_Y:
3842 ctrl_x_mode = CTRL_X_SCROLL;
3843 if (!(State & REPLACE_FLAG))
3844 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3845 else
3846 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3847 edit_submode_pre = NULL;
3848 showmode();
3849 break;
3850 case Ctrl_L:
3851 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3852 break;
3853 case Ctrl_F:
3854 ctrl_x_mode = CTRL_X_FILES;
3855 break;
3856 case Ctrl_K:
3857 ctrl_x_mode = CTRL_X_DICTIONARY;
3858 break;
3859 case Ctrl_R:
3860 /* Simply allow ^R to happen without affecting ^X mode */
3861 break;
3862 case Ctrl_T:
3863 ctrl_x_mode = CTRL_X_THESAURUS;
3864 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003865#ifdef FEAT_COMPL_FUNC
3866 case Ctrl_U:
3867 ctrl_x_mode = CTRL_X_FUNCTION;
3868 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003869 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003870 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003872#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003873 case 's':
3874 case Ctrl_S:
3875 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003876#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003877 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003878 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003879 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003880#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003881 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case Ctrl_RSB:
3883 ctrl_x_mode = CTRL_X_TAGS;
3884 break;
3885#ifdef FEAT_FIND_ID
3886 case Ctrl_I:
3887 case K_S_TAB:
3888 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3889 break;
3890 case Ctrl_D:
3891 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3892 break;
3893#endif
3894 case Ctrl_V:
3895 case Ctrl_Q:
3896 ctrl_x_mode = CTRL_X_CMDLINE;
3897 break;
3898 case Ctrl_P:
3899 case Ctrl_N:
3900 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3901 * just started ^X mode, or there were enough ^X's to cancel
3902 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3903 * do normal expansion when interrupting a different mode (say
3904 * ^X^F^X^P or ^P^X^X^P, see below)
3905 * nothing changes if interrupting mode 0, (eg, the flag
3906 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907 if (!(compl_cont_status & CONT_INTRPT))
3908 compl_cont_status |= CONT_LOCAL;
3909 else if (compl_cont_mode != 0)
3910 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 /* FALLTHROUGH */
3912 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003913 /* If we have typed at least 2 ^X's... for modes != 0, we set
3914 * compl_cont_status = 0 (eg, as if we had just started ^X
3915 * mode).
3916 * For mode 0, we set "compl_cont_mode" to an impossible
3917 * value, in both cases ^X^X can be used to restart the same
3918 * mode (avoiding ADDING mode).
3919 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3920 * 'complete' and local ^P expansions respectively.
3921 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3922 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (c == Ctrl_X)
3924 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003925 if (compl_cont_mode != 0)
3926 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003930 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 edit_submode = NULL;
3932 showmode();
3933 break;
3934 }
3935 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003936 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
3938 /* We're already in CTRL-X mode, do we stay in it? */
3939 if (!vim_is_ctrl_x_key(c))
3940 {
3941 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003942 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 else
3944 ctrl_x_mode = CTRL_X_FINISHED;
3945 edit_submode = NULL;
3946 }
3947 showmode();
3948 }
3949
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 /* Show error message from attempted keyword completion (probably
3953 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003954 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003956 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3957 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 || ctrl_x_mode == CTRL_X_FINISHED)
3959 {
3960 /* Get here when we have finished typing a sequence of ^N and
3961 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003963 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003966 * If any of the original typed text has been changed, eg when
3967 * ignorecase is set, we must add back-spaces to the redo
3968 * buffer. We add as few as necessary to delete just the part
3969 * of the original text that has changed.
3970 * When using the longest match, edited the match or used
3971 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003973 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3974 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003975 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003976 ptr = NULL;
3977 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979
3980#ifdef FEAT_CINDENT
3981 want_cindent = (can_cindent && cindent_on());
3982#endif
3983 /*
3984 * When completing whole lines: fix indent for 'cindent'.
3985 * Otherwise, break line if it's too long.
3986 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003987 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
3989#ifdef FEAT_CINDENT
3990 /* re-indent the current line */
3991 if (want_cindent)
3992 {
3993 do_c_expr_indent();
3994 want_cindent = FALSE; /* don't do it again */
3995 }
3996#endif
3997 }
3998 else
3999 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004000 int prev_col = curwin->w_cursor.col;
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004003 if (prev_col > 0)
4004 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004005 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004006 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004008 if (prev_col > 0
4009 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4010 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004013 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004014 * the selection without inserting anything. When
4015 * compl_enter_selects is set the Enter key does the same. */
4016 if ((c == Ctrl_Y || (compl_enter_selects
4017 && (c == CAR || c == K_KENTER || c == NL)))
4018 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004019 retval = TRUE;
4020
Bram Moolenaar47247282016-08-02 22:36:02 +02004021 /* CTRL-E means completion is Ended, go back to the typed text.
4022 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004023 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004024 {
4025 ins_compl_delete();
4026 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004027 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004028 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004029 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004030 retval = TRUE;
4031 }
4032
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004033 auto_format(FALSE, TRUE);
4034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004036 compl_started = FALSE;
4037 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004038 if (!shortmess(SHM_COMPLETIONMENU))
4039 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004040 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004041 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (edit_submode != NULL)
4043 {
4044 edit_submode = NULL;
4045 showmode();
4046 }
4047
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004048#ifdef FEAT_CMDWIN
4049 if (c == Ctrl_C && cmdwin_type != 0)
4050 /* Avoid the popup menu remains displayed when leaving the
4051 * command line window. */
4052 update_screen(0);
4053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#ifdef FEAT_CINDENT
4055 /*
4056 * Indent now if a key was typed that is in 'cinkeys'.
4057 */
4058 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4059 do_c_expr_indent();
4060#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004061 /* Trigger the CompleteDone event to give scripts a chance to act
4062 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004063 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004066 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4067 /* Trigger the CompleteDone event to give scripts a chance to act
4068 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004069 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 /* reset continue_* if we left expansion-mode, if we stay they'll be
4072 * (re)set properly in ins_complete() */
4073 if (!vim_is_ctrl_x_key(c))
4074 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 compl_cont_status = 0;
4076 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004078
4079 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080}
4081
4082/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004083 * Fix the redo buffer for the completion leader replacing some of the typed
4084 * text. This inserts backspaces and appends the changed text.
4085 * "ptr" is the known leader text or NUL.
4086 */
4087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004088ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004089{
4090 int len;
4091 char_u *p;
4092 char_u *ptr = ptr_arg;
4093
4094 if (ptr == NULL)
4095 {
4096 if (compl_leader != NULL)
4097 ptr = compl_leader;
4098 else
4099 return; /* nothing to do */
4100 }
4101 if (compl_orig_text != NULL)
4102 {
4103 p = compl_orig_text;
4104 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4105 ;
Bram Moolenaar62951b12011-09-21 18:23:05 +02004106 if (len > 0)
4107 len -= (*mb_head_off)(p, p + len);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004108 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004109 AppendCharToRedobuff(K_BS);
4110 }
4111 else
4112 len = 0;
4113 if (ptr != NULL)
4114 AppendToRedobuffLit(ptr + len, -1);
4115}
4116
4117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4119 * (depending on flag) starting from buf and looking for a non-scanned
4120 * buffer (other than curbuf). curbuf is special, if it is called with
4121 * buf=curbuf then it has to be the first call for a given flag/expansion.
4122 *
4123 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4124 */
4125 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 if (flag == 'w') /* just windows */
4131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (buf == curbuf) /* first call for this flag/expansion */
4133 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004134 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 && wp->w_buffer->b_scanned)
4136 ;
4137 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139 else
4140 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4141 * (unlisted buffers)
4142 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004143 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 && ((flag == 'U'
4145 ? buf->b_p_bl
4146 : (!buf->b_p_bl
4147 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004148 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 ;
4150 return buf;
4151}
4152
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004153#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004155 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004156 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004157 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004159expand_by_function(
4160 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4161 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004162{
Bram Moolenaar82139082011-09-14 16:52:09 +02004163 list_T *matchlist = NULL;
4164 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004165 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004166 char_u *funcname;
4167 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004168 win_T *curwin_save;
4169 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004170 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004171 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004172
Bram Moolenaare344bea2005-09-01 20:46:49 +00004173 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4174 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004175 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004176
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004177 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004178 args[0].v_type = VAR_NUMBER;
4179 args[0].vval.v_number = 0;
4180 args[1].v_type = VAR_STRING;
4181 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4182 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004183
Bram Moolenaare344bea2005-09-01 20:46:49 +00004184 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004185 curwin_save = curwin;
4186 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004187
4188 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004189 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004190 {
4191 switch (rettv.v_type)
4192 {
4193 case VAR_LIST:
4194 matchlist = rettv.vval.v_list;
4195 break;
4196 case VAR_DICT:
4197 matchdict = rettv.vval.v_dict;
4198 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004199 case VAR_SPECIAL:
4200 if (rettv.vval.v_number == VVAL_NONE)
4201 compl_opt_suppress_empty = TRUE;
4202 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004203 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004204 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004205 clear_tv(&rettv);
4206 break;
4207 }
4208 }
4209
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004210 if (curwin_save != curwin || curbuf_save != curbuf)
4211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004212 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004213 goto theend;
4214 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004215 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004216 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004217 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004219 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004220 goto theend;
4221 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004222
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004223 if (matchlist != NULL)
4224 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004225 else if (matchdict != NULL)
4226 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004227
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004228theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004229 // Restore State, it might have been changed.
4230 State = save_State;
4231
Bram Moolenaar82139082011-09-14 16:52:09 +02004232 if (matchdict != NULL)
4233 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004234 if (matchlist != NULL)
4235 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004236}
4237#endif /* FEAT_COMPL_FUNC */
4238
Bram Moolenaar39f05632006-03-19 22:15:26 +00004239#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004240/*
4241 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004242 */
4243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004245{
4246 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004247 int dir = compl_direction;
4248
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004249 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004250 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004251 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004252 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4253 /* if dir was BACKWARD then honor it just once */
4254 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004255 else if (did_emsg)
4256 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004257 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004258}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004259
4260/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004261 * Add completions from a dict.
4262 */
4263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004264ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004265{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004266 dictitem_T *di_refresh;
4267 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004268
4269 /* Check for optional "refresh" item. */
4270 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004271 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4272 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004273 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004274 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004275
4276 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4277 compl_opt_refresh_always = TRUE;
4278 }
4279
4280 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004281 di_words = dict_find(dict, (char_u *)"words", 5);
4282 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4283 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004284}
4285
4286/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004287 * Add a match to the list of matches from a typeval_T.
4288 * If the given string is already in the list of completions, then return
4289 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4290 * maybe because alloc() returns NULL, then FAIL is returned.
4291 */
4292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004293ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004294{
4295 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004296 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004297 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004298 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004299 char_u *(cptext[CPT_COUNT]);
4300
4301 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4302 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004303 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4304 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004305 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004306 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004307 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004308 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004309 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004310 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004311 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004312 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004313 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004314 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4315 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4316 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4317 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4318 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4319 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004320 }
4321 else
4322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004323 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004324 vim_memset(cptext, 0, sizeof(cptext));
4325 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004326 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004327 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004328 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004329}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004330#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004331
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332/*
4333 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004334 * The search starts at position "ini" in curbuf and in the direction
4335 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004336 * When "compl_started" is FALSE start at that position, otherwise continue
4337 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 * This may return before finding all the matches.
4339 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 */
4341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004342ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
4344 static pos_T first_match_pos;
4345 static pos_T last_match_pos;
4346 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 static int found_all = FALSE; /* Found all matches of a
4348 certain type. */
4349 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
Bram Moolenaar572cb562005-08-05 21:35:02 +00004351 pos_T *pos;
4352 char_u **matches;
4353 int save_p_scs;
4354 int save_p_ws;
4355 int save_p_ic;
4356 int i;
4357 int num_matches;
4358 int len;
4359 int found_new_match;
4360 int type = ctrl_x_mode;
4361 char_u *ptr;
4362 char_u *dict = NULL;
4363 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004364 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004368 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 ins_buf->b_scanned = 0;
4370 found_all = FALSE;
4371 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004373 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 last_match_pos = first_match_pos = *ini;
4375 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004376 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4377 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar4475b622017-05-01 20:46:52 +02004379 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004380 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004381
4382 /*
4383 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 for (;;)
4386 {
4387 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004388 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 * or if found_all says this entry is done. For ^X^L only use the
4392 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004393 if ((ctrl_x_mode == CTRL_X_NORMAL
4394 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
4397 found_all = FALSE;
4398 while (*e_cpt == ',' || *e_cpt == ' ')
4399 e_cpt++;
4400 if (*e_cpt == '.' && !curbuf->b_scanned)
4401 {
4402 ins_buf = curbuf;
4403 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004404 /* Move the cursor back one character so that ^N can match the
4405 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004406 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004407 {
4408 /* Move the cursor to after the last character in the
4409 * buffer, so that word at start of buffer is found
4410 * correctly. */
4411 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4412 first_match_pos.col =
4413 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 last_match_pos = first_match_pos;
4416 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004417
4418 /* Remember the first match so that the loop stops when we
4419 * wrap and come back there a second time. */
4420 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4423 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4424 {
4425 /* Scan a buffer, but not the current one. */
4426 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4427 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 first_match_pos.col = last_match_pos.col = 0;
4430 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4431 last_match_pos.lnum = 0;
4432 type = 0;
4433 }
4434 else /* unloaded buffer, scan like dictionary */
4435 {
4436 found_all = TRUE;
4437 if (ins_buf->b_fname == NULL)
4438 continue;
4439 type = CTRL_X_DICTIONARY;
4440 dict = ins_buf->b_fname;
4441 dict_f = DICT_EXACT;
4442 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004443 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 ins_buf->b_fname == NULL
4445 ? buf_spname(ins_buf)
4446 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004447 ? ins_buf->b_fname
4448 : ins_buf->b_sfname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004449 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451 else if (*e_cpt == NUL)
4452 break;
4453 else
4454 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004455 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 type = -1;
4457 else if (*e_cpt == 'k' || *e_cpt == 's')
4458 {
4459 if (*e_cpt == 'k')
4460 type = CTRL_X_DICTIONARY;
4461 else
4462 type = CTRL_X_THESAURUS;
4463 if (*++e_cpt != ',' && *e_cpt != NUL)
4464 {
4465 dict = e_cpt;
4466 dict_f = DICT_FIRST;
4467 }
4468 }
4469#ifdef FEAT_FIND_ID
4470 else if (*e_cpt == 'i')
4471 type = CTRL_X_PATH_PATTERNS;
4472 else if (*e_cpt == 'd')
4473 type = CTRL_X_PATH_DEFINES;
4474#endif
4475 else if (*e_cpt == ']' || *e_cpt == 't')
4476 {
4477 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004478 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004479 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481 else
4482 type = -1;
4483
4484 /* in any case e_cpt is advanced to the next entry */
4485 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4486
4487 found_all = TRUE;
4488 if (type == -1)
4489 continue;
4490 }
4491 }
4492
Bram Moolenaar4475b622017-05-01 20:46:52 +02004493 /* If complete() was called then compl_pattern has been reset. The
4494 * following won't work then, bail out. */
4495 if (compl_pattern == NULL)
4496 break;
4497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 switch (type)
4499 {
4500 case -1:
4501 break;
4502#ifdef FEAT_FIND_ID
4503 case CTRL_X_PATH_PATTERNS:
4504 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004505 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4510 (linenr_T)1, (linenr_T)MAXLNUM);
4511 break;
4512#endif
4513
4514 case CTRL_X_DICTIONARY:
4515 case CTRL_X_THESAURUS:
4516 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004517 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 : (type == CTRL_X_THESAURUS
4519 ? (*curbuf->b_p_tsr == NUL
4520 ? p_tsr
4521 : curbuf->b_p_tsr)
4522 : (*curbuf->b_p_dict == NUL
4523 ? p_dict
4524 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004525 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004526 dict != NULL ? dict_f
4527 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 dict = NULL;
4529 break;
4530
4531 case CTRL_X_TAGS:
4532 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4533 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004536 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 * of matches is found when compl_pattern is empty */
4538 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004539 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4540 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4542 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004543 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 p_ic = save_p_ic;
4546 break;
4547
4548 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4551 {
4552
4553 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004555 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 break;
4558
4559 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560 if (expand_cmdline(&compl_xp, compl_pattern,
4561 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004563 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 break;
4565
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004566#ifdef FEAT_COMPL_FUNC
4567 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004568 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004569 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004570 break;
4571#endif
4572
Bram Moolenaar488c6512005-08-11 20:09:58 +00004573 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004574#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004575 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004576 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004577 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004578 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004579#endif
4580 break;
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 default: /* normal ^P/^N and ^X^L */
4583 /*
4584 * If 'infercase' is set, don't use 'smartcase' here
4585 */
4586 save_p_scs = p_scs;
4587 if (ins_buf->b_p_inf)
4588 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589
Bram Moolenaar361aa502014-01-23 22:45:58 +01004590 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 * end but never from the middle, thus setting nowrapscan in this
4592 * buffers is a good idea, on the other hand, we always set
4593 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4594 save_p_ws = p_ws;
4595 if (ins_buf != curbuf)
4596 p_ws = FALSE;
4597 else if (*e_cpt == '.')
4598 p_ws = TRUE;
4599 for (;;)
4600 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004601 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004603 ++msg_silent; /* Don't want messages for wrapscan. */
4604
Bram Moolenaare4214502015-03-05 18:08:43 +01004605 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4606 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004607 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004608 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004611 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004613 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004614 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004615 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004616 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004617 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004618 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004620 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 first_match_pos = *pos;
4623 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004624 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004627 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 found_new_match = FAIL;
4629 if (found_new_match == FAIL)
4630 {
4631 if (ins_buf == curbuf)
4632 found_all = TRUE;
4633 break;
4634 }
4635
4636 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 && ini->lnum == pos->lnum
4639 && ini->col == pos->col)
4640 continue;
4641 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004642 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4647 continue;
4648 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4649 if (!p_paste)
4650 ptr = skipwhite(ptr);
4651 }
4652 len = (int)STRLEN(ptr);
4653 }
4654 else
4655 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 char_u *tmp_ptr = ptr;
4657
4658 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 /* Skip if already inside a word. */
4662 if (vim_iswordp(tmp_ptr))
4663 continue;
4664 /* Find start of next word. */
4665 tmp_ptr = find_word_start(tmp_ptr);
4666 }
4667 /* Find end of this word. */
4668 tmp_ptr = find_word_end(tmp_ptr);
4669 len = (int)(tmp_ptr - ptr);
4670
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004671 if ((compl_cont_status & CONT_ADDING)
4672 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
4674 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4675 {
4676 /* Try next line, if any. the new word will be
4677 * "join" as if the normal command "J" was used.
4678 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 * works -- Acevedo */
4681 STRNCPY(IObuff, ptr, len);
4682 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4683 tmp_ptr = ptr = skipwhite(ptr);
4684 /* Find start of next word. */
4685 tmp_ptr = find_word_start(tmp_ptr);
4686 /* Find end of next word. */
4687 tmp_ptr = find_word_end(tmp_ptr);
4688 if (tmp_ptr > ptr)
4689 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004690 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004692 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 IObuff[len++] = ' ';
4694 /* IObuf =~ "\k.* ", thus len >= 2 */
4695 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004696 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 || (vim_strchr(p_cpo, CPO_JOINSP)
4698 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004699 && (IObuff[len - 2] == '?'
4700 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 IObuff[len++] = ' ';
4702 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004703 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (tmp_ptr - ptr >= IOSIZE - len)
4705 tmp_ptr = ptr + IOSIZE - len - 1;
4706 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4707 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004708 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710 IObuff[len] = NUL;
4711 ptr = IObuff;
4712 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004713 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 continue;
4715 }
4716 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004717 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004718 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004719 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 found_new_match = OK;
4722 break;
4723 }
4724 }
4725 p_scs = save_p_scs;
4726 p_ws = save_p_ws;
4727 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004730 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004731 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 found_new_match = OK;
4733
4734 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004735 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4736 * match */
4737 if ((ctrl_x_mode != CTRL_X_NORMAL
4738 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004739 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004740 {
4741 if (got_int)
4742 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004743 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004744 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004745 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004746
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004747 if ((ctrl_x_mode != CTRL_X_NORMAL
4748 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004749 || compl_interrupted)
4750 break;
4751 compl_started = TRUE;
4752 }
4753 else
4754 {
4755 /* Mark a buffer scanned when it has been scanned completely */
4756 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4757 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004759 compl_started = FALSE;
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004762 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004764 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 && *e_cpt == NUL) /* Got to end of 'complete' */
4766 found_new_match = FAIL;
4767
4768 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004769 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4770 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 i = ins_compl_make_cyclic();
4772
Bram Moolenaar4475b622017-05-01 20:46:52 +02004773 if (compl_old_match != NULL)
4774 {
4775 /* If several matches were added (FORWARD) or the search failed and has
4776 * just been made cyclic then we have to move compl_curr_match to the
4777 * next or previous entry (if any) -- Acevedo */
4778 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4779 : compl_old_match->cp_prev;
4780 if (compl_curr_match == NULL)
4781 compl_curr_match = compl_old_match;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 return i;
4784}
4785
4786/* Delete the old text being completed. */
4787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004788ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004790 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 /*
4793 * In insert mode: Delete the typed part.
4794 * In replace mode: Put the old characters back, if any.
4795 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004796 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4797 if ((int)curwin->w_cursor.col > col)
4798 {
4799 if (stop_arrow() == FAIL)
4800 return;
4801 backspace_until_column(col);
4802 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004803
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004804 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4805 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004807 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004808 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809}
4810
Bram Moolenaar472e8592016-10-15 17:06:47 +02004811/*
4812 * Insert the new text being completed.
4813 * "in_compl_func" is TRUE when called from complete_check().
4814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004816ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004818 dict_T *dict;
4819
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004820 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004821 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4822 compl_used_match = FALSE;
4823 else
4824 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004825
4826 /* Set completed item. */
4827 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004828 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004829 if (dict != NULL)
4830 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004831 dict_add_string(dict, "word", compl_shown_match->cp_str);
4832 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4833 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4834 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4835 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4836 dict_add_string(dict, "user_data",
4837 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004838 }
4839 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004840 if (!in_compl_func)
4841 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842}
4843
4844/*
4845 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004846 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4847 * get more completions. If it is FALSE, then we just do nothing when there
4848 * are no more completions in a given direction. The latter case is used when
4849 * we are still in the middle of finding completions, to allow browsing
4850 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 * Return the total number of matches, or -1 if still unknown -- webb.
4852 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004853 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4854 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 *
4856 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004857 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4858 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 */
4860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004861ins_compl_next(
4862 int allow_get_expansion,
4863 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004864 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004865 int insert_match, /* Insert the newly selected match */
4866 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004869 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004870 compl_T *found_compl = NULL;
4871 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004872 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004873 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004875 /* When user complete function return -1 for findstart which is next
4876 * time of 'always', compl_shown_match become NULL. */
4877 if (compl_shown_match == NULL)
4878 return -1;
4879
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004880 if (compl_leader != NULL
4881 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004883 /* Set "compl_shown_match" to the actually shown match, it may differ
4884 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004885 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004886 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004887 && compl_shown_match->cp_next != NULL
4888 && compl_shown_match->cp_next != compl_first_match)
4889 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004890
4891 /* If we didn't find it searching forward, and compl_shows_dir is
4892 * backward, find the last match. */
4893 if (compl_shows_dir == BACKWARD
4894 && !ins_compl_equal(compl_shown_match,
4895 compl_leader, (int)STRLEN(compl_leader))
4896 && (compl_shown_match->cp_next == NULL
4897 || compl_shown_match->cp_next == compl_first_match))
4898 {
4899 while (!ins_compl_equal(compl_shown_match,
4900 compl_leader, (int)STRLEN(compl_leader))
4901 && compl_shown_match->cp_prev != NULL
4902 && compl_shown_match->cp_prev != compl_first_match)
4903 compl_shown_match = compl_shown_match->cp_prev;
4904 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004905 }
4906
4907 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004908 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 /* Delete old text to be replaced */
4910 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004911
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004912 /* When finding the longest common text we stick at the original text,
4913 * don't let CTRL-N or CTRL-P move to the first match. */
4914 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4915
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004916 /* When restarting the search don't insert the first match either. */
4917 if (compl_restarting)
4918 {
4919 advance = FALSE;
4920 compl_restarting = FALSE;
4921 }
4922
Bram Moolenaare3226be2005-12-18 22:10:00 +00004923 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4924 * around. */
4925 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004927 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004929 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004930 found_end = (compl_first_match != NULL
4931 && (compl_shown_match->cp_next == compl_first_match
4932 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004933 }
4934 else if (compl_shows_dir == BACKWARD
4935 && compl_shown_match->cp_prev != NULL)
4936 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004937 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004938 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004939 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004942 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004943 if (!allow_get_expansion)
4944 {
4945 if (advance)
4946 {
4947 if (compl_shows_dir == BACKWARD)
4948 compl_pending -= todo + 1;
4949 else
4950 compl_pending += todo + 1;
4951 }
4952 return -1;
4953 }
4954
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004955 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004956 {
4957 if (compl_shows_dir == BACKWARD)
4958 --compl_pending;
4959 else
4960 ++compl_pending;
4961 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004962
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004963 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004964 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004965
4966 /* handle any pending completions */
4967 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004968 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004969 {
4970 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4971 {
4972 compl_shown_match = compl_shown_match->cp_next;
4973 --compl_pending;
4974 }
4975 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4976 {
4977 compl_shown_match = compl_shown_match->cp_prev;
4978 ++compl_pending;
4979 }
4980 else
4981 break;
4982 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004983 found_end = FALSE;
4984 }
4985 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4986 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004987 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004988 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004989 ++todo;
4990 else
4991 /* Remember a matching item. */
4992 found_compl = compl_shown_match;
4993
4994 /* Stop at the end of the list when we found a usable match. */
4995 if (found_end)
4996 {
4997 if (found_compl != NULL)
4998 {
4999 compl_shown_match = found_compl;
5000 break;
5001 }
5002 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005006 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005007 if (compl_no_insert && !started)
5008 {
5009 ins_bytes(compl_orig_text + ins_compl_len());
5010 compl_used_match = FALSE;
5011 }
5012 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005013 {
5014 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005015 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005016 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005017 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005018 }
5019 else
5020 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022 if (!allow_get_expansion)
5023 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005024 /* may undisplay the popup menu first */
5025 ins_compl_upd_pum();
5026
Bram Moolenaarae654382019-01-17 21:09:05 +01005027 // Redraw before showing the popup menu to show the user what was
5028 // inserted.
5029 pum_call_update_screen();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005030
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005031 /* display the updated popup menu */
5032 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005033#ifdef FEAT_GUI
5034 if (gui.in_use)
5035 {
5036 /* Show the cursor after the match, not after the redrawn text. */
5037 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005038 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005039 }
5040#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /* Delete old text to be replaced, since we're still searching and
5043 * don't want to match ourselves! */
5044 ins_compl_delete();
5045 }
5046
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005047 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005048 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005049 if (compl_no_insert && !started)
5050 compl_enter_selects = TRUE;
5051 else
5052 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 /*
5055 * Show the file name for the match (if any)
5056 * Truncate the file name to avoid a wait for return.
5057 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005058 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005060 char *lead = _("match in file");
5061 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5062 char_u *s;
5063 char_u *e;
5064
5065 if (space > 0)
5066 {
5067 /* We need the tail that fits. With double-byte encoding going
5068 * back from the end is very slow, thus go from the start and keep
5069 * the text that fits in "space" between "s" and "e". */
5070 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5071 {
5072 space -= ptr2cells(e);
5073 while (space < 0)
5074 {
5075 space += ptr2cells(s);
5076 MB_PTR_ADV(s);
5077 }
5078 }
5079 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5080 s > compl_shown_match->cp_fname ? "<" : "", s);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005081 msg((char *)IObuff);
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005082 redraw_cmdline = FALSE; /* don't overwrite! */
5083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085
5086 return num_matches;
5087}
5088
5089/*
5090 * Call this while finding completions, to check whether the user has hit a key
5091 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005092 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005094 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005095 * "in_compl_func" is TRUE when called from complete_check(), don't set
5096 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 */
5098 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005099ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
5101 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005102 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005104 /* Don't check when reading keys from a script, :normal or feedkeys().
5105 * That would break the test scripts. But do check for keys when called
5106 * from complete_check(). */
5107 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 return;
5109
5110 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005111 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 return;
5113 count = 0;
5114
Bram Moolenaara260a972006-06-23 19:36:29 +00005115 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5116 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (c != NUL)
5119 {
5120 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5121 {
5122 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005123 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005124 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005125 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005127 else
5128 {
5129 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005130 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005131 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005132 if (c != K_IGNORE)
5133 {
5134 /* Don't interrupt completion when the character wasn't typed,
5135 * e.g., when doing @q to replay keys. */
5136 if (c != Ctrl_R && KeyTyped)
5137 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005138
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005139 vungetc(c);
5140 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005143 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005144 {
5145 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5146
5147 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005148 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005149 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005150}
5151
5152/*
5153 * Decide the direction of Insert mode complete from the key typed.
5154 * Returns BACKWARD or FORWARD.
5155 */
5156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005157ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005158{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005159 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005160 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005161 return BACKWARD;
5162 return FORWARD;
5163}
5164
5165/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005166 * Return TRUE for keys that are used for completion only when the popup menu
5167 * is visible.
5168 */
5169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005170ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005171{
5172 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005173 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5174 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005175}
5176
5177/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005178 * Decide the number of completions to move forward.
5179 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5180 */
5181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005182ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005183{
5184 int h;
5185
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005186 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005187 {
5188 h = pum_get_height();
5189 if (h > 3)
5190 h -= 2; /* keep some context */
5191 return h;
5192 }
5193 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194}
5195
5196/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005197 * Return TRUE if completion with "c" should insert the match, FALSE if only
5198 * to change the currently selected completion.
5199 */
5200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005201ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005202{
5203 switch (c)
5204 {
5205 case K_UP:
5206 case K_DOWN:
5207 case K_PAGEDOWN:
5208 case K_KPAGEDOWN:
5209 case K_S_DOWN:
5210 case K_PAGEUP:
5211 case K_KPAGEUP:
5212 case K_S_UP:
5213 return FALSE;
5214 }
5215 return TRUE;
5216}
5217
5218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 * Do Insert mode completion.
5220 * Called when character "c" was typed, which has a meaning for completion.
5221 * Returns OK if completion was done, FAIL if something failed (out of mem).
5222 */
5223 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005224ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 char_u *line;
5227 int startcol = 0; /* column where searched text starts */
5228 colnr_T curs_col; /* cursor column */
5229 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005230 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005231 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005232 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005233 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaare3226be2005-12-18 22:10:00 +00005235 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005236 insert_match = ins_compl_use_match(c);
5237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 /* First time we hit ^N or ^P (in a row, I mean) */
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 did_ai = FALSE;
5243#ifdef FEAT_SMARTINDENT
5244 did_si = FALSE;
5245 can_si = FALSE;
5246 can_si_back = FALSE;
5247#endif
5248 if (stop_arrow() == FAIL)
5249 return FAIL;
5250
5251 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005253 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005255 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 * "compl_startpos" to the cursor as a pattern to add a new word
5257 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005258 * "compl_startpos" is not in the same line as the cursor then fix it
5259 * (the line has been split because it was longer than 'tw'). if SOL
5260 * is set then skip the previous pattern, a word at the beginning of
5261 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005262 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5263 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
5265 /*
5266 * it is a continued search
5267 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005269 if (ctrl_x_mode == CTRL_X_NORMAL
5270 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5271 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 /* line (probably) wrapped, set compl_startpos to the
5276 * first non_blank in the line, if it is not a wordchar
5277 * include it to get a better pattern, but then we don't
5278 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005279 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 compl_startpos.col = compl_col;
5281 compl_startpos.lnum = curwin->w_cursor.lnum;
5282 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284 else
5285 {
5286 /* S_IPOS was set when we inserted a word that was at the
5287 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 * mode but first we need to redefine compl_startpos */
5289 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 compl_cont_status |= CONT_SOL;
5292 compl_startpos.col = (colnr_T)(skipwhite(
5293 line + compl_length
5294 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005299 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005300 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 compl_cont_status &= ~CONT_SOL;
5305 compl_length = (IOSIZE - MIN_SPACE);
5306 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5309 if (compl_length < 1)
5310 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005312 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 }
5317 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005323 if (ctrl_x_mode != CTRL_X_NORMAL)
5324 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_cont_status = 0;
5326 compl_cont_status |= CONT_N_ADDS;
5327 compl_startpos = curwin->w_cursor;
5328 startcol = (int)curs_col;
5329 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331
5332 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005333 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 compl_col += ++startcol;
5343 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_pattern = str_foldcase(line + compl_col,
5347 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_pattern = vim_strnsave(line + compl_col,
5350 compl_length);
5351 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return FAIL;
5353 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 char_u *prefix = (char_u *)"\\<";
5357
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005358 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005360 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 if (!vim_iswordp(line + compl_col)
5364 || (compl_col > 0
Bram Moolenaar13505972019-01-24 15:04:48 +01005365 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 STRCPY((char *)compl_pattern, prefix);
5368 (void)quote_meta(compl_pattern + STRLEN(prefix),
5369 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
Bram Moolenaar13505972019-01-24 15:04:48 +01005371 else if (--startcol < 0
5372 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5376 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 compl_col += curs_col;
5379 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
5381 else
5382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 /* Search the point of change class of multibyte character
5384 * or not a word single byte character backward. */
5385 if (has_mbyte)
5386 {
5387 int base_class;
5388 int head_off;
5389
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 startcol -= (*mb_head_off)(line, line + startcol);
5391 base_class = mb_get_class(line + startcol);
5392 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 head_off = (*mb_head_off)(line, line + startcol);
5395 if (base_class != mb_get_class(line + startcol
5396 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400 }
5401 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005402 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 compl_col += ++startcol;
5405 compl_length = (int)curs_col - startcol;
5406 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 /* Only match word with at least two chars -- webb
5409 * there's no need to call quote_meta,
5410 * alloc(7) is enough -- Acevedo
5411 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 compl_pattern = alloc(7);
5413 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 STRCPY((char *)compl_pattern, "\\<");
5416 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5417 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419 else
5420 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005422 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 STRCPY((char *)compl_pattern, "\\<");
5426 (void)quote_meta(compl_pattern + 2, line + compl_col,
5427 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
5429 }
5430 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005431 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005433 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 compl_length = (int)curs_col - (int)compl_col;
5435 if (compl_length < 0) /* cursor in indent: empty pattern */
5436 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 compl_pattern = str_foldcase(line + compl_col, compl_length,
5439 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5442 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 return FAIL;
5444 }
5445 else if (ctrl_x_mode == CTRL_X_FILES)
5446 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005447 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005448 if (startcol > 0)
5449 {
5450 char_u *p = line + startcol;
5451
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005452 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005453 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005454 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005455 if (p == line && vim_isfilec(PTR2CHAR(p)))
5456 startcol = 0;
5457 else
5458 startcol = (int)(p - line) + 1;
5459 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005460
Bram Moolenaar0300e462013-09-08 16:03:45 +02005461 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 compl_length = (int)curs_col - startcol;
5463 compl_pattern = addstar(line + compl_col, compl_length,
5464 EXPAND_FILES);
5465 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 return FAIL;
5467 }
5468 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5469 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 compl_pattern = vim_strnsave(line, curs_col);
5471 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005474 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005475 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5476 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005477 /* No completion possible, use an empty pattern to get a
5478 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005479 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005480 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005481 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5482 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005484 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005485 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005486#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005487 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005488 * Call user defined function 'completefunc' with "a:findstart"
5489 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005490 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005491 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005492 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005493 char_u *funcname;
5494 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005495 win_T *curwin_save;
5496 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005497 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005498
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005499 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005500 * string */
5501 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5502 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5503 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005505 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005506 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005507 /* restore did_ai, so that adding comment leader works */
5508 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005509 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005510 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005511
Bram Moolenaarffa96842018-06-12 22:05:14 +02005512 args[0].v_type = VAR_NUMBER;
5513 args[0].vval.v_number = 1;
5514 args[1].v_type = VAR_STRING;
5515 args[1].vval.v_string = (char_u *)"";
5516 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005517 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005518 curwin_save = curwin;
5519 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005520 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005521
5522 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005523 if (curwin_save != curwin || curbuf_save != curbuf)
5524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005525 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005526 return FAIL;
5527 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005528 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005529 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005530 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005532 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005533 return FAIL;
5534 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005535
Bram Moolenaar6110a002012-01-26 18:58:38 +01005536 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005537 * cancel the complete without an error.
5538 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005539 if (col == -2)
5540 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005541 if (col == -3)
5542 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005543 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005544 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005545 if (!shortmess(SHM_COMPLETIONMENU))
5546 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005547 return FAIL;
5548 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005549
Bram Moolenaar82139082011-09-14 16:52:09 +02005550 /*
5551 * Reset extended parameters of completion, when start new
5552 * completion.
5553 */
5554 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005555 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005556
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005557 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005558 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005559 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005560 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005561 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005562
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005563 /* Setup variables for completion. Need to obtain "line" again,
5564 * it may have become invalid. */
5565 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005566 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005567 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5568 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005569#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005570 return FAIL;
5571 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005572 else if (ctrl_x_mode == CTRL_X_SPELL)
5573 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005574#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005575 if (spell_bad_len > 0)
5576 compl_col = curs_col - spell_bad_len;
5577 else
5578 compl_col = spell_word_start(startcol);
5579 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005580 {
5581 compl_length = 0;
5582 compl_col = curs_col;
5583 }
5584 else
5585 {
5586 spell_expand_check_cap(compl_col);
5587 compl_length = (int)curs_col - compl_col;
5588 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005589 /* Need to obtain "line" again, it may have become invalid. */
5590 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005591 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5592 if (compl_pattern == NULL)
5593#endif
5594 return FAIL;
5595 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 else
5597 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005598 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005599 return FAIL;
5600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005602 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005605 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 /* Insert a new line, keep indentation but ignore 'comments' */
5608#ifdef FEAT_COMMENTS
5609 char_u *old = curbuf->b_p_com;
5610
5611 curbuf->b_p_com = (char_u *)"";
5612#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005613 compl_startpos.lnum = curwin->w_cursor.lnum;
5614 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 ins_eol('\r');
5616#ifdef FEAT_COMMENTS
5617 curbuf->b_p_com = old;
5618#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005619 compl_length = 0;
5620 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
5622 }
5623 else
5624 {
5625 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005626 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005629 if (compl_cont_status & CONT_LOCAL)
5630 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
5632 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5633
Bram Moolenaar62951b12011-09-21 18:23:05 +02005634 /* If any of the original typed text has been changed we need to fix
5635 * the redo buffer. */
5636 ins_compl_fixRedoBufForLeader(NULL);
5637
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005638 /* Always add completion for the original text. */
5639 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005640 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5641 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005642 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005644 VIM_CLEAR(compl_pattern);
5645 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 return FAIL;
5647 }
5648
5649 /* showmode might reset the internal line pointers, so it must
5650 * be called before line = ml_get(), or when this address is no
5651 * longer needed. -- Acevedo.
5652 */
5653 edit_submode_extra = (char_u *)_("-- Searching...");
5654 edit_submode_highl = HLF_COUNT;
5655 showmode();
5656 edit_submode_extra = NULL;
5657 out_flush();
5658 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005659 else if (insert_match && stop_arrow() == FAIL)
5660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005662 compl_shown_match = compl_curr_match;
5663 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005666 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005668 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005669 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005670 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005672 /* may undisplay the popup menu */
5673 ins_compl_upd_pum();
5674
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005675 if (n > 1) /* all matches have been found */
5676 compl_matches = n;
5677 compl_curr_match = compl_shown_match;
5678 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaard68071d2006-05-02 22:08:30 +00005680 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5681 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (got_int && !global_busy)
5683 {
5684 (void)vgetc();
5685 got_int = FALSE;
5686 }
5687
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005688 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005689 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005691 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5692 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5694 edit_submode_highl = HLF_E;
5695 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5696 * because we couldn't expand anything at first place, but if we used
5697 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5698 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005699 if ( compl_length > 1
5700 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005701 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5703 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005704 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706
Bram Moolenaar572cb562005-08-05 21:35:02 +00005707 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005708 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005710 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 if (edit_submode_extra == NULL)
5713 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005714 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
5716 edit_submode_extra = (char_u *)_("Back at original");
5717 edit_submode_highl = HLF_W;
5718 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005719 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 edit_submode_extra = (char_u *)_("Word from other line");
5722 edit_submode_highl = HLF_COUNT;
5723 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005724 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 edit_submode_extra = (char_u *)_("The only match");
5727 edit_submode_highl = HLF_COUNT;
5728 }
5729 else
5730 {
5731 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005732 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005734 int number = 0;
5735 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005737 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 /* search backwards for the first valid (!= -1) number.
5740 * This should normally succeed already at the first loop
5741 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005742 for (match = compl_curr_match->cp_prev; match != NULL
5743 && match != compl_first_match;
5744 match = match->cp_prev)
5745 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005747 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 break;
5749 }
5750 if (match != NULL)
5751 /* go up and assign all numbers which are not assigned
5752 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005753 for (match = match->cp_next;
5754 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005755 match = match->cp_next)
5756 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758 else /* BACKWARD */
5759 {
5760 /* search forwards (upwards) for the first valid (!= -1)
5761 * number. This should normally succeed already at the
5762 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005763 for (match = compl_curr_match->cp_next; match != NULL
5764 && match != compl_first_match;
5765 match = match->cp_next)
5766 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005768 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 break;
5770 }
5771 if (match != NULL)
5772 /* go down and assign all numbers which are not
5773 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005774 for (match = match->cp_prev; match
5775 && match->cp_number == -1;
5776 match = match->cp_prev)
5777 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779 }
5780
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005781 /* The match should always have a sequence number now, this is
5782 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005783 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005785 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5786 * Translations may need more than twice that. */
5787 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005789 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005790 vim_snprintf((char *)match_ref, sizeof(match_ref),
5791 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005792 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005794 vim_snprintf((char *)match_ref, sizeof(match_ref),
5795 _("match %d"),
5796 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 edit_submode_extra = match_ref;
5798 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005799 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 curs_columns(FALSE);
5801 }
5802 }
5803 }
5804
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005805 // Show a message about what (completion) mode we're in.
5806 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005808 showmode();
5809 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005810 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005811 if (edit_submode_extra != NULL)
5812 {
5813 if (!p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005814 msg_attr((char *)edit_submode_extra,
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005815 edit_submode_highl < HLF_COUNT
5816 ? HL_ATTR(edit_submode_highl) : 0);
5817 }
5818 else
5819 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822
Bram Moolenaard68071d2006-05-02 22:08:30 +00005823 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005824 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005825 show_pum(save_w_wrow, save_w_leftcol);
5826
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005827 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005828 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 return OK;
5831}
5832
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005833 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005834show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005835{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005836 /* RedrawingDisabled may be set when invoked through complete(). */
5837 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005838
Bram Moolenaarcb036422017-03-01 12:29:10 +01005839 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005840
Bram Moolenaarcb036422017-03-01 12:29:10 +01005841 /* If the cursor moved or the display scrolled we need to remove the pum
5842 * first. */
5843 setcursor();
5844 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5845 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005846
Bram Moolenaarcb036422017-03-01 12:29:10 +01005847 ins_compl_show_pum();
5848 setcursor();
5849 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005850}
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852/*
5853 * Looks in the first "len" chars. of "src" for search-metachars.
5854 * If dest is not NULL the chars. are copied there quoting (with
5855 * a backslash) the metachars, and dest would be NUL terminated.
5856 * Returns the length (needed) of dest
5857 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005858 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005859quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005861 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005863 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
5865 switch (*src)
5866 {
5867 case '.':
5868 case '*':
5869 case '[':
5870 if (ctrl_x_mode == CTRL_X_DICTIONARY
5871 || ctrl_x_mode == CTRL_X_THESAURUS)
5872 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005873 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 case '~':
5875 if (!p_magic) /* quote these only if magic is set */
5876 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005877 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 case '\\':
5879 if (ctrl_x_mode == CTRL_X_DICTIONARY
5880 || ctrl_x_mode == CTRL_X_THESAURUS)
5881 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005882 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 case '^': /* currently it's not needed. */
5884 case '$':
5885 m++;
5886 if (dest != NULL)
5887 *dest++ = '\\';
5888 break;
5889 }
5890 if (dest != NULL)
5891 *dest++ = *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 /* Copy remaining bytes of a multibyte character. */
5893 if (has_mbyte)
5894 {
5895 int i, mb_len;
5896
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005897 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 if (mb_len > 0 && len >= mb_len)
5899 for (i = 0; i < mb_len; ++i)
5900 {
5901 --len;
5902 ++src;
5903 if (dest != NULL)
5904 *dest++ = *src;
5905 }
5906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 if (dest != NULL)
5909 *dest = NUL;
5910
5911 return m;
5912}
5913#endif /* FEAT_INS_EXPAND */
5914
5915/*
5916 * Next character is interpreted literally.
5917 * A one, two or three digit decimal number is interpreted as its byte value.
5918 * If one or two digits are entered, the next character is given to vungetc().
5919 * For Unicode a character > 255 may be returned.
5920 */
5921 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005922get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 int cc;
5925 int nc;
5926 int i;
5927 int hex = FALSE;
5928 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 if (got_int)
5932 return Ctrl_C;
5933
5934#ifdef FEAT_GUI
5935 /*
5936 * In GUI there is no point inserting the internal code for a special key.
5937 * It is more useful to insert the string "<KEY>" instead. This would
5938 * probably be useful in a text window too, but it would not be
5939 * vi-compatible (maybe there should be an option for it?) -- webb
5940 */
5941 if (gui.in_use)
5942 ++allow_keys;
5943#endif
5944#ifdef USE_ON_FLY_SCROLL
5945 dont_scroll = TRUE; /* disallow scrolling here */
5946#endif
5947 ++no_mapping; /* don't map the next key hits */
5948 cc = 0;
5949 i = 0;
5950 for (;;)
5951 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005952 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01005954 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 add_to_showcmd(nc);
5956#endif
5957 if (nc == 'x' || nc == 'X')
5958 hex = TRUE;
5959 else if (nc == 'o' || nc == 'O')
5960 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 else if (nc == 'u' || nc == 'U')
5962 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 else
5964 {
Bram Moolenaar13505972019-01-24 15:04:48 +01005965 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {
5967 if (!vim_isxdigit(nc))
5968 break;
5969 cc = cc * 16 + hex2nr(nc);
5970 }
5971 else if (octal)
5972 {
5973 if (nc < '0' || nc > '7')
5974 break;
5975 cc = cc * 8 + nc - '0';
5976 }
5977 else
5978 {
5979 if (!VIM_ISDIGIT(nc))
5980 break;
5981 cc = cc * 10 + nc - '0';
5982 }
5983
5984 ++i;
5985 }
5986
Bram Moolenaar13505972019-01-24 15:04:48 +01005987 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 cc = 255; /* limit range to 0-255 */
5989 nc = 0;
5990
5991 if (hex) /* hex: up to two chars */
5992 {
5993 if (i >= 2)
5994 break;
5995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 else if (unicode) /* Unicode: up to four or eight chars */
5997 {
5998 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5999 break;
6000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 else if (i >= 3) /* decimal or octal: up to three chars */
6002 break;
6003 }
6004 if (i == 0) /* no number entered */
6005 {
6006 if (nc == K_ZERO) /* NUL is stored as NL */
6007 {
6008 cc = '\n';
6009 nc = 0;
6010 }
6011 else
6012 {
6013 cc = nc;
6014 nc = 0;
6015 }
6016 }
6017
6018 if (cc == 0) /* NUL is stored as NL */
6019 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006020 if (enc_dbcs && (cc & 0xff) == 0)
6021 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6022 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023
6024 --no_mapping;
6025#ifdef FEAT_GUI
6026 if (gui.in_use)
6027 --allow_keys;
6028#endif
6029 if (nc)
6030 vungetc(nc);
6031 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6032 return cc;
6033}
6034
6035/*
6036 * Insert character, taking care of special keys and mod_mask
6037 */
6038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039insert_special(
6040 int c,
6041 int allow_modmask,
6042 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 char_u *p;
6045 int len;
6046
6047 /*
6048 * Special function key, translate into "<Key>". Up to the last '>' is
6049 * inserted with ins_str(), so as not to replace characters in replace
6050 * mode.
6051 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6052 * unless 'allow_modmask' is TRUE.
6053 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006054#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 /* Command-key never produces a normal key */
6056 if (mod_mask & MOD_MASK_CMD)
6057 allow_modmask = TRUE;
6058#endif
6059 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6060 {
6061 p = get_special_key_name(c, mod_mask);
6062 len = (int)STRLEN(p);
6063 c = p[len - 1];
6064 if (len > 2)
6065 {
6066 if (stop_arrow() == FAIL)
6067 return;
6068 p[len - 1] = NUL;
6069 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006070 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 ctrlv = FALSE;
6072 }
6073 }
6074 if (stop_arrow() == OK)
6075 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6076}
6077
6078/*
6079 * Special characters in this context are those that need processing other
6080 * than the simple insertion that can be performed here. This includes ESC
6081 * which terminates the insert, and CR/NL which need special processing to
6082 * open up a new line. This routine tries to optimize insertions performed by
6083 * the "redo", "undo" or "put" commands, so it needs to know when it should
6084 * stop and defer processing to the "normal" mechanism.
6085 * '0' and '^' are special, because they can be followed by CTRL-D.
6086 */
6087#ifdef EBCDIC
6088# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6089#else
6090# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6091#endif
6092
Bram Moolenaar13505972019-01-24 15:04:48 +01006093#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006095/*
6096 * "flags": INSCHAR_FORMAT - force formatting
6097 * INSCHAR_CTRLV - char typed just after CTRL-V
6098 * INSCHAR_NO_FEX - don't use 'formatexpr'
6099 *
6100 * NOTE: passes the flags value straight through to internal_format() which,
6101 * beside INSCHAR_FORMAT (above), is also looking for these:
6102 * INSCHAR_DO_COM - format comments
6103 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006106insertchar(
6107 int c, /* character to insert or NUL */
6108 int flags, /* INSCHAR_FORMAT, etc. */
6109 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 int textwidth;
6112#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006116 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006118 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 /*
6122 * Try to break the line in two or more pieces when:
6123 * - Always do this if we have been called to do formatting only.
6124 * - Always do this when 'formatoptions' has the 'a' flag and the line
6125 * ends in white space.
6126 * - Otherwise:
6127 * - Don't do this if inserting a blank
6128 * - Don't do this if an existing character is being replaced, unless
6129 * we're in VREPLACE mode.
6130 * - Do this if the cursor is not on the line where insert started
6131 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6132 * before the insert.
6133 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6134 * before 'textwidth'
6135 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006136 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006137 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006138 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 && *ml_get_cursor() != NUL)
6142 && (curwin->w_cursor.lnum != Insstart.lnum
6143 || ((!has_format_option(FO_INS_LONG)
6144 || Insstart_textlen <= (colnr_T)textwidth)
6145 && (!fo_ins_blank
6146 || Insstart_blank_vcol <= (colnr_T)textwidth
6147 ))))))
6148 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006149 /* Format with 'formatexpr' when it's set. Use internal formatting
6150 * when 'formatexpr' isn't set or it returns non-zero. */
6151#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006152 int do_internal = TRUE;
6153 colnr_T virtcol = get_nolist_virtcol()
6154 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006155
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006156 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6157 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006158 {
6159 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6160 /* It may be required to save for undo again, e.g. when setline()
6161 * was called. */
6162 ins_need_undo = TRUE;
6163 }
6164 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006166 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (c == NUL) /* only formatting was wanted */
6170 return;
6171
6172#ifdef FEAT_COMMENTS
6173 /* Check whether this character should end a comment. */
6174 if (did_ai && (int)c == end_comment_pending)
6175 {
6176 char_u *line;
6177 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6178 int middle_len, end_len;
6179 int i;
6180
6181 /*
6182 * Need to remove existing (middle) comment leader and insert end
6183 * comment leader. First, check what comment leader we can find.
6184 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006185 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6187 {
6188 /* Skip middle-comment string */
6189 while (*p && p[-1] != ':') /* find end of middle flags */
6190 ++p;
6191 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6192 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006193 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 --middle_len;
6195
6196 /* Find the end-comment string */
6197 while (*p && p[-1] != ':') /* find end of end flags */
6198 ++p;
6199 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6200
6201 /* Skip white space before the cursor */
6202 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006203 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 ;
6205 i++;
6206
6207 /* Skip to before the middle leader */
6208 i -= middle_len;
6209
6210 /* Check some expected things before we go on */
6211 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6212 {
6213 /* Backspace over all the stuff we want to replace */
6214 backspace_until_column(i);
6215
6216 /*
6217 * Insert the end-comment string, except for the last
6218 * character, which will get inserted as normal later.
6219 */
6220 ins_bytes_len(lead_end, end_len - 1);
6221 }
6222 }
6223 }
6224 end_comment_pending = NUL;
6225#endif
6226
6227 did_ai = FALSE;
6228#ifdef FEAT_SMARTINDENT
6229 did_si = FALSE;
6230 can_si = FALSE;
6231 can_si_back = FALSE;
6232#endif
6233
6234 /*
6235 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6236 * This speeds up normal text input considerably.
6237 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6238 * need to re-indent at a ':', or any other character (but not what
6239 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006240 * Don't do this when there an InsertCharPre autocommand is defined,
6241 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006242 * Do the check for InsertCharPre before the call to vpeekc() because the
6243 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 */
6245#ifdef USE_ON_FLY_SCROLL
6246 dont_scroll = FALSE; /* allow scrolling here */
6247#endif
6248
6249 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02006251 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 && vpeekc() != NUL
6253 && !(State & REPLACE_FLAG)
6254#ifdef FEAT_CINDENT
6255 && !cindent_on()
6256#endif
6257#ifdef FEAT_RIGHTLEFT
6258 && !p_ri
6259#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006260 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
6262#define INPUT_BUFLEN 100
6263 char_u buf[INPUT_BUFLEN + 1];
6264 int i;
6265 colnr_T virtcol = 0;
6266
6267 buf[0] = c;
6268 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006269 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 virtcol = get_nolist_virtcol();
6271 /*
6272 * Stop the string when:
6273 * - no more chars available
6274 * - finding a special character (command key)
6275 * - buffer is full
6276 * - running into the 'textwidth' boundary
6277 * - need to check for abbreviation: A non-word char after a word-char
6278 */
6279 while ( (c = vpeekc()) != NUL
6280 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006283# ifdef FEAT_FKMAP
6284 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 && (textwidth == 0
6287 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6288 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6289 {
6290#ifdef FEAT_RIGHTLEFT
6291 c = vgetc();
6292 if (p_hkmap && KeyTyped)
6293 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 buf[i++] = c;
6295#else
6296 buf[i++] = vgetc();
6297#endif
6298 }
6299
6300#ifdef FEAT_DIGRAPHS
6301 do_digraph(-1); /* clear digraphs */
6302 do_digraph(buf[i-1]); /* may be the start of a digraph */
6303#endif
6304 buf[i] = NUL;
6305 ins_str(buf);
6306 if (flags & INSCHAR_CTRLV)
6307 {
6308 redo_literal(*buf);
6309 i = 1;
6310 }
6311 else
6312 i = 0;
6313 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006314 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316 else
6317 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006318 int cc;
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6321 {
6322 char_u buf[MB_MAXBYTES + 1];
6323
6324 (*mb_char2bytes)(c, buf);
6325 buf[cc] = NUL;
6326 ins_char_bytes(buf, cc);
6327 AppendCharToRedobuff(c);
6328 }
6329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
6331 ins_char(c);
6332 if (flags & INSCHAR_CTRLV)
6333 redo_literal(c);
6334 else
6335 AppendCharToRedobuff(c);
6336 }
6337 }
6338}
6339
6340/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006341 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006342 *
6343 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6344 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345 */
6346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006347internal_format(
6348 int textwidth,
6349 int second_indent,
6350 int flags,
6351 int format_only,
6352 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006353{
6354 int cc;
6355 int save_char = NUL;
6356 int haveto_redraw = FALSE;
6357 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359 int fo_white_par = has_format_option(FO_WHITE_PAR);
6360 int first_line = TRUE;
6361#ifdef FEAT_COMMENTS
6362 colnr_T leader_len;
6363 int no_leader = FALSE;
6364 int do_comments = (flags & INSCHAR_DO_COM);
6365#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006366#ifdef FEAT_LINEBREAK
6367 int has_lbr = curwin->w_p_lbr;
6368
6369 /* make sure win_lbr_chartabsize() counts correctly */
6370 curwin->w_p_lbr = FALSE;
6371#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006372
6373 /*
6374 * When 'ai' is off we don't want a space under the cursor to be
6375 * deleted. Replace it with an 'x' temporarily.
6376 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006377 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 {
6379 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006380 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006381 {
6382 save_char = cc;
6383 pchar_cursor('x');
6384 }
6385 }
6386
6387 /*
6388 * Repeat breaking lines, until the current line is not too long.
6389 */
6390 while (!got_int)
6391 {
6392 int startcol; /* Cursor column at entry */
6393 int wantcol; /* column at textwidth border */
6394 int foundcol; /* column for start of spaces */
6395 int end_foundcol = 0; /* column for start of word */
6396 colnr_T len;
6397 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006398 int orig_col = 0;
6399 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006401 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006402 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006403
Bram Moolenaar97b98102009-11-17 16:41:01 +00006404 virtcol = get_nolist_virtcol()
6405 + char2cells(c != NUL ? c : gchar_cursor());
6406 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006407 break;
6408
6409#ifdef FEAT_COMMENTS
6410 if (no_leader)
6411 do_comments = FALSE;
6412 else if (!(flags & INSCHAR_FORMAT)
6413 && has_format_option(FO_WRAP_COMS))
6414 do_comments = TRUE;
6415
6416 /* Don't break until after the comment leader */
6417 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006418 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 else
6420 leader_len = 0;
6421
6422 /* If the line doesn't start with a comment leader, then don't
6423 * start one in a following broken line. Avoids that a %word
6424 * moved to the start of the next line causes all following lines
6425 * to start with %. */
6426 if (leader_len == 0)
6427 no_leader = TRUE;
6428#endif
6429 if (!(flags & INSCHAR_FORMAT)
6430#ifdef FEAT_COMMENTS
6431 && leader_len == 0
6432#endif
6433 && !has_format_option(FO_WRAP))
6434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006435 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 if ((startcol = curwin->w_cursor.col) == 0)
6437 break;
6438
6439 /* find column of textwidth border */
6440 coladvance((colnr_T)textwidth);
6441 wantcol = curwin->w_cursor.col;
6442
Bram Moolenaar97b98102009-11-17 16:41:01 +00006443 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 foundcol = 0;
6445
6446 /*
6447 * Find position to break at.
6448 * Stop at first entered white when 'formatoptions' has 'v'
6449 */
6450 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006451 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 || curwin->w_cursor.lnum != Insstart.lnum
6453 || curwin->w_cursor.col >= Insstart.col)
6454 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006455 if (curwin->w_cursor.col == startcol && c != NUL)
6456 cc = c;
6457 else
6458 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006459 if (WHITECHAR(cc))
6460 {
6461 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006462 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006464 // find start of sequence of blanks
6465 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6467 {
6468 dec_cursor();
6469 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006470
6471 // Increment count of how many whitespace chars in this
6472 // group; we only need to know if it's more than one.
6473 if (wcc < 2)
6474 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475 }
6476 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6477 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006478
6479 // Don't break after a period when 'formatoptions' has 'p' and
6480 // there are less than two spaces.
6481 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6482 continue;
6483
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484#ifdef FEAT_COMMENTS
6485 /* Don't break until after the comment leader */
6486 if (curwin->w_cursor.col < leader_len)
6487 break;
6488#endif
6489 if (has_format_option(FO_ONE_LETTER))
6490 {
6491 /* do not break after one-letter words */
6492 if (curwin->w_cursor.col == 0)
6493 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006494#ifdef FEAT_COMMENTS
6495 /* do not break "#a b" when 'tw' is 2 */
6496 if (curwin->w_cursor.col <= leader_len)
6497 break;
6498#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499 col = curwin->w_cursor.col;
6500 dec_cursor();
6501 cc = gchar_cursor();
6502
6503 if (WHITECHAR(cc))
6504 continue; /* one-letter, continue */
6505 curwin->w_cursor.col = col;
6506 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006507
6508 inc_cursor();
6509
6510 end_foundcol = end_col + 1;
6511 foundcol = curwin->w_cursor.col;
6512 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513 break;
6514 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006515 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006516 {
6517 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006518 if (curwin->w_cursor.col != startcol)
6519 {
6520#ifdef FEAT_COMMENTS
6521 /* Don't break until after the comment leader */
6522 if (curwin->w_cursor.col < leader_len)
6523 break;
6524#endif
6525 col = curwin->w_cursor.col;
6526 inc_cursor();
6527 /* Don't change end_foundcol if already set. */
6528 if (foundcol != curwin->w_cursor.col)
6529 {
6530 foundcol = curwin->w_cursor.col;
6531 end_foundcol = foundcol;
6532 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6533 break;
6534 }
6535 curwin->w_cursor.col = col;
6536 }
6537
6538 if (curwin->w_cursor.col == 0)
6539 break;
6540
6541 col = curwin->w_cursor.col;
6542
6543 dec_cursor();
6544 cc = gchar_cursor();
6545
6546 if (WHITECHAR(cc))
6547 continue; /* break with space */
6548#ifdef FEAT_COMMENTS
6549 /* Don't break until after the comment leader */
6550 if (curwin->w_cursor.col < leader_len)
6551 break;
6552#endif
6553
6554 curwin->w_cursor.col = col;
6555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006556 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006558 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6559 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561 if (curwin->w_cursor.col == 0)
6562 break;
6563 dec_cursor();
6564 }
6565
6566 if (foundcol == 0) /* no spaces, cannot break line */
6567 {
6568 curwin->w_cursor.col = startcol;
6569 break;
6570 }
6571
6572 /* Going to break the line, remove any "$" now. */
6573 undisplay_dollar();
6574
6575 /*
6576 * Offset between cursor position and line break is used by replace
6577 * stack functions. VREPLACE does not use this, and backspaces
6578 * over the text instead.
6579 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580 if (State & VREPLACE_FLAG)
6581 orig_col = startcol; /* Will start backspacing from here */
6582 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006583 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006584
6585 /*
6586 * adjust startcol for spaces that will be deleted and
6587 * characters that will remain on top line
6588 */
6589 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006590 while ((cc = gchar_cursor(), WHITECHAR(cc))
6591 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 inc_cursor();
6593 startcol -= curwin->w_cursor.col;
6594 if (startcol < 0)
6595 startcol = 0;
6596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006597 if (State & VREPLACE_FLAG)
6598 {
6599 /*
6600 * In VREPLACE mode, we will backspace over the text to be
6601 * wrapped, so save a copy now to put on the next line.
6602 */
6603 saved_text = vim_strsave(ml_get_cursor());
6604 curwin->w_cursor.col = orig_col;
6605 if (saved_text == NULL)
6606 break; /* Can't do it, out of memory */
6607 saved_text[startcol] = NUL;
6608
6609 /* Backspace over characters that will move to the next line */
6610 if (!fo_white_par)
6611 backspace_until_column(foundcol);
6612 }
6613 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006614 {
6615 /* put cursor after pos. to break line */
6616 if (!fo_white_par)
6617 curwin->w_cursor.col = foundcol;
6618 }
6619
6620 /*
6621 * Split the line just before the margin.
6622 * Only insert/delete lines, but don't really redraw the window.
6623 */
6624 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6625 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6626#ifdef FEAT_COMMENTS
6627 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006628 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006630 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6631 if (!(flags & INSCHAR_COM_LIST))
6632 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006633
6634 replace_offset = 0;
6635 if (first_line)
6636 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006637 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006638 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006639 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006640 * This section is for auto-wrap of numeric lists. When not
6641 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6642 * flag will be set and open_line() will handle it (as seen
6643 * above). The code here (and in get_number_indent()) will
6644 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006645 */
6646 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006647 second_indent =
6648 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006649 if (second_indent >= 0)
6650 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006651 if (State & VREPLACE_FLAG)
6652 change_indent(INDENT_SET, second_indent,
6653 FALSE, NUL, TRUE);
6654 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006655#ifdef FEAT_COMMENTS
6656 if (leader_len > 0 && second_indent - leader_len > 0)
6657 {
6658 int i;
6659 int padding = second_indent - leader_len;
6660
6661 /* We started at the first_line of a numbered list
6662 * that has a comment. the open_line() function has
6663 * inserted the proper comment leader and positioned
6664 * the cursor at the end of the split line. Now we
6665 * add the additional whitespace needed after the
6666 * comment leader for the numbered list. */
6667 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006668 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006669 }
6670 else
6671 {
6672#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006673 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006674#ifdef FEAT_COMMENTS
6675 }
6676#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006677 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006678 }
6679 first_line = FALSE;
6680 }
6681
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006682 if (State & VREPLACE_FLAG)
6683 {
6684 /*
6685 * In VREPLACE mode we have backspaced over the text to be
6686 * moved, now we re-insert it into the new line.
6687 */
6688 ins_bytes(saved_text);
6689 vim_free(saved_text);
6690 }
6691 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006692 {
6693 /*
6694 * Check if cursor is not past the NUL off the line, cindent
6695 * may have added or removed indent.
6696 */
6697 curwin->w_cursor.col += startcol;
6698 len = (colnr_T)STRLEN(ml_get_curline());
6699 if (curwin->w_cursor.col > len)
6700 curwin->w_cursor.col = len;
6701 }
6702
6703 haveto_redraw = TRUE;
6704#ifdef FEAT_CINDENT
6705 can_cindent = TRUE;
6706#endif
6707 /* moved the cursor, don't autoindent or cindent now */
6708 did_ai = FALSE;
6709#ifdef FEAT_SMARTINDENT
6710 did_si = FALSE;
6711 can_si = FALSE;
6712 can_si_back = FALSE;
6713#endif
6714 line_breakcheck();
6715 }
6716
6717 if (save_char != NUL) /* put back space after cursor */
6718 pchar_cursor(save_char);
6719
Bram Moolenaar0026d472014-09-09 16:32:39 +02006720#ifdef FEAT_LINEBREAK
6721 curwin->w_p_lbr = has_lbr;
6722#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006723 if (!format_only && haveto_redraw)
6724 {
6725 update_topline();
6726 redraw_curbuf_later(VALID);
6727 }
6728}
6729
6730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 * Called after inserting or deleting text: When 'formatoptions' includes the
6732 * 'a' flag format from the current line until the end of the paragraph.
6733 * Keep the cursor at the same position relative to the text.
6734 * The caller must have saved the cursor line for undo, following ones will be
6735 * saved here.
6736 */
6737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738auto_format(
6739 int trailblank, /* when TRUE also format with trailing blank */
6740 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 pos_T pos;
6743 colnr_T len;
6744 char_u *old;
6745 char_u *new, *pnew;
6746 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006747 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 if (!has_format_option(FO_AUTO))
6750 return;
6751
6752 pos = curwin->w_cursor;
6753 old = ml_get_curline();
6754
6755 /* may remove added space */
6756 check_auto_format(FALSE);
6757
6758 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6759 * user might insert normal text next. Also skip formatting when "1" is
6760 * in 'formatoptions' and there is a single character before the cursor.
6761 * Otherwise the line would be broken and when typing another non-white
6762 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006763 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (*old != NUL && !trailblank && wasatend)
6765 {
6766 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006767 cc = gchar_cursor();
6768 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6769 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006771 cc = gchar_cursor();
6772 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
6774 curwin->w_cursor = pos;
6775 return;
6776 }
6777 curwin->w_cursor = pos;
6778 }
6779
6780#ifdef FEAT_COMMENTS
6781 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6782 * comments. */
6783 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006784 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 return;
6786#endif
6787
6788 /*
6789 * May start formatting in a previous line, so that after "x" a word is
6790 * moved to the previous line if it fits there now. Only when this is not
6791 * the start of a paragraph.
6792 */
6793 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6794 {
6795 --curwin->w_cursor.lnum;
6796 if (u_save_cursor() == FAIL)
6797 return;
6798 }
6799
6800 /*
6801 * Do the formatting and restore the cursor position. "saved_cursor" will
6802 * be adjusted for the text formatting.
6803 */
6804 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006805 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 curwin->w_cursor = saved_cursor;
6807 saved_cursor.lnum = 0;
6808
6809 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6810 {
6811 /* "cannot happen" */
6812 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6813 coladvance((colnr_T)MAXCOL);
6814 }
6815 else
6816 check_cursor_col();
6817
6818 /* Insert mode: If the cursor is now after the end of the line while it
6819 * previously wasn't, the line was broken. Because of the rule above we
6820 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6821 * formatted. */
6822 if (!wasatend && has_format_option(FO_WHITE_PAR))
6823 {
6824 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006825 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (curwin->w_cursor.col == len)
6827 {
6828 pnew = vim_strnsave(new, len + 2);
6829 pnew[len] = ' ';
6830 pnew[len + 1] = NUL;
6831 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6832 /* remove the space later */
6833 did_add_space = TRUE;
6834 }
6835 else
6836 /* may remove added space */
6837 check_auto_format(FALSE);
6838 }
6839
6840 check_cursor();
6841}
6842
6843/*
6844 * When an extra space was added to continue a paragraph for auto-formatting,
6845 * delete it now. The space must be under the cursor, just after the insert
6846 * position.
6847 */
6848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849check_auto_format(
6850 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006853 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 if (did_add_space)
6856 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006857 cc = gchar_cursor();
6858 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 /* Somehow the space was removed already. */
6860 did_add_space = FALSE;
6861 else
6862 {
6863 if (!end_insert)
6864 {
6865 inc_cursor();
6866 c = gchar_cursor();
6867 dec_cursor();
6868 }
6869 if (c != NUL)
6870 {
6871 /* The space is no longer at the end of the line, delete it. */
6872 del_char(FALSE);
6873 did_add_space = FALSE;
6874 }
6875 }
6876 }
6877}
6878
6879/*
6880 * Find out textwidth to be used for formatting:
6881 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006882 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 * if invalid value, use 0.
6884 * Set default to window width (maximum 79) for "gq" operator.
6885 */
6886 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006887comp_textwidth(
6888 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 int textwidth;
6891
6892 textwidth = curbuf->b_p_tw;
6893 if (textwidth == 0 && curbuf->b_p_wm)
6894 {
6895 /* The width is the window width minus 'wrapmargin' minus all the
6896 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006897 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898#ifdef FEAT_CMDWIN
6899 if (cmdwin_type != 0)
6900 textwidth -= 1;
6901#endif
6902#ifdef FEAT_FOLDING
6903 textwidth -= curwin->w_p_fdc;
6904#endif
6905#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006906 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 textwidth -= 1;
6908#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006909 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 textwidth -= 8;
6911 }
6912 if (textwidth < 0)
6913 textwidth = 0;
6914 if (ff && textwidth == 0)
6915 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006916 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (textwidth > 79)
6918 textwidth = 79;
6919 }
6920 return textwidth;
6921}
6922
6923/*
6924 * Put a character in the redo buffer, for when just after a CTRL-V.
6925 */
6926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006927redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 char_u buf[10];
6930
6931 /* Only digits need special treatment. Translate them into a string of
6932 * three digits. */
6933 if (VIM_ISDIGIT(c))
6934 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006935 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 AppendToRedobuff(buf);
6937 }
6938 else
6939 AppendCharToRedobuff(c);
6940}
6941
6942/*
6943 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006944 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 */
6946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006947start_arrow(
6948 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006950 start_arrow_common(end_insert_pos, TRUE);
6951}
6952
6953/*
6954 * Like start_arrow() but with end_change argument.
6955 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6956 */
6957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006958start_arrow_with_change(
6959 pos_T *end_insert_pos, /* can be NULL */
6960 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006961{
6962 start_arrow_common(end_insert_pos, end_change);
6963 if (!end_change)
6964 {
6965 AppendCharToRedobuff(Ctrl_G);
6966 AppendCharToRedobuff('U');
6967 }
6968}
6969
6970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971start_arrow_common(
6972 pos_T *end_insert_pos, /* can be NULL */
6973 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006974{
6975 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {
6977 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006978 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 arrow_used = TRUE; /* this means we stopped the current insert */
6980 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006981#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006982 check_spell_redraw();
6983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984}
6985
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006986#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006987/*
6988 * If we skipped highlighting word at cursor, do it now.
6989 * It may be skipped again, thus reset spell_redraw_lnum first.
6990 */
6991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006992check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006993{
6994 if (spell_redraw_lnum != 0)
6995 {
6996 linenr_T lnum = spell_redraw_lnum;
6997
6998 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01006999 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007000 }
7001}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007002
7003/*
7004 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7005 * spelled word, if there is one.
7006 */
7007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007008spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007009{
7010 pos_T tpos = curwin->w_cursor;
7011
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007012 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007013 if (curwin->w_cursor.col != tpos.col)
7014 start_arrow(&tpos);
7015}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007016#endif
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018/*
7019 * stop_arrow() is called before a change is made in insert mode.
7020 * If an arrow key has been used, start a new insertion.
7021 * Returns FAIL if undo is impossible, shouldn't insert then.
7022 */
7023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007024stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 if (arrow_used)
7027 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007028 Insstart = curwin->w_cursor; /* new insertion starts here */
7029 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7030 /* Don't update the original insert position when moved to the
7031 * right, except when nothing was inserted yet. */
7032 update_Insstart_orig = FALSE;
7033 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (u_save_cursor() == OK)
7036 {
7037 arrow_used = FALSE;
7038 ins_need_undo = FALSE;
7039 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (State & VREPLACE_FLAG)
7043 {
7044 orig_line_count = curbuf->b_ml.ml_line_count;
7045 vr_lines_changed = 1;
7046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 ResetRedobuff();
7048 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007049 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051 else if (ins_need_undo)
7052 {
7053 if (u_save_cursor() == OK)
7054 ins_need_undo = FALSE;
7055 }
7056
7057#ifdef FEAT_FOLDING
7058 /* Always open fold at the cursor line when inserting something. */
7059 foldOpenCursor();
7060#endif
7061
7062 return (arrow_used || ins_need_undo ? FAIL : OK);
7063}
7064
7065/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007066 * Do a few things to stop inserting.
7067 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7068 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 */
7070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007071stop_insert(
7072 pos_T *end_insert_pos,
7073 int esc, /* called by ins_esc() */
7074 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007076 int cc;
7077 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 stop_redo_ins();
7080 replace_flush(); /* abandon replace stack */
7081
7082 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007083 * Save the inserted text for later redo with ^@ and CTRL-A.
7084 * Don't do it when "restart_edit" was set and nothing was inserted,
7085 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007087 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007088 if (did_restart_edit == 0 || (ptr != NULL
7089 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007090 {
7091 vim_free(last_insert);
7092 last_insert = ptr;
7093 last_insert_skip = new_insert_skip;
7094 }
7095 else
7096 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007098 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
7100 /* Auto-format now. It may seem strange to do this when stopping an
7101 * insertion (or moving the cursor), but it's required when appending
7102 * a line and having it end in a space. But only do it when something
7103 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007104 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007106 pos_T tpos = curwin->w_cursor;
7107
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 /* When the cursor is at the end of the line after a space the
7109 * formatting will move it to the following word. Avoid that by
7110 * moving the cursor onto the space. */
7111 cc = 'x';
7112 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7113 {
7114 dec_cursor();
7115 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007116 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007117 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119
7120 auto_format(TRUE, FALSE);
7121
Bram Moolenaar1c465442017-03-12 20:10:05 +01007122 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007123 {
7124 if (gchar_cursor() != NUL)
7125 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007126 // If the cursor is still at the same character, also keep
7127 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007128 if (gchar_cursor() == NUL
7129 && curwin->w_cursor.lnum == tpos.lnum
7130 && curwin->w_cursor.col == tpos.col)
7131 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134
7135 /* If a space was inserted for auto-formatting, remove it now. */
7136 check_auto_format(TRUE);
7137
7138 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007139 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007140 * Do this when ESC was used or moving the cursor up/down.
7141 * Check for the old position still being valid, just in case the text
7142 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007143 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007144 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7145 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007147 pos_T tpos = curwin->w_cursor;
7148
7149 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007150 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007151 for (;;)
7152 {
7153 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7154 --curwin->w_cursor.col;
7155 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007156 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007157 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007158 if (del_char(TRUE) == FAIL)
7159 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007160 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007161 if (curwin->w_cursor.lnum != tpos.lnum)
7162 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007163 else
7164 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007165 /* reset tpos, could have been invalidated in the loop above */
7166 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007167 tpos.col++;
7168 if (cc != NUL && gchar_pos(&tpos) == NUL)
7169 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 /* <C-S-Right> may have started Visual mode, adjust the position for
7173 * deleted characters. */
7174 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7175 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007176 int len = (int)STRLEN(ml_get_curline());
7177
7178 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007180 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
7185 }
7186 did_ai = FALSE;
7187#ifdef FEAT_SMARTINDENT
7188 did_si = FALSE;
7189 can_si = FALSE;
7190 can_si_back = FALSE;
7191#endif
7192
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007193 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7194 * now in a different buffer. */
7195 if (end_insert_pos != NULL)
7196 {
7197 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007198 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007199 curbuf->b_op_end = *end_insert_pos;
7200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201}
7202
7203/*
7204 * Set the last inserted text to a single character.
7205 * Used for the replace command.
7206 */
7207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 char_u *s;
7211
7212 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (last_insert != NULL)
7215 {
7216 s = last_insert;
7217 /* Use the CTRL-V only when entering a special char */
7218 if (c < ' ' || c == DEL)
7219 *s++ = Ctrl_V;
7220 s = add_char2buf(c, s);
7221 *s++ = ESC;
7222 *s++ = NUL;
7223 last_insert_skip = 0;
7224 }
7225}
7226
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007227#if defined(EXITFREE) || defined(PROTO)
7228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007229free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007230{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007231 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007232# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007233 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007234# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007235}
7236#endif
7237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238/*
7239 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7240 * and CSI. Handle multi-byte characters.
7241 * Returns a pointer to after the added bytes.
7242 */
7243 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007246 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 int i;
7248 int len;
7249
7250 len = (*mb_char2bytes)(c, temp);
7251 for (i = 0; i < len; ++i)
7252 {
7253 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7255 if (c == K_SPECIAL)
7256 {
7257 *s++ = K_SPECIAL;
7258 *s++ = KS_SPECIAL;
7259 *s++ = KE_FILLER;
7260 }
7261#ifdef FEAT_GUI
7262 else if (c == CSI)
7263 {
7264 *s++ = CSI;
7265 *s++ = KS_EXTRA;
7266 *s++ = (int)KE_CSI;
7267 }
7268#endif
7269 else
7270 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 return s;
7273}
7274
7275/*
7276 * move cursor to start of line
7277 * if flags & BL_WHITE move to first non-white
7278 * if flags & BL_SOL move to first non-white if startofline is set,
7279 * otherwise keep "curswant" column
7280 * if flags & BL_FIX don't leave the cursor on a NUL.
7281 */
7282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 if ((flags & BL_SOL) && !p_sol)
7286 coladvance(curwin->w_curswant);
7287 else
7288 {
7289 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
7292 if (flags & (BL_WHITE | BL_SOL))
7293 {
7294 char_u *ptr;
7295
Bram Moolenaar1c465442017-03-12 20:10:05 +01007296 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7298 ++curwin->w_cursor.col;
7299 }
7300 curwin->w_set_curswant = TRUE;
7301 }
7302}
7303
7304/*
7305 * oneright oneleft cursor_down cursor_up
7306 *
7307 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007308 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 * Return OK when successful, FAIL when we hit a line of file boundary.
7310 */
7311
7312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (virtual_active())
7319 {
7320 pos_T prevpos = curwin->w_cursor;
7321
7322 /* Adjust for multi-wide char (excluding TAB) */
7323 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007324 coladvance(getviscol() + ((*ptr != TAB
7325 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 ? ptr2cells(ptr) : 1));
7327 curwin->w_set_curswant = TRUE;
7328 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7329 return (prevpos.col != curwin->w_cursor.col
7330 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
7333 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007334 if (*ptr == NUL)
7335 return FAIL; /* already at the very end */
7336
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007337 if (has_mbyte)
7338 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007340 l = 1;
7341
7342 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7343 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007344 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007345 return FAIL;
7346 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 curwin->w_set_curswant = TRUE;
7349 return OK;
7350}
7351
7352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (virtual_active())
7356 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007357#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 int v = getviscol();
7361
7362 if (v == 0)
7363 return FAIL;
7364
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007365#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 /* We might get stuck on 'showbreak', skip over it. */
7367 width = 1;
7368 for (;;)
7369 {
7370 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007371 /* getviscol() is slow, skip it when 'showbreak' is empty,
7372 * 'breakindent' is not set and there are no multi-byte
7373 * characters */
7374 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01007375 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 break;
7377 ++width;
7378 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007379#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 if (curwin->w_cursor.coladd == 1)
7384 {
7385 char_u *ptr;
7386
7387 /* Adjust for multi-wide char (not a TAB) */
7388 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007389 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
7390 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 curwin->w_cursor.coladd = 0;
7392 }
7393
7394 curwin->w_set_curswant = TRUE;
7395 return OK;
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
7398 if (curwin->w_cursor.col == 0)
7399 return FAIL;
7400
7401 curwin->w_set_curswant = TRUE;
7402 --curwin->w_cursor.col;
7403
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 /* if the character on the left of the current cursor is a multi-byte
7405 * character, move to its first byte */
7406 if (has_mbyte)
7407 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 return OK;
7409}
7410
7411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412cursor_up(
7413 long n,
7414 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 linenr_T lnum;
7417
7418 if (n > 0)
7419 {
7420 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007421 /* This fails if the cursor is already in the first line or the count
7422 * is larger than the line number and '-' is in 'cpoptions' */
7423 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 return FAIL;
7425 if (n >= lnum)
7426 lnum = 1;
7427 else
7428#ifdef FEAT_FOLDING
7429 if (hasAnyFolding(curwin))
7430 {
7431 /*
7432 * Count each sequence of folded lines as one logical line.
7433 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007434 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 (void)hasFolding(lnum, &lnum, NULL);
7436
7437 while (n--)
7438 {
7439 /* move up one line */
7440 --lnum;
7441 if (lnum <= 1)
7442 break;
7443 /* If we entered a fold, move to the beginning, unless in
7444 * Insert mode or when 'foldopen' contains "all": it will open
7445 * in a moment. */
7446 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7447 (void)hasFolding(lnum, &lnum, NULL);
7448 }
7449 if (lnum < 1)
7450 lnum = 1;
7451 }
7452 else
7453#endif
7454 lnum -= n;
7455 curwin->w_cursor.lnum = lnum;
7456 }
7457
7458 /* try to advance to the column we want to be at */
7459 coladvance(curwin->w_curswant);
7460
7461 if (upd_topline)
7462 update_topline(); /* make sure curwin->w_topline is valid */
7463
7464 return OK;
7465}
7466
7467/*
7468 * Cursor down a number of logical lines.
7469 */
7470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007471cursor_down(
7472 long n,
7473 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 linenr_T lnum;
7476
7477 if (n > 0)
7478 {
7479 lnum = curwin->w_cursor.lnum;
7480#ifdef FEAT_FOLDING
7481 /* Move to last line of fold, will fail if it's the end-of-file. */
7482 (void)hasFolding(lnum, NULL, &lnum);
7483#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007484 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007485 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007486 if (lnum >= curbuf->b_ml.ml_line_count
7487 || (lnum + n > curbuf->b_ml.ml_line_count
7488 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 return FAIL;
7490 if (lnum + n >= curbuf->b_ml.ml_line_count)
7491 lnum = curbuf->b_ml.ml_line_count;
7492 else
7493#ifdef FEAT_FOLDING
7494 if (hasAnyFolding(curwin))
7495 {
7496 linenr_T last;
7497
7498 /* count each sequence of folded lines as one logical line */
7499 while (n--)
7500 {
7501 if (hasFolding(lnum, NULL, &last))
7502 lnum = last + 1;
7503 else
7504 ++lnum;
7505 if (lnum >= curbuf->b_ml.ml_line_count)
7506 break;
7507 }
7508 if (lnum > curbuf->b_ml.ml_line_count)
7509 lnum = curbuf->b_ml.ml_line_count;
7510 }
7511 else
7512#endif
7513 lnum += n;
7514 curwin->w_cursor.lnum = lnum;
7515 }
7516
7517 /* try to advance to the column we want to be at */
7518 coladvance(curwin->w_curswant);
7519
7520 if (upd_topline)
7521 update_topline(); /* make sure curwin->w_topline is valid */
7522
7523 return OK;
7524}
7525
7526/*
7527 * Stuff the last inserted text in the read buffer.
7528 * Last_insert actually is a copy of the redo buffer, so we
7529 * first have to remove the command.
7530 */
7531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532stuff_inserted(
7533 int c, /* Command character to be inserted */
7534 long count, /* Repeat this many times */
7535 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 char_u *esc_ptr;
7538 char_u *ptr;
7539 char_u *last_ptr;
7540 char_u last = NUL;
7541
7542 ptr = get_last_insert();
7543 if (ptr == NULL)
7544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 return FAIL;
7547 }
7548
7549 /* may want to stuff the command character, to start Insert mode */
7550 if (c != NUL)
7551 stuffcharReadbuff(c);
7552 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7553 *esc_ptr = NUL; /* remove the ESC */
7554
7555 /* when the last char is either "0" or "^" it will be quoted if no ESC
7556 * comes after it OR if it will inserted more than once and "ptr"
7557 * starts with ^D. -- Acevedo
7558 */
7559 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7560 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7561 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7562 {
7563 last = *last_ptr;
7564 *last_ptr = NUL;
7565 }
7566
7567 do
7568 {
7569 stuffReadbuff(ptr);
7570 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7571 if (last)
7572 stuffReadbuff((char_u *)(last == '0'
7573 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7574 : IF_EB("\026^", CTRL_V_STR "^")));
7575 }
7576 while (--count > 0);
7577
7578 if (last)
7579 *last_ptr = last;
7580
7581 if (esc_ptr != NULL)
7582 *esc_ptr = ESC; /* put the ESC back */
7583
7584 /* may want to stuff a trailing ESC, to get out of Insert mode */
7585 if (!no_esc)
7586 stuffcharReadbuff(ESC);
7587
7588 return OK;
7589}
7590
7591 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007592get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 if (last_insert == NULL)
7595 return NULL;
7596 return last_insert + last_insert_skip;
7597}
7598
7599/*
7600 * Get last inserted string, and remove trailing <Esc>.
7601 * Returns pointer to allocated memory (must be freed) or NULL.
7602 */
7603 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
7606 char_u *s;
7607 int len;
7608
7609 if (last_insert == NULL)
7610 return NULL;
7611 s = vim_strsave(last_insert + last_insert_skip);
7612 if (s != NULL)
7613 {
7614 len = (int)STRLEN(s);
7615 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7616 s[len - 1] = NUL;
7617 }
7618 return s;
7619}
7620
7621/*
7622 * Check the word in front of the cursor for an abbreviation.
7623 * Called when the non-id character "c" has been entered.
7624 * When an abbreviation is recognized it is removed from the text and
7625 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7626 */
7627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007628echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 /* Don't check for abbreviation in paste mode, when disabled and just
7631 * after moving around with cursor keys. */
7632 if (p_paste || no_abbr || arrow_used)
7633 return FALSE;
7634
7635 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7636 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7637}
7638
7639/*
7640 * replace-stack functions
7641 *
7642 * When replacing characters, the replaced characters are remembered for each
7643 * new character. This is used to re-insert the old text when backspacing.
7644 *
7645 * There is a NUL headed list of characters for each character that is
7646 * currently in the file after the insertion point. When BS is used, one NUL
7647 * headed list is put back for the deleted character.
7648 *
7649 * For a newline, there are two NUL headed lists. One contains the characters
7650 * that the NL replaced. The extra one stores the characters after the cursor
7651 * that were deleted (always white space).
7652 *
7653 * Replace_offset is normally 0, in which case replace_push will add a new
7654 * character at the end of the stack. If replace_offset is not 0, that many
7655 * characters will be left on the stack above the newly inserted character.
7656 */
7657
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007658static char_u *replace_stack = NULL;
7659static long replace_stack_nr = 0; /* next entry in replace stack */
7660static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
7662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007663replace_push(
7664 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 char_u *p;
7667
7668 if (replace_stack_nr < replace_offset) /* nothing to do */
7669 return;
7670 if (replace_stack_len <= replace_stack_nr)
7671 {
7672 replace_stack_len += 50;
7673 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7674 if (p == NULL) /* out of memory */
7675 {
7676 replace_stack_len -= 50;
7677 return;
7678 }
7679 if (replace_stack != NULL)
7680 {
7681 mch_memmove(p, replace_stack,
7682 (size_t)(replace_stack_nr * sizeof(char_u)));
7683 vim_free(replace_stack);
7684 }
7685 replace_stack = p;
7686 }
7687 p = replace_stack + replace_stack_nr - replace_offset;
7688 if (replace_offset)
7689 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7690 *p = c;
7691 ++replace_stack_nr;
7692}
7693
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007694/*
7695 * Push a character onto the replace stack. Handles a multi-byte character in
7696 * reverse byte order, so that the first byte is popped off first.
7697 * Return the number of bytes done (includes composing characters).
7698 */
7699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007700replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007701{
7702 int l = (*mb_ptr2len)(p);
7703 int j;
7704
7705 for (j = l - 1; j >= 0; --j)
7706 replace_push(p[j]);
7707 return l;
7708}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007709
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710/*
7711 * Pop one item from the replace stack.
7712 * return -1 if stack empty
7713 * return replaced character or NUL otherwise
7714 */
7715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007716replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
7718 if (replace_stack_nr == 0)
7719 return -1;
7720 return (int)replace_stack[--replace_stack_nr];
7721}
7722
7723/*
7724 * Join the top two items on the replace stack. This removes to "off"'th NUL
7725 * encountered.
7726 */
7727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007728replace_join(
7729 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 int i;
7732
7733 for (i = replace_stack_nr; --i >= 0; )
7734 if (replace_stack[i] == NUL && off-- <= 0)
7735 {
7736 --replace_stack_nr;
7737 mch_memmove(replace_stack + i, replace_stack + i + 1,
7738 (size_t)(replace_stack_nr - i));
7739 return;
7740 }
7741}
7742
7743/*
7744 * Pop bytes from the replace stack until a NUL is found, and insert them
7745 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7746 */
7747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007748replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
7750 int cc;
7751 int oldState = State;
7752
7753 State = NORMAL; /* don't want REPLACE here */
7754 while ((cc = replace_pop()) > 0)
7755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 dec_cursor();
7758 }
7759 State = oldState;
7760}
7761
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762/*
7763 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7764 * indicates a multi-byte char, pop the other bytes too.
7765 */
7766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007767mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007770 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 int i;
7772 int c;
7773
7774 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7775 {
7776 buf[0] = cc;
7777 for (i = 1; i < n; ++i)
7778 buf[i] = replace_pop();
7779 ins_bytes_len(buf, n);
7780 }
7781 else
7782 ins_char(cc);
7783
7784 if (enc_utf8)
7785 /* Handle composing chars. */
7786 for (;;)
7787 {
7788 c = replace_pop();
7789 if (c == -1) /* stack empty */
7790 break;
7791 if ((n = MB_BYTE2LEN(c)) == 1)
7792 {
7793 /* Not a multi-byte char, put it back. */
7794 replace_push(c);
7795 break;
7796 }
7797 else
7798 {
7799 buf[0] = c;
7800 for (i = 1; i < n; ++i)
7801 buf[i] = replace_pop();
7802 if (utf_iscomposing(utf_ptr2char(buf)))
7803 ins_bytes_len(buf, n);
7804 else
7805 {
7806 /* Not a composing char, put it back. */
7807 for (i = n - 1; i >= 0; --i)
7808 replace_push(buf[i]);
7809 break;
7810 }
7811 }
7812 }
7813}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
7815/*
7816 * make the replace stack empty
7817 * (called when exiting replace mode)
7818 */
7819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007820replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007822 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 replace_stack_len = 0;
7824 replace_stack_nr = 0;
7825}
7826
7827/*
7828 * Handle doing a BS for one character.
7829 * cc < 0: replace stack empty, just move cursor
7830 * cc == 0: character was inserted, delete it
7831 * cc > 0: character was replaced, put cc (first byte of original char) back
7832 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007833 * When "limit_col" is >= 0, don't delete before this column. Matters when
7834 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 */
7836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 int orig_len = 0;
7841 int ins_len;
7842 int orig_vcols = 0;
7843 colnr_T start_vcol;
7844 char_u *p;
7845 int i;
7846 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
7848 cc = replace_pop();
7849 if (cc > 0)
7850 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007851#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007852 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007853
7854 if (curbuf->b_has_textprop)
7855 {
7856 // Do not adjust text properties for individual delete and insert
7857 // operations, do it afterwards on the resulting text.
7858 len_before = STRLEN(ml_get_curline());
7859 ++text_prop_frozen;
7860 }
7861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (State & VREPLACE_FLAG)
7863 {
7864 /* Get the number of screen cells used by the character we are
7865 * going to delete. */
7866 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7867 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (has_mbyte)
7870 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007871 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007873 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 replace_push(cc);
7875 }
7876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
7878 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007880 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 }
7882 replace_pop_ins();
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (State & VREPLACE_FLAG)
7885 {
7886 /* Get the number of screen cells used by the inserted characters */
7887 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007888 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 vcol = start_vcol;
7890 for (i = 0; i < ins_len; ++i)
7891 {
7892 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007893 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895 vcol -= start_vcol;
7896
7897 /* Delete spaces that were inserted after the cursor to keep the
7898 * text aligned. */
7899 curwin->w_cursor.col += ins_len;
7900 while (vcol > orig_vcols && gchar_cursor() == ' ')
7901 {
7902 del_char(FALSE);
7903 ++orig_vcols;
7904 }
7905 curwin->w_cursor.col -= ins_len;
7906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
Bram Moolenaar196d1572019-01-02 23:47:18 +01007908 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01007910
7911#ifdef FEAT_TEXT_PROP
7912 if (curbuf->b_has_textprop)
7913 {
7914 size_t len_now = STRLEN(ml_get_curline());
7915
7916 --text_prop_frozen;
7917 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
7918 (int)(len_now - len_before));
7919 }
7920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 }
7922 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007923 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924}
7925
7926#ifdef FEAT_CINDENT
7927/*
7928 * Return TRUE if C-indenting is on.
7929 */
7930 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007931cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 return (!p_paste && (curbuf->b_p_cin
7934# ifdef FEAT_EVAL
7935 || *curbuf->b_p_inde != NUL
7936# endif
7937 ));
7938}
7939#endif
7940
7941#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7942/*
7943 * Re-indent the current line, based on the current contents of it and the
7944 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7945 * confused what all the part that handles Control-T is doing that I'm not.
7946 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7947 */
7948
7949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007950fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007952 int amount = get_the_indent();
7953
7954 if (amount >= 0)
7955 {
7956 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7957 if (linewhite(curwin->w_cursor.lnum))
7958 did_ai = TRUE; /* delete the indent if the line stays empty */
7959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960}
7961
7962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007963fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 if (p_paste)
7966 return;
7967# ifdef FEAT_LISP
7968 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7969 fixthisline(get_lisp_indent);
7970# endif
7971# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7972 else
7973# endif
7974# ifdef FEAT_CINDENT
7975 if (cindent_on())
7976 do_c_expr_indent();
7977# endif
7978}
7979
7980#endif
7981
7982#ifdef FEAT_CINDENT
7983/*
7984 * return TRUE if 'cinkeys' contains the key "keytyped",
7985 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007986 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7988 *
7989 * "keytyped" can have a few special values:
7990 * KEY_OPEN_FORW
7991 * KEY_OPEN_BACK
7992 * KEY_COMPLETE just finished completion.
7993 *
7994 * If line_is_empty is TRUE accept keys with '0' before them.
7995 */
7996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007997in_cinkeys(
7998 int keytyped,
7999 int when,
8000 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002 char_u *look;
8003 int try_match;
8004 int try_match_word;
8005 char_u *p;
8006 char_u *line;
8007 int icase;
8008 int i;
8009
Bram Moolenaar30848942009-12-24 14:46:12 +00008010 if (keytyped == NUL)
8011 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8012 return FALSE;
8013
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014#ifdef FEAT_EVAL
8015 if (*curbuf->b_p_inde != NUL)
8016 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8017 else
8018#endif
8019 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8020 while (*look)
8021 {
8022 /*
8023 * Find out if we want to try a match with this key, depending on
8024 * 'when' and a '*' or '!' before the key.
8025 */
8026 switch (when)
8027 {
8028 case '*': try_match = (*look == '*'); break;
8029 case '!': try_match = (*look == '!'); break;
8030 default: try_match = (*look != '*'); break;
8031 }
8032 if (*look == '*' || *look == '!')
8033 ++look;
8034
8035 /*
8036 * If there is a '0', only accept a match if the line is empty.
8037 * But may still match when typing last char of a word.
8038 */
8039 if (*look == '0')
8040 {
8041 try_match_word = try_match;
8042 if (!line_is_empty)
8043 try_match = FALSE;
8044 ++look;
8045 }
8046 else
8047 try_match_word = FALSE;
8048
8049 /*
8050 * does it look like a control character?
8051 */
8052 if (*look == '^'
8053#ifdef EBCDIC
8054 && (Ctrl_chr(look[1]) != 0)
8055#else
8056 && look[1] >= '?' && look[1] <= '_'
8057#endif
8058 )
8059 {
8060 if (try_match && keytyped == Ctrl_chr(look[1]))
8061 return TRUE;
8062 look += 2;
8063 }
8064 /*
8065 * 'o' means "o" command, open forward.
8066 * 'O' means "O" command, open backward.
8067 */
8068 else if (*look == 'o')
8069 {
8070 if (try_match && keytyped == KEY_OPEN_FORW)
8071 return TRUE;
8072 ++look;
8073 }
8074 else if (*look == 'O')
8075 {
8076 if (try_match && keytyped == KEY_OPEN_BACK)
8077 return TRUE;
8078 ++look;
8079 }
8080
8081 /*
8082 * 'e' means to check for "else" at start of line and just before the
8083 * cursor.
8084 */
8085 else if (*look == 'e')
8086 {
8087 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8088 {
8089 p = ml_get_curline();
8090 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8091 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8092 return TRUE;
8093 }
8094 ++look;
8095 }
8096
8097 /*
8098 * ':' only causes an indent if it is at the end of a label or case
8099 * statement, or when it was before typing the ':' (to fix
8100 * class::method for C++).
8101 */
8102 else if (*look == ':')
8103 {
8104 if (try_match && keytyped == ':')
8105 {
8106 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008107 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008109 /* Need to get the line again after cin_islabel(). */
8110 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (curwin->w_cursor.col > 2
8112 && p[curwin->w_cursor.col - 1] == ':'
8113 && p[curwin->w_cursor.col - 2] == ':')
8114 {
8115 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008116 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008117 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 p = ml_get_curline();
8119 p[curwin->w_cursor.col - 1] = ':';
8120 if (i)
8121 return TRUE;
8122 }
8123 }
8124 ++look;
8125 }
8126
8127
8128 /*
8129 * Is it a key in <>, maybe?
8130 */
8131 else if (*look == '<')
8132 {
8133 if (try_match)
8134 {
8135 /*
8136 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8137 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8138 * >, *, : and ! keys if they really really want to.
8139 */
8140 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8141 && keytyped == look[1])
8142 return TRUE;
8143
8144 if (keytyped == get_special_key_code(look + 1))
8145 return TRUE;
8146 }
8147 while (*look && *look != '>')
8148 look++;
8149 while (*look == '>')
8150 look++;
8151 }
8152
8153 /*
8154 * Is it a word: "=word"?
8155 */
8156 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8157 {
8158 ++look;
8159 if (*look == '~')
8160 {
8161 icase = TRUE;
8162 ++look;
8163 }
8164 else
8165 icase = FALSE;
8166 p = vim_strchr(look, ',');
8167 if (p == NULL)
8168 p = look + STRLEN(look);
8169 if ((try_match || try_match_word)
8170 && curwin->w_cursor.col >= (colnr_T)(p - look))
8171 {
8172 int match = FALSE;
8173
8174#ifdef FEAT_INS_EXPAND
8175 if (keytyped == KEY_COMPLETE)
8176 {
8177 char_u *s;
8178
8179 /* Just completed a word, check if it starts with "look".
8180 * search back for the start of a word. */
8181 line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 if (has_mbyte)
8183 {
8184 char_u *n;
8185
8186 for (s = line + curwin->w_cursor.col; s > line; s = n)
8187 {
8188 n = mb_prevptr(line, s);
8189 if (!vim_iswordp(n))
8190 break;
8191 }
8192 }
8193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 for (s = line + curwin->w_cursor.col; s > line; --s)
8195 if (!vim_iswordc(s[-1]))
8196 break;
8197 if (s + (p - look) <= line + curwin->w_cursor.col
8198 && (icase
8199 ? MB_STRNICMP(s, look, p - look)
8200 : STRNCMP(s, look, p - look)) == 0)
8201 match = TRUE;
8202 }
8203 else
8204#endif
8205 /* TODO: multi-byte */
8206 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8207 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8208 {
8209 line = ml_get_cursor();
8210 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8211 || !vim_iswordc(line[-(p - look) - 1]))
8212 && (icase
8213 ? MB_STRNICMP(line - (p - look), look, p - look)
8214 : STRNCMP(line - (p - look), look, p - look))
8215 == 0)
8216 match = TRUE;
8217 }
8218 if (match && try_match_word && !try_match)
8219 {
8220 /* "0=word": Check if there are only blanks before the
8221 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008222 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 (int)(curwin->w_cursor.col - (p - look)))
8224 match = FALSE;
8225 }
8226 if (match)
8227 return TRUE;
8228 }
8229 look = p;
8230 }
8231
8232 /*
8233 * ok, it's a boring generic character.
8234 */
8235 else
8236 {
8237 if (try_match && *look == keytyped)
8238 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008239 if (*look != NUL)
8240 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
8242
8243 /*
8244 * Skip over ", ".
8245 */
8246 look = skip_to_option_part(look);
8247 }
8248 return FALSE;
8249}
8250#endif /* FEAT_CINDENT */
8251
8252#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8253/*
8254 * Map Hebrew keyboard when in hkmap mode.
8255 */
8256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008257hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8260 {
8261 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8262 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8263 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8264 static char_u map[26] =
8265 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8266 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8267 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8268 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8269 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8270 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8271 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8272 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8273 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8274
8275 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8276 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8277 /* '-1'='sofit' */
8278 else if (c == 'x')
8279 return 'X';
8280 else if (c == 'q')
8281 return '\''; /* {geresh}={'} */
8282 else if (c == 246)
8283 return ' '; /* \"o --> ' ' for a german keyboard */
8284 else if (c == 228)
8285 return ' '; /* \"a --> ' ' -- / -- */
8286 else if (c == 252)
8287 return ' '; /* \"u --> ' ' -- / -- */
8288#ifdef EBCDIC
8289 else if (islower(c))
8290#else
8291 /* NOTE: islower() does not do the right thing for us on Linux so we
8292 * do this the same was as 5.7 and previous, so it works correctly on
8293 * all systems. Specifically, the e.g. Delete and Arrow keys are
8294 * munged and won't work if e.g. searching for Hebrew text.
8295 */
8296 else if (c >= 'a' && c <= 'z')
8297#endif
8298 return (int)(map[CharOrdLow(c)] + p_aleph);
8299 else
8300 return c;
8301 }
8302 else
8303 {
8304 switch (c)
8305 {
8306 case '`': return ';';
8307 case '/': return '.';
8308 case '\'': return ',';
8309 case 'q': return '/';
8310 case 'w': return '\'';
8311
8312 /* Hebrew letters - set offset from 'a' */
8313 case ',': c = '{'; break;
8314 case '.': c = 'v'; break;
8315 case ';': c = 't'; break;
8316 default: {
8317 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8318
8319#ifdef EBCDIC
8320 /* see note about islower() above */
8321 if (!islower(c))
8322#else
8323 if (c < 'a' || c > 'z')
8324#endif
8325 return c;
8326 c = str[CharOrdLow(c)];
8327 break;
8328 }
8329 }
8330
8331 return (int)(CharOrdLow(c) + p_aleph);
8332 }
8333}
8334#endif
8335
8336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008337ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338{
8339 int need_redraw = FALSE;
8340 int regname;
8341 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008342 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
8344 /*
8345 * If we are going to wait for a character, show a '"'.
8346 */
8347 pc_status = PC_STATUS_UNSET;
8348 if (redrawing() && !char_avail())
8349 {
8350 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008351 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 edit_putchar('"', TRUE);
8354#ifdef FEAT_CMDL_INFO
8355 add_to_showcmd_c(Ctrl_R);
8356#endif
8357 }
8358
8359#ifdef USE_ON_FLY_SCROLL
8360 dont_scroll = TRUE; /* disallow scrolling here */
8361#endif
8362
8363 /*
8364 * Don't map the register name. This also prevents the mode message to be
8365 * deleted when ESC is hit.
8366 */
8367 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008368 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8371 {
8372 /* Get a third key for literal register insertion */
8373 literally = regname;
8374#ifdef FEAT_CMDL_INFO
8375 add_to_showcmd_c(literally);
8376#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008377 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 }
8380 --no_mapping;
8381
8382#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008383 /* Don't call u_sync() while typing the expression or giving an error
8384 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 ++no_u_sync;
8386 if (regname == '=')
8387 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008388 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008389# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008391# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008392 /* Sync undo when evaluating the expression calls setline() or
8393 * append(), so that it can be undone separately. */
8394 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008397
8398 // Cursor may be moved back a column.
8399 curwin->w_cursor = curpos;
8400 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008401# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008402 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (im_on)
8404 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008407 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8408 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008409 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 else
8413 {
8414#endif
8415 if (literally == Ctrl_O || literally == Ctrl_P)
8416 {
8417 /* Append the command to the redo buffer. */
8418 AppendCharToRedobuff(Ctrl_R);
8419 AppendCharToRedobuff(literally);
8420 AppendCharToRedobuff(regname);
8421
8422 do_put(regname, BACKWARD, 1L,
8423 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8424 }
8425 else if (insert_reg(regname, literally) == FAIL)
8426 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008427 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 need_redraw = TRUE; /* remove the '"' */
8429 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008430 else if (stop_insert_mode)
8431 /* When the '=' register was used and a function was invoked that
8432 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8433 * insert anything, need to remove the '"' */
8434 need_redraw = TRUE;
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436#ifdef FEAT_EVAL
8437 }
8438 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008439 if (u_sync_once == 1)
8440 ins_need_undo = TRUE;
8441 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442#endif
8443#ifdef FEAT_CMDL_INFO
8444 clear_showcmd();
8445#endif
8446
8447 /* If the inserted register is empty, we need to remove the '"' */
8448 if (need_redraw || stuff_empty())
8449 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008450
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008451 /* Disallow starting Visual mode here, would get a weird mode. */
8452 if (!vis_active && VIsual_active)
8453 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454}
8455
8456/*
8457 * CTRL-G commands in Insert mode.
8458 */
8459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008460ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 int c;
8463
8464#ifdef FEAT_INS_EXPAND
8465 /* Right after CTRL-X the cursor will be after the ruler. */
8466 setcursor();
8467#endif
8468
8469 /*
8470 * Don't map the second key. This also prevents the mode message to be
8471 * deleted when ESC is hit.
8472 */
8473 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008474 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 --no_mapping;
8476 switch (c)
8477 {
8478 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8479 case K_UP:
8480 case Ctrl_K:
8481 case 'k': ins_up(TRUE);
8482 break;
8483
8484 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8485 case K_DOWN:
8486 case Ctrl_J:
8487 case 'j': ins_down(TRUE);
8488 break;
8489
8490 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008491 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008493
8494 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008495 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008496 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008497 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 break;
8499
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008500 /* CTRL-G U: do not break undo with the next char */
8501 case 'U':
8502 /* Allow one left/right cursor movement with the next char,
8503 * without breaking undo. */
8504 dont_sync_undo = MAYBE;
8505 break;
8506
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008508 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 }
8510}
8511
8512/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008513 * CTRL-^ in Insert mode.
8514 */
8515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008517{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008518 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008519 {
8520 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8521 if (State & LANGMAP)
8522 {
8523 curbuf->b_p_iminsert = B_IMODE_NONE;
8524 State &= ~LANGMAP;
8525 }
8526 else
8527 {
8528 curbuf->b_p_iminsert = B_IMODE_LMAP;
8529 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008530#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008531 im_set_active(FALSE);
8532#endif
8533 }
8534 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008535#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008536 else
8537 {
8538 /* There are no ":lmap" mappings, toggle IM */
8539 if (im_get_status())
8540 {
8541 curbuf->b_p_iminsert = B_IMODE_NONE;
8542 im_set_active(FALSE);
8543 }
8544 else
8545 {
8546 curbuf->b_p_iminsert = B_IMODE_IM;
8547 State &= ~LANGMAP;
8548 im_set_active(TRUE);
8549 }
8550 }
8551#endif
8552 set_iminsert_global();
8553 showmode();
8554#ifdef FEAT_GUI
8555 /* may show different cursor shape or color */
8556 if (gui.in_use)
8557 gui_update_cursor(TRUE, FALSE);
8558#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008559#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008560 /* Show/unshow value of 'keymap' in status lines. */
8561 status_redraw_curbuf();
8562#endif
8563}
8564
8565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 * Handle ESC in insert mode.
8567 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8568 * insert.
8569 */
8570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008571ins_esc(
8572 long *count,
8573 int cmdchar,
8574 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 int temp;
8577 static int disabled_redraw = FALSE;
8578
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008579#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008580 check_spell_redraw();
8581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#if defined(FEAT_HANGULIN)
8583# if defined(ESC_CHG_TO_ENG_MODE)
8584 hangul_input_state_set(0);
8585# endif
8586 if (composing_hangul)
8587 {
8588 push_raw_key(composing_hangul_buffer, 2);
8589 composing_hangul = 0;
8590 }
8591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 temp = curwin->w_cursor.col;
8594 if (disabled_redraw)
8595 {
8596 --RedrawingDisabled;
8597 disabled_redraw = FALSE;
8598 }
8599 if (!arrow_used)
8600 {
8601 /*
8602 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008603 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8604 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 */
8606 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008607 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609 /*
8610 * Repeating insert may take a long time. Check for
8611 * interrupt now and then.
8612 */
8613 if (*count > 0)
8614 {
8615 line_breakcheck();
8616 if (got_int)
8617 *count = 0;
8618 }
8619
8620 if (--*count > 0) /* repeat what was typed */
8621 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008622 /* Vi repeats the insert without replacing characters. */
8623 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8624 State &= ~REPLACE_FLAG;
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 (void)start_redo_ins();
8627 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008628 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 ++RedrawingDisabled;
8630 disabled_redraw = TRUE;
8631 return FALSE; /* repeat the insert */
8632 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008633 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 undisplay_dollar();
8635 }
8636
8637 /* When an autoindent was removed, curswant stays after the
8638 * indent */
8639 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8640 curwin->w_set_curswant = TRUE;
8641
8642 /* Remember the last Insert position in the '^ mark. */
8643 if (!cmdmod.keepjumps)
8644 curbuf->b_last_insert = curwin->w_cursor;
8645
8646 /*
8647 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008648 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008650 if (!nomove
8651 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008652 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00008653 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008654 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655#ifdef FEAT_RIGHTLEFT
8656 && !revins_on
8657#endif
8658 )
8659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8661 {
8662 oneleft();
8663 if (restart_edit != NUL)
8664 ++curwin->w_cursor.coladd;
8665 }
8666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {
8668 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 /* Correct cursor for multi-byte character. */
8670 if (has_mbyte)
8671 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 }
8673 }
8674
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008675#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 /* Disable IM to allow typing English directly for Normal mode commands.
8677 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8678 * well). */
8679 if (!(State & LANGMAP))
8680 im_save_status(&curbuf->b_p_iminsert);
8681 im_set_active(FALSE);
8682#endif
8683
8684 State = NORMAL;
8685 /* need to position cursor again (e.g. when on a TAB ) */
8686 changed_cline_bef_curs();
8687
8688#ifdef FEAT_MOUSE
8689 setmouse();
8690#endif
8691#ifdef CURSOR_SHAPE
8692 ui_cursor_shape(); /* may show different cursor shape */
8693#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008694 if (!p_ek)
8695 /* Re-enable bracketed paste mode. */
8696 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697
8698 /*
8699 * When recording or for CTRL-O, need to display the new mode.
8700 * Otherwise remove the mode message.
8701 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008702 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 showmode();
Bram Moolenaarcb574f42019-01-25 22:29:57 +01008704 else if (p_smd && !skip_showmode())
Bram Moolenaar32526b32019-01-19 17:43:09 +01008705 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
8707 return TRUE; /* exit Insert mode */
8708}
8709
8710#ifdef FEAT_RIGHTLEFT
8711/*
8712 * Toggle language: hkmap and revins_on.
8713 * Move to end of reverse inserted text.
8714 */
8715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008716ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 if (revins_on && revins_chars && revins_scol >= 0)
8719 {
8720 while (gchar_cursor() != NUL && revins_chars--)
8721 ++curwin->w_cursor.col;
8722 }
8723 p_ri = !p_ri;
8724 revins_on = (State == INSERT && p_ri);
8725 if (revins_on)
8726 {
8727 revins_scol = curwin->w_cursor.col;
8728 revins_legal++;
8729 revins_chars = 0;
8730 undisplay_dollar();
8731 }
8732 else
8733 revins_scol = -1;
8734#ifdef FEAT_FKMAP
8735 if (p_altkeymap)
8736 {
8737 /*
8738 * to be consistent also for redo command, using '.'
8739 * set arrow_used to true and stop it - causing to redo
8740 * characters entered in one mode (normal/reverse insert).
8741 */
8742 arrow_used = TRUE;
8743 (void)stop_arrow();
8744 p_fkmap = curwin->w_p_rl ^ p_ri;
8745 if (p_fkmap && p_ri)
8746 State = INSERT;
8747 }
8748 else
8749#endif
8750 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8751 showmode();
8752}
8753#endif
8754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755/*
8756 * If 'keymodel' contains "startsel", may start selection.
8757 * Returns TRUE when a CTRL-O and other keys stuffed.
8758 */
8759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008760ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 if (km_startsel)
8763 switch (c)
8764 {
8765 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 case K_PAGEUP:
8768 case K_KPAGEUP:
8769 case K_PAGEDOWN:
8770 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008771# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 case K_LEFT:
8773 case K_RIGHT:
8774 case K_UP:
8775 case K_DOWN:
8776 case K_END:
8777 case K_HOME:
8778# endif
8779 if (!(mod_mask & MOD_MASK_SHIFT))
8780 break;
8781 /* FALLTHROUGH */
8782 case K_S_LEFT:
8783 case K_S_RIGHT:
8784 case K_S_UP:
8785 case K_S_DOWN:
8786 case K_S_END:
8787 case K_S_HOME:
8788 /* Start selection right away, the cursor can move with
8789 * CTRL-O when beyond the end of the line. */
8790 start_selection();
8791
8792 /* Execute the key in (insert) Select mode. */
8793 stuffcharReadbuff(Ctrl_O);
8794 if (mod_mask)
8795 {
8796 char_u buf[4];
8797
8798 buf[0] = K_SPECIAL;
8799 buf[1] = KS_MODIFIER;
8800 buf[2] = mod_mask;
8801 buf[3] = NUL;
8802 stuffReadbuff(buf);
8803 }
8804 stuffcharReadbuff(c);
8805 return TRUE;
8806 }
8807 return FALSE;
8808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
8810/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008811 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008815{
8816#ifdef FEAT_FKMAP
8817 if (p_fkmap && p_ri)
8818 {
8819 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008820 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008821 return;
8822 }
8823#endif
8824
Bram Moolenaar1e015462005-09-25 22:16:38 +00008825# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008826 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008827 (char_u *)((State & REPLACE_FLAG) ? "i"
8828 : replaceState == VREPLACE ? "v"
8829 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008830# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008831 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008832 if (State & REPLACE_FLAG)
8833 State = INSERT | (State & LANGMAP);
8834 else
8835 State = replaceState | (State & LANGMAP);
8836 AppendCharToRedobuff(K_INS);
8837 showmode();
8838#ifdef CURSOR_SHAPE
8839 ui_cursor_shape(); /* may show different cursor shape */
8840#endif
8841}
8842
8843/*
8844 * Pressed CTRL-O in Insert mode.
8845 */
8846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008847ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008848{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008849 if (State & VREPLACE_FLAG)
8850 restart_edit = 'V';
8851 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008852 if (State & REPLACE_FLAG)
8853 restart_edit = 'R';
8854 else
8855 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008856 if (virtual_active())
8857 ins_at_eol = FALSE; /* cursor always keeps its column */
8858 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008859 ins_at_eol = (gchar_cursor() == NUL);
8860}
8861
8862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 * If the cursor is on an indent, ^T/^D insert/delete one
8864 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008865 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 * with vi. But vi only supports ^T and ^D after an
8867 * autoindent, we support it everywhere.
8868 */
8869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008870ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 if (stop_arrow() == FAIL)
8873 return;
8874 AppendCharToRedobuff(c);
8875
8876 /*
8877 * 0^D and ^^D: remove all indent.
8878 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008879 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8880 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
8882 --curwin->w_cursor.col;
8883 (void)del_char(FALSE); /* delete the '^' or '0' */
8884 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8885 if (State & REPLACE_FLAG)
8886 replace_pop_ins();
8887 if (lastc == '^')
8888 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008889 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008892 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8895 did_ai = FALSE;
8896#ifdef FEAT_SMARTINDENT
8897 did_si = FALSE;
8898 can_si = FALSE;
8899 can_si_back = FALSE;
8900#endif
8901#ifdef FEAT_CINDENT
8902 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8903#endif
8904}
8905
8906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008907ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 int temp;
8910
8911 if (stop_arrow() == FAIL)
8912 return;
8913 if (gchar_cursor() == NUL) /* delete newline */
8914 {
8915 temp = curwin->w_cursor.col;
8916 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008917 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008918 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008922 /* Adjust orig_line_count in case more lines have been deleted than
8923 * have been added. That makes sure, that open_line() later
8924 * can access all buffer lines correctly */
8925 if (State & VREPLACE_FLAG &&
8926 orig_line_count > curbuf->b_ml.ml_line_count)
8927 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008930 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8931 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 did_ai = FALSE;
8933#ifdef FEAT_SMARTINDENT
8934 did_si = FALSE;
8935 can_si = FALSE;
8936 can_si_back = FALSE;
8937#endif
8938 AppendCharToRedobuff(K_DEL);
8939}
8940
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008941/*
8942 * Delete one character for ins_bs().
8943 */
8944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008945ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008946{
8947 dec_cursor();
8948 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8949 if (State & REPLACE_FLAG)
8950 {
8951 /* Don't delete characters before the insert point when in
8952 * Replace mode */
8953 if (curwin->w_cursor.lnum != Insstart.lnum
8954 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008955 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008956 }
8957 else
8958 (void)del_char(FALSE);
8959}
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961/*
8962 * Handle Backspace, delete-word and delete-line in Insert mode.
8963 * Return TRUE when backspace was actually used.
8964 */
8965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008966ins_bs(
8967 int c,
8968 int mode,
8969 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
8971 linenr_T lnum;
8972 int cc;
8973 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008974 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 colnr_T mincol;
8976 int did_backspace = FALSE;
8977 int in_indent;
8978 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008979 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981 /*
8982 * can't delete anything in an empty file
8983 * can't backup past first character in buffer
8984 * can't backup past starting point unless 'backspace' > 1
8985 * can backup to a previous line if 'backspace' == 0
8986 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008987 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 || (
8989#ifdef FEAT_RIGHTLEFT
8990 !revins_on &&
8991#endif
8992 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8993 || (!can_bs(BS_START)
8994 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008995 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8996 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8998 && curwin->w_cursor.col <= ai_col)
8999 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9000 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009001 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 return FALSE;
9003 }
9004
9005 if (stop_arrow() == FAIL)
9006 return FALSE;
9007 in_indent = inindent(0);
9008#ifdef FEAT_CINDENT
9009 if (in_indent)
9010 can_cindent = FALSE;
9011#endif
9012#ifdef FEAT_COMMENTS
9013 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9014#endif
9015#ifdef FEAT_RIGHTLEFT
9016 if (revins_on) /* put cursor after last inserted char */
9017 inc_cursor();
9018#endif
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 /* Virtualedit:
9021 * BACKSPACE_CHAR eats a virtual space
9022 * BACKSPACE_WORD eats all coladd
9023 * BACKSPACE_LINE eats all coladd and keeps going
9024 */
9025 if (curwin->w_cursor.coladd > 0)
9026 {
9027 if (mode == BACKSPACE_CHAR)
9028 {
9029 --curwin->w_cursor.coladd;
9030 return TRUE;
9031 }
9032 if (mode == BACKSPACE_WORD)
9033 {
9034 curwin->w_cursor.coladd = 0;
9035 return TRUE;
9036 }
9037 curwin->w_cursor.coladd = 0;
9038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009041 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 */
9043 if (curwin->w_cursor.col == 0)
9044 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009045 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009046 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047#ifdef FEAT_RIGHTLEFT
9048 || revins_on
9049#endif
9050 )
9051 {
9052 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9053 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9054 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009055 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009056 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 }
9058 /*
9059 * In replace mode:
9060 * cc < 0: NL was inserted, delete it
9061 * cc >= 0: NL was replaced, put original characters back
9062 */
9063 cc = -1;
9064 if (State & REPLACE_FLAG)
9065 cc = replace_pop(); /* returns -1 if NL was inserted */
9066 /*
9067 * In replace mode, in the line we started replacing, we only move the
9068 * cursor.
9069 */
9070 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9071 {
9072 dec_cursor();
9073 }
9074 else
9075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 if (!(State & VREPLACE_FLAG)
9077 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 {
9079 temp = gchar_cursor(); /* remember current char */
9080 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009081
9082 /* When "aw" is in 'formatoptions' we must delete the space at
9083 * the end of the line, otherwise the line will be broken
9084 * again when auto-formatting. */
9085 if (has_format_option(FO_AUTO)
9086 && has_format_option(FO_WHITE_PAR))
9087 {
9088 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9089 TRUE);
9090 int len;
9091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009092 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009093 if (len > 0 && ptr[len - 1] == ' ')
9094 ptr[len - 1] = NUL;
9095 }
9096
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009097 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 if (temp == NUL && gchar_cursor() != NUL)
9099 inc_cursor();
9100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
9102 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103
9104 /*
9105 * In REPLACE mode we have to put back the text that was replaced
9106 * by the NL. On the replace stack is first a NUL-terminated
9107 * sequence of characters that were deleted and then the
9108 * characters that NL replaced.
9109 */
9110 if (State & REPLACE_FLAG)
9111 {
9112 /*
9113 * Do the next ins_char() in NORMAL state, to
9114 * prevent ins_char() from replacing characters and
9115 * avoiding showmatch().
9116 */
9117 oldState = State;
9118 State = NORMAL;
9119 /*
9120 * restore characters (blanks) deleted after cursor
9121 */
9122 while (cc > 0)
9123 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009124 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009126 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 cc = replace_pop();
9128 }
9129 /* restore the characters that NL replaced */
9130 replace_pop_ins();
9131 State = oldState;
9132 }
9133 }
9134 did_ai = FALSE;
9135 }
9136 else
9137 {
9138 /*
9139 * Delete character(s) before the cursor.
9140 */
9141#ifdef FEAT_RIGHTLEFT
9142 if (revins_on) /* put cursor on last inserted char */
9143 dec_cursor();
9144#endif
9145 mincol = 0;
9146 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009147 if (mode == BACKSPACE_LINE
9148 && (curbuf->b_p_ai
9149#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009150 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009151#endif
9152 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153#ifdef FEAT_RIGHTLEFT
9154 && !revins_on
9155#endif
9156 )
9157 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009158 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009160 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009162 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 }
9164
9165 /*
9166 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9167 */
9168 if ( mode == BACKSPACE_CHAR
9169 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009170 || ((get_sts_value() != 0
9171#ifdef FEAT_VARTABS
9172 || tabstop_count(curbuf->b_p_vsts_array)
9173#endif
9174 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009175 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 && (*(ml_get_cursor() - 1) == TAB
9177 || (*(ml_get_cursor() - 1) == ' '
9178 && (!*inserted_space_p
9179 || arrow_used))))))
9180 {
9181 int ts;
9182 colnr_T vcol;
9183 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009184 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
9186 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 /* Compute the virtual column where we want to be. Since
9188 * 'showbreak' may get in the way, need to get the last column of
9189 * the previous character. */
9190 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009191 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 dec_cursor();
9193 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9194 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009195#ifdef FEAT_VARTABS
9196 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009197 {
9198 ts = (int)get_sw_value(curbuf);
9199 want_vcol = (want_vcol / ts) * ts;
9200 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009201 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009202 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009203 curbuf->b_p_vsts_array);
9204#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009205 if (p_sta && in_indent)
9206 ts = (int)get_sw_value(curbuf);
9207 else
9208 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211
9212 /* delete characters until we are at or before want_vcol */
9213 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009214 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009215 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216
9217 /* insert extra spaces until we are at want_vcol */
9218 while (vcol < want_vcol)
9219 {
9220 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009221 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9222 && curwin->w_cursor.col < Insstart_orig.col)
9223 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (State & VREPLACE_FLAG)
9226 ins_char(' ');
9227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 {
9229 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009230 if ((State & REPLACE_FLAG))
9231 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 }
9233 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9234 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009235
9236 /* If we are now back where we started delete one character. Can
9237 * happen when using 'sts' and 'linebreak'. */
9238 if (vcol >= start_vcol)
9239 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 }
9241
9242 /*
9243 * Delete upto starting point, start of line or previous word.
9244 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009247 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009249 if (has_mbyte)
9250 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009251 do
9252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009254 if (!revins_on) /* put cursor on char to be deleted */
9255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009257
9258 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009259 /* look multi-byte character class */
9260 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009262 prev_cclass = cclass;
9263 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009265
9266 /* start of word? */
9267 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9268 {
9269 mode = BACKSPACE_WORD_NOT_SPACE;
9270 temp = vim_iswordc(cc);
9271 }
9272 /* end of word? */
9273 else if (mode == BACKSPACE_WORD_NOT_SPACE
9274 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01009275 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009278 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009280 inc_cursor();
9281#ifdef FEAT_RIGHTLEFT
9282 else if (State & REPLACE_FLAG)
9283 dec_cursor();
9284#endif
9285 break;
9286 }
9287 if (State & REPLACE_FLAG)
9288 replace_do_bs(-1);
9289 else
9290 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009291 if (enc_utf8 && p_deco)
9292 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009293 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009294 /*
9295 * If there are combining characters and 'delcombine' is set
9296 * move the cursor back. Don't back up before the base
9297 * character.
9298 */
9299 if (enc_utf8 && p_deco && cpc[0] != NUL)
9300 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009301#ifdef FEAT_RIGHTLEFT
9302 if (revins_chars)
9303 {
9304 revins_chars--;
9305 revins_legal++;
9306 }
9307 if (revins_on && gchar_cursor() == NUL)
9308 break;
9309#endif
9310 }
9311 /* Just a single backspace?: */
9312 if (mode == BACKSPACE_CHAR)
9313 break;
9314 } while (
9315#ifdef FEAT_RIGHTLEFT
9316 revins_on ||
9317#endif
9318 (curwin->w_cursor.col > mincol
9319 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9320 || curwin->w_cursor.col != Insstart_orig.col)));
9321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 did_backspace = TRUE;
9323 }
9324#ifdef FEAT_SMARTINDENT
9325 did_si = FALSE;
9326 can_si = FALSE;
9327 can_si_back = FALSE;
9328#endif
9329 if (curwin->w_cursor.col <= 1)
9330 did_ai = FALSE;
9331 /*
9332 * It's a little strange to put backspaces into the redo
9333 * buffer, but it makes auto-indent a lot easier to deal
9334 * with.
9335 */
9336 AppendCharToRedobuff(c);
9337
9338 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009339 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009340 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009341 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
9343 /* vi behaviour: the cursor moves backward but the character that
9344 * was there remains visible
9345 * Vim behaviour: the cursor moves backward and the character that
9346 * was there is erased from the screen.
9347 * We can emulate the vi behaviour by pretending there is a dollar
9348 * displayed even when there isn't.
9349 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009350 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 dollar_vcol = curwin->w_virtcol;
9352
Bram Moolenaarce3be472008-01-14 19:12:28 +00009353#ifdef FEAT_FOLDING
9354 /* When deleting a char the cursor line must never be in a closed fold.
9355 * E.g., when 'foldmethod' is indent and deleting the first non-white
9356 * char before a Tab. */
9357 if (did_backspace)
9358 foldOpenCursor();
9359#endif
9360
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 return did_backspace;
9362}
9363
9364#ifdef FEAT_MOUSE
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009369 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
9371# ifdef FEAT_GUI
9372 /* When GUI is active, also move/paste when 'mouse' is empty */
9373 if (!gui.in_use)
9374# endif
9375 if (!mouse_has(MOUSE_INSERT))
9376 return;
9377
9378 undisplay_dollar();
9379 tpos = curwin->w_cursor;
9380 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9381 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009382 win_T *new_curwin = curwin;
9383
9384 if (curwin != old_curwin && win_valid(old_curwin))
9385 {
9386 /* Mouse took us to another window. We need to go back to the
9387 * previous one to stop insert there properly. */
9388 curwin = old_curwin;
9389 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009390#ifdef FEAT_JOB_CHANNEL
9391 if (bt_prompt(curbuf))
9392 // Restart Insert mode when re-entering the prompt buffer.
9393 curbuf->b_prompt_insert = 'A';
9394#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009395 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009396 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009397 if (curwin != new_curwin && win_valid(new_curwin))
9398 {
9399 curwin = new_curwin;
9400 curbuf = curwin->w_buffer;
9401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402# ifdef FEAT_CINDENT
9403 can_cindent = TRUE;
9404# endif
9405 }
9406
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 /* redraw status lines (in case another window became active) */
9408 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409}
9410
9411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009412ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009415 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009416# ifdef FEAT_INS_EXPAND
9417 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418# endif
9419
9420 tpos = curwin->w_cursor;
9421
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009422 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 {
9424 int row, col;
9425
9426 row = mouse_row;
9427 col = mouse_col;
9428
9429 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009430 wp = mouse_find_win(&row, &col);
9431 if (wp == NULL)
9432 return;
9433 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 curbuf = curwin->w_buffer;
9435 }
9436 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 undisplay_dollar();
9438
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009439# ifdef FEAT_INS_EXPAND
9440 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009441 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009442# endif
9443 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009444 if (dir == MSCR_DOWN || dir == MSCR_UP)
9445 {
9446 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9447 scroll_redraw(dir,
9448 (long)(curwin->w_botline - curwin->w_topline));
9449 else
9450 scroll_redraw(dir, 3L);
9451 }
9452#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009453 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009454 {
9455 int val, step = 6;
9456
9457 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009458 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009459 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9460 if (val < 0)
9461 val = 0;
9462 gui_do_horiz_scroll(val, TRUE);
9463 }
9464#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009465# ifdef FEAT_INS_EXPAND
9466 did_scroll = TRUE;
9467# endif
9468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 curwin->w_redr_status = TRUE;
9471
9472 curwin = old_curwin;
9473 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009475# ifdef FEAT_INS_EXPAND
9476 /* The popup menu may overlay the window, need to redraw it.
9477 * TODO: Would be more efficient to only redraw the windows that are
9478 * overlapped by the popup menu. */
9479 if (pum_visible() && did_scroll)
9480 {
9481 redraw_all_later(NOT_VALID);
9482 ins_compl_show_pum();
9483 }
9484# endif
9485
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009486 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
9488 start_arrow(&tpos);
9489# ifdef FEAT_CINDENT
9490 can_cindent = TRUE;
9491# endif
9492 }
9493}
9494#endif
9495
Bram Moolenaarec2da362017-01-21 20:04:22 +01009496/*
9497 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9498 * P_PE literally.
9499 * When "drop" is TRUE then consume the text and drop it.
9500 */
9501 int
9502bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9503{
9504 int c;
9505 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9506 int idx = 0;
9507 char_u *end = find_termcode((char_u *)"PE");
9508 int ret_char = -1;
9509 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009510 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009511
9512 /* If the end code is too long we can't detect it, read everything. */
9513 if (STRLEN(end) >= NUMBUFLEN)
9514 end = NULL;
9515 ++no_mapping;
9516 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009517 if (!p_paste)
9518 // Also have the side effects of setting 'paste' to make it work much
9519 // faster.
9520 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009521
Bram Moolenaarec2da362017-01-21 20:04:22 +01009522 for (;;)
9523 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009524 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009525 if (end == NULL && vpeekc() == NUL)
9526 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009527 do
9528 {
9529 c = vgetc();
9530 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9531 if (c == NUL || got_int)
9532 // When CTRL-C was encountered the typeahead will be flushed and we
9533 // won't get the end sequence.
9534 break;
9535
Bram Moolenaarec2da362017-01-21 20:04:22 +01009536 if (has_mbyte)
9537 idx += (*mb_char2bytes)(c, buf + idx);
9538 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009539 buf[idx++] = c;
9540 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009541 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009542 {
9543 if (end[idx] == NUL)
9544 break; /* Found the end of paste code. */
9545 continue;
9546 }
9547 if (!drop)
9548 {
9549 switch (mode)
9550 {
9551 case PASTE_CMDLINE:
9552 put_on_cmdline(buf, idx, TRUE);
9553 break;
9554
9555 case PASTE_EX:
9556 if (gap != NULL && ga_grow(gap, idx) == OK)
9557 {
9558 mch_memmove((char *)gap->ga_data + gap->ga_len,
9559 buf, (size_t)idx);
9560 gap->ga_len += idx;
9561 }
9562 break;
9563
9564 case PASTE_INSERT:
9565 if (stop_arrow() == OK)
9566 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009567 c = buf[0];
9568 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9569 ins_eol(c);
9570 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009571 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009572 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009573 AppendToRedobuffLit(buf, idx);
9574 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009575 }
9576 break;
9577
9578 case PASTE_ONE_CHAR:
9579 if (ret_char == -1)
9580 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01009581 if (has_mbyte)
9582 ret_char = (*mb_ptr2char)(buf);
9583 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009584 ret_char = buf[0];
9585 }
9586 break;
9587 }
9588 }
9589 idx = 0;
9590 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009591
Bram Moolenaarec2da362017-01-21 20:04:22 +01009592 --no_mapping;
9593 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009594 if (!save_paste)
9595 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009596
9597 return ret_char;
9598}
9599
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009600#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009602ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009603{
9604 /* We will be leaving the current window, unless closing another tab. */
9605 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9606 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9607 {
9608 undisplay_dollar();
9609 start_arrow(&curwin->w_cursor);
9610# ifdef FEAT_CINDENT
9611 can_cindent = TRUE;
9612# endif
9613 }
9614
9615 if (c == K_TABLINE)
9616 goto_tabpage(current_tab);
9617 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009618 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009619 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009620 redraw_statuslines(); /* will redraw the tabline when needed */
9621 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009622}
9623#endif
9624
9625#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009627ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 pos_T tpos;
9630
9631 undisplay_dollar();
9632 tpos = curwin->w_cursor;
9633 if (gui_do_scroll())
9634 {
9635 start_arrow(&tpos);
9636# ifdef FEAT_CINDENT
9637 can_cindent = TRUE;
9638# endif
9639 }
9640}
9641
9642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 pos_T tpos;
9646
9647 undisplay_dollar();
9648 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009649 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 {
9651 start_arrow(&tpos);
9652# ifdef FEAT_CINDENT
9653 can_cindent = TRUE;
9654# endif
9655 }
9656}
9657#endif
9658
9659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009660ins_left(
9661 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 pos_T tpos;
9664
9665#ifdef FEAT_FOLDING
9666 if ((fdo_flags & FDO_HOR) && KeyTyped)
9667 foldOpenCursor();
9668#endif
9669 undisplay_dollar();
9670 tpos = curwin->w_cursor;
9671 if (oneleft() == OK)
9672 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009673#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9674 /* Only call start_arrow() when not busy with preediting, it will
9675 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009676 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009677#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009678 {
9679 start_arrow_with_change(&tpos, end_change);
9680 if (!end_change)
9681 AppendCharToRedobuff(K_LEFT);
9682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683#ifdef FEAT_RIGHTLEFT
9684 /* If exit reversed string, position is fixed */
9685 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9686 revins_legal++;
9687 revins_chars++;
9688#endif
9689 }
9690
9691 /*
9692 * if 'whichwrap' set for cursor in insert mode may go to
9693 * previous line
9694 */
9695 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9696 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009697 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 start_arrow(&tpos);
9699 --(curwin->w_cursor.lnum);
9700 coladvance((colnr_T)MAXCOL);
9701 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9702 }
9703 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009704 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009705 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706}
9707
9708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009709ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 pos_T tpos;
9712
9713#ifdef FEAT_FOLDING
9714 if ((fdo_flags & FDO_HOR) && KeyTyped)
9715 foldOpenCursor();
9716#endif
9717 undisplay_dollar();
9718 tpos = curwin->w_cursor;
9719 if (c == K_C_HOME)
9720 curwin->w_cursor.lnum = 1;
9721 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 curwin->w_curswant = 0;
9724 start_arrow(&tpos);
9725}
9726
9727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009728ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729{
9730 pos_T tpos;
9731
9732#ifdef FEAT_FOLDING
9733 if ((fdo_flags & FDO_HOR) && KeyTyped)
9734 foldOpenCursor();
9735#endif
9736 undisplay_dollar();
9737 tpos = curwin->w_cursor;
9738 if (c == K_C_END)
9739 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9740 coladvance((colnr_T)MAXCOL);
9741 curwin->w_curswant = MAXCOL;
9742
9743 start_arrow(&tpos);
9744}
9745
9746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749#ifdef FEAT_FOLDING
9750 if ((fdo_flags & FDO_HOR) && KeyTyped)
9751 foldOpenCursor();
9752#endif
9753 undisplay_dollar();
9754 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9755 {
9756 start_arrow(&curwin->w_cursor);
9757 (void)bck_word(1L, FALSE, FALSE);
9758 curwin->w_set_curswant = TRUE;
9759 }
9760 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009761 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762}
9763
9764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765ins_right(
9766 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
9768#ifdef FEAT_FOLDING
9769 if ((fdo_flags & FDO_HOR) && KeyTyped)
9770 foldOpenCursor();
9771#endif
9772 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009773 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009775 start_arrow_with_change(&curwin->w_cursor, end_change);
9776 if (!end_change)
9777 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 if (virtual_active())
9780 oneright();
9781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009784 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 ++curwin->w_cursor.col;
9787 }
9788
9789#ifdef FEAT_RIGHTLEFT
9790 revins_legal++;
9791 if (revins_chars)
9792 revins_chars--;
9793#endif
9794 }
9795 /* if 'whichwrap' set for cursor in insert mode, may move the
9796 * cursor to the next line */
9797 else if (vim_strchr(p_ww, ']') != NULL
9798 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9799 {
9800 start_arrow(&curwin->w_cursor);
9801 curwin->w_set_curswant = TRUE;
9802 ++curwin->w_cursor.lnum;
9803 curwin->w_cursor.col = 0;
9804 }
9805 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009806 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009807 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808}
9809
9810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009811ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813#ifdef FEAT_FOLDING
9814 if ((fdo_flags & FDO_HOR) && KeyTyped)
9815 foldOpenCursor();
9816#endif
9817 undisplay_dollar();
9818 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9819 || gchar_cursor() != NUL)
9820 {
9821 start_arrow(&curwin->w_cursor);
9822 (void)fwd_word(1L, FALSE, 0);
9823 curwin->w_set_curswant = TRUE;
9824 }
9825 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009826 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827}
9828
9829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009830ins_up(
9831 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832{
9833 pos_T tpos;
9834 linenr_T old_topline = curwin->w_topline;
9835#ifdef FEAT_DIFF
9836 int old_topfill = curwin->w_topfill;
9837#endif
9838
9839 undisplay_dollar();
9840 tpos = curwin->w_cursor;
9841 if (cursor_up(1L, TRUE) == OK)
9842 {
9843 if (startcol)
9844 coladvance(getvcol_nolist(&Insstart));
9845 if (old_topline != curwin->w_topline
9846#ifdef FEAT_DIFF
9847 || old_topfill != curwin->w_topfill
9848#endif
9849 )
9850 redraw_later(VALID);
9851 start_arrow(&tpos);
9852#ifdef FEAT_CINDENT
9853 can_cindent = TRUE;
9854#endif
9855 }
9856 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009857 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858}
9859
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 pos_T tpos;
9864
9865 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009866
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009867 if (mod_mask & MOD_MASK_CTRL)
9868 {
9869 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009870 if (first_tabpage->tp_next != NULL)
9871 {
9872 start_arrow(&curwin->w_cursor);
9873 goto_tabpage(-1);
9874 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009875 return;
9876 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009877
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 tpos = curwin->w_cursor;
9879 if (onepage(BACKWARD, 1L) == OK)
9880 {
9881 start_arrow(&tpos);
9882#ifdef FEAT_CINDENT
9883 can_cindent = TRUE;
9884#endif
9885 }
9886 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009887 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888}
9889
9890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009891ins_down(
9892 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894 pos_T tpos;
9895 linenr_T old_topline = curwin->w_topline;
9896#ifdef FEAT_DIFF
9897 int old_topfill = curwin->w_topfill;
9898#endif
9899
9900 undisplay_dollar();
9901 tpos = curwin->w_cursor;
9902 if (cursor_down(1L, TRUE) == OK)
9903 {
9904 if (startcol)
9905 coladvance(getvcol_nolist(&Insstart));
9906 if (old_topline != curwin->w_topline
9907#ifdef FEAT_DIFF
9908 || old_topfill != curwin->w_topfill
9909#endif
9910 )
9911 redraw_later(VALID);
9912 start_arrow(&tpos);
9913#ifdef FEAT_CINDENT
9914 can_cindent = TRUE;
9915#endif
9916 }
9917 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009918 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919}
9920
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924 pos_T tpos;
9925
9926 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009927
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009928 if (mod_mask & MOD_MASK_CTRL)
9929 {
9930 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009931 if (first_tabpage->tp_next != NULL)
9932 {
9933 start_arrow(&curwin->w_cursor);
9934 goto_tabpage(0);
9935 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009936 return;
9937 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009938
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 tpos = curwin->w_cursor;
9940 if (onepage(FORWARD, 1L) == OK)
9941 {
9942 start_arrow(&tpos);
9943#ifdef FEAT_CINDENT
9944 can_cindent = TRUE;
9945#endif
9946 }
9947 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009948 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951#ifdef FEAT_DND
9952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009953ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
9955 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9956}
9957#endif
9958
9959/*
9960 * Handle TAB in Insert or Replace mode.
9961 * Return TRUE when the TAB needs to be inserted like a normal character.
9962 */
9963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009964ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966 int ind;
9967 int i;
9968 int temp;
9969
9970 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9971 Insstart_blank_vcol = get_nolist_virtcol();
9972 if (echeck_abbr(TAB + ABBR_OFF))
9973 return FALSE;
9974
9975 ind = inindent(0);
9976#ifdef FEAT_CINDENT
9977 if (ind)
9978 can_cindent = FALSE;
9979#endif
9980
9981 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009982 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 */
9984 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009985#ifdef FEAT_VARTABS
9986 && !(p_sta && ind
9987 /* These five lines mean 'tabstop' != 'shiftwidth' */
9988 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
9989 || (tabstop_count(curbuf->b_p_vts_array) == 1
9990 && tabstop_first(curbuf->b_p_vts_array)
9991 != get_sw_value(curbuf))
9992 || (tabstop_count(curbuf->b_p_vts_array) == 0
9993 && curbuf->b_p_ts != get_sw_value(curbuf))))
9994 && tabstop_count(curbuf->b_p_vsts_array) == 0
9995#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009996 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009997#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009998 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 return TRUE;
10000
10001 if (stop_arrow() == FAIL)
10002 return TRUE;
10003
10004 did_ai = FALSE;
10005#ifdef FEAT_SMARTINDENT
10006 did_si = FALSE;
10007 can_si = FALSE;
10008 can_si_back = FALSE;
10009#endif
10010 AppendToRedobuff((char_u *)"\t");
10011
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010012#ifdef FEAT_VARTABS
10013 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10014 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010015 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010016 temp -= get_nolist_virtcol() % temp;
10017 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010018 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010019 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010020 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010021 curbuf->b_p_vsts_array);
10022 else /* otherwise use 'tabstop' */
10023 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10024 curbuf->b_p_vts_array);
10025#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010027 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010028 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10029 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 else /* otherwise use 'tabstop' */
10031 temp = (int)curbuf->b_p_ts;
10032 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
10035 /*
10036 * Insert the first space with ins_char(). It will delete one char in
10037 * replace mode. Insert the rest with ins_str(); it will not delete any
10038 * chars. For VREPLACE mode, we use ins_char() for all characters.
10039 */
10040 ins_char(' ');
10041 while (--temp > 0)
10042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (State & VREPLACE_FLAG)
10044 ins_char(' ');
10045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 {
10047 ins_str((char_u *)" ");
10048 if (State & REPLACE_FLAG) /* no char replaced */
10049 replace_push(NUL);
10050 }
10051 }
10052
10053 /*
10054 * When 'expandtab' not set: Replace spaces by TABs where possible.
10055 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010056#ifdef FEAT_VARTABS
10057 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10058 || get_sts_value() > 0
10059 || (p_sta && ind)))
10060#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010061 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
10064 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 char_u *saved_line = NULL; /* init for GCC */
10066 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 pos_T fpos;
10068 pos_T *cursor;
10069 colnr_T want_vcol, vcol;
10070 int change_col = -1;
10071 int save_list = curwin->w_p_list;
10072
10073 /*
10074 * Get the current line. For VREPLACE mode, don't make real changes
10075 * yet, just work on a copy of the line.
10076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 if (State & VREPLACE_FLAG)
10078 {
10079 pos = curwin->w_cursor;
10080 cursor = &pos;
10081 saved_line = vim_strsave(ml_get_curline());
10082 if (saved_line == NULL)
10083 return FALSE;
10084 ptr = saved_line + pos.col;
10085 }
10086 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
10088 ptr = ml_get_cursor();
10089 cursor = &curwin->w_cursor;
10090 }
10091
10092 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10093 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10094 curwin->w_p_list = FALSE;
10095
10096 /* Find first white before the cursor */
10097 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010098 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
10100 --fpos.col;
10101 --ptr;
10102 }
10103
10104 /* In Replace mode, don't change characters before the insert point. */
10105 if ((State & REPLACE_FLAG)
10106 && fpos.lnum == Insstart.lnum
10107 && fpos.col < Insstart.col)
10108 {
10109 ptr += Insstart.col - fpos.col;
10110 fpos.col = Insstart.col;
10111 }
10112
10113 /* compute virtual column numbers of first white and cursor */
10114 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10115 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10116
Bram Moolenaar597a4222014-06-25 14:39:50 +020010117 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10118 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010119 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010121 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 if (vcol + i > want_vcol)
10123 break;
10124 if (*ptr != TAB)
10125 {
10126 *ptr = TAB;
10127 if (change_col < 0)
10128 {
10129 change_col = fpos.col; /* Column of first change */
10130 /* May have to adjust Insstart */
10131 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10132 Insstart.col = fpos.col;
10133 }
10134 }
10135 ++fpos.col;
10136 ++ptr;
10137 vcol += i;
10138 }
10139
10140 if (change_col >= 0)
10141 {
10142 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010143 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144
10145 /* Skip over the spaces we need. */
10146 while (vcol < want_vcol && *ptr == ' ')
10147 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010148 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 ++ptr;
10150 ++repl_off;
10151 }
10152 if (vcol > want_vcol)
10153 {
10154 /* Must have a char with 'showbreak' just before it. */
10155 --ptr;
10156 --repl_off;
10157 }
10158 fpos.col += repl_off;
10159
10160 /* Delete following spaces. */
10161 i = cursor->col - fpos.col;
10162 if (i > 0)
10163 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010164 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010166 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 for (temp = i; --temp >= 0; )
10168 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010169#ifdef FEAT_TEXT_PROP
10170 curbuf->b_ml.ml_line_len -= i;
10171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010173#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010174 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010175 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010176 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010177 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10178 (char_u *)"\t", 1);
10179 }
10180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 cursor->col -= i;
10182
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 /*
10184 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10185 * backspacing over the changed spacing and then inserting the new
10186 * spacing.
10187 */
10188 if (State & VREPLACE_FLAG)
10189 {
10190 /* Backspace from real cursor to change_col */
10191 backspace_until_column(change_col);
10192
10193 /* Insert each char in saved_line from changed_col to
10194 * ptr-cursor */
10195 ins_bytes_len(saved_line + change_col,
10196 cursor->col - change_col);
10197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 }
10199
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (State & VREPLACE_FLAG)
10201 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 curwin->w_p_list = save_list;
10203 }
10204
10205 return FALSE;
10206}
10207
10208/*
10209 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010210 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 */
10212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010213ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 int i;
10216
10217 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010218 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010220 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 undisplay_dollar();
10222
10223 /*
10224 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10225 * character under the cursor. Only push a NUL on the replace stack,
10226 * nothing to put back when the NL is deleted.
10227 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010228 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 replace_push(NUL);
10230
10231 /*
10232 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10233 * replacing the next line, so we push all of the characters left on the
10234 * line onto the replace stack. This is not done here though, it is done
10235 * in open_line().
10236 */
10237
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010238 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10239 * CTRL-O). */
10240 if (virtual_active() && curwin->w_cursor.coladd > 0)
10241 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010242
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243#ifdef FEAT_RIGHTLEFT
10244# ifdef FEAT_FKMAP
10245 if (p_altkeymap && p_fkmap)
10246 fkmap(NL);
10247# endif
10248 /* NL in reverse insert will always start in the end of
10249 * current line. */
10250 if (revins_on)
10251 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10252#endif
10253
10254 AppendToRedobuff(NL_STR);
10255 i = open_line(FORWARD,
10256#ifdef FEAT_COMMENTS
10257 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10258#endif
10259 0, old_indent);
10260 old_indent = 0;
10261#ifdef FEAT_CINDENT
10262 can_cindent = TRUE;
10263#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010264#ifdef FEAT_FOLDING
10265 /* When inserting a line the cursor line must never be in a closed fold. */
10266 foldOpenCursor();
10267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010269 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270}
10271
10272#ifdef FEAT_DIGRAPHS
10273/*
10274 * Handle digraph in insert mode.
10275 * Returns character still to be inserted, or NUL when nothing remaining to be
10276 * done.
10277 */
10278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010279ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280{
10281 int c;
10282 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010283 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284
10285 pc_status = PC_STATUS_UNSET;
10286 if (redrawing() && !char_avail())
10287 {
10288 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010289 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290
10291 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010292 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293#ifdef FEAT_CMDL_INFO
10294 add_to_showcmd_c(Ctrl_K);
10295#endif
10296 }
10297
10298#ifdef USE_ON_FLY_SCROLL
10299 dont_scroll = TRUE; /* disallow scrolling here */
10300#endif
10301
10302 /* don't map the digraph chars. This also prevents the
10303 * mode message to be deleted when ESC is hit */
10304 ++no_mapping;
10305 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010306 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 --no_mapping;
10308 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010309 if (did_putchar)
10310 /* when the line fits in 'columns' the '?' is at the start of the next
10311 * line and will not be removed by the redraw */
10312 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010313
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 if (IS_SPECIAL(c) || mod_mask) /* special key */
10315 {
10316#ifdef FEAT_CMDL_INFO
10317 clear_showcmd();
10318#endif
10319 insert_special(c, TRUE, FALSE);
10320 return NUL;
10321 }
10322 if (c != ESC)
10323 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010324 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 if (redrawing() && !char_avail())
10326 {
10327 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010328 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329
10330 if (char2cells(c) == 1)
10331 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010332 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010334 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 }
10336#ifdef FEAT_CMDL_INFO
10337 add_to_showcmd_c(c);
10338#endif
10339 }
10340 ++no_mapping;
10341 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010342 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 --no_mapping;
10344 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010345 if (did_putchar)
10346 /* when the line fits in 'columns' the '?' is at the start of the
10347 * next line and will not be removed by a redraw */
10348 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (cc != ESC)
10350 {
10351 AppendToRedobuff((char_u *)CTRL_V_STR);
10352 c = getdigraph(c, cc, TRUE);
10353#ifdef FEAT_CMDL_INFO
10354 clear_showcmd();
10355#endif
10356 return c;
10357 }
10358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359#ifdef FEAT_CMDL_INFO
10360 clear_showcmd();
10361#endif
10362 return NUL;
10363}
10364#endif
10365
10366/*
10367 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10368 * Returns the char to be inserted, or NUL if none found.
10369 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010371ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372{
10373 int c;
10374 int temp;
10375 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010376 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
10378 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10379 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010380 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 return NUL;
10382 }
10383
10384 /* try to advance to the cursor column */
10385 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010386 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 prev_ptr = ptr;
10388 validate_virtcol();
10389 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10390 {
10391 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010392 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394 if ((colnr_T)temp > curwin->w_virtcol)
10395 ptr = prev_ptr;
10396
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010399 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 return c;
10401}
10402
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010403/*
10404 * CTRL-Y or CTRL-E typed in Insert mode.
10405 */
10406 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010407ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010408{
10409 int c = tc;
10410
10411#ifdef FEAT_INS_EXPAND
10412 if (ctrl_x_mode == CTRL_X_SCROLL)
10413 {
10414 if (c == Ctrl_Y)
10415 scrolldown_clamp();
10416 else
10417 scrollup_clamp();
10418 redraw_later(VALID);
10419 }
10420 else
10421#endif
10422 {
10423 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10424 if (c != NUL)
10425 {
10426 long tw_save;
10427
10428 /* The character must be taken literally, insert like it
10429 * was typed after a CTRL-V, and pretend 'textwidth'
10430 * wasn't set. Digits, 'o' and 'x' are special after a
10431 * CTRL-V, don't use it for these. */
10432 if (c < 256 && !isalnum(c))
10433 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10434 tw_save = curbuf->b_p_tw;
10435 curbuf->b_p_tw = -1;
10436 insert_special(c, TRUE, FALSE);
10437 curbuf->b_p_tw = tw_save;
10438#ifdef FEAT_RIGHTLEFT
10439 revins_chars++;
10440 revins_legal++;
10441#endif
10442 c = Ctrl_V; /* pretend CTRL-V is last character */
10443 auto_format(FALSE, TRUE);
10444 }
10445 }
10446 return c;
10447}
10448
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449#ifdef FEAT_SMARTINDENT
10450/*
10451 * Try to do some very smart auto-indenting.
10452 * Used when inserting a "normal" character.
10453 */
10454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010455ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
10457 pos_T *pos, old_pos;
10458 char_u *ptr;
10459 int i;
10460 int temp;
10461
10462 /*
10463 * do some very smart indenting when entering '{' or '}'
10464 */
10465 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10466 {
10467 /*
10468 * for '}' set indent equal to indent of line containing matching '{'
10469 */
10470 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10471 {
10472 old_pos = curwin->w_cursor;
10473 /*
10474 * If the matching '{' has a ')' immediately before it (ignoring
10475 * white-space), then line up with the start of the line
10476 * containing the matching '(' if there is one. This handles the
10477 * case where an "if (..\n..) {" statement continues over multiple
10478 * lines -- webb
10479 */
10480 ptr = ml_get(pos->lnum);
10481 i = pos->col;
10482 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010483 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 ;
10485 curwin->w_cursor.lnum = pos->lnum;
10486 curwin->w_cursor.col = i;
10487 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10488 curwin->w_cursor = *pos;
10489 i = get_indent();
10490 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010492 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 (void)set_indent(i, SIN_CHANGED);
10495 }
10496 else if (curwin->w_cursor.col > 0)
10497 {
10498 /*
10499 * when inserting '{' after "O" reduce indent, but not
10500 * more than indent of previous line
10501 */
10502 temp = TRUE;
10503 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10504 {
10505 old_pos = curwin->w_cursor;
10506 i = get_indent();
10507 while (curwin->w_cursor.lnum > 1)
10508 {
10509 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10510
10511 /* ignore empty lines and lines starting with '#'. */
10512 if (*ptr != '#' && *ptr != NUL)
10513 break;
10514 }
10515 if (get_indent() >= i)
10516 temp = FALSE;
10517 curwin->w_cursor = old_pos;
10518 }
10519 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010520 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 }
10522 }
10523
10524 /*
10525 * set indent of '#' always to 0
10526 */
10527 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10528 {
10529 /* remember current indent for next line */
10530 old_indent = get_indent();
10531 (void)set_indent(0, SIN_CHANGED);
10532 }
10533
10534 /* Adjust ai_col, the char at this position can be deleted. */
10535 if (ai_col > curwin->w_cursor.col)
10536 ai_col = curwin->w_cursor.col;
10537}
10538#endif
10539
10540/*
10541 * Get the value that w_virtcol would have when 'list' is off.
10542 * Unless 'cpo' contains the 'L' flag.
10543 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010544 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010545get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010547 // check validity of cursor in current buffer
10548 if (curwin->w_buffer == NULL
10549 || curwin->w_buffer->b_ml.ml_mfp == NULL
10550 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10551 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10553 return getvcol_nolist(&curwin->w_cursor);
10554 validate_virtcol();
10555 return curwin->w_virtcol;
10556}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010557
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010558#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010559/*
10560 * Handle the InsertCharPre autocommand.
10561 * "c" is the character that was typed.
10562 * Return a pointer to allocated memory with the replacement string.
10563 * Return NULL to continue inserting "c".
10564 */
10565 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010566do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010567{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010568 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010569 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010570 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010571
10572 /* Return quickly when there is nothing to do. */
10573 if (!has_insertcharpre())
10574 return NULL;
10575
Bram Moolenaar704984a2012-06-01 14:57:51 +020010576 if (has_mbyte)
10577 buf[(*mb_char2bytes)(c, buf)] = NUL;
10578 else
Bram Moolenaar704984a2012-06-01 14:57:51 +020010579 {
10580 buf[0] = c;
10581 buf[1] = NUL;
10582 }
10583
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010584 /* Lock the text to avoid weird things from happening. */
10585 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010586 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010587
Bram Moolenaar704984a2012-06-01 14:57:51 +020010588 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010589 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010590 {
10591 /* Get the value of v:char. It may be empty or more than one
10592 * character. Only use it when changed, otherwise continue with the
10593 * original character to avoid breaking autoindent. */
10594 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10595 res = vim_strsave(get_vim_var_str(VV_CHAR));
10596 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010597
10598 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10599 --textlock;
10600
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010601 // Restore the State, it may have been changed.
10602 State = save_State;
10603
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010604 return res;
10605}
10606#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010607
10608/*
10609 * Trigger "event" and take care of fixing undo.
10610 */
10611 static int
10612ins_apply_autocmds(event_T event)
10613{
10614 varnumber_T tick = CHANGEDTICK(curbuf);
10615 int r;
10616
10617 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10618
10619 // If u_savesub() was called then we are not prepared to start
10620 // a new line. Call u_save() with no contents to fix that.
10621 if (tick != CHANGEDTICK(curbuf))
10622 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10623
10624 return r;
10625}