blob: 1b88f6140a0b1aa78506e1f2e6e90967de5d6c36 [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;
153
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static void ins_ctrl_x(void);
155static int has_compl_option(int dict_opt);
156static int ins_compl_accept_char(int c);
157static 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 +0100158static void ins_compl_longest_match(compl_T *match);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ins_compl_del_pum(void);
160static int pum_wanted(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100175# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100176static void ins_compl_add_list(list_T *list);
177static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100178# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100179static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200180static void ins_compl_insert(int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static int ins_compl_key2dir(int c);
182static int ins_compl_pum_key(int c);
183static int ins_compl_key2count(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100184static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100185static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#endif /* FEAT_INS_EXPAND */
188
189#define BACKSPACE_CHAR 1
190#define BACKSPACE_WORD 2
191#define BACKSPACE_WORD_NOT_SPACE 3
192#define BACKSPACE_LINE 4
193
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100194static void ins_redraw(int ready);
195static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200196#ifdef FEAT_JOB_CHANNEL
197static void init_prompt(int cmdchar_todo);
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void undisplay_dollar(void);
200static void insert_special(int, int, int);
201static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
202static void check_auto_format(int);
203static void redo_literal(int c);
204static void start_arrow(pos_T *end_insert_pos);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100205static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000206#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100207static void check_spell_redraw(void);
208static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000209static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000210#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100211static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
212static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void replace_join(int off);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void replace_flush(void);
218static void replace_do_bs(int limit_col);
219static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ins_reg(void);
224static void ins_ctrl_g(void);
225static void ins_ctrl_hat(void);
226static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static int ins_start_select(int c);
231static void ins_insert(int replaceState);
232static void ins_ctrl_o(void);
233static void ins_shift(int c, int lastc);
234static void ins_del(void);
235static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ins_mouse(int c);
238static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000240#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ins_left(int end_change);
244static void ins_home(int c);
245static void ins_end(int c);
246static void ins_s_left(void);
247static void ins_right(int end_change);
248static void ins_s_right(void);
249static void ins_up(int startcol);
250static void ins_pageup(void);
251static void ins_down(int startcol);
252static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static int ins_tab(void);
257static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100265#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100267#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200268static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
270static colnr_T Insstart_textlen; /* length of line when insert started */
271static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100272static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static char_u *last_insert = NULL; /* the text of the previous insert,
275 K_SPECIAL and CSI are escaped */
276static int last_insert_skip; /* nr of chars in front of previous insert */
277static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000278static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280#ifdef FEAT_CINDENT
281static int can_cindent; /* may do cindenting on this line */
282#endif
283
284static int old_indent = 0; /* for ^^D command in insert mode */
285
286#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000287static int revins_on; /* reverse insert mode on */
288static int revins_chars; /* how much to skip after edit */
289static int revins_legal; /* was the last char 'legal'? */
290static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#endif
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static int ins_need_undo; /* call u_save() before inserting a
294 char. Set when edit() is called.
295 after that arrow_used is used. */
296
297static int did_add_space = FALSE; /* auto_format() added an extra space
298 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200299static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
300 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302/*
303 * edit(): Start inserting text.
304 *
305 * "cmdchar" can be:
306 * 'i' normal insert command
307 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100308 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 * 'R' replace command
310 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
311 * but still only one <CR> is inserted. The <Esc> is not used for redo.
312 * 'g' "gI" command.
313 * 'V' "gR" command for Virtual Replace mode.
314 * 'v' "gr" command for single character Virtual Replace mode.
315 *
316 * This function is not called recursively. For CTRL-O commands, it returns
317 * and lets the caller handle the Normal-mode command.
318 *
319 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
320 */
321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100322edit(
323 int cmdchar,
324 int startln, /* if set, insert at start of line */
325 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 int c = 0;
328 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100329 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000330 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 int i;
333 int did_backspace = TRUE; /* previous char was backspace */
334#ifdef FEAT_CINDENT
335 int line_is_white = FALSE; /* line is empty before insert */
336#endif
337 linenr_T old_topline = 0; /* topline before insertion */
338#ifdef FEAT_DIFF
339 int old_topfill = -1;
340#endif
341 int inserted_space = FALSE; /* just inserted a space */
342 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000343 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200344#ifdef FEAT_JOB_CHANNEL
345 int cmdchar_todo = cmdchar;
346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000348 /* Remember whether editing was restarted after CTRL-O. */
349 did_restart_edit = restart_edit;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /* sleep before redrawing, needed for "CTRL-O :" that results in an
352 * error message */
353 check_for_delay(TRUE);
354
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100355 /* set Insstart_orig to Insstart */
356 update_Insstart_orig = TRUE;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef HAVE_SANDBOX
359 /* Don't allow inserting in the sandbox. */
360 if (sandbox != 0)
361 {
362 EMSG(_(e_sandbox));
363 return FALSE;
364 }
365#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000366 /* Don't allow changes in the buffer while editing the cmdline. The
367 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000368 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000369 {
370 EMSG(_(e_secure));
371 return FALSE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000376 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 {
378 EMSG(_(e_secure));
379 return FALSE;
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 ins_compl_clear(); /* clear stuff for CTRL-X mode */
382#endif
383
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384 /*
385 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
386 */
387 if (cmdchar != 'r' && cmdchar != 'v')
388 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100389 pos_T save_cursor = curwin->w_cursor;
390
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100391#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000392 if (cmdchar == 'R')
393 ptr = (char_u *)"r";
394 else if (cmdchar == 'V')
395 ptr = (char_u *)"v";
396 else
397 ptr = (char_u *)"i";
398 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200399 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100400#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200401 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402
Bram Moolenaar097c9922013-05-19 21:15:15 +0200403 /* Make sure the cursor didn't move. Do call check_cursor_col() in
404 * case the text was modified. Since Insert mode was not started yet
405 * a call to check_cursor_col() may move the cursor, especially with
406 * the "A" command, thus set State to avoid that. Also check that the
407 * line number is still valid (lines may have been deleted).
408 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100409 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100410#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200411 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100412#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200413 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100414 {
415 int save_state = State;
416
417 curwin->w_cursor = save_cursor;
418 State = INSERT;
419 check_cursor_col();
420 State = save_state;
421 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200424#ifdef FEAT_CONCEAL
425 /* Check if the cursor line needs redrawing before changing State. If
426 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200427 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200428#endif
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#ifdef FEAT_MOUSE
431 /*
432 * When doing a paste with the middle mouse button, Insstart is set to
433 * where the paste started.
434 */
435 if (where_paste_started.lnum != 0)
436 Insstart = where_paste_started;
437 else
438#endif
439 {
440 Insstart = curwin->w_cursor;
441 if (startln)
442 Insstart.col = 0;
443 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000444 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 Insstart_blank_vcol = MAXCOL;
446 if (!did_ai)
447 ai_col = 0;
448
449 if (cmdchar != NUL && restart_edit == 0)
450 {
451 ResetRedobuff();
452 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (cmdchar == 'V' || cmdchar == 'v')
454 {
455 /* "gR" or "gr" command */
456 AppendCharToRedobuff('g');
457 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
458 }
459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100461 if (cmdchar == K_PS)
462 AppendCharToRedobuff('a');
463 else
464 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (cmdchar == 'g') /* "gI" command */
466 AppendCharToRedobuff('I');
467 else if (cmdchar == 'r') /* "r<CR>" command */
468 count = 1; /* insert only one <CR> */
469 }
470 }
471
472 if (cmdchar == 'R')
473 {
474#ifdef FEAT_FKMAP
475 if (p_fkmap && p_ri)
476 {
477 beep_flush();
478 EMSG(farsi_text_3); /* encoded in Farsi */
479 State = INSERT;
480 }
481 else
482#endif
483 State = REPLACE;
484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 else if (cmdchar == 'V' || cmdchar == 'v')
486 {
487 State = VREPLACE;
488 replaceState = VREPLACE;
489 orig_line_count = curbuf->b_ml.ml_line_count;
490 vr_lines_changed = 1;
491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 else
493 State = INSERT;
494
495 stop_insert_mode = FALSE;
496
497 /*
498 * Need to recompute the cursor position, it might move when the cursor is
499 * on a TAB or special character.
500 */
501 curs_columns(TRUE);
502
503 /*
504 * Enable langmap or IME, indicated by 'iminsert'.
505 * Note that IME may enabled/disabled without us noticing here, thus the
506 * 'iminsert' value may not reflect what is actually used. It is updated
507 * when hitting <Esc>.
508 */
509 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
510 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100511#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
513#endif
514
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515#ifdef FEAT_MOUSE
516 setmouse();
517#endif
518#ifdef FEAT_CMDL_INFO
519 clear_showcmd();
520#endif
521#ifdef FEAT_RIGHTLEFT
522 /* there is no reverse replace mode */
523 revins_on = (State == INSERT && p_ri);
524 if (revins_on)
525 undisplay_dollar();
526 revins_chars = 0;
527 revins_legal = 0;
528 revins_scol = -1;
529#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100530 if (!p_ek)
531 /* Disable bracketed paste mode, we won't recognize the escape
532 * sequences. */
533 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 /*
536 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100537 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
538 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
540 if (restart_edit != 0 && stuff_empty())
541 {
542#ifdef FEAT_MOUSE
543 /*
544 * After a paste we consider text typed to be part of the insert for
545 * the pasted text. You can backspace over the pasted text too.
546 */
547 if (where_paste_started.lnum)
548 arrow_used = FALSE;
549 else
550#endif
551 arrow_used = TRUE;
552 restart_edit = 0;
553
554 /*
555 * If the cursor was after the end-of-line before the CTRL-O and it is
556 * now at the end-of-line, put it after the end-of-line (this is not
557 * correct in very rare cases).
558 * Also do this if curswant is greater than the current virtual
559 * column. Eg after "^O$" or "^O80|".
560 */
561 validate_virtcol();
562 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000563 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 || curwin->w_curswant > curwin->w_virtcol)
565 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
566 {
567 if (ptr[1] == NUL)
568 ++curwin->w_cursor.col;
569#ifdef FEAT_MBYTE
570 else if (has_mbyte)
571 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000572 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (ptr[i] == NUL)
574 curwin->w_cursor.col += i;
575 }
576#endif
577 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000578 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 else
581 arrow_used = FALSE;
582
583 /* we are in insert mode now, don't need to start it anymore */
584 need_start_insertmode = FALSE;
585
586 /* Need to save the line for undo before inserting the first char. */
587 ins_need_undo = TRUE;
588
589#ifdef FEAT_MOUSE
590 where_paste_started.lnum = 0;
591#endif
592#ifdef FEAT_CINDENT
593 can_cindent = TRUE;
594#endif
595#ifdef FEAT_FOLDING
596 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
597 * restarting. */
598 if (!p_im && did_restart_edit == 0)
599 foldOpenCursor();
600#endif
601
602 /*
603 * If 'showmode' is set, show the current (insert/replace/..) mode.
604 * A warning message for changing a readonly file is given here, before
605 * actually changing anything. It's put after the mode, if any.
606 */
607 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000608 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 i = showmode();
610
611 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000612 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614#ifdef CURSOR_SHAPE
615 ui_cursor_shape(); /* may show different cursor shape */
616#endif
617#ifdef FEAT_DIGRAPHS
618 do_digraph(-1); /* clear digraphs */
619#endif
620
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000621 /*
622 * Get the current length of the redo buffer, those characters have to be
623 * skipped if we want to get to the inserted characters.
624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 ptr = get_inserted();
626 if (ptr == NULL)
627 new_insert_skip = 0;
628 else
629 {
630 new_insert_skip = (int)STRLEN(ptr);
631 vim_free(ptr);
632 }
633
634 old_indent = 0;
635
636 /*
637 * Main loop in Insert mode: repeat until Insert mode is left.
638 */
639 for (;;)
640 {
641#ifdef FEAT_RIGHTLEFT
642 if (!revins_legal)
643 revins_scol = -1; /* reset on illegal motions */
644 else
645 revins_legal = 0;
646#endif
647 if (arrow_used) /* don't repeat insert when arrow key used */
648 count = 0;
649
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100650 if (update_Insstart_orig)
651 Insstart_orig = Insstart;
652
Bram Moolenaar00672e12016-06-26 18:38:13 +0200653 if (stop_insert_mode
654#ifdef FEAT_INS_EXPAND
655 && !pum_visible()
656#endif
657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
659 /* ":stopinsert" used or 'insertmode' reset */
660 count = 0;
661 goto doESCkey;
662 }
663
664 /* set curwin->w_curswant for next K_DOWN or K_UP */
665 if (!arrow_used)
666 curwin->w_set_curswant = TRUE;
667
668 /* If there is no typeahead may check for timestamps (e.g., for when a
669 * menu invoked a shell command). */
670 if (stuff_empty())
671 {
672 did_check_timestamps = FALSE;
673 if (need_check_timestamps)
674 check_timestamps(FALSE);
675 }
676
677 /*
678 * When emsg() was called msg_scroll will have been set.
679 */
680 msg_scroll = FALSE;
681
682#ifdef FEAT_GUI
683 /* When 'mousefocus' is set a mouse movement may have taken us to
684 * another window. "need_mouse_correct" may then be set because of an
685 * autocommand. */
686 if (need_mouse_correct)
687 gui_mouse_correct();
688#endif
689
690#ifdef FEAT_FOLDING
691 /* Open fold at the cursor line, according to 'foldopen'. */
692 if (fdo_flags & FDO_INSERT)
693 foldOpenCursor();
694 /* Close folds where the cursor isn't, according to 'foldclose' */
695 if (!char_avail())
696 foldCheckClose();
697#endif
698
Bram Moolenaarf2732452018-06-03 14:47:35 +0200699#ifdef FEAT_JOB_CHANNEL
700 if (bt_prompt(curbuf))
701 {
702 init_prompt(cmdchar_todo);
703 cmdchar_todo = NUL;
704 }
705#endif
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 /*
708 * If we inserted a character at the last position of the last line in
709 * the window, scroll the window one line up. This avoids an extra
710 * redraw.
711 * This is detected when the cursor column is smaller after inserting
712 * something.
713 * Don't do this when the topline changed already, it has
714 * already been adjusted (by insertchar() calling open_line())).
715 */
716 if (curbuf->b_mod_set
717 && curwin->w_p_wrap
718 && !did_backspace
719 && curwin->w_topline == old_topline
720#ifdef FEAT_DIFF
721 && curwin->w_topfill == old_topfill
722#endif
723 )
724 {
725 mincol = curwin->w_wcol;
726 validate_cursor_col();
727
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200728 if (
729#ifdef FEAT_VARTABS
730 (int)curwin->w_wcol < mincol - tabstop_at(
731 get_nolist_virtcol(), curbuf->b_p_ts,
732 curbuf->b_p_vts_array)
733#else
734 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 && curwin->w_wrow == W_WINROW(curwin)
737 + curwin->w_height - 1 - p_so
738 && (curwin->w_cursor.lnum != curwin->w_topline
739#ifdef FEAT_DIFF
740 || curwin->w_topfill > 0
741#endif
742 ))
743 {
744#ifdef FEAT_DIFF
745 if (curwin->w_topfill > 0)
746 --curwin->w_topfill;
747 else
748#endif
749#ifdef FEAT_FOLDING
750 if (hasFolding(curwin->w_topline, NULL, &old_topline))
751 set_topline(curwin, old_topline + 1);
752 else
753#endif
754 set_topline(curwin, curwin->w_topline + 1);
755 }
756 }
757
758 /* May need to adjust w_topline to show the cursor. */
759 update_topline();
760
761 did_backspace = FALSE;
762
763 validate_cursor(); /* may set must_redraw */
764
765 /*
766 * Redraw the display when no characters are waiting.
767 * Also shows mode, ruler and positions cursor.
768 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000769 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (curwin->w_p_scb)
772 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
Bram Moolenaar860cae12010-06-05 23:22:07 +0200774 if (curwin->w_p_crb)
775 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 update_curswant();
777 old_topline = curwin->w_topline;
778#ifdef FEAT_DIFF
779 old_topfill = curwin->w_topfill;
780#endif
781
782#ifdef USE_ON_FLY_SCROLL
783 dont_scroll = FALSE; /* allow scrolling here */
784#endif
785
786 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100787 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100789 if (c != K_CURSORHOLD)
790 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200791
792 /* After using CTRL-G U the next cursor key will not break undo. */
793 if (dont_sync_undo == MAYBE)
794 dont_sync_undo = TRUE;
795 else
796 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100797 if (cmdchar == K_PS)
798 /* Got here from normal mode when bracketed paste started. */
799 c = K_PS;
800 else
801 do
802 {
803 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200804
805 if (stop_insert_mode)
806 {
807 // Insert mode ended, possibly from a callback.
808 count = 0;
809 nomove = TRUE;
810 goto doESCkey;
811 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100812 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000814 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
815 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#ifdef FEAT_RIGHTLEFT
818 if (p_hkmap && KeyTyped)
819 c = hkmap(c); /* Hebrew mode mapping */
820#endif
821#ifdef FEAT_FKMAP
822 if (p_fkmap && KeyTyped)
823 c = fkmap(c); /* Farsi mode mapping */
824#endif
825
826#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 /*
828 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000829 * and the cursor is still in the completed word. Only when there is
830 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000831 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000832 if (compl_started
833 && pum_wanted()
834 && curwin->w_cursor.col >= compl_col
835 && (compl_shown_match == NULL
836 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000837 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000838 /* BS: Delete one character from "compl_leader". */
839 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000840 && curwin->w_cursor.col > compl_col
841 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 continue;
843
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 /* When no match was selected or it was edited. */
845 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000846 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000847 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000848 * "compl_leader". Except when at the original match and
849 * there is nothing to add, CTRL-L works like CTRL-P then. */
850 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100851 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000852 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000853 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000854 {
855 ins_compl_addfrommatch();
856 continue;
857 }
858
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000859 /* A non-white character that fits in with the current
860 * completion: Add to "compl_leader". */
861 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000862 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100863#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100864 /* Trigger InsertCharPre. */
865 char_u *str = do_insert_char_pre(c);
866 char_u *p;
867
868 if (str != NULL)
869 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100870 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100871 ins_compl_addleader(PTR2CHAR(p));
872 vim_free(str);
873 }
874 else
875#endif
876 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000877 continue;
878 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000879
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000880 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000881 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200882 if ((c == Ctrl_Y || (compl_enter_selects
883 && (c == CAR || c == K_KENTER || c == NL)))
884 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000885 {
886 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200887 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000888 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000889 }
890 }
891
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
893 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000894 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000895 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000896 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#endif
898
Bram Moolenaar488c6512005-08-11 20:09:58 +0000899 /* CTRL-\ CTRL-N goes to Normal mode,
900 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
901 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (c == Ctrl_BSL)
903 {
904 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000905 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 ++no_mapping;
907 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000908 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 --no_mapping;
910 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000913 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 vungetc(c);
915 c = Ctrl_BSL;
916 }
917 else if (c == Ctrl_G && p_im)
918 continue;
919 else
920 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000921 if (c == Ctrl_O)
922 {
923 ins_ctrl_o();
924 ins_at_eol = FALSE; /* cursor keeps its column */
925 nomove = TRUE;
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 count = 0;
928 goto doESCkey;
929 }
930 }
931
932#ifdef FEAT_DIGRAPHS
933 c = do_digraph(c);
934#endif
935
936#ifdef FEAT_INS_EXPAND
937 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
938 goto docomplete;
939#endif
940 if (c == Ctrl_V || c == Ctrl_Q)
941 {
942 ins_ctrl_v();
943 c = Ctrl_V; /* pretend CTRL-V is last typed character */
944 continue;
945 }
946
947#ifdef FEAT_CINDENT
948 if (cindent_on()
949# ifdef FEAT_INS_EXPAND
950 && ctrl_x_mode == 0
951# endif
952 )
953 {
954 /* A key name preceded by a bang means this key is not to be
955 * inserted. Skip ahead to the re-indenting below.
956 * A key name preceded by a star means that indenting has to be
957 * done before inserting the key. */
958 line_is_white = inindent(0);
959 if (in_cinkeys(c, '!', line_is_white))
960 goto force_cindent;
961 if (can_cindent && in_cinkeys(c, '*', line_is_white)
962 && stop_arrow() == OK)
963 do_c_expr_indent();
964 }
965#endif
966
967#ifdef FEAT_RIGHTLEFT
968 if (curwin->w_p_rl)
969 switch (c)
970 {
971 case K_LEFT: c = K_RIGHT; break;
972 case K_S_LEFT: c = K_S_RIGHT; break;
973 case K_C_LEFT: c = K_C_RIGHT; break;
974 case K_RIGHT: c = K_LEFT; break;
975 case K_S_RIGHT: c = K_S_LEFT; break;
976 case K_C_RIGHT: c = K_C_LEFT; break;
977 }
978#endif
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /*
981 * If 'keymodel' contains "startsel", may start selection. If it
982 * does, a CTRL-O and c will be stuffed, we need to get these
983 * characters.
984 */
985 if (ins_start_select(c))
986 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /*
989 * The big switch to handle a character in insert mode.
990 */
991 switch (c)
992 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000993 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (echeck_abbr(ESC + ABBR_OFF))
995 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200996 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#ifdef FEAT_CMDWIN
1000 if (c == Ctrl_C && cmdwin_type != 0)
1001 {
1002 /* Close the cmdline window. */
1003 cmdwin_result = K_IGNORE;
1004 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001005 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 goto doESCkey;
1007 }
1008#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001009#ifdef FEAT_JOB_CHANNEL
1010 if (c == Ctrl_C && bt_prompt(curbuf))
1011 {
1012 if (invoke_prompt_interrupt())
1013 {
1014 if (!bt_prompt(curbuf))
1015 // buffer changed to a non-prompt buffer, get out of
1016 // Insert mode
1017 goto doESCkey;
1018 break;
1019 }
1020 }
1021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023#ifdef UNIX
1024do_intr:
1025#endif
1026 /* when 'insertmode' set, and not halfway a mapping, don't leave
1027 * Insert mode */
1028 if (goto_im())
1029 {
1030 if (got_int)
1031 {
1032 (void)vgetc(); /* flush all buffers */
1033 got_int = FALSE;
1034 }
1035 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001036 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 break;
1038 }
1039doESCkey:
1040 /*
1041 * This is the ONLY return from edit()!
1042 */
1043 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1044 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001045 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 o_lnum = curwin->w_cursor.lnum;
1047
Bram Moolenaar488c6512005-08-11 20:09:58 +00001048 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001049 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001050 // When CTRL-C was typed got_int will be set, with the result
1051 // that the autocommands won't be executed. When mapped got_int
1052 // is not set, but let's keep the behavior the same.
1053 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001054 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001055 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 continue;
1059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case Ctrl_Z: /* suspend when 'insertmode' set */
1061 if (!p_im)
1062 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001063 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001064#ifdef CURSOR_SHAPE
1065 ui_cursor_shape(); /* may need to update cursor shape */
1066#endif
1067 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068
1069 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001070#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001071 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 goto docomplete;
1073#endif
1074 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1075 break;
1076 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001077
1078#ifdef FEAT_VIRTUALEDIT
1079 /* don't move the cursor left when 'virtualedit' has "onemore". */
1080 if (ve_flags & VE_ONEMORE)
1081 {
1082 ins_at_eol = FALSE;
1083 nomove = TRUE;
1084 }
1085#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 count = 0;
1087 goto doESCkey;
1088
Bram Moolenaar572cb562005-08-05 21:35:02 +00001089 case K_INS: /* toggle insert/replace mode */
1090 case K_KINS:
1091 ins_insert(replaceState);
1092 break;
1093
1094 case K_SELECT: /* end of Select mode mapping - ignore */
1095 break;
1096
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case K_HELP: /* Help key works like <ESC> <Help> */
1098 case K_F1:
1099 case K_XF1:
1100 stuffcharReadbuff(K_HELP);
1101 if (p_im)
1102 need_start_insertmode = TRUE;
1103 goto doESCkey;
1104
1105#ifdef FEAT_NETBEANS_INTG
1106 case K_F21: /* NetBeans command */
1107 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001108 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 --no_mapping;
1110 netbeans_keycommand(i);
1111 break;
1112#endif
1113
1114 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 case NUL:
1116 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 /* For ^@ the trailing ESC will end the insert, unless there is an
1118 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1120 && c != Ctrl_A && !p_im)
1121 goto doESCkey; /* quit insert mode */
1122 inserted_space = FALSE;
1123 break;
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 ins_reg();
1127 auto_format(FALSE, TRUE);
1128 inserted_space = FALSE;
1129 break;
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ins_ctrl_g();
1133 break;
1134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case Ctrl_HAT: /* switch input mode and/or langmap */
1136 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 break;
1138
1139#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (!p_ari)
1142 goto normalchar;
1143 ins_ctrl_();
1144 break;
1145#endif
1146
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001147 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1149 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1150 goto docomplete;
1151#endif
1152 /* FALLTHROUGH */
1153
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001154 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155# ifdef FEAT_INS_EXPAND
1156 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1157 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 if (has_compl_option(FALSE))
1159 goto docomplete;
1160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162# endif
1163 ins_shift(c, lastc);
1164 auto_format(FALSE, TRUE);
1165 inserted_space = FALSE;
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 case K_KDEL:
1170 ins_del();
1171 auto_format(FALSE, TRUE);
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case Ctrl_H:
1176 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1177 auto_format(FALSE, TRUE);
1178 break;
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001181#ifdef FEAT_JOB_CHANNEL
1182 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1183 {
1184 // In a prompt window CTRL-W is used for window commands.
1185 // Use Shift-CTRL-W to delete a word.
1186 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001187 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001188 nomove = TRUE;
1189 count = 0;
1190 goto doESCkey;
1191 }
1192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1194 auto_format(FALSE, TRUE);
1195 break;
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001198# ifdef FEAT_COMPL_FUNC
1199 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001201 goto docomplete;
1202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1204 auto_format(FALSE, TRUE);
1205 inserted_space = FALSE;
1206 break;
1207
1208#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_LEFTMOUSE_NM:
1211 case K_LEFTDRAG:
1212 case K_LEFTRELEASE:
1213 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001214 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 case K_MIDDLEMOUSE:
1216 case K_MIDDLEDRAG:
1217 case K_MIDDLERELEASE:
1218 case K_RIGHTMOUSE:
1219 case K_RIGHTDRAG:
1220 case K_RIGHTRELEASE:
1221 case K_X1MOUSE:
1222 case K_X1DRAG:
1223 case K_X1RELEASE:
1224 case K_X2MOUSE:
1225 case K_X2DRAG:
1226 case K_X2RELEASE:
1227 ins_mouse(c);
1228 break;
1229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001230 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001231 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 break;
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001235 ins_mousescroll(MSCR_UP);
1236 break;
1237
1238 case K_MOUSELEFT: /* Scroll wheel left */
1239 ins_mousescroll(MSCR_LEFT);
1240 break;
1241
1242 case K_MOUSERIGHT: /* Scroll wheel right */
1243 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 break;
1245#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001246 case K_PS:
1247 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1248 if (cmdchar == K_PS)
1249 /* invoked from normal mode, bail out */
1250 goto doESCkey;
1251 break;
1252 case K_PE:
1253 /* Got K_PE without K_PS, ignore. */
1254 break;
1255
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001256#ifdef FEAT_GUI_TABLINE
1257 case K_TABLINE:
1258 case K_TABMENU:
1259 ins_tabline(c);
1260 break;
1261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 break;
1265
Bram Moolenaar754b5602006-02-09 23:53:20 +00001266 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001267 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001268 did_cursorhold = TRUE;
1269 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001270
Bram Moolenaar4770d092006-01-12 23:22:24 +00001271#ifdef FEAT_GUI_W32
1272 /* On Win32 ignore <M-F4>, we get it when closing the window was
1273 * cancelled. */
1274 case K_F4:
1275 if (mod_mask != MOD_MASK_ALT)
1276 goto normalchar;
1277 break;
1278#endif
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280#ifdef FEAT_GUI
1281 case K_VER_SCROLLBAR:
1282 ins_scroll();
1283 break;
1284
1285 case K_HOR_SCROLLBAR:
1286 ins_horscroll();
1287 break;
1288#endif
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 case K_S_HOME:
1293 case K_C_HOME:
1294 ins_home(c);
1295 break;
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 case K_S_END:
1300 case K_C_END:
1301 ins_end(c);
1302 break;
1303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001305 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1306 ins_s_left();
1307 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001308 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 case K_C_LEFT:
1313 ins_s_left();
1314 break;
1315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001316 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001317 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1318 ins_s_right();
1319 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001320 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 break;
1322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001323 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 case K_C_RIGHT:
1325 ins_s_right();
1326 break;
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001329#ifdef FEAT_INS_EXPAND
1330 if (pum_visible())
1331 goto docomplete;
1332#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001333 if (mod_mask & MOD_MASK_SHIFT)
1334 ins_pageup();
1335 else
1336 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 break;
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 case K_PAGEUP:
1341 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001342#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001343 if (pum_visible())
1344 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 ins_pageup();
1347 break;
1348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001350#ifdef FEAT_INS_EXPAND
1351 if (pum_visible())
1352 goto docomplete;
1353#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001354 if (mod_mask & MOD_MASK_SHIFT)
1355 ins_pagedown();
1356 else
1357 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 break;
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 case K_PAGEDOWN:
1362 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001363#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001364 if (pum_visible())
1365 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ins_pagedown();
1368 break;
1369
1370#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ins_drop();
1373 break;
1374#endif
1375
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001376 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 c = TAB;
1378 /* FALLTHROUGH */
1379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001380 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1382 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1383 goto docomplete;
1384#endif
1385 inserted_space = FALSE;
1386 if (ins_tab())
1387 goto normalchar; /* insert TAB as a normal char */
1388 auto_format(FALSE, TRUE);
1389 break;
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 c = CAR;
1393 /* FALLTHROUGH */
1394 case CAR:
1395 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001396#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /* In a quickfix window a <CR> jumps to the error under the
1398 * cursor. */
1399 if (bt_quickfix(curbuf) && c == CAR)
1400 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001401 if (curwin->w_llist_ref == NULL) /* quickfix window */
1402 do_cmdline_cmd((char_u *)".cc");
1403 else /* location list window */
1404 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 break;
1406 }
1407#endif
1408#ifdef FEAT_CMDWIN
1409 if (cmdwin_type != 0)
1410 {
1411 /* Execute the command in the cmdline window. */
1412 cmdwin_result = CAR;
1413 goto doESCkey;
1414 }
1415#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001416#ifdef FEAT_JOB_CHANNEL
1417 if (bt_prompt(curbuf))
1418 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001419 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001420 if (!bt_prompt(curbuf))
1421 // buffer changed to a non-prompt buffer, get out of
1422 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001423 goto doESCkey;
1424 break;
1425 }
1426#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001427 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 goto doESCkey; /* out of memory */
1429 auto_format(FALSE, FALSE);
1430 inserted_space = FALSE;
1431 break;
1432
Bram Moolenaar860cae12010-06-05 23:22:07 +02001433#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001434 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435# ifdef FEAT_INS_EXPAND
1436 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1437 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001438 if (has_compl_option(TRUE))
1439 goto docomplete;
1440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442# endif
1443# ifdef FEAT_DIGRAPHS
1444 c = ins_digraph();
1445 if (c == NUL)
1446 break;
1447# endif
1448 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001452 case Ctrl_X: /* Enter CTRL-X mode */
1453 ins_ctrl_x();
1454 break;
1455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (ctrl_x_mode != CTRL_X_TAGS)
1458 goto normalchar;
1459 goto docomplete;
1460
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001461 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (ctrl_x_mode != CTRL_X_FILES)
1463 goto normalchar;
1464 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001465
1466 case 's': /* Spelling completion after ^X */
1467 case Ctrl_S:
1468 if (ctrl_x_mode != CTRL_X_SPELL)
1469 goto normalchar;
1470 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#endif
1472
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001473 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#ifdef FEAT_INS_EXPAND
1475 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1476#endif
1477 {
1478 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1479 if (p_im)
1480 {
1481 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1482 break;
1483 goto doESCkey;
1484 }
1485 goto normalchar;
1486 }
1487#ifdef FEAT_INS_EXPAND
1488 /* FALLTHROUGH */
1489
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001490 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 case Ctrl_N:
1492 /* if 'complete' is empty then plain ^P is no longer special,
1493 * but it is under other ^X modes */
1494 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001495 && (ctrl_x_mode == CTRL_X_NORMAL
1496 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001497 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 goto normalchar;
1499
1500docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001501 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001502#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001503 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001504#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001505 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001506 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001507#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001508 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001509#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001510 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 break;
1512#endif /* FEAT_INS_EXPAND */
1513
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001514 case Ctrl_Y: /* copy from previous line or scroll down */
1515 case Ctrl_E: /* copy from next line or scroll up */
1516 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 break;
1518
1519 default:
1520#ifdef UNIX
1521 if (c == intr_char) /* special interrupt char */
1522 goto do_intr;
1523#endif
1524
Bram Moolenaare659c952011-05-19 17:25:41 +02001525normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001527 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001529#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001530 if (!p_paste)
1531 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001532 /* Trigger InsertCharPre. */
1533 char_u *str = do_insert_char_pre(c);
1534 char_u *p;
1535
1536 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001537 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001538 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001539 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001540 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001541 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001542 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 c = PTR2CHAR(p);
1544 if (c == CAR || c == K_KENTER || c == NL)
1545 ins_eol(c);
1546 else
1547 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001549 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 vim_free(str);
1552 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 }
1554
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001555 /* If the new value is already inserted or an empty string
1556 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001557 if (c == NUL)
1558 break;
1559 }
1560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#ifdef FEAT_SMARTINDENT
1562 /* Try to perform smart-indenting. */
1563 ins_try_si(c);
1564#endif
1565
1566 if (c == ' ')
1567 {
1568 inserted_space = TRUE;
1569#ifdef FEAT_CINDENT
1570 if (inindent(0))
1571 can_cindent = FALSE;
1572#endif
1573 if (Insstart_blank_vcol == MAXCOL
1574 && curwin->w_cursor.lnum == Insstart.lnum)
1575 Insstart_blank_vcol = get_nolist_virtcol();
1576 }
1577
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001578 /* Insert a normal character and check for abbreviations on a
1579 * special character. Let CTRL-] expand abbreviations without
1580 * inserting it. */
1581 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582#ifdef FEAT_MBYTE
1583 /* Add ABBR_OFF for characters above 0x100, this is
1584 * what check_abbr() expects. */
1585 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1586#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001587 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 insert_special(c, FALSE, FALSE);
1590#ifdef FEAT_RIGHTLEFT
1591 revins_legal++;
1592 revins_chars++;
1593#endif
1594 }
1595
1596 auto_format(FALSE, TRUE);
1597
1598#ifdef FEAT_FOLDING
1599 /* When inserting a character the cursor line must never be in a
1600 * closed fold. */
1601 foldOpenCursor();
1602#endif
1603 break;
1604 } /* end of switch (c) */
1605
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001606 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001607 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001608#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001609 /* but not in CTRL-X mode, a script can't restore the state */
1610 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001611#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001612 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001613 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 /* If the cursor was moved we didn't just insert a space */
1616 if (arrow_used)
1617 inserted_space = FALSE;
1618
1619#ifdef FEAT_CINDENT
1620 if (can_cindent && cindent_on()
1621# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001622 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623# endif
1624 )
1625 {
1626force_cindent:
1627 /*
1628 * Indent now if a key was typed that is in 'cinkeys'.
1629 */
1630 if (in_cinkeys(c, ' ', line_is_white))
1631 {
1632 if (stop_arrow() == OK)
1633 /* re-indent the current line */
1634 do_c_expr_indent();
1635 }
1636 }
1637#endif /* FEAT_CINDENT */
1638
1639 } /* for (;;) */
1640 /* NOTREACHED */
1641}
1642
1643/*
1644 * Redraw for Insert mode.
1645 * This is postponed until getting the next character to make '$' in the 'cpo'
1646 * option work correctly.
1647 * Only redraw when there are no characters available. This speeds up
1648 * inserting sequences of characters (e.g., for CTRL-R).
1649 */
1650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001651ins_redraw(
1652 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001654#ifdef FEAT_CONCEAL
1655 linenr_T conceal_old_cursor_line = 0;
1656 linenr_T conceal_new_cursor_line = 0;
1657 int conceal_update_lines = FALSE;
1658#endif
1659
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660 if (char_avail())
1661 return;
1662
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1665 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 if (ready && (has_cursormovedI()
1667# if defined(FEAT_CONCEAL)
1668 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001669# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001671 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001673 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001674# endif
1675 )
1676 {
1677# ifdef FEAT_SYN_HL
1678 /* Need to update the screen first, to make sure syntax
1679 * highlighting is correct after making a change (e.g., inserting
1680 * a "(". The autocommand may also require a redraw, so it's done
1681 * again below, unfortunately. */
1682 if (syntax_present(curwin) && must_redraw)
1683 update_screen(0);
1684# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001686 {
1687 /* Make sure curswant is correct, an autocommand may call
1688 * getcurpos(). */
1689 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001690 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001691 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001692# ifdef FEAT_CONCEAL
1693 if (curwin->w_p_cole > 0)
1694 {
1695 conceal_old_cursor_line = last_cursormoved.lnum;
1696 conceal_new_cursor_line = curwin->w_cursor.lnum;
1697 conceal_update_lines = TRUE;
1698 }
1699# endif
1700 last_cursormoved = curwin->w_cursor;
1701 }
1702#endif
1703
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001704 /* Trigger TextChangedI if b_changedtick differs. */
1705 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001706 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001707#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001708 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001710 )
1711 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001712 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001713 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001714
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001715 // save and restore curwin and curbuf, in case the autocmd changes them
1716 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001718 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001719 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001720 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1721 u_save(curwin->w_cursor.lnum,
1722 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001724
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001725#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001726 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1727 * TextChangedI will need to trigger for backwards compatibility, thus use
1728 * different b_last_changedtick* variables. */
1729 if (ready && has_textchangedP()
1730 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1731 && pum_visible())
1732 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001733 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001734 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001735
1736 // save and restore curwin and curbuf, in case the autocmd changes them
1737 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001738 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001739 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001740 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001741 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1742 u_save(curwin->w_cursor.lnum,
1743 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001744 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001745#endif
1746
1747 if (must_redraw)
1748 update_screen(0);
1749 else if (clear_cmdline || redraw_cmdline)
1750 showmode(); /* clear cmdline and show mode */
1751# if defined(FEAT_CONCEAL)
1752 if ((conceal_update_lines
1753 && (conceal_old_cursor_line != conceal_new_cursor_line
1754 || conceal_cursor_line(curwin)))
1755 || need_cursor_line_redraw)
1756 {
1757 if (conceal_old_cursor_line != conceal_new_cursor_line)
1758 update_single_line(curwin, conceal_old_cursor_line);
1759 update_single_line(curwin, conceal_new_cursor_line == 0
1760 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1761 curwin->w_valid &= ~VALID_CROW;
1762 }
1763# endif
1764 showruler(FALSE);
1765 setcursor();
1766 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
1769/*
1770 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1771 */
1772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001776 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001779 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 did_putchar = TRUE;
1785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1787
1788#ifdef FEAT_CMDL_INFO
1789 add_to_showcmd_c(Ctrl_V);
1790#endif
1791
1792 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001793 if (did_putchar)
1794 /* when the line fits in 'columns' the '^' is at the start of the next
1795 * line and will not removed by the redraw */
1796 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#ifdef FEAT_CMDL_INFO
1798 clear_showcmd();
1799#endif
1800 insert_special(c, FALSE, TRUE);
1801#ifdef FEAT_RIGHTLEFT
1802 revins_chars++;
1803 revins_legal++;
1804#endif
1805}
1806
1807/*
1808 * Put a character directly onto the screen. It's not stored in a buffer.
1809 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1810 */
1811static int pc_status;
1812#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1813#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1814#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1815#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817static int pc_attr;
1818static int pc_row;
1819static int pc_col;
1820
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int attr;
1825
1826 if (ScreenLines != NULL)
1827 {
1828 update_topline(); /* just in case w_topline isn't valid */
1829 validate_cursor();
1830 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001831 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 else
1833 attr = 0;
1834 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001835 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1837 pc_status = PC_STATUS_UNSET;
1838#endif
1839#ifdef FEAT_RIGHTLEFT
1840 if (curwin->w_p_rl)
1841 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001842 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843# ifdef FEAT_MBYTE
1844 if (has_mbyte)
1845 {
1846 int fix_col = mb_fix_col(pc_col, pc_row);
1847
1848 if (fix_col != pc_col)
1849 {
1850 screen_putchar(' ', pc_row, fix_col, attr);
1851 --curwin->w_wcol;
1852 pc_status = PC_STATUS_RIGHT;
1853 }
1854 }
1855# endif
1856 }
1857 else
1858#endif
1859 {
1860 pc_col += curwin->w_wcol;
1861#ifdef FEAT_MBYTE
1862 if (mb_lefthalve(pc_row, pc_col))
1863 pc_status = PC_STATUS_LEFT;
1864#endif
1865 }
1866
1867 /* save the character to be able to put it back */
1868#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1869 if (pc_status == PC_STATUS_UNSET)
1870#endif
1871 {
1872 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1873 pc_status = PC_STATUS_SET;
1874 }
1875 screen_putchar(c, pc_row, pc_col, attr);
1876 }
1877}
1878
Bram Moolenaarf2732452018-06-03 14:47:35 +02001879#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1880/*
1881 * Return the effective prompt for the current buffer.
1882 */
1883 char_u *
1884prompt_text(void)
1885{
1886 if (curbuf->b_prompt_text == NULL)
1887 return (char_u *)"% ";
1888 return curbuf->b_prompt_text;
1889}
1890
1891/*
1892 * Prepare for prompt mode: Make sure the last line has the prompt text.
1893 * Move the cursor to this line.
1894 */
1895 static void
1896init_prompt(int cmdchar_todo)
1897{
1898 char_u *prompt = prompt_text();
1899 char_u *text;
1900
1901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1902 text = ml_get_curline();
1903 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1904 {
1905 // prompt is missing, insert it or append a line with it
1906 if (*text == NUL)
1907 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1908 else
1909 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1910 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1911 coladvance((colnr_T)MAXCOL);
1912 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1913 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001914
1915 // Insert always starts after the prompt, allow editing text after it.
1916 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1917 || Insstart_orig.col != (int)STRLEN(prompt))
1918 {
1919 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001920 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001921 Insstart_orig = Insstart;
1922 Insstart_textlen = Insstart.col;
1923 Insstart_blank_vcol = MAXCOL;
1924 arrow_used = FALSE;
1925 }
1926
Bram Moolenaarf2732452018-06-03 14:47:35 +02001927 if (cmdchar_todo == 'A')
1928 coladvance((colnr_T)MAXCOL);
1929 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001930 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001931 /* Make sure the cursor is in a valid position. */
1932 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001933}
1934
1935/*
1936 * Return TRUE if the cursor is in the editable position of the prompt line.
1937 */
1938 int
1939prompt_curpos_editable()
1940{
1941 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1942 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1943}
1944#endif
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946/*
1947 * Undo the previous edit_putchar().
1948 */
1949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001950edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1953 {
1954#if defined(FEAT_MBYTE)
1955 if (pc_status == PC_STATUS_RIGHT)
1956 ++curwin->w_wcol;
1957 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaar90a99792018-09-12 21:52:18 +02001958 redrawWinline(curwin, curwin->w_cursor.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 else
1960#endif
1961 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1962 }
1963}
1964
1965/*
1966 * Called when p_dollar is set: display a '$' at the end of the changed text
1967 * Only works when cursor is in the line that changes.
1968 */
1969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 colnr_T save_col;
1973
1974 if (!redrawing())
1975 return;
1976
1977 cursor_off();
1978 save_col = curwin->w_cursor.col;
1979 curwin->w_cursor.col = col;
1980#ifdef FEAT_MBYTE
1981 if (has_mbyte)
1982 {
1983 char_u *p;
1984
1985 /* If on the last byte of a multi-byte move to the first byte. */
1986 p = ml_get_curline();
1987 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1988 }
1989#endif
1990 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001991 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 edit_putchar('$', FALSE);
1994 dollar_vcol = curwin->w_virtcol;
1995 }
1996 curwin->w_cursor.col = save_col;
1997}
1998
1999/*
2000 * Call this function before moving the cursor from the normal insert position
2001 * in insert mode.
2002 */
2003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002004undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002006 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002008 dollar_vcol = -1;
Bram Moolenaar90a99792018-09-12 21:52:18 +02002009 redrawWinline(curwin, curwin->w_cursor.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011}
2012
2013/*
2014 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2015 * Keep the cursor on the same character.
2016 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2017 * type == INDENT_DEC decrease indent (for CTRL-D)
2018 * type == INDENT_SET set indent to "amount"
2019 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2020 */
2021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002022change_indent(
2023 int type,
2024 int amount,
2025 int round,
2026 int replaced, /* replaced character, put on replace stack */
2027 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int vcol;
2030 int last_vcol;
2031 int insstart_less; /* reduction for Insstart.col */
2032 int new_cursor_col;
2033 int i;
2034 char_u *ptr;
2035 int save_p_list;
2036 int start_col;
2037 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 colnr_T orig_col = 0; /* init for GCC */
2039 char_u *new_line, *orig_line = NULL; /* init for GCC */
2040
2041 /* VREPLACE mode needs to know what the line was like before changing */
2042 if (State & VREPLACE_FLAG)
2043 {
2044 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2045 orig_col = curwin->w_cursor.col;
2046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 /* for the following tricks we don't want list mode */
2049 save_p_list = curwin->w_p_list;
2050 curwin->w_p_list = FALSE;
2051 vc = getvcol_nolist(&curwin->w_cursor);
2052 vcol = vc;
2053
2054 /*
2055 * For Replace mode we need to fix the replace stack later, which is only
2056 * possible when the cursor is in the indent. Remember the number of
2057 * characters before the cursor if it's possible.
2058 */
2059 start_col = curwin->w_cursor.col;
2060
2061 /* determine offset from first non-blank */
2062 new_cursor_col = curwin->w_cursor.col;
2063 beginline(BL_WHITE);
2064 new_cursor_col -= curwin->w_cursor.col;
2065
2066 insstart_less = curwin->w_cursor.col;
2067
2068 /*
2069 * If the cursor is in the indent, compute how many screen columns the
2070 * cursor is to the left of the first non-blank.
2071 */
2072 if (new_cursor_col < 0)
2073 vcol = get_indent() - vcol;
2074
2075 if (new_cursor_col > 0) /* can't fix replace stack */
2076 start_col = -1;
2077
2078 /*
2079 * Set the new indent. The cursor will be put on the first non-blank.
2080 */
2081 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002082 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 else
2084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 int save_State = State;
2086
2087 /* Avoid being called recursively. */
2088 if (State & VREPLACE_FLAG)
2089 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002090 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 insstart_less -= curwin->w_cursor.col;
2094
2095 /*
2096 * Try to put cursor on same character.
2097 * If the cursor is at or after the first non-blank in the line,
2098 * compute the cursor column relative to the column of the first
2099 * non-blank character.
2100 * If we are not in insert mode, leave the cursor on the first non-blank.
2101 * If the cursor is before the first non-blank, position it relative
2102 * to the first non-blank, counted in screen columns.
2103 */
2104 if (new_cursor_col >= 0)
2105 {
2106 /*
2107 * When changing the indent while the cursor is touching it, reset
2108 * Insstart_col to 0.
2109 */
2110 if (new_cursor_col == 0)
2111 insstart_less = MAXCOL;
2112 new_cursor_col += curwin->w_cursor.col;
2113 }
2114 else if (!(State & INSERT))
2115 new_cursor_col = curwin->w_cursor.col;
2116 else
2117 {
2118 /*
2119 * Compute the screen column where the cursor should be.
2120 */
2121 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002122 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 /*
2125 * Advance the cursor until we reach the right screen column.
2126 */
2127 vcol = last_vcol = 0;
2128 new_cursor_col = -1;
2129 ptr = ml_get_curline();
2130 while (vcol <= (int)curwin->w_virtcol)
2131 {
2132 last_vcol = vcol;
2133#ifdef FEAT_MBYTE
2134 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002135 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 else
2137#endif
2138 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002139 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141 vcol = last_vcol;
2142
2143 /*
2144 * May need to insert spaces to be able to position the cursor on
2145 * the right screen column.
2146 */
2147 if (vcol != (int)curwin->w_virtcol)
2148 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002149 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002151 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (ptr != NULL)
2153 {
2154 new_cursor_col += i;
2155 ptr[i] = NUL;
2156 while (--i >= 0)
2157 ptr[i] = ' ';
2158 ins_str(ptr);
2159 vim_free(ptr);
2160 }
2161 }
2162
2163 /*
2164 * When changing the indent while the cursor is in it, reset
2165 * Insstart_col to 0.
2166 */
2167 insstart_less = MAXCOL;
2168 }
2169
2170 curwin->w_p_list = save_p_list;
2171
2172 if (new_cursor_col <= 0)
2173 curwin->w_cursor.col = 0;
2174 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002175 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_set_curswant = TRUE;
2177 changed_cline_bef_curs();
2178
2179 /*
2180 * May have to adjust the start of the insert.
2181 */
2182 if (State & INSERT)
2183 {
2184 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2185 {
2186 if ((int)Insstart.col <= insstart_less)
2187 Insstart.col = 0;
2188 else
2189 Insstart.col -= insstart_less;
2190 }
2191 if ((int)ai_col <= insstart_less)
2192 ai_col = 0;
2193 else
2194 ai_col -= insstart_less;
2195 }
2196
2197 /*
2198 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2199 * If the number of characters before the cursor decreased, need to pop a
2200 * few characters from the replace stack.
2201 * If the number of characters before the cursor increased, need to push a
2202 * few NULs onto the replace stack.
2203 */
2204 if (REPLACE_NORMAL(State) && start_col >= 0)
2205 {
2206 while (start_col > (int)curwin->w_cursor.col)
2207 {
2208 replace_join(0); /* remove a NUL from the replace stack */
2209 --start_col;
2210 }
2211 while (start_col < (int)curwin->w_cursor.col || replaced)
2212 {
2213 replace_push(NUL);
2214 if (replaced)
2215 {
2216 replace_push(replaced);
2217 replaced = NUL;
2218 }
2219 ++start_col;
2220 }
2221 }
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 /*
2224 * For VREPLACE mode, we also have to fix the replace stack. In this case
2225 * it is always possible because we backspace over the whole line and then
2226 * put it back again the way we wanted it.
2227 */
2228 if (State & VREPLACE_FLAG)
2229 {
2230 /* If orig_line didn't allocate, just return. At least we did the job,
2231 * even if you can't backspace. */
2232 if (orig_line == NULL)
2233 return;
2234
2235 /* Save new line */
2236 new_line = vim_strsave(ml_get_curline());
2237 if (new_line == NULL)
2238 return;
2239
2240 /* We only put back the new line up to the cursor */
2241 new_line[curwin->w_cursor.col] = NUL;
2242
2243 /* Put back original line */
2244 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2245 curwin->w_cursor.col = orig_col;
2246
2247 /* Backspace from cursor to start of line */
2248 backspace_until_column(0);
2249
2250 /* Insert new stuff into line again */
2251 ins_bytes(new_line);
2252
2253 vim_free(new_line);
2254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255}
2256
2257/*
2258 * Truncate the space at the end of a line. This is to be used only in an
2259 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2260 * modes.
2261 */
2262 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002263truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int i;
2266
2267 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002268 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 if (State & REPLACE_FLAG)
2271 replace_join(0); /* remove a NUL from the replace stack */
2272 }
2273 line[i + 1] = NUL;
2274}
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276/*
2277 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2278 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002279 * Will attempt not to go before "col" even when there is a composing
2280 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
2282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 while ((int)curwin->w_cursor.col > col)
2286 {
2287 curwin->w_cursor.col--;
2288 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002289 replace_do_bs(col);
2290 else if (!del_char_after_col(col))
2291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002295/*
2296 * Like del_char(), but make sure not to go before column "limit_col".
2297 * Only matters when there are composing characters.
2298 * Return TRUE when something was deleted.
2299 */
2300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002302{
2303#ifdef FEAT_MBYTE
2304 if (enc_utf8 && limit_col >= 0)
2305 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002306 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002307
2308 /* Make sure the cursor is at the start of a character, but
2309 * skip forward again when going too far back because of a
2310 * composing character. */
2311 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002312 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002313 {
2314 int l = utf_ptr2len(ml_get_cursor());
2315
2316 if (l == 0) /* end of line */
2317 break;
2318 curwin->w_cursor.col += l;
2319 }
2320 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2321 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002322 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002323 }
2324 else
2325#endif
2326 (void)del_char(FALSE);
2327 return TRUE;
2328}
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2331/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002332 * CTRL-X pressed in Insert mode.
2333 */
2334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002335ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002336{
2337 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2338 * CTRL-V works like CTRL-N */
2339 if (ctrl_x_mode != CTRL_X_CMDLINE)
2340 {
2341 /* if the next ^X<> won't ADD nothing, then reset
2342 * compl_cont_status */
2343 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002344 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002345 else
2346 compl_cont_status = 0;
2347 /* We're not sure which CTRL-X mode it will be yet */
2348 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2349 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2350 edit_submode_pre = NULL;
2351 showmode();
2352 }
2353}
2354
2355/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002356 * Whether other than default completion has been selected.
2357 */
2358 int
2359ctrl_x_mode_not_default(void)
2360{
2361 return ctrl_x_mode != CTRL_X_NORMAL;
2362}
2363
2364/*
2365 * Whether CTRL-X was typed without a following character.
2366 */
2367 int
2368ctrl_x_mode_not_defined_yet(void)
2369{
2370 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2371}
2372
2373/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002374 * Return TRUE if the 'dict' or 'tsr' option can be used.
2375 */
2376 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002377has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002378{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002379 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002380# ifdef FEAT_SPELL
2381 && !curwin->w_p_spell
2382# endif
2383 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002384 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2385 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002386 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002387 edit_submode = NULL;
2388 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2389 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002391 if (emsg_silent == 0)
2392 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002393 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002394 setcursor();
2395 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002396#ifdef FEAT_EVAL
2397 if (!get_vim_var_nr(VV_TESTING))
2398#endif
2399 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002400 }
2401 return FALSE;
2402 }
2403 return TRUE;
2404}
2405
2406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2408 * This depends on the current mode.
2409 */
2410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002411vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002413 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (c == Ctrl_R)
2415 return TRUE;
2416
Bram Moolenaare3226be2005-12-18 22:10:00 +00002417 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002419 return TRUE;
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 switch (ctrl_x_mode)
2422 {
2423 case 0: /* Not in any CTRL-X mode */
2424 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2425 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002426 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2428 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2429 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002430 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002431 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CTRL_X_SCROLL:
2433 return (c == Ctrl_Y || c == Ctrl_E);
2434 case CTRL_X_WHOLE_LINE:
2435 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2436 case CTRL_X_FILES:
2437 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2438 case CTRL_X_DICTIONARY:
2439 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2440 case CTRL_X_THESAURUS:
2441 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2442 case CTRL_X_TAGS:
2443 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2444#ifdef FEAT_FIND_ID
2445 case CTRL_X_PATH_PATTERNS:
2446 return (c == Ctrl_P || c == Ctrl_N);
2447 case CTRL_X_PATH_DEFINES:
2448 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2449#endif
2450 case CTRL_X_CMDLINE:
2451 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2452 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002453#ifdef FEAT_COMPL_FUNC
2454 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002455 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002456 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002457 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002458#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002459 case CTRL_X_SPELL:
2460 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002461 case CTRL_X_EVAL:
2462 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002464 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 return FALSE;
2466}
2467
2468/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002469 * Return TRUE when character "c" is part of the item currently being
2470 * completed. Used to decide whether to abandon complete mode when the menu
2471 * is visible.
2472 */
2473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002474ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002475{
2476 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2477 /* When expanding an identifier only accept identifier chars. */
2478 return vim_isIDc(c);
2479
2480 switch (ctrl_x_mode)
2481 {
2482 case CTRL_X_FILES:
2483 /* When expanding file name only accept file name chars. But not
2484 * path separators, so that "proto/<Tab>" expands files in
2485 * "proto", not "proto/" as a whole */
2486 return vim_isfilec(c) && !vim_ispathsep(c);
2487
2488 case CTRL_X_CMDLINE:
2489 case CTRL_X_OMNI:
2490 /* Command line and Omni completion can work with just about any
2491 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002492 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002493
2494 case CTRL_X_WHOLE_LINE:
2495 /* For while line completion a space can be part of the line. */
2496 return vim_isprintc(c);
2497 }
2498 return vim_iswordc(c);
2499}
2500
2501/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002502 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002504 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * the rest of the word to be in -- webb
2506 */
2507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002508ins_compl_add_infercase(
2509 char_u *str,
2510 int len,
2511 int icase,
2512 char_u *fname,
2513 int dir,
2514 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 char_u *p;
2517 int i, c;
2518 int actual_len; /* Take multi-byte characters */
2519 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002520 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002521 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 int has_lower = FALSE;
2523 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002525 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002527 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 /* Find actual length of completion. */
2530#ifdef FEAT_MBYTE
2531 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002533 p = str;
2534 actual_len = 0;
2535 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002537 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002538 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002541 else
2542#endif
2543 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaara2993e12007-08-12 14:38:46 +00002545 /* Find actual length of original text. */
2546#ifdef FEAT_MBYTE
2547 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002549 p = compl_orig_text;
2550 actual_compl_length = 0;
2551 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002553 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002554 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
2556 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002557 else
2558#endif
2559 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002561 /* "actual_len" may be smaller than "actual_compl_length" when using
2562 * thesaurus, only use the minimum when comparing. */
2563 min_len = actual_len < actual_compl_length
2564 ? actual_len : actual_compl_length;
2565
Bram Moolenaara2993e12007-08-12 14:38:46 +00002566 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002567 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002568 if (wca != NULL)
2569 {
2570 p = str;
2571 for (i = 0; i < actual_len; ++i)
2572#ifdef FEAT_MBYTE
2573 if (has_mbyte)
2574 wca[i] = mb_ptr2char_adv(&p);
2575 else
2576#endif
2577 wca[i] = *(p++);
2578
2579 /* Rule 1: Were any chars converted to lower? */
2580 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002581 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002582 {
2583#ifdef FEAT_MBYTE
2584 if (has_mbyte)
2585 c = mb_ptr2char_adv(&p);
2586 else
2587#endif
2588 c = *(p++);
2589 if (MB_ISLOWER(c))
2590 {
2591 has_lower = TRUE;
2592 if (MB_ISUPPER(wca[i]))
2593 {
2594 /* Rule 1 is satisfied. */
2595 for (i = actual_compl_length; i < actual_len; ++i)
2596 wca[i] = MB_TOLOWER(wca[i]);
2597 break;
2598 }
2599 }
2600 }
2601
2602 /*
2603 * Rule 2: No lower case, 2nd consecutive letter converted to
2604 * upper case.
2605 */
2606 if (!has_lower)
2607 {
2608 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002609 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002610 {
2611#ifdef FEAT_MBYTE
2612 if (has_mbyte)
2613 c = mb_ptr2char_adv(&p);
2614 else
2615#endif
2616 c = *(p++);
2617 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2618 {
2619 /* Rule 2 is satisfied. */
2620 for (i = actual_compl_length; i < actual_len; ++i)
2621 wca[i] = MB_TOUPPER(wca[i]);
2622 break;
2623 }
2624 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2625 }
2626 }
2627
2628 /* Copy the original case of the part we typed. */
2629 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002630 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002631 {
2632#ifdef FEAT_MBYTE
2633 if (has_mbyte)
2634 c = mb_ptr2char_adv(&p);
2635 else
2636#endif
2637 c = *(p++);
2638 if (MB_ISLOWER(c))
2639 wca[i] = MB_TOLOWER(wca[i]);
2640 else if (MB_ISUPPER(c))
2641 wca[i] = MB_TOUPPER(wca[i]);
2642 }
2643
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002644 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002645 * Generate encoding specific output from wide character array.
2646 * Multi-byte characters can occupy up to five bytes more than
2647 * ASCII characters, and we also need one byte for NUL, so stay
2648 * six bytes away from the edge of IObuff.
2649 */
2650 p = IObuff;
2651 i = 0;
2652 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2653#ifdef FEAT_MBYTE
2654 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002655 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002656 else
2657#endif
2658 *(p++) = wca[i++];
2659 *p = NUL;
2660
2661 vim_free(wca);
2662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002664 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2665 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002667 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668}
2669
2670/*
2671 * Add a match to the list of matches.
2672 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002673 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002674 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002676 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677ins_compl_add(
2678 char_u *str,
2679 int len,
2680 int icase,
2681 char_u *fname,
2682 char_u **cptext, /* extra text for popup menu or NULL */
2683 int cdir,
2684 int flags,
2685 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002687 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002688 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 ui_breakcheck();
2691 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (len < 0)
2694 len = (int)STRLEN(str);
2695
2696 /*
2697 * If the same match is already present, don't add it.
2698 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002699 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002701 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 do
2703 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002704 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002705 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002706 && match->cp_str[len] == NUL)
2707 return NOTDONE;
2708 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002709 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002712 /* Remove any popup menu before changing the list of matches. */
2713 ins_compl_del_pum();
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /*
2716 * Allocate a new match structure.
2717 * Copy the values to the new match structure.
2718 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002719 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002721 return FAIL;
2722 match->cp_number = -1;
2723 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002724 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002725 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002730 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002733 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2735 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002736 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002737 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002738 && compl_curr_match->cp_fname != NULL
2739 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002740 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002741 else if (fname != NULL)
2742 {
2743 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002744 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 match->cp_fname = NULL;
2748 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002749
2750 if (cptext != NULL)
2751 {
2752 int i;
2753
2754 for (i = 0; i < CPT_COUNT; ++i)
2755 if (cptext[i] != NULL && *cptext[i] != NUL)
2756 match->cp_text[i] = vim_strsave(cptext[i]);
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 /*
2760 * Link the new match structure in the list of matches.
2761 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002762 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002763 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else if (dir == FORWARD)
2765 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002766 match->cp_next = compl_curr_match->cp_next;
2767 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else /* BACKWARD */
2770 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002771 match->cp_next = compl_curr_match;
2772 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 if (match->cp_next)
2775 match->cp_next->cp_prev = match;
2776 if (match->cp_prev)
2777 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002779 compl_first_match = match;
2780 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002782 /*
2783 * Find the longest common string if still doing that.
2784 */
2785 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2786 ins_compl_longest_match(match);
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return OK;
2789}
2790
2791/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002792 * Return TRUE if "str[len]" matches with match->cp_str, considering
2793 * match->cp_icase.
2794 */
2795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002796ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002797{
2798 if (match->cp_icase)
2799 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2800 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2801}
2802
2803/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002804 * Reduce the longest common string for match "match".
2805 */
2806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002808{
2809 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002810 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002811 int had_match;
2812
2813 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002814 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002815 /* First match, use it as a whole. */
2816 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002817 if (compl_leader != NULL)
2818 {
2819 had_match = (curwin->w_cursor.col > compl_col);
2820 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002821 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002822 ins_redraw(FALSE);
2823
2824 /* When the match isn't there (to avoid matching itself) remove it
2825 * again after redrawing. */
2826 if (!had_match)
2827 ins_compl_delete();
2828 compl_used_match = FALSE;
2829 }
2830 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002831 else
2832 {
2833 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002834 p = compl_leader;
2835 s = match->cp_str;
2836 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002837 {
2838#ifdef FEAT_MBYTE
2839 if (has_mbyte)
2840 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002841 c1 = mb_ptr2char(p);
2842 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002843 }
2844 else
2845#endif
2846 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002847 c1 = *p;
2848 c2 = *s;
2849 }
2850 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2851 : (c1 != c2))
2852 break;
2853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002856 MB_PTR_ADV(p);
2857 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002858 }
2859 else
2860#endif
2861 {
2862 ++p;
2863 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002864 }
2865 }
2866
2867 if (*p != NUL)
2868 {
2869 /* Leader was shortened, need to change the inserted text. */
2870 *p = NUL;
2871 had_match = (curwin->w_cursor.col > compl_col);
2872 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002873 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002874 ins_redraw(FALSE);
2875
2876 /* When the match isn't there (to avoid matching itself) remove it
2877 * again after redrawing. */
2878 if (!had_match)
2879 ins_compl_delete();
2880 }
2881
2882 compl_used_match = FALSE;
2883 }
2884}
2885
2886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 * Add an array of matches to the list of matches.
2888 * Frees matches[].
2889 */
2890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002891ins_compl_add_matches(
2892 int num_matches,
2893 char_u **matches,
2894 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 int i;
2897 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002898 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar572cb562005-08-05 21:35:02 +00002900 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002901 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002902 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002904 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 FreeWild(num_matches, matches);
2906}
2907
2908/* Make the completion list cyclic.
2909 * Return the number of matches (excluding the original).
2910 */
2911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002914 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 int count = 0;
2916
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002917 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 /*
2920 * Find the end of the list.
2921 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002922 match = compl_first_match;
2923 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002924 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002926 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 ++count;
2928 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002929 match->cp_next = compl_first_match;
2930 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932 return count;
2933}
2934
Bram Moolenaara94bc432006-03-10 21:42:59 +00002935/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002936 * Set variables that store noselect and noinsert behavior from the
2937 * 'completeopt' value.
2938 */
2939 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002940completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002941{
2942 compl_no_insert = FALSE;
2943 compl_no_select = FALSE;
2944 if (strstr((char *)p_cot, "noselect") != NULL)
2945 compl_no_select = TRUE;
2946 if (strstr((char *)p_cot, "noinsert") != NULL)
2947 compl_no_insert = TRUE;
2948}
2949
2950/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002951 * Start completion for the complete() function.
2952 * "startcol" is where the matched text starts (1 is first column).
2953 * "list" is the list of matches.
2954 */
2955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002957{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002958 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002959 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002960
Bram Moolenaara94bc432006-03-10 21:42:59 +00002961 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002962 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002963 ins_compl_prep(' ');
2964 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002965 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002966
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002967 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002968 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002969 startcol = curwin->w_cursor.col;
2970 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002971 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972 /* compl_pattern doesn't need to be set */
2973 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2974 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002975 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002976 return;
2977
Bram Moolenaare4214502015-03-05 18:08:43 +01002978 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002979
2980 ins_compl_add_list(list);
2981 compl_matches = ins_compl_make_cyclic();
2982 compl_started = TRUE;
2983 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002984 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002985
2986 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002987 if (compl_no_insert || compl_no_select)
2988 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002989 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002990 if (compl_no_select)
2991 /* Down/Up has no real effect. */
2992 ins_complete(K_UP, FALSE);
2993 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002994 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002995 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002996 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002997
2998 /* Lazily show the popup menu, unless we got interrupted. */
2999 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003000 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003001 out_flush();
3002}
3003
3004
Bram Moolenaar9372a112005-12-06 19:59:18 +00003005/* "compl_match_array" points the currently displayed list of entries in the
3006 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003007static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008static int compl_match_arraysize;
3009
3010/*
3011 * Update the screen and when there is any scrolling remove the popup menu.
3012 */
3013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003014ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003015{
3016 int h;
3017
3018 if (compl_match_array != NULL)
3019 {
3020 h = curwin->w_cline_height;
3021 update_screen(0);
3022 if (h != curwin->w_cline_height)
3023 ins_compl_del_pum();
3024 }
3025}
3026
3027/*
3028 * Remove any popup menu.
3029 */
3030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003031ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003032{
3033 if (compl_match_array != NULL)
3034 {
3035 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003036 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003037 }
3038}
3039
3040/*
3041 * Return TRUE if the popup menu should be displayed.
3042 */
3043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003045{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003046 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003047 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048 return FALSE;
3049
3050 /* The display looks bad on a B&W display. */
3051 if (t_colors < 8
3052#ifdef FEAT_GUI
3053 && !gui.in_use
3054#endif
3055 )
3056 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003057 return TRUE;
3058}
3059
3060/*
3061 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003062 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003063 */
3064 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003065pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003066{
3067 compl_T *compl;
3068 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003069
3070 /* Don't display the popup menu if there are no matches or there is only
3071 * one (ignoring the original text). */
3072 compl = compl_first_match;
3073 i = 0;
3074 do
3075 {
3076 if (compl == NULL
3077 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3078 break;
3079 compl = compl->cp_next;
3080 } while (compl != compl_first_match);
3081
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003082 if (strstr((char *)p_cot, "menuone") != NULL)
3083 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003084 return (i >= 2);
3085}
3086
3087/*
3088 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003089 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003091 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003092ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003093{
3094 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003095 compl_T *shown_compl = NULL;
3096 int did_find_shown_match = FALSE;
3097 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003098 int i;
3099 int cur = -1;
3100 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003101 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003103 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104 return;
3105
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003106#if defined(FEAT_EVAL)
3107 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3108 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3109#endif
3110
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003111 /* Update the screen before drawing the popup menu over it. */
3112 update_screen(0);
3113
3114 if (compl_match_array == NULL)
3115 {
3116 /* Need to build the popup menu list. */
3117 compl_match_arraysize = 0;
3118 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003119 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003120 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003121 do
3122 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003123 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3124 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003125 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003126 ++compl_match_arraysize;
3127 compl = compl->cp_next;
3128 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003129 if (compl_match_arraysize == 0)
3130 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003131 compl_match_array = (pumitem_T *)alloc_clear(
3132 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003133 * compl_match_arraysize));
3134 if (compl_match_array != NULL)
3135 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003136 /* If the current match is the original text don't find the first
3137 * match after it, don't highlight anything. */
3138 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3139 shown_match_ok = TRUE;
3140
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003141 i = 0;
3142 compl = compl_first_match;
3143 do
3144 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003145 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3146 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003147 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003148 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003149 if (!shown_match_ok)
3150 {
3151 if (compl == compl_shown_match || did_find_shown_match)
3152 {
3153 /* This item is the shown match or this is the
3154 * first displayed item after the shown match. */
3155 compl_shown_match = compl;
3156 did_find_shown_match = TRUE;
3157 shown_match_ok = TRUE;
3158 }
3159 else
3160 /* Remember this displayed match for when the
3161 * shown match is just below it. */
3162 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003163 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003164 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003165
3166 if (compl->cp_text[CPT_ABBR] != NULL)
3167 compl_match_array[i].pum_text =
3168 compl->cp_text[CPT_ABBR];
3169 else
3170 compl_match_array[i].pum_text = compl->cp_str;
3171 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3172 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3173 if (compl->cp_text[CPT_MENU] != NULL)
3174 compl_match_array[i++].pum_extra =
3175 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003176 else
3177 compl_match_array[i++].pum_extra = compl->cp_fname;
3178 }
3179
3180 if (compl == compl_shown_match)
3181 {
3182 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003183
3184 /* When the original text is the shown match don't set
3185 * compl_shown_match. */
3186 if (compl->cp_flags & ORIGINAL_TEXT)
3187 shown_match_ok = TRUE;
3188
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003189 if (!shown_match_ok && shown_compl != NULL)
3190 {
3191 /* The shown match isn't displayed, set it to the
3192 * previously displayed match. */
3193 compl_shown_match = shown_compl;
3194 shown_match_ok = TRUE;
3195 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003196 }
3197 compl = compl->cp_next;
3198 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003199
3200 if (!shown_match_ok) /* no displayed match at all */
3201 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003202 }
3203 }
3204 else
3205 {
3206 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003207 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003208 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3209 || compl_match_array[i].pum_text
3210 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003211 {
3212 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003213 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003214 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003215 }
3216
3217 if (compl_match_array != NULL)
3218 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003219 /* In Replace mode when a $ is displayed at the end of the line only
3220 * part of the screen would be updated. We do need to redraw here. */
3221 dollar_vcol = -1;
3222
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003223 /* Compute the screen column of the start of the completed text.
3224 * Use the cursor to get all wrapping and other settings right. */
3225 col = curwin->w_cursor.col;
3226 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003227 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003228 curwin->w_cursor.col = col;
3229 }
3230}
3231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#define DICT_FIRST (1) /* use just first element in "dict" */
3233#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003236 * Add any identifiers that match the given pattern in the list of dictionary
3237 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 */
3239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240ins_compl_dictionaries(
3241 char_u *dict_start,
3242 char_u *pat,
3243 int flags, /* DICT_FIRST and/or DICT_EXACT */
3244 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 char_u *ptr;
3248 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 char_u **files;
3251 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003253 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaar0b238792006-03-02 22:49:12 +00003255 if (*dict == NUL)
3256 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003257#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 /* When 'dictionary' is empty and spell checking is enabled use
3259 * "spell". */
3260 if (!thesaurus && curwin->w_p_spell)
3261 dict = (char_u *)"spell";
3262 else
3263#endif
3264 return;
3265 }
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003268 if (buf == NULL)
3269 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003270 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /* If 'infercase' is set, don't use 'smartcase' here */
3273 save_p_scs = p_scs;
3274 if (curbuf->b_p_inf)
3275 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003276
3277 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3278 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003279 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003280 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003281 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003283 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003284
3285 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003286 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003287 len = STRLEN(pat_esc) + 10;
3288 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003290 {
3291 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003292 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003294 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003295 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3296 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003297 vim_free(ptr);
3298 }
3299 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003300 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003301 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003302 if (regmatch.regprog == NULL)
3303 goto theend;
3304 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3307 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003308 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 /* copy one dictionary file name into buf */
3311 if (flags == DICT_EXACT)
3312 {
3313 count = 1;
3314 files = &dict;
3315 }
3316 else
3317 {
3318 /* Expand wildcards in the dictionary name, but do not allow
3319 * backticks (for security, the 'dict' option may have been set in
3320 * a modeline). */
3321 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003322# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003323 if (!thesaurus && STRCMP(buf, "spell") == 0)
3324 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003325 else
3326# endif
3327 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 || expand_wildcards(1, &buf, &count, &files,
3329 EW_FILE|EW_SILENT) != OK)
3330 count = 0;
3331 }
3332
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003333# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003334 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003336 /* Complete from active spelling. Skip "\<" in the pattern, we
3337 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003338 if (pat[0] == '\\' && pat[1] == '<')
3339 ptr = pat + 2;
3340 else
3341 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003344 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003345# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003346 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003347 {
3348 ins_compl_files(count, files, thesaurus, flags,
3349 &regmatch, buf, &dir);
3350 if (flags != DICT_EXACT)
3351 FreeWild(count, files);
3352 }
3353 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 break;
3355 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003356
3357theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003359 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 vim_free(buf);
3361}
3362
Bram Moolenaar0b238792006-03-02 22:49:12 +00003363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364ins_compl_files(
3365 int count,
3366 char_u **files,
3367 int thesaurus,
3368 int flags,
3369 regmatch_T *regmatch,
3370 char_u *buf,
3371 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003372{
3373 char_u *ptr;
3374 int i;
3375 FILE *fp;
3376 int add_r;
3377
3378 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3379 {
3380 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3381 if (flags != DICT_EXACT)
3382 {
3383 vim_snprintf((char *)IObuff, IOSIZE,
3384 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003385 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003386 }
3387
3388 if (fp != NULL)
3389 {
3390 /*
3391 * Read dictionary file line by line.
3392 * Check each line for a match.
3393 */
3394 while (!got_int && !compl_interrupted
3395 && !vim_fgets(buf, LSIZE, fp))
3396 {
3397 ptr = buf;
3398 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3399 {
3400 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003401 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003402 ptr = find_line_end(ptr);
3403 else
3404 ptr = find_word_end(ptr);
3405 add_r = ins_compl_add_infercase(regmatch->startp[0],
3406 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003407 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003408 if (thesaurus)
3409 {
3410 char_u *wstart;
3411
3412 /*
3413 * Add the other matches on the line
3414 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003415 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003416 while (!got_int)
3417 {
3418 /* Find start of the next word. Skip white
3419 * space and punctuation. */
3420 ptr = find_word_start(ptr);
3421 if (*ptr == NUL || *ptr == NL)
3422 break;
3423 wstart = ptr;
3424
Bram Moolenaara2993e12007-08-12 14:38:46 +00003425 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003426#ifdef FEAT_MBYTE
3427 if (has_mbyte)
3428 /* Japanese words may have characters in
3429 * different classes, only separate words
3430 * with single-byte non-word characters. */
3431 while (*ptr != NUL)
3432 {
3433 int l = (*mb_ptr2len)(ptr);
3434
3435 if (l < 2 && !vim_iswordc(*ptr))
3436 break;
3437 ptr += l;
3438 }
3439 else
3440#endif
3441 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003442
3443 /* Add the word. Skip the regexp match. */
3444 if (wstart != regmatch->startp[0])
3445 add_r = ins_compl_add_infercase(wstart,
3446 (int)(ptr - wstart),
3447 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003448 }
3449 }
3450 if (add_r == OK)
3451 /* if dir was BACKWARD then honor it just once */
3452 *dir = FORWARD;
3453 else if (add_r == FAIL)
3454 break;
3455 /* avoid expensive call to vim_regexec() when at end
3456 * of line */
3457 if (*ptr == '\n' || got_int)
3458 break;
3459 }
3460 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003461 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003462 }
3463 fclose(fp);
3464 }
3465 }
3466}
3467
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468/*
3469 * Find the start of the next word.
3470 * Returns a pointer to the first char of the word. Also stops at a NUL.
3471 */
3472 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475#ifdef FEAT_MBYTE
3476 if (has_mbyte)
3477 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003478 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 else
3480#endif
3481 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3482 ++ptr;
3483 return ptr;
3484}
3485
3486/*
3487 * Find the end of the word. Assumes it starts inside a word.
3488 * Returns a pointer to just after the word.
3489 */
3490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003491find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493#ifdef FEAT_MBYTE
3494 int start_class;
3495
3496 if (has_mbyte)
3497 {
3498 start_class = mb_get_class(ptr);
3499 if (start_class > 1)
3500 while (*ptr != NUL)
3501 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003502 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (mb_get_class(ptr) != start_class)
3504 break;
3505 }
3506 }
3507 else
3508#endif
3509 while (vim_iswordc(*ptr))
3510 ++ptr;
3511 return ptr;
3512}
3513
3514/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003515 * Find the end of the line, omitting CR and NL at the end.
3516 * Returns a pointer to just after the line.
3517 */
3518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003519find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520{
3521 char_u *s;
3522
3523 s = ptr + STRLEN(ptr);
3524 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3525 --s;
3526 return s;
3527}
3528
3529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 * Free the list of completions
3531 */
3532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003533ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003535 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003536 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaard23a8232018-02-10 18:45:26 +01003538 VIM_CLEAR(compl_pattern);
3539 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003543
3544 ins_compl_del_pum();
3545 pum_clear();
3546
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 do
3549 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003551 compl_curr_match = compl_curr_match->cp_next;
3552 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003554 if (match->cp_flags & FREE_FNAME)
3555 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003556 for (i = 0; i < CPT_COUNT; ++i)
3557 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3560 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003561 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003562 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
3564
3565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003568 compl_cont_status = 0;
3569 compl_started = FALSE;
3570 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003571 VIM_CLEAR(compl_pattern);
3572 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003574 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003575 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003576 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003577 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
3580/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003581 * Return TRUE when Insert completion is active.
3582 */
3583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003585{
3586 return compl_started;
3587}
3588
3589/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003590 * Delete one character before the cursor and show the subset of the matches
3591 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003592 * Returns the character to be used, NUL if the work is done and another char
3593 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003594 */
3595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003596ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003597{
3598 char_u *line;
3599 char_u *p;
3600
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003601 line = ml_get_curline();
3602 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003603 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003604
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003605 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003606 * allow the word to be deleted, we won't match everything.
3607 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003608 if ((int)(p - line) - (int)compl_col < 0
3609 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003610 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3611 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3612 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003613 return K_BS;
3614
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003615 /* Deleted more than what was used to find matches or didn't finish
3616 * finding all matches: need to look for matches all over again. */
3617 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003618 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003619 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003620
Bram Moolenaara6557602006-02-04 22:43:20 +00003621 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003622 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003623 if (compl_leader != NULL)
3624 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003625 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003626 if (compl_shown_match != NULL)
3627 /* Make sure current match is not a hidden item. */
3628 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003629 return NUL;
3630 }
3631 return K_BS;
3632}
Bram Moolenaara6557602006-02-04 22:43:20 +00003633
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003634/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003635 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3636 * be called.
3637 */
3638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003639ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003640{
3641 /* Return TRUE if we didn't complete finding matches or when the
3642 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3643 return compl_was_interrupted
3644 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3645 && compl_opt_refresh_always);
3646}
3647
3648/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003649 * Called after changing "compl_leader".
3650 * Show the popup menu with a different set of matches.
3651 * May also search for matches again if the previous search was interrupted.
3652 */
3653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003655{
3656 ins_compl_del_pum();
3657 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003658 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003659 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003660
3661 if (compl_started)
3662 ins_compl_set_original_text(compl_leader);
3663 else
3664 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003665#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003666 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003667#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003668 /*
3669 * Matches were cleared, need to search for them now. First display
3670 * the changed text before the cursor. Set "compl_restarting" to
3671 * avoid that the first match is inserted.
3672 */
3673 update_screen(0);
3674#ifdef FEAT_GUI
3675 if (gui.in_use)
3676 {
3677 /* Show the cursor after the match, not after the redrawn text. */
3678 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003679 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003680 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003681#endif
3682 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003683 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684 compl_cont_status = 0;
3685 compl_restarting = FALSE;
3686 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003687
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003688 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003689
3690 /* Show the popup menu with a different set of matches. */
3691 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003692
3693 /* Don't let Enter select the original text when there is no popup menu. */
3694 if (compl_match_array == NULL)
3695 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003696}
3697
3698/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003699 * Return the length of the completion, from the completion start column to
3700 * the cursor column. Making sure it never goes below zero.
3701 */
3702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003703ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003704{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003705 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003706
3707 if (off < 0)
3708 return 0;
3709 return off;
3710}
3711
3712/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003713 * Append one character to the match leader. May reduce the number of
3714 * matches.
3715 */
3716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003718{
3719#ifdef FEAT_MBYTE
3720 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003721#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003722
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003723 if (stop_arrow() == FAIL)
3724 return;
3725#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003726 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3727 {
3728 char_u buf[MB_MAXBYTES + 1];
3729
3730 (*mb_char2bytes)(c, buf);
3731 buf[cc] = NUL;
3732 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003733 if (compl_opt_refresh_always)
3734 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003735 }
3736 else
3737#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003738 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003739 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003740 if (compl_opt_refresh_always)
3741 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003742 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003743
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003744 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003745 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003746 ins_compl_restart();
3747
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003748 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003749 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003750 * break redo. */
3751 if (!compl_opt_refresh_always)
3752 {
3753 vim_free(compl_leader);
3754 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003755 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003756 if (compl_leader != NULL)
3757 ins_compl_new_leader();
3758 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003759}
3760
3761/*
3762 * Setup for finding completions again without leaving CTRL-X mode. Used when
3763 * BS or a key was typed while still searching for matches.
3764 */
3765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003767{
3768 ins_compl_free();
3769 compl_started = FALSE;
3770 compl_matches = 0;
3771 compl_cont_status = 0;
3772 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003773}
3774
3775/*
3776 * Set the first match, the original text.
3777 */
3778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003780{
3781 char_u *p;
3782
Bram Moolenaare87edf32018-04-17 22:14:32 +02003783 /* Replace the original text entry.
3784 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3785 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003786 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3787 {
3788 p = vim_strsave(str);
3789 if (p != NULL)
3790 {
3791 vim_free(compl_first_match->cp_str);
3792 compl_first_match->cp_str = p;
3793 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003794 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003795 else if (compl_first_match->cp_prev != NULL
3796 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3797 {
3798 p = vim_strsave(str);
3799 if (p != NULL)
3800 {
3801 vim_free(compl_first_match->cp_prev->cp_str);
3802 compl_first_match->cp_prev->cp_str = p;
3803 }
3804 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003805}
3806
3807/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 * Append one character to the match leader. May reduce the number of
3809 * matches.
3810 */
3811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003812ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003813{
3814 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003815 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003817 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003818
3819 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003820 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003821 {
3822 /* When still at the original match use the first entry that matches
3823 * the leader. */
3824 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3825 {
3826 p = NULL;
3827 for (cp = compl_shown_match->cp_next; cp != NULL
3828 && cp != compl_first_match; cp = cp->cp_next)
3829 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003830 if (compl_leader == NULL
3831 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003832 (int)STRLEN(compl_leader)))
3833 {
3834 p = cp->cp_str;
3835 break;
3836 }
3837 }
3838 if (p == NULL || (int)STRLEN(p) <= len)
3839 return;
3840 }
3841 else
3842 return;
3843 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003844 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003845 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 ins_compl_addleader(c);
3847}
3848
3849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003851 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003852 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003859 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 /* Forget any previous 'special' messages if this is actually
3862 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3863 */
3864 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3865 edit_submode_extra = NULL;
3866
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003867 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003868 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3869 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003870 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003872 /* Set "compl_get_longest" when finding the first matches. */
3873 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003874 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003875 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003876 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003877 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003878
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003879 }
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3882 {
3883 /*
3884 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3885 * it will be yet. Now we decide.
3886 */
3887 switch (c)
3888 {
3889 case Ctrl_E:
3890 case Ctrl_Y:
3891 ctrl_x_mode = CTRL_X_SCROLL;
3892 if (!(State & REPLACE_FLAG))
3893 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3894 else
3895 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3896 edit_submode_pre = NULL;
3897 showmode();
3898 break;
3899 case Ctrl_L:
3900 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3901 break;
3902 case Ctrl_F:
3903 ctrl_x_mode = CTRL_X_FILES;
3904 break;
3905 case Ctrl_K:
3906 ctrl_x_mode = CTRL_X_DICTIONARY;
3907 break;
3908 case Ctrl_R:
3909 /* Simply allow ^R to happen without affecting ^X mode */
3910 break;
3911 case Ctrl_T:
3912 ctrl_x_mode = CTRL_X_THESAURUS;
3913 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003914#ifdef FEAT_COMPL_FUNC
3915 case Ctrl_U:
3916 ctrl_x_mode = CTRL_X_FUNCTION;
3917 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003919 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003921#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003922 case 's':
3923 case Ctrl_S:
3924 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003925#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003926 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003927 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003929#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 case Ctrl_RSB:
3932 ctrl_x_mode = CTRL_X_TAGS;
3933 break;
3934#ifdef FEAT_FIND_ID
3935 case Ctrl_I:
3936 case K_S_TAB:
3937 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3938 break;
3939 case Ctrl_D:
3940 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3941 break;
3942#endif
3943 case Ctrl_V:
3944 case Ctrl_Q:
3945 ctrl_x_mode = CTRL_X_CMDLINE;
3946 break;
3947 case Ctrl_P:
3948 case Ctrl_N:
3949 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3950 * just started ^X mode, or there were enough ^X's to cancel
3951 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3952 * do normal expansion when interrupting a different mode (say
3953 * ^X^F^X^P or ^P^X^X^P, see below)
3954 * nothing changes if interrupting mode 0, (eg, the flag
3955 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003956 if (!(compl_cont_status & CONT_INTRPT))
3957 compl_cont_status |= CONT_LOCAL;
3958 else if (compl_cont_mode != 0)
3959 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 /* FALLTHROUGH */
3961 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 /* If we have typed at least 2 ^X's... for modes != 0, we set
3963 * compl_cont_status = 0 (eg, as if we had just started ^X
3964 * mode).
3965 * For mode 0, we set "compl_cont_mode" to an impossible
3966 * value, in both cases ^X^X can be used to restart the same
3967 * mode (avoiding ADDING mode).
3968 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3969 * 'complete' and local ^P expansions respectively.
3970 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3971 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (c == Ctrl_X)
3973 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 if (compl_cont_mode != 0)
3975 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003979 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 edit_submode = NULL;
3981 showmode();
3982 break;
3983 }
3984 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003985 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 /* We're already in CTRL-X mode, do we stay in it? */
3988 if (!vim_is_ctrl_x_key(c))
3989 {
3990 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003991 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 else
3993 ctrl_x_mode = CTRL_X_FINISHED;
3994 edit_submode = NULL;
3995 }
3996 showmode();
3997 }
3998
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003999 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
4001 /* Show error message from attempted keyword completion (probably
4002 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004003 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004005 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4006 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 || ctrl_x_mode == CTRL_X_FINISHED)
4008 {
4009 /* Get here when we have finished typing a sequence of ^N and
4010 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004011 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004012 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
4014 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004015 * If any of the original typed text has been changed, eg when
4016 * ignorecase is set, we must add back-spaces to the redo
4017 * buffer. We add as few as necessary to delete just the part
4018 * of the original text that has changed.
4019 * When using the longest match, edited the match or used
4020 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004022 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4023 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004024 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004025 ptr = NULL;
4026 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029#ifdef FEAT_CINDENT
4030 want_cindent = (can_cindent && cindent_on());
4031#endif
4032 /*
4033 * When completing whole lines: fix indent for 'cindent'.
4034 * Otherwise, break line if it's too long.
4035 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004036 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038#ifdef FEAT_CINDENT
4039 /* re-indent the current line */
4040 if (want_cindent)
4041 {
4042 do_c_expr_indent();
4043 want_cindent = FALSE; /* don't do it again */
4044 }
4045#endif
4046 }
4047 else
4048 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004049 int prev_col = curwin->w_cursor.col;
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004052 if (prev_col > 0)
4053 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004054 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004055 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004057 if (prev_col > 0
4058 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4059 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004062 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004063 * the selection without inserting anything. When
4064 * compl_enter_selects is set the Enter key does the same. */
4065 if ((c == Ctrl_Y || (compl_enter_selects
4066 && (c == CAR || c == K_KENTER || c == NL)))
4067 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004068 retval = TRUE;
4069
Bram Moolenaar47247282016-08-02 22:36:02 +02004070 /* CTRL-E means completion is Ended, go back to the typed text.
4071 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004072 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004073 {
4074 ins_compl_delete();
4075 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004076 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004077 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004078 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004079 retval = TRUE;
4080 }
4081
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004082 auto_format(FALSE, TRUE);
4083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 compl_started = FALSE;
4086 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004087 if (!shortmess(SHM_COMPLETIONMENU))
4088 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004089 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004090 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (edit_submode != NULL)
4092 {
4093 edit_submode = NULL;
4094 showmode();
4095 }
4096
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004097#ifdef FEAT_CMDWIN
4098 if (c == Ctrl_C && cmdwin_type != 0)
4099 /* Avoid the popup menu remains displayed when leaving the
4100 * command line window. */
4101 update_screen(0);
4102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#ifdef FEAT_CINDENT
4104 /*
4105 * Indent now if a key was typed that is in 'cinkeys'.
4106 */
4107 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4108 do_c_expr_indent();
4109#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004110 /* Trigger the CompleteDone event to give scripts a chance to act
4111 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004112 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004115 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4116 /* Trigger the CompleteDone event to give scripts a chance to act
4117 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004118 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 /* reset continue_* if we left expansion-mode, if we stay they'll be
4121 * (re)set properly in ins_complete() */
4122 if (!vim_is_ctrl_x_key(c))
4123 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 compl_cont_status = 0;
4125 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004127
4128 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129}
4130
4131/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004132 * Fix the redo buffer for the completion leader replacing some of the typed
4133 * text. This inserts backspaces and appends the changed text.
4134 * "ptr" is the known leader text or NUL.
4135 */
4136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004138{
4139 int len;
4140 char_u *p;
4141 char_u *ptr = ptr_arg;
4142
4143 if (ptr == NULL)
4144 {
4145 if (compl_leader != NULL)
4146 ptr = compl_leader;
4147 else
4148 return; /* nothing to do */
4149 }
4150 if (compl_orig_text != NULL)
4151 {
4152 p = compl_orig_text;
4153 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4154 ;
4155#ifdef FEAT_MBYTE
4156 if (len > 0)
4157 len -= (*mb_head_off)(p, p + len);
4158#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004159 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004160 AppendCharToRedobuff(K_BS);
4161 }
4162 else
4163 len = 0;
4164 if (ptr != NULL)
4165 AppendToRedobuffLit(ptr + len, -1);
4166}
4167
4168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4170 * (depending on flag) starting from buf and looking for a non-scanned
4171 * buffer (other than curbuf). curbuf is special, if it is called with
4172 * buf=curbuf then it has to be the first call for a given flag/expansion.
4173 *
4174 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4175 */
4176 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 if (flag == 'w') /* just windows */
4182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (buf == curbuf) /* first call for this flag/expansion */
4184 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004185 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 && wp->w_buffer->b_scanned)
4187 ;
4188 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 else
4191 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4192 * (unlisted buffers)
4193 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004194 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 && ((flag == 'U'
4196 ? buf->b_p_bl
4197 : (!buf->b_p_bl
4198 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004199 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 ;
4201 return buf;
4202}
4203
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004204#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004205/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004206 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004207 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004208 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210expand_by_function(
4211 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4212 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004213{
Bram Moolenaar82139082011-09-14 16:52:09 +02004214 list_T *matchlist = NULL;
4215 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004216 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004217 char_u *funcname;
4218 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004219 win_T *curwin_save;
4220 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004221 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004222
Bram Moolenaare344bea2005-09-01 20:46:49 +00004223 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4224 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004225 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004226
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004227 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004228 args[0].v_type = VAR_NUMBER;
4229 args[0].vval.v_number = 0;
4230 args[1].v_type = VAR_STRING;
4231 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4232 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004233
Bram Moolenaare344bea2005-09-01 20:46:49 +00004234 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004235 curwin_save = curwin;
4236 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004237
4238 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004239 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004240 {
4241 switch (rettv.v_type)
4242 {
4243 case VAR_LIST:
4244 matchlist = rettv.vval.v_list;
4245 break;
4246 case VAR_DICT:
4247 matchdict = rettv.vval.v_dict;
4248 break;
4249 default:
4250 /* TODO: Give error message? */
4251 clear_tv(&rettv);
4252 break;
4253 }
4254 }
4255
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004256 if (curwin_save != curwin || curbuf_save != curbuf)
4257 {
4258 EMSG(_(e_complwin));
4259 goto theend;
4260 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004261 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004262 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004263 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004264 {
4265 EMSG(_(e_compldel));
4266 goto theend;
4267 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004268
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004269 if (matchlist != NULL)
4270 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004271 else if (matchdict != NULL)
4272 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004273
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004274theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004275 if (matchdict != NULL)
4276 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004277 if (matchlist != NULL)
4278 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004279}
4280#endif /* FEAT_COMPL_FUNC */
4281
Bram Moolenaar39f05632006-03-19 22:15:26 +00004282#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004283/*
4284 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004285 */
4286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004287ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004288{
4289 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004290 int dir = compl_direction;
4291
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004292 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004293 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004294 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004295 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4296 /* if dir was BACKWARD then honor it just once */
4297 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004298 else if (did_emsg)
4299 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004300 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004301}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004302
4303/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004304 * Add completions from a dict.
4305 */
4306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004307ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004308{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004309 dictitem_T *di_refresh;
4310 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004311
4312 /* Check for optional "refresh" item. */
4313 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004314 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4315 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004316 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004317 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004318
4319 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4320 compl_opt_refresh_always = TRUE;
4321 }
4322
4323 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004324 di_words = dict_find(dict, (char_u *)"words", 5);
4325 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4326 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004327}
4328
4329/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004330 * Add a match to the list of matches from a typeval_T.
4331 * If the given string is already in the list of completions, then return
4332 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4333 * maybe because alloc() returns NULL, then FAIL is returned.
4334 */
4335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004336ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004337{
4338 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004339 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004340 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004341 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004342 char_u *(cptext[CPT_COUNT]);
4343
4344 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4345 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004346 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4347 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004348 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004349 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004350 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004351 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004352 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004353 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004354 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004355 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004356 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004357 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4358 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4359 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4360 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4361 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4362 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004363 }
4364 else
4365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004366 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004367 vim_memset(cptext, 0, sizeof(cptext));
4368 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004369 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004370 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004371 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004372}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004373#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375/*
4376 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004377 * The search starts at position "ini" in curbuf and in the direction
4378 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004379 * When "compl_started" is FALSE start at that position, otherwise continue
4380 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381 * This may return before finding all the matches.
4382 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 */
4384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004385ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
4387 static pos_T first_match_pos;
4388 static pos_T last_match_pos;
4389 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 static int found_all = FALSE; /* Found all matches of a
4391 certain type. */
4392 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
Bram Moolenaar572cb562005-08-05 21:35:02 +00004394 pos_T *pos;
4395 char_u **matches;
4396 int save_p_scs;
4397 int save_p_ws;
4398 int save_p_ic;
4399 int i;
4400 int num_matches;
4401 int len;
4402 int found_new_match;
4403 int type = ctrl_x_mode;
4404 char_u *ptr;
4405 char_u *dict = NULL;
4406 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004407 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004411 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 ins_buf->b_scanned = 0;
4413 found_all = FALSE;
4414 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004416 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 last_match_pos = first_match_pos = *ini;
4418 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004419 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4420 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar4475b622017-05-01 20:46:52 +02004422 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004423 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004424
4425 /*
4426 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 for (;;)
4429 {
4430 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004431 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 * or if found_all says this entry is done. For ^X^L only use the
4435 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004436 if ((ctrl_x_mode == CTRL_X_NORMAL
4437 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 found_all = FALSE;
4441 while (*e_cpt == ',' || *e_cpt == ' ')
4442 e_cpt++;
4443 if (*e_cpt == '.' && !curbuf->b_scanned)
4444 {
4445 ins_buf = curbuf;
4446 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004447 /* Move the cursor back one character so that ^N can match the
4448 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004449 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004450 {
4451 /* Move the cursor to after the last character in the
4452 * buffer, so that word at start of buffer is found
4453 * correctly. */
4454 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4455 first_match_pos.col =
4456 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 last_match_pos = first_match_pos;
4459 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004460
4461 /* Remember the first match so that the loop stops when we
4462 * wrap and come back there a second time. */
4463 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4466 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4467 {
4468 /* Scan a buffer, but not the current one. */
4469 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4470 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 first_match_pos.col = last_match_pos.col = 0;
4473 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4474 last_match_pos.lnum = 0;
4475 type = 0;
4476 }
4477 else /* unloaded buffer, scan like dictionary */
4478 {
4479 found_all = TRUE;
4480 if (ins_buf->b_fname == NULL)
4481 continue;
4482 type = CTRL_X_DICTIONARY;
4483 dict = ins_buf->b_fname;
4484 dict_f = DICT_EXACT;
4485 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004486 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 ins_buf->b_fname == NULL
4488 ? buf_spname(ins_buf)
4489 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004490 ? ins_buf->b_fname
4491 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004492 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 else if (*e_cpt == NUL)
4495 break;
4496 else
4497 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004498 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 type = -1;
4500 else if (*e_cpt == 'k' || *e_cpt == 's')
4501 {
4502 if (*e_cpt == 'k')
4503 type = CTRL_X_DICTIONARY;
4504 else
4505 type = CTRL_X_THESAURUS;
4506 if (*++e_cpt != ',' && *e_cpt != NUL)
4507 {
4508 dict = e_cpt;
4509 dict_f = DICT_FIRST;
4510 }
4511 }
4512#ifdef FEAT_FIND_ID
4513 else if (*e_cpt == 'i')
4514 type = CTRL_X_PATH_PATTERNS;
4515 else if (*e_cpt == 'd')
4516 type = CTRL_X_PATH_DEFINES;
4517#endif
4518 else if (*e_cpt == ']' || *e_cpt == 't')
4519 {
4520 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004521 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004522 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 else
4525 type = -1;
4526
4527 /* in any case e_cpt is advanced to the next entry */
4528 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4529
4530 found_all = TRUE;
4531 if (type == -1)
4532 continue;
4533 }
4534 }
4535
Bram Moolenaar4475b622017-05-01 20:46:52 +02004536 /* If complete() was called then compl_pattern has been reset. The
4537 * following won't work then, bail out. */
4538 if (compl_pattern == NULL)
4539 break;
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 switch (type)
4542 {
4543 case -1:
4544 break;
4545#ifdef FEAT_FIND_ID
4546 case CTRL_X_PATH_PATTERNS:
4547 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004548 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4553 (linenr_T)1, (linenr_T)MAXLNUM);
4554 break;
4555#endif
4556
4557 case CTRL_X_DICTIONARY:
4558 case CTRL_X_THESAURUS:
4559 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004560 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 : (type == CTRL_X_THESAURUS
4562 ? (*curbuf->b_p_tsr == NUL
4563 ? p_tsr
4564 : curbuf->b_p_tsr)
4565 : (*curbuf->b_p_dict == NUL
4566 ? p_dict
4567 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004568 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004569 dict != NULL ? dict_f
4570 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 dict = NULL;
4572 break;
4573
4574 case CTRL_X_TAGS:
4575 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4576 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004579 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 * of matches is found when compl_pattern is empty */
4581 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004582 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4583 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4585 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004586 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 p_ic = save_p_ic;
4589 break;
4590
4591 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4594 {
4595
4596 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004598 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600 break;
4601
4602 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 if (expand_cmdline(&compl_xp, compl_pattern,
4604 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004606 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 break;
4608
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004609#ifdef FEAT_COMPL_FUNC
4610 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004611 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004612 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004613 break;
4614#endif
4615
Bram Moolenaar488c6512005-08-11 20:09:58 +00004616 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004617#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004618 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004619 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004620 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004621 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004622#endif
4623 break;
4624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 default: /* normal ^P/^N and ^X^L */
4626 /*
4627 * If 'infercase' is set, don't use 'smartcase' here
4628 */
4629 save_p_scs = p_scs;
4630 if (ins_buf->b_p_inf)
4631 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632
Bram Moolenaar361aa502014-01-23 22:45:58 +01004633 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 * end but never from the middle, thus setting nowrapscan in this
4635 * buffers is a good idea, on the other hand, we always set
4636 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4637 save_p_ws = p_ws;
4638 if (ins_buf != curbuf)
4639 p_ws = FALSE;
4640 else if (*e_cpt == '.')
4641 p_ws = TRUE;
4642 for (;;)
4643 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004644 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004646 ++msg_silent; /* Don't want messages for wrapscan. */
4647
Bram Moolenaare4214502015-03-05 18:08:43 +01004648 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4649 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004650 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004651 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004654 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004656 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004657 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004659 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004660 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004661 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004663 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004664 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 first_match_pos = *pos;
4666 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004667 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004670 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 found_new_match = FAIL;
4672 if (found_new_match == FAIL)
4673 {
4674 if (ins_buf == curbuf)
4675 found_all = TRUE;
4676 break;
4677 }
4678
4679 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 && ini->lnum == pos->lnum
4682 && ini->col == pos->col)
4683 continue;
4684 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004685 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4690 continue;
4691 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4692 if (!p_paste)
4693 ptr = skipwhite(ptr);
4694 }
4695 len = (int)STRLEN(ptr);
4696 }
4697 else
4698 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004699 char_u *tmp_ptr = ptr;
4700
4701 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004703 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 /* Skip if already inside a word. */
4705 if (vim_iswordp(tmp_ptr))
4706 continue;
4707 /* Find start of next word. */
4708 tmp_ptr = find_word_start(tmp_ptr);
4709 }
4710 /* Find end of this word. */
4711 tmp_ptr = find_word_end(tmp_ptr);
4712 len = (int)(tmp_ptr - ptr);
4713
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 if ((compl_cont_status & CONT_ADDING)
4715 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4718 {
4719 /* Try next line, if any. the new word will be
4720 * "join" as if the normal command "J" was used.
4721 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004722 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 * works -- Acevedo */
4724 STRNCPY(IObuff, ptr, len);
4725 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4726 tmp_ptr = ptr = skipwhite(ptr);
4727 /* Find start of next word. */
4728 tmp_ptr = find_word_start(tmp_ptr);
4729 /* Find end of next word. */
4730 tmp_ptr = find_word_end(tmp_ptr);
4731 if (tmp_ptr > ptr)
4732 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004733 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004735 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 IObuff[len++] = ' ';
4737 /* IObuf =~ "\k.* ", thus len >= 2 */
4738 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004739 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 || (vim_strchr(p_cpo, CPO_JOINSP)
4741 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004742 && (IObuff[len - 2] == '?'
4743 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 IObuff[len++] = ' ';
4745 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004746 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (tmp_ptr - ptr >= IOSIZE - len)
4748 tmp_ptr = ptr + IOSIZE - len - 1;
4749 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4750 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004751 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 IObuff[len] = NUL;
4754 ptr = IObuff;
4755 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 continue;
4758 }
4759 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004760 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004761 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004762 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 found_new_match = OK;
4765 break;
4766 }
4767 }
4768 p_scs = save_p_scs;
4769 p_ws = save_p_ws;
4770 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004771
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004772 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004773 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004774 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 found_new_match = OK;
4776
4777 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004778 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4779 * match */
4780 if ((ctrl_x_mode != CTRL_X_NORMAL
4781 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004782 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004783 {
4784 if (got_int)
4785 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004786 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004787 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004788 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004789
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004790 if ((ctrl_x_mode != CTRL_X_NORMAL
4791 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004792 || compl_interrupted)
4793 break;
4794 compl_started = TRUE;
4795 }
4796 else
4797 {
4798 /* Mark a buffer scanned when it has been scanned completely */
4799 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4800 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004802 compl_started = FALSE;
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004807 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 && *e_cpt == NUL) /* Got to end of 'complete' */
4809 found_new_match = FAIL;
4810
4811 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004812 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4813 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 i = ins_compl_make_cyclic();
4815
Bram Moolenaar4475b622017-05-01 20:46:52 +02004816 if (compl_old_match != NULL)
4817 {
4818 /* If several matches were added (FORWARD) or the search failed and has
4819 * just been made cyclic then we have to move compl_curr_match to the
4820 * next or previous entry (if any) -- Acevedo */
4821 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4822 : compl_old_match->cp_prev;
4823 if (compl_curr_match == NULL)
4824 compl_curr_match = compl_old_match;
4825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 return i;
4827}
4828
4829/* Delete the old text being completed. */
4830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004831ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004833 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 /*
4836 * In insert mode: Delete the typed part.
4837 * In replace mode: Put the old characters back, if any.
4838 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004839 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4840 if ((int)curwin->w_cursor.col > col)
4841 {
4842 if (stop_arrow() == FAIL)
4843 return;
4844 backspace_until_column(col);
4845 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004846
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004847 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4848 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004850 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004851 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852}
4853
Bram Moolenaar472e8592016-10-15 17:06:47 +02004854/*
4855 * Insert the new text being completed.
4856 * "in_compl_func" is TRUE when called from complete_check().
4857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004859ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004861 dict_T *dict;
4862
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004863 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004864 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4865 compl_used_match = FALSE;
4866 else
4867 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004868
4869 /* Set completed item. */
4870 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004871 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004872 if (dict != NULL)
4873 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004874 dict_add_string(dict, "word", compl_shown_match->cp_str);
4875 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4876 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4877 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4878 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4879 dict_add_string(dict, "user_data",
4880 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004881 }
4882 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004883 if (!in_compl_func)
4884 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885}
4886
4887/*
4888 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004889 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4890 * get more completions. If it is FALSE, then we just do nothing when there
4891 * are no more completions in a given direction. The latter case is used when
4892 * we are still in the middle of finding completions, to allow browsing
4893 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 * Return the total number of matches, or -1 if still unknown -- webb.
4895 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004896 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4897 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 *
4899 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004900 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4901 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 */
4903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004904ins_compl_next(
4905 int allow_get_expansion,
4906 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004907 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004908 int insert_match, /* Insert the newly selected match */
4909 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
4911 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004912 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004913 compl_T *found_compl = NULL;
4914 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004915 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004916 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004918 /* When user complete function return -1 for findstart which is next
4919 * time of 'always', compl_shown_match become NULL. */
4920 if (compl_shown_match == NULL)
4921 return -1;
4922
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004923 if (compl_leader != NULL
4924 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004926 /* Set "compl_shown_match" to the actually shown match, it may differ
4927 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004928 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004929 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004930 && compl_shown_match->cp_next != NULL
4931 && compl_shown_match->cp_next != compl_first_match)
4932 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004933
4934 /* If we didn't find it searching forward, and compl_shows_dir is
4935 * backward, find the last match. */
4936 if (compl_shows_dir == BACKWARD
4937 && !ins_compl_equal(compl_shown_match,
4938 compl_leader, (int)STRLEN(compl_leader))
4939 && (compl_shown_match->cp_next == NULL
4940 || compl_shown_match->cp_next == compl_first_match))
4941 {
4942 while (!ins_compl_equal(compl_shown_match,
4943 compl_leader, (int)STRLEN(compl_leader))
4944 && compl_shown_match->cp_prev != NULL
4945 && compl_shown_match->cp_prev != compl_first_match)
4946 compl_shown_match = compl_shown_match->cp_prev;
4947 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004948 }
4949
4950 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004951 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /* Delete old text to be replaced */
4953 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004954
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004955 /* When finding the longest common text we stick at the original text,
4956 * don't let CTRL-N or CTRL-P move to the first match. */
4957 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4958
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004959 /* When restarting the search don't insert the first match either. */
4960 if (compl_restarting)
4961 {
4962 advance = FALSE;
4963 compl_restarting = FALSE;
4964 }
4965
Bram Moolenaare3226be2005-12-18 22:10:00 +00004966 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4967 * around. */
4968 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004972 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004973 found_end = (compl_first_match != NULL
4974 && (compl_shown_match->cp_next == compl_first_match
4975 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004976 }
4977 else if (compl_shows_dir == BACKWARD
4978 && compl_shown_match->cp_prev != NULL)
4979 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004980 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004982 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004985 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004986 if (!allow_get_expansion)
4987 {
4988 if (advance)
4989 {
4990 if (compl_shows_dir == BACKWARD)
4991 compl_pending -= todo + 1;
4992 else
4993 compl_pending += todo + 1;
4994 }
4995 return -1;
4996 }
4997
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004998 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004999 {
5000 if (compl_shows_dir == BACKWARD)
5001 --compl_pending;
5002 else
5003 ++compl_pending;
5004 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005005
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005006 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005007 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005008
5009 /* handle any pending completions */
5010 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005011 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005012 {
5013 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5014 {
5015 compl_shown_match = compl_shown_match->cp_next;
5016 --compl_pending;
5017 }
5018 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5019 {
5020 compl_shown_match = compl_shown_match->cp_prev;
5021 ++compl_pending;
5022 }
5023 else
5024 break;
5025 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005026 found_end = FALSE;
5027 }
5028 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5029 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005030 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005031 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005032 ++todo;
5033 else
5034 /* Remember a matching item. */
5035 found_compl = compl_shown_match;
5036
5037 /* Stop at the end of the list when we found a usable match. */
5038 if (found_end)
5039 {
5040 if (found_compl != NULL)
5041 {
5042 compl_shown_match = found_compl;
5043 break;
5044 }
5045 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005049 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005050 if (compl_no_insert && !started)
5051 {
5052 ins_bytes(compl_orig_text + ins_compl_len());
5053 compl_used_match = FALSE;
5054 }
5055 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005056 {
5057 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005058 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005060 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005061 }
5062 else
5063 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
5065 if (!allow_get_expansion)
5066 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005067 /* may undisplay the popup menu first */
5068 ins_compl_upd_pum();
5069
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005070 /* redraw to show the user what was inserted */
5071 update_screen(0);
5072
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005073 /* display the updated popup menu */
5074 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005075#ifdef FEAT_GUI
5076 if (gui.in_use)
5077 {
5078 /* Show the cursor after the match, not after the redrawn text. */
5079 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005080 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005081 }
5082#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 /* Delete old text to be replaced, since we're still searching and
5085 * don't want to match ourselves! */
5086 ins_compl_delete();
5087 }
5088
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005089 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005090 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005091 if (compl_no_insert && !started)
5092 compl_enter_selects = TRUE;
5093 else
5094 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /*
5097 * Show the file name for the match (if any)
5098 * Truncate the file name to avoid a wait for return.
5099 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005100 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005102 char *lead = _("match in file");
5103 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5104 char_u *s;
5105 char_u *e;
5106
5107 if (space > 0)
5108 {
5109 /* We need the tail that fits. With double-byte encoding going
5110 * back from the end is very slow, thus go from the start and keep
5111 * the text that fits in "space" between "s" and "e". */
5112 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5113 {
5114 space -= ptr2cells(e);
5115 while (space < 0)
5116 {
5117 space += ptr2cells(s);
5118 MB_PTR_ADV(s);
5119 }
5120 }
5121 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5122 s > compl_shown_match->cp_fname ? "<" : "", s);
5123 msg(IObuff);
5124 redraw_cmdline = FALSE; /* don't overwrite! */
5125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127
5128 return num_matches;
5129}
5130
5131/*
5132 * Call this while finding completions, to check whether the user has hit a key
5133 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005134 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005136 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005137 * "in_compl_func" is TRUE when called from complete_check(), don't set
5138 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 */
5140 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005141ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005144 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005146 /* Don't check when reading keys from a script, :normal or feedkeys().
5147 * That would break the test scripts. But do check for keys when called
5148 * from complete_check(). */
5149 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 return;
5151
5152 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005153 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 return;
5155 count = 0;
5156
Bram Moolenaara260a972006-06-23 19:36:29 +00005157 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5158 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (c != NUL)
5161 {
5162 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5163 {
5164 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005165 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005166 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005167 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005169 else
5170 {
5171 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005172 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005173 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005174 if (c != K_IGNORE)
5175 {
5176 /* Don't interrupt completion when the character wasn't typed,
5177 * e.g., when doing @q to replay keys. */
5178 if (c != Ctrl_R && KeyTyped)
5179 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005180
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005181 vungetc(c);
5182 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005185 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005186 {
5187 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5188
5189 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005190 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005191 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005192}
5193
5194/*
5195 * Decide the direction of Insert mode complete from the key typed.
5196 * Returns BACKWARD or FORWARD.
5197 */
5198 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005199ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005200{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005201 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005202 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005203 return BACKWARD;
5204 return FORWARD;
5205}
5206
5207/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005208 * Return TRUE for keys that are used for completion only when the popup menu
5209 * is visible.
5210 */
5211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005212ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005213{
5214 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005215 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5216 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005217}
5218
5219/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005220 * Decide the number of completions to move forward.
5221 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5222 */
5223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005224ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005225{
5226 int h;
5227
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005228 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005229 {
5230 h = pum_get_height();
5231 if (h > 3)
5232 h -= 2; /* keep some context */
5233 return h;
5234 }
5235 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236}
5237
5238/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005239 * Return TRUE if completion with "c" should insert the match, FALSE if only
5240 * to change the currently selected completion.
5241 */
5242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005243ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005244{
5245 switch (c)
5246 {
5247 case K_UP:
5248 case K_DOWN:
5249 case K_PAGEDOWN:
5250 case K_KPAGEDOWN:
5251 case K_S_DOWN:
5252 case K_PAGEUP:
5253 case K_KPAGEUP:
5254 case K_S_UP:
5255 return FALSE;
5256 }
5257 return TRUE;
5258}
5259
5260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 * Do Insert mode completion.
5262 * Called when character "c" was typed, which has a meaning for completion.
5263 * Returns OK if completion was done, FAIL if something failed (out of mem).
5264 */
5265 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005266ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 char_u *line;
5269 int startcol = 0; /* column where searched text starts */
5270 colnr_T curs_col; /* cursor column */
5271 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005272 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005273 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005274 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005275 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
Bram Moolenaare3226be2005-12-18 22:10:00 +00005277 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005278 insert_match = ins_compl_use_match(c);
5279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 /* First time we hit ^N or ^P (in a row, I mean) */
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 did_ai = FALSE;
5285#ifdef FEAT_SMARTINDENT
5286 did_si = FALSE;
5287 can_si = FALSE;
5288 can_si_back = FALSE;
5289#endif
5290 if (stop_arrow() == FAIL)
5291 return FAIL;
5292
5293 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005295 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005297 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 * "compl_startpos" to the cursor as a pattern to add a new word
5299 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005300 * "compl_startpos" is not in the same line as the cursor then fix it
5301 * (the line has been split because it was longer than 'tw'). if SOL
5302 * is set then skip the previous pattern, a word at the beginning of
5303 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005304 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5305 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 /*
5308 * it is a continued search
5309 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005311 if (ctrl_x_mode == CTRL_X_NORMAL
5312 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5313 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 /* line (probably) wrapped, set compl_startpos to the
5318 * first non_blank in the line, if it is not a wordchar
5319 * include it to get a better pattern, but then we don't
5320 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005321 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_startpos.col = compl_col;
5323 compl_startpos.lnum = curwin->w_cursor.lnum;
5324 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
5326 else
5327 {
5328 /* S_IPOS was set when we inserted a word that was at the
5329 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 * mode but first we need to redefine compl_startpos */
5331 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 compl_cont_status |= CONT_SOL;
5334 compl_startpos.col = (colnr_T)(skipwhite(
5335 line + compl_length
5336 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005341 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005342 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_cont_status &= ~CONT_SOL;
5347 compl_length = (IOSIZE - MIN_SPACE);
5348 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5351 if (compl_length < 1)
5352 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005354 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005365 if (ctrl_x_mode != CTRL_X_NORMAL)
5366 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 compl_cont_status = 0;
5368 compl_cont_status |= CONT_N_ADDS;
5369 compl_startpos = curwin->w_cursor;
5370 startcol = (int)curs_col;
5371 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373
5374 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005375 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5379 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 compl_col += ++startcol;
5385 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005388 compl_pattern = str_foldcase(line + compl_col,
5389 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 compl_pattern = vim_strnsave(line + compl_col,
5392 compl_length);
5393 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 return FAIL;
5395 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 char_u *prefix = (char_u *)"\\<";
5399
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005400 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005402 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 if (!vim_iswordp(line + compl_col)
5406 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 && (
5408#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005411 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
5413 )))
5414 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 STRCPY((char *)compl_pattern, prefix);
5416 (void)quote_meta(compl_pattern + STRLEN(prefix),
5417 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#endif
5425 )
5426 {
5427 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5429 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 compl_col += curs_col;
5432 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434 else
5435 {
5436#ifdef FEAT_MBYTE
5437 /* Search the point of change class of multibyte character
5438 * or not a word single byte character backward. */
5439 if (has_mbyte)
5440 {
5441 int base_class;
5442 int head_off;
5443
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005444 startcol -= (*mb_head_off)(line, line + startcol);
5445 base_class = mb_get_class(line + startcol);
5446 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005448 head_off = (*mb_head_off)(line, line + startcol);
5449 if (base_class != mb_get_class(line + startcol
5450 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454 }
5455 else
5456#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005459 compl_col += ++startcol;
5460 compl_length = (int)curs_col - startcol;
5461 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 /* Only match word with at least two chars -- webb
5464 * there's no need to call quote_meta,
5465 * alloc(7) is enough -- Acevedo
5466 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 compl_pattern = alloc(7);
5468 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 STRCPY((char *)compl_pattern, "\\<");
5471 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5472 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 else
5475 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005477 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 STRCPY((char *)compl_pattern, "\\<");
5481 (void)quote_meta(compl_pattern + 2, line + compl_col,
5482 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 }
5485 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005486 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005488 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005489 compl_length = (int)curs_col - (int)compl_col;
5490 if (compl_length < 0) /* cursor in indent: empty pattern */
5491 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005493 compl_pattern = str_foldcase(line + compl_col, compl_length,
5494 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005496 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5497 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 return FAIL;
5499 }
5500 else if (ctrl_x_mode == CTRL_X_FILES)
5501 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005502 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005503 if (startcol > 0)
5504 {
5505 char_u *p = line + startcol;
5506
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005507 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005508 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005509 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005510 if (p == line && vim_isfilec(PTR2CHAR(p)))
5511 startcol = 0;
5512 else
5513 startcol = (int)(p - line) + 1;
5514 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005515
Bram Moolenaar0300e462013-09-08 16:03:45 +02005516 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005517 compl_length = (int)curs_col - startcol;
5518 compl_pattern = addstar(line + compl_col, compl_length,
5519 EXPAND_FILES);
5520 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 return FAIL;
5522 }
5523 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5524 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 compl_pattern = vim_strnsave(line, curs_col);
5526 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005529 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5531 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005532 /* No completion possible, use an empty pattern to get a
5533 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005534 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005535 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005536 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5537 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005539 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005540 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005541#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005542 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005543 * Call user defined function 'completefunc' with "a:findstart"
5544 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005545 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005546 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005547 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005548 char_u *funcname;
5549 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005550 win_T *curwin_save;
5551 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005552
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005553 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005554 * string */
5555 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5556 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5557 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005558 {
5559 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5560 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005561 /* restore did_ai, so that adding comment leader works */
5562 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005563 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005564 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005565
Bram Moolenaarffa96842018-06-12 22:05:14 +02005566 args[0].v_type = VAR_NUMBER;
5567 args[0].vval.v_number = 1;
5568 args[1].v_type = VAR_STRING;
5569 args[1].vval.v_string = (char_u *)"";
5570 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005571 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005572 curwin_save = curwin;
5573 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005574 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005575 if (curwin_save != curwin || curbuf_save != curbuf)
5576 {
5577 EMSG(_(e_complwin));
5578 return FAIL;
5579 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005580 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005581 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005582 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005583 {
5584 EMSG(_(e_compldel));
5585 return FAIL;
5586 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005587
Bram Moolenaar6110a002012-01-26 18:58:38 +01005588 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005589 * cancel the complete without an error.
5590 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005591 if (col == -2)
5592 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005593 if (col == -3)
5594 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005595 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005596 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005597 if (!shortmess(SHM_COMPLETIONMENU))
5598 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005599 return FAIL;
5600 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005601
Bram Moolenaar82139082011-09-14 16:52:09 +02005602 /*
5603 * Reset extended parameters of completion, when start new
5604 * completion.
5605 */
5606 compl_opt_refresh_always = FALSE;
5607
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005608 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005609 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005610 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005611 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005612 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005613
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005614 /* Setup variables for completion. Need to obtain "line" again,
5615 * it may have become invalid. */
5616 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005617 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005618 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5619 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005620#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005621 return FAIL;
5622 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005623 else if (ctrl_x_mode == CTRL_X_SPELL)
5624 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005625#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005626 if (spell_bad_len > 0)
5627 compl_col = curs_col - spell_bad_len;
5628 else
5629 compl_col = spell_word_start(startcol);
5630 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005631 {
5632 compl_length = 0;
5633 compl_col = curs_col;
5634 }
5635 else
5636 {
5637 spell_expand_check_cap(compl_col);
5638 compl_length = (int)curs_col - compl_col;
5639 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005640 /* Need to obtain "line" again, it may have become invalid. */
5641 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005642 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5643 if (compl_pattern == NULL)
5644#endif
5645 return FAIL;
5646 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005647 else
5648 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005649 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005650 return FAIL;
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005653 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005656 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 /* Insert a new line, keep indentation but ignore 'comments' */
5659#ifdef FEAT_COMMENTS
5660 char_u *old = curbuf->b_p_com;
5661
5662 curbuf->b_p_com = (char_u *)"";
5663#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005664 compl_startpos.lnum = curwin->w_cursor.lnum;
5665 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 ins_eol('\r');
5667#ifdef FEAT_COMMENTS
5668 curbuf->b_p_com = old;
5669#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005670 compl_length = 0;
5671 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 }
5674 else
5675 {
5676 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005680 if (compl_cont_status & CONT_LOCAL)
5681 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 else
5683 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5684
Bram Moolenaar62951b12011-09-21 18:23:05 +02005685 /* If any of the original typed text has been changed we need to fix
5686 * the redo buffer. */
5687 ins_compl_fixRedoBufForLeader(NULL);
5688
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005689 /* Always add completion for the original text. */
5690 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005691 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5692 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005693 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005695 VIM_CLEAR(compl_pattern);
5696 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 return FAIL;
5698 }
5699
5700 /* showmode might reset the internal line pointers, so it must
5701 * be called before line = ml_get(), or when this address is no
5702 * longer needed. -- Acevedo.
5703 */
5704 edit_submode_extra = (char_u *)_("-- Searching...");
5705 edit_submode_highl = HLF_COUNT;
5706 showmode();
5707 edit_submode_extra = NULL;
5708 out_flush();
5709 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005710 else if (insert_match && stop_arrow() == FAIL)
5711 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005713 compl_shown_match = compl_curr_match;
5714 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005717 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005719 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005720 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005721 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005723 /* may undisplay the popup menu */
5724 ins_compl_upd_pum();
5725
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005726 if (n > 1) /* all matches have been found */
5727 compl_matches = n;
5728 compl_curr_match = compl_shown_match;
5729 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaard68071d2006-05-02 22:08:30 +00005731 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5732 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (got_int && !global_busy)
5734 {
5735 (void)vgetc();
5736 got_int = FALSE;
5737 }
5738
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005739 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005740 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005742 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5743 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5745 edit_submode_highl = HLF_E;
5746 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5747 * because we couldn't expand anything at first place, but if we used
5748 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5749 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005750 if ( compl_length > 1
5751 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005752 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5754 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005755 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757
Bram Moolenaar572cb562005-08-05 21:35:02 +00005758 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005759 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005761 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 if (edit_submode_extra == NULL)
5764 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005765 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 edit_submode_extra = (char_u *)_("Back at original");
5768 edit_submode_highl = HLF_W;
5769 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005770 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 edit_submode_extra = (char_u *)_("Word from other line");
5773 edit_submode_highl = HLF_COUNT;
5774 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005775 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
5777 edit_submode_extra = (char_u *)_("The only match");
5778 edit_submode_highl = HLF_COUNT;
5779 }
5780 else
5781 {
5782 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005783 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005785 int number = 0;
5786 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005788 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 /* search backwards for the first valid (!= -1) number.
5791 * This should normally succeed already at the first loop
5792 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005793 for (match = compl_curr_match->cp_prev; match != NULL
5794 && match != compl_first_match;
5795 match = match->cp_prev)
5796 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005798 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800 }
5801 if (match != NULL)
5802 /* go up and assign all numbers which are not assigned
5803 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005804 for (match = match->cp_next;
5805 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005806 match = match->cp_next)
5807 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else /* BACKWARD */
5810 {
5811 /* search forwards (upwards) for the first valid (!= -1)
5812 * number. This should normally succeed already at the
5813 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005814 for (match = compl_curr_match->cp_next; match != NULL
5815 && match != compl_first_match;
5816 match = match->cp_next)
5817 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005819 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 if (match != NULL)
5823 /* go down and assign all numbers which are not
5824 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005825 for (match = match->cp_prev; match
5826 && match->cp_number == -1;
5827 match = match->cp_prev)
5828 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830 }
5831
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005832 /* The match should always have a sequence number now, this is
5833 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005834 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005836 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5837 * Translations may need more than twice that. */
5838 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005840 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005841 vim_snprintf((char *)match_ref, sizeof(match_ref),
5842 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005843 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005845 vim_snprintf((char *)match_ref, sizeof(match_ref),
5846 _("match %d"),
5847 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 edit_submode_extra = match_ref;
5849 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005850 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 curs_columns(FALSE);
5852 }
5853 }
5854 }
5855
5856 /* Show a message about what (completion) mode we're in. */
5857 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005858 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005860 if (edit_submode_extra != NULL)
5861 {
5862 if (!p_smd)
5863 msg_attr(edit_submode_extra,
5864 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005865 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005866 }
5867 else
5868 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaard68071d2006-05-02 22:08:30 +00005871 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005872 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005873 show_pum(save_w_wrow, save_w_leftcol);
5874
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005875 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005876 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 return OK;
5879}
5880
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005881 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005882show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005883{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005884 /* RedrawingDisabled may be set when invoked through complete(). */
5885 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005886
Bram Moolenaarcb036422017-03-01 12:29:10 +01005887 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005888
Bram Moolenaarcb036422017-03-01 12:29:10 +01005889 /* If the cursor moved or the display scrolled we need to remove the pum
5890 * first. */
5891 setcursor();
5892 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5893 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005894
Bram Moolenaarcb036422017-03-01 12:29:10 +01005895 ins_compl_show_pum();
5896 setcursor();
5897 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005898}
5899
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900/*
5901 * Looks in the first "len" chars. of "src" for search-metachars.
5902 * If dest is not NULL the chars. are copied there quoting (with
5903 * a backslash) the metachars, and dest would be NUL terminated.
5904 * Returns the length (needed) of dest
5905 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005906 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005909 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005911 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
5913 switch (*src)
5914 {
5915 case '.':
5916 case '*':
5917 case '[':
5918 if (ctrl_x_mode == CTRL_X_DICTIONARY
5919 || ctrl_x_mode == CTRL_X_THESAURUS)
5920 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005921 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 case '~':
5923 if (!p_magic) /* quote these only if magic is set */
5924 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005925 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 case '\\':
5927 if (ctrl_x_mode == CTRL_X_DICTIONARY
5928 || ctrl_x_mode == CTRL_X_THESAURUS)
5929 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005930 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 case '^': /* currently it's not needed. */
5932 case '$':
5933 m++;
5934 if (dest != NULL)
5935 *dest++ = '\\';
5936 break;
5937 }
5938 if (dest != NULL)
5939 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005940# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 /* Copy remaining bytes of a multibyte character. */
5942 if (has_mbyte)
5943 {
5944 int i, mb_len;
5945
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005946 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 if (mb_len > 0 && len >= mb_len)
5948 for (i = 0; i < mb_len; ++i)
5949 {
5950 --len;
5951 ++src;
5952 if (dest != NULL)
5953 *dest++ = *src;
5954 }
5955 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958 if (dest != NULL)
5959 *dest = NUL;
5960
5961 return m;
5962}
5963#endif /* FEAT_INS_EXPAND */
5964
5965/*
5966 * Next character is interpreted literally.
5967 * A one, two or three digit decimal number is interpreted as its byte value.
5968 * If one or two digits are entered, the next character is given to vungetc().
5969 * For Unicode a character > 255 may be returned.
5970 */
5971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005972get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973{
5974 int cc;
5975 int nc;
5976 int i;
5977 int hex = FALSE;
5978 int octal = FALSE;
5979#ifdef FEAT_MBYTE
5980 int unicode = 0;
5981#endif
5982
5983 if (got_int)
5984 return Ctrl_C;
5985
5986#ifdef FEAT_GUI
5987 /*
5988 * In GUI there is no point inserting the internal code for a special key.
5989 * It is more useful to insert the string "<KEY>" instead. This would
5990 * probably be useful in a text window too, but it would not be
5991 * vi-compatible (maybe there should be an option for it?) -- webb
5992 */
5993 if (gui.in_use)
5994 ++allow_keys;
5995#endif
5996#ifdef USE_ON_FLY_SCROLL
5997 dont_scroll = TRUE; /* disallow scrolling here */
5998#endif
5999 ++no_mapping; /* don't map the next key hits */
6000 cc = 0;
6001 i = 0;
6002 for (;;)
6003 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006004 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005#ifdef FEAT_CMDL_INFO
6006 if (!(State & CMDLINE)
6007# ifdef FEAT_MBYTE
6008 && MB_BYTE2LEN_CHECK(nc) == 1
6009# endif
6010 )
6011 add_to_showcmd(nc);
6012#endif
6013 if (nc == 'x' || nc == 'X')
6014 hex = TRUE;
6015 else if (nc == 'o' || nc == 'O')
6016 octal = TRUE;
6017#ifdef FEAT_MBYTE
6018 else if (nc == 'u' || nc == 'U')
6019 unicode = nc;
6020#endif
6021 else
6022 {
6023 if (hex
6024#ifdef FEAT_MBYTE
6025 || unicode != 0
6026#endif
6027 )
6028 {
6029 if (!vim_isxdigit(nc))
6030 break;
6031 cc = cc * 16 + hex2nr(nc);
6032 }
6033 else if (octal)
6034 {
6035 if (nc < '0' || nc > '7')
6036 break;
6037 cc = cc * 8 + nc - '0';
6038 }
6039 else
6040 {
6041 if (!VIM_ISDIGIT(nc))
6042 break;
6043 cc = cc * 10 + nc - '0';
6044 }
6045
6046 ++i;
6047 }
6048
6049 if (cc > 255
6050#ifdef FEAT_MBYTE
6051 && unicode == 0
6052#endif
6053 )
6054 cc = 255; /* limit range to 0-255 */
6055 nc = 0;
6056
6057 if (hex) /* hex: up to two chars */
6058 {
6059 if (i >= 2)
6060 break;
6061 }
6062#ifdef FEAT_MBYTE
6063 else if (unicode) /* Unicode: up to four or eight chars */
6064 {
6065 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6066 break;
6067 }
6068#endif
6069 else if (i >= 3) /* decimal or octal: up to three chars */
6070 break;
6071 }
6072 if (i == 0) /* no number entered */
6073 {
6074 if (nc == K_ZERO) /* NUL is stored as NL */
6075 {
6076 cc = '\n';
6077 nc = 0;
6078 }
6079 else
6080 {
6081 cc = nc;
6082 nc = 0;
6083 }
6084 }
6085
6086 if (cc == 0) /* NUL is stored as NL */
6087 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006088#ifdef FEAT_MBYTE
6089 if (enc_dbcs && (cc & 0xff) == 0)
6090 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6091 second byte will cause trouble! */
6092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
6094 --no_mapping;
6095#ifdef FEAT_GUI
6096 if (gui.in_use)
6097 --allow_keys;
6098#endif
6099 if (nc)
6100 vungetc(nc);
6101 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6102 return cc;
6103}
6104
6105/*
6106 * Insert character, taking care of special keys and mod_mask
6107 */
6108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006109insert_special(
6110 int c,
6111 int allow_modmask,
6112 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 char_u *p;
6115 int len;
6116
6117 /*
6118 * Special function key, translate into "<Key>". Up to the last '>' is
6119 * inserted with ins_str(), so as not to replace characters in replace
6120 * mode.
6121 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6122 * unless 'allow_modmask' is TRUE.
6123 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006124#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 /* Command-key never produces a normal key */
6126 if (mod_mask & MOD_MASK_CMD)
6127 allow_modmask = TRUE;
6128#endif
6129 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6130 {
6131 p = get_special_key_name(c, mod_mask);
6132 len = (int)STRLEN(p);
6133 c = p[len - 1];
6134 if (len > 2)
6135 {
6136 if (stop_arrow() == FAIL)
6137 return;
6138 p[len - 1] = NUL;
6139 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006140 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 ctrlv = FALSE;
6142 }
6143 }
6144 if (stop_arrow() == OK)
6145 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6146}
6147
6148/*
6149 * Special characters in this context are those that need processing other
6150 * than the simple insertion that can be performed here. This includes ESC
6151 * which terminates the insert, and CR/NL which need special processing to
6152 * open up a new line. This routine tries to optimize insertions performed by
6153 * the "redo", "undo" or "put" commands, so it needs to know when it should
6154 * stop and defer processing to the "normal" mechanism.
6155 * '0' and '^' are special, because they can be followed by CTRL-D.
6156 */
6157#ifdef EBCDIC
6158# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6159#else
6160# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6161#endif
6162
6163#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006164# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006166# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167#endif
6168
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006169/*
6170 * "flags": INSCHAR_FORMAT - force formatting
6171 * INSCHAR_CTRLV - char typed just after CTRL-V
6172 * INSCHAR_NO_FEX - don't use 'formatexpr'
6173 *
6174 * NOTE: passes the flags value straight through to internal_format() which,
6175 * beside INSCHAR_FORMAT (above), is also looking for these:
6176 * INSCHAR_DO_COM - format comments
6177 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006180insertchar(
6181 int c, /* character to insert or NUL */
6182 int flags, /* INSCHAR_FORMAT, etc. */
6183 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 int textwidth;
6186#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006190 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006192 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
6195 /*
6196 * Try to break the line in two or more pieces when:
6197 * - Always do this if we have been called to do formatting only.
6198 * - Always do this when 'formatoptions' has the 'a' flag and the line
6199 * ends in white space.
6200 * - Otherwise:
6201 * - Don't do this if inserting a blank
6202 * - Don't do this if an existing character is being replaced, unless
6203 * we're in VREPLACE mode.
6204 * - Do this if the cursor is not on the line where insert started
6205 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6206 * before the insert.
6207 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6208 * before 'textwidth'
6209 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006211 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006212 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 && *ml_get_cursor() != NUL)
6216 && (curwin->w_cursor.lnum != Insstart.lnum
6217 || ((!has_format_option(FO_INS_LONG)
6218 || Insstart_textlen <= (colnr_T)textwidth)
6219 && (!fo_ins_blank
6220 || Insstart_blank_vcol <= (colnr_T)textwidth
6221 ))))))
6222 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 /* Format with 'formatexpr' when it's set. Use internal formatting
6224 * when 'formatexpr' isn't set or it returns non-zero. */
6225#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006226 int do_internal = TRUE;
6227 colnr_T virtcol = get_nolist_virtcol()
6228 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006229
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006230 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6231 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006232 {
6233 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6234 /* It may be required to save for undo again, e.g. when setline()
6235 * was called. */
6236 ins_need_undo = TRUE;
6237 }
6238 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006240 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (c == NUL) /* only formatting was wanted */
6244 return;
6245
6246#ifdef FEAT_COMMENTS
6247 /* Check whether this character should end a comment. */
6248 if (did_ai && (int)c == end_comment_pending)
6249 {
6250 char_u *line;
6251 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6252 int middle_len, end_len;
6253 int i;
6254
6255 /*
6256 * Need to remove existing (middle) comment leader and insert end
6257 * comment leader. First, check what comment leader we can find.
6258 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006259 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6261 {
6262 /* Skip middle-comment string */
6263 while (*p && p[-1] != ':') /* find end of middle flags */
6264 ++p;
6265 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6266 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006267 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 --middle_len;
6269
6270 /* Find the end-comment string */
6271 while (*p && p[-1] != ':') /* find end of end flags */
6272 ++p;
6273 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6274
6275 /* Skip white space before the cursor */
6276 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006277 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 ;
6279 i++;
6280
6281 /* Skip to before the middle leader */
6282 i -= middle_len;
6283
6284 /* Check some expected things before we go on */
6285 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6286 {
6287 /* Backspace over all the stuff we want to replace */
6288 backspace_until_column(i);
6289
6290 /*
6291 * Insert the end-comment string, except for the last
6292 * character, which will get inserted as normal later.
6293 */
6294 ins_bytes_len(lead_end, end_len - 1);
6295 }
6296 }
6297 }
6298 end_comment_pending = NUL;
6299#endif
6300
6301 did_ai = FALSE;
6302#ifdef FEAT_SMARTINDENT
6303 did_si = FALSE;
6304 can_si = FALSE;
6305 can_si_back = FALSE;
6306#endif
6307
6308 /*
6309 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6310 * This speeds up normal text input considerably.
6311 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6312 * need to re-indent at a ':', or any other character (but not what
6313 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006314 * Don't do this when there an InsertCharPre autocommand is defined,
6315 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006316 * Do the check for InsertCharPre before the call to vpeekc() because the
6317 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
6319#ifdef USE_ON_FLY_SCROLL
6320 dont_scroll = FALSE; /* allow scrolling here */
6321#endif
6322
6323 if ( !ISSPECIAL(c)
6324#ifdef FEAT_MBYTE
6325 && (!has_mbyte || (*mb_char2len)(c) == 1)
6326#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006327 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 && vpeekc() != NUL
6329 && !(State & REPLACE_FLAG)
6330#ifdef FEAT_CINDENT
6331 && !cindent_on()
6332#endif
6333#ifdef FEAT_RIGHTLEFT
6334 && !p_ri
6335#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
6338#define INPUT_BUFLEN 100
6339 char_u buf[INPUT_BUFLEN + 1];
6340 int i;
6341 colnr_T virtcol = 0;
6342
6343 buf[0] = c;
6344 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 virtcol = get_nolist_virtcol();
6347 /*
6348 * Stop the string when:
6349 * - no more chars available
6350 * - finding a special character (command key)
6351 * - buffer is full
6352 * - running into the 'textwidth' boundary
6353 * - need to check for abbreviation: A non-word char after a word-char
6354 */
6355 while ( (c = vpeekc()) != NUL
6356 && !ISSPECIAL(c)
6357#ifdef FEAT_MBYTE
6358 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6359#endif
6360 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006361# ifdef FEAT_FKMAP
6362 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 && (textwidth == 0
6365 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6366 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6367 {
6368#ifdef FEAT_RIGHTLEFT
6369 c = vgetc();
6370 if (p_hkmap && KeyTyped)
6371 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 buf[i++] = c;
6373#else
6374 buf[i++] = vgetc();
6375#endif
6376 }
6377
6378#ifdef FEAT_DIGRAPHS
6379 do_digraph(-1); /* clear digraphs */
6380 do_digraph(buf[i-1]); /* may be the start of a digraph */
6381#endif
6382 buf[i] = NUL;
6383 ins_str(buf);
6384 if (flags & INSCHAR_CTRLV)
6385 {
6386 redo_literal(*buf);
6387 i = 1;
6388 }
6389 else
6390 i = 0;
6391 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006392 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394 else
6395 {
6396#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 int cc;
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6400 {
6401 char_u buf[MB_MAXBYTES + 1];
6402
6403 (*mb_char2bytes)(c, buf);
6404 buf[cc] = NUL;
6405 ins_char_bytes(buf, cc);
6406 AppendCharToRedobuff(c);
6407 }
6408 else
6409#endif
6410 {
6411 ins_char(c);
6412 if (flags & INSCHAR_CTRLV)
6413 redo_literal(c);
6414 else
6415 AppendCharToRedobuff(c);
6416 }
6417 }
6418}
6419
6420/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006422 *
6423 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6424 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425 */
6426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006427internal_format(
6428 int textwidth,
6429 int second_indent,
6430 int flags,
6431 int format_only,
6432 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433{
6434 int cc;
6435 int save_char = NUL;
6436 int haveto_redraw = FALSE;
6437 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6438#ifdef FEAT_MBYTE
6439 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6440#endif
6441 int fo_white_par = has_format_option(FO_WHITE_PAR);
6442 int first_line = TRUE;
6443#ifdef FEAT_COMMENTS
6444 colnr_T leader_len;
6445 int no_leader = FALSE;
6446 int do_comments = (flags & INSCHAR_DO_COM);
6447#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006448#ifdef FEAT_LINEBREAK
6449 int has_lbr = curwin->w_p_lbr;
6450
6451 /* make sure win_lbr_chartabsize() counts correctly */
6452 curwin->w_p_lbr = FALSE;
6453#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454
6455 /*
6456 * When 'ai' is off we don't want a space under the cursor to be
6457 * deleted. Replace it with an 'x' temporarily.
6458 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006459 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 {
6461 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006462 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463 {
6464 save_char = cc;
6465 pchar_cursor('x');
6466 }
6467 }
6468
6469 /*
6470 * Repeat breaking lines, until the current line is not too long.
6471 */
6472 while (!got_int)
6473 {
6474 int startcol; /* Cursor column at entry */
6475 int wantcol; /* column at textwidth border */
6476 int foundcol; /* column for start of spaces */
6477 int end_foundcol = 0; /* column for start of word */
6478 colnr_T len;
6479 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480 int orig_col = 0;
6481 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006483 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484
Bram Moolenaar97b98102009-11-17 16:41:01 +00006485 virtcol = get_nolist_virtcol()
6486 + char2cells(c != NUL ? c : gchar_cursor());
6487 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488 break;
6489
6490#ifdef FEAT_COMMENTS
6491 if (no_leader)
6492 do_comments = FALSE;
6493 else if (!(flags & INSCHAR_FORMAT)
6494 && has_format_option(FO_WRAP_COMS))
6495 do_comments = TRUE;
6496
6497 /* Don't break until after the comment leader */
6498 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006499 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006500 else
6501 leader_len = 0;
6502
6503 /* If the line doesn't start with a comment leader, then don't
6504 * start one in a following broken line. Avoids that a %word
6505 * moved to the start of the next line causes all following lines
6506 * to start with %. */
6507 if (leader_len == 0)
6508 no_leader = TRUE;
6509#endif
6510 if (!(flags & INSCHAR_FORMAT)
6511#ifdef FEAT_COMMENTS
6512 && leader_len == 0
6513#endif
6514 && !has_format_option(FO_WRAP))
6515
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006516 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006517 if ((startcol = curwin->w_cursor.col) == 0)
6518 break;
6519
6520 /* find column of textwidth border */
6521 coladvance((colnr_T)textwidth);
6522 wantcol = curwin->w_cursor.col;
6523
Bram Moolenaar97b98102009-11-17 16:41:01 +00006524 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006525 foundcol = 0;
6526
6527 /*
6528 * Find position to break at.
6529 * Stop at first entered white when 'formatoptions' has 'v'
6530 */
6531 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006532 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533 || curwin->w_cursor.lnum != Insstart.lnum
6534 || curwin->w_cursor.col >= Insstart.col)
6535 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006536 if (curwin->w_cursor.col == startcol && c != NUL)
6537 cc = c;
6538 else
6539 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540 if (WHITECHAR(cc))
6541 {
6542 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006543 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006544
6545 /* find start of sequence of blanks */
6546 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6547 {
6548 dec_cursor();
6549 cc = gchar_cursor();
6550 }
6551 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6552 break; /* only spaces in front of text */
6553#ifdef FEAT_COMMENTS
6554 /* Don't break until after the comment leader */
6555 if (curwin->w_cursor.col < leader_len)
6556 break;
6557#endif
6558 if (has_format_option(FO_ONE_LETTER))
6559 {
6560 /* do not break after one-letter words */
6561 if (curwin->w_cursor.col == 0)
6562 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006563#ifdef FEAT_COMMENTS
6564 /* do not break "#a b" when 'tw' is 2 */
6565 if (curwin->w_cursor.col <= leader_len)
6566 break;
6567#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006568 col = curwin->w_cursor.col;
6569 dec_cursor();
6570 cc = gchar_cursor();
6571
6572 if (WHITECHAR(cc))
6573 continue; /* one-letter, continue */
6574 curwin->w_cursor.col = col;
6575 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006576
6577 inc_cursor();
6578
6579 end_foundcol = end_col + 1;
6580 foundcol = curwin->w_cursor.col;
6581 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582 break;
6583 }
6584#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006585 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006586 {
6587 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006588 if (curwin->w_cursor.col != startcol)
6589 {
6590#ifdef FEAT_COMMENTS
6591 /* Don't break until after the comment leader */
6592 if (curwin->w_cursor.col < leader_len)
6593 break;
6594#endif
6595 col = curwin->w_cursor.col;
6596 inc_cursor();
6597 /* Don't change end_foundcol if already set. */
6598 if (foundcol != curwin->w_cursor.col)
6599 {
6600 foundcol = curwin->w_cursor.col;
6601 end_foundcol = foundcol;
6602 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6603 break;
6604 }
6605 curwin->w_cursor.col = col;
6606 }
6607
6608 if (curwin->w_cursor.col == 0)
6609 break;
6610
6611 col = curwin->w_cursor.col;
6612
6613 dec_cursor();
6614 cc = gchar_cursor();
6615
6616 if (WHITECHAR(cc))
6617 continue; /* break with space */
6618#ifdef FEAT_COMMENTS
6619 /* Don't break until after the comment leader */
6620 if (curwin->w_cursor.col < leader_len)
6621 break;
6622#endif
6623
6624 curwin->w_cursor.col = col;
6625
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006628 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6629 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006630 }
6631#endif
6632 if (curwin->w_cursor.col == 0)
6633 break;
6634 dec_cursor();
6635 }
6636
6637 if (foundcol == 0) /* no spaces, cannot break line */
6638 {
6639 curwin->w_cursor.col = startcol;
6640 break;
6641 }
6642
6643 /* Going to break the line, remove any "$" now. */
6644 undisplay_dollar();
6645
6646 /*
6647 * Offset between cursor position and line break is used by replace
6648 * stack functions. VREPLACE does not use this, and backspaces
6649 * over the text instead.
6650 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006651 if (State & VREPLACE_FLAG)
6652 orig_col = startcol; /* Will start backspacing from here */
6653 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006654 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006655
6656 /*
6657 * adjust startcol for spaces that will be deleted and
6658 * characters that will remain on top line
6659 */
6660 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006661 while ((cc = gchar_cursor(), WHITECHAR(cc))
6662 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006663 inc_cursor();
6664 startcol -= curwin->w_cursor.col;
6665 if (startcol < 0)
6666 startcol = 0;
6667
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006668 if (State & VREPLACE_FLAG)
6669 {
6670 /*
6671 * In VREPLACE mode, we will backspace over the text to be
6672 * wrapped, so save a copy now to put on the next line.
6673 */
6674 saved_text = vim_strsave(ml_get_cursor());
6675 curwin->w_cursor.col = orig_col;
6676 if (saved_text == NULL)
6677 break; /* Can't do it, out of memory */
6678 saved_text[startcol] = NUL;
6679
6680 /* Backspace over characters that will move to the next line */
6681 if (!fo_white_par)
6682 backspace_until_column(foundcol);
6683 }
6684 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685 {
6686 /* put cursor after pos. to break line */
6687 if (!fo_white_par)
6688 curwin->w_cursor.col = foundcol;
6689 }
6690
6691 /*
6692 * Split the line just before the margin.
6693 * Only insert/delete lines, but don't really redraw the window.
6694 */
6695 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6696 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6697#ifdef FEAT_COMMENTS
6698 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006699 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006701 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6702 if (!(flags & INSCHAR_COM_LIST))
6703 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006704
6705 replace_offset = 0;
6706 if (first_line)
6707 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006708 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006710 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006711 * This section is for auto-wrap of numeric lists. When not
6712 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6713 * flag will be set and open_line() will handle it (as seen
6714 * above). The code here (and in get_number_indent()) will
6715 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006716 */
6717 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006718 second_indent =
6719 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006720 if (second_indent >= 0)
6721 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006722 if (State & VREPLACE_FLAG)
6723 change_indent(INDENT_SET, second_indent,
6724 FALSE, NUL, TRUE);
6725 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006726#ifdef FEAT_COMMENTS
6727 if (leader_len > 0 && second_indent - leader_len > 0)
6728 {
6729 int i;
6730 int padding = second_indent - leader_len;
6731
6732 /* We started at the first_line of a numbered list
6733 * that has a comment. the open_line() function has
6734 * inserted the proper comment leader and positioned
6735 * the cursor at the end of the split line. Now we
6736 * add the additional whitespace needed after the
6737 * comment leader for the numbered list. */
6738 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006739 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006740 }
6741 else
6742 {
6743#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006744 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006745#ifdef FEAT_COMMENTS
6746 }
6747#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006748 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006749 }
6750 first_line = FALSE;
6751 }
6752
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006753 if (State & VREPLACE_FLAG)
6754 {
6755 /*
6756 * In VREPLACE mode we have backspaced over the text to be
6757 * moved, now we re-insert it into the new line.
6758 */
6759 ins_bytes(saved_text);
6760 vim_free(saved_text);
6761 }
6762 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006763 {
6764 /*
6765 * Check if cursor is not past the NUL off the line, cindent
6766 * may have added or removed indent.
6767 */
6768 curwin->w_cursor.col += startcol;
6769 len = (colnr_T)STRLEN(ml_get_curline());
6770 if (curwin->w_cursor.col > len)
6771 curwin->w_cursor.col = len;
6772 }
6773
6774 haveto_redraw = TRUE;
6775#ifdef FEAT_CINDENT
6776 can_cindent = TRUE;
6777#endif
6778 /* moved the cursor, don't autoindent or cindent now */
6779 did_ai = FALSE;
6780#ifdef FEAT_SMARTINDENT
6781 did_si = FALSE;
6782 can_si = FALSE;
6783 can_si_back = FALSE;
6784#endif
6785 line_breakcheck();
6786 }
6787
6788 if (save_char != NUL) /* put back space after cursor */
6789 pchar_cursor(save_char);
6790
Bram Moolenaar0026d472014-09-09 16:32:39 +02006791#ifdef FEAT_LINEBREAK
6792 curwin->w_p_lbr = has_lbr;
6793#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006794 if (!format_only && haveto_redraw)
6795 {
6796 update_topline();
6797 redraw_curbuf_later(VALID);
6798 }
6799}
6800
6801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 * Called after inserting or deleting text: When 'formatoptions' includes the
6803 * 'a' flag format from the current line until the end of the paragraph.
6804 * Keep the cursor at the same position relative to the text.
6805 * The caller must have saved the cursor line for undo, following ones will be
6806 * saved here.
6807 */
6808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006809auto_format(
6810 int trailblank, /* when TRUE also format with trailing blank */
6811 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813 pos_T pos;
6814 colnr_T len;
6815 char_u *old;
6816 char_u *new, *pnew;
6817 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006818 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 if (!has_format_option(FO_AUTO))
6821 return;
6822
6823 pos = curwin->w_cursor;
6824 old = ml_get_curline();
6825
6826 /* may remove added space */
6827 check_auto_format(FALSE);
6828
6829 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6830 * user might insert normal text next. Also skip formatting when "1" is
6831 * in 'formatoptions' and there is a single character before the cursor.
6832 * Otherwise the line would be broken and when typing another non-white
6833 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006834 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (*old != NUL && !trailblank && wasatend)
6836 {
6837 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006838 cc = gchar_cursor();
6839 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6840 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006842 cc = gchar_cursor();
6843 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
6845 curwin->w_cursor = pos;
6846 return;
6847 }
6848 curwin->w_cursor = pos;
6849 }
6850
6851#ifdef FEAT_COMMENTS
6852 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6853 * comments. */
6854 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006855 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 return;
6857#endif
6858
6859 /*
6860 * May start formatting in a previous line, so that after "x" a word is
6861 * moved to the previous line if it fits there now. Only when this is not
6862 * the start of a paragraph.
6863 */
6864 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6865 {
6866 --curwin->w_cursor.lnum;
6867 if (u_save_cursor() == FAIL)
6868 return;
6869 }
6870
6871 /*
6872 * Do the formatting and restore the cursor position. "saved_cursor" will
6873 * be adjusted for the text formatting.
6874 */
6875 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006876 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 curwin->w_cursor = saved_cursor;
6878 saved_cursor.lnum = 0;
6879
6880 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6881 {
6882 /* "cannot happen" */
6883 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6884 coladvance((colnr_T)MAXCOL);
6885 }
6886 else
6887 check_cursor_col();
6888
6889 /* Insert mode: If the cursor is now after the end of the line while it
6890 * previously wasn't, the line was broken. Because of the rule above we
6891 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6892 * formatted. */
6893 if (!wasatend && has_format_option(FO_WHITE_PAR))
6894 {
6895 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006896 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (curwin->w_cursor.col == len)
6898 {
6899 pnew = vim_strnsave(new, len + 2);
6900 pnew[len] = ' ';
6901 pnew[len + 1] = NUL;
6902 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6903 /* remove the space later */
6904 did_add_space = TRUE;
6905 }
6906 else
6907 /* may remove added space */
6908 check_auto_format(FALSE);
6909 }
6910
6911 check_cursor();
6912}
6913
6914/*
6915 * When an extra space was added to continue a paragraph for auto-formatting,
6916 * delete it now. The space must be under the cursor, just after the insert
6917 * position.
6918 */
6919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006920check_auto_format(
6921 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006924 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926 if (did_add_space)
6927 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006928 cc = gchar_cursor();
6929 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 /* Somehow the space was removed already. */
6931 did_add_space = FALSE;
6932 else
6933 {
6934 if (!end_insert)
6935 {
6936 inc_cursor();
6937 c = gchar_cursor();
6938 dec_cursor();
6939 }
6940 if (c != NUL)
6941 {
6942 /* The space is no longer at the end of the line, delete it. */
6943 del_char(FALSE);
6944 did_add_space = FALSE;
6945 }
6946 }
6947 }
6948}
6949
6950/*
6951 * Find out textwidth to be used for formatting:
6952 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006953 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 * if invalid value, use 0.
6955 * Set default to window width (maximum 79) for "gq" operator.
6956 */
6957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006958comp_textwidth(
6959 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int textwidth;
6962
6963 textwidth = curbuf->b_p_tw;
6964 if (textwidth == 0 && curbuf->b_p_wm)
6965 {
6966 /* The width is the window width minus 'wrapmargin' minus all the
6967 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006968 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_CMDWIN
6970 if (cmdwin_type != 0)
6971 textwidth -= 1;
6972#endif
6973#ifdef FEAT_FOLDING
6974 textwidth -= curwin->w_p_fdc;
6975#endif
6976#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006977 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 textwidth -= 1;
6979#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006980 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 textwidth -= 8;
6982 }
6983 if (textwidth < 0)
6984 textwidth = 0;
6985 if (ff && textwidth == 0)
6986 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006987 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (textwidth > 79)
6989 textwidth = 79;
6990 }
6991 return textwidth;
6992}
6993
6994/*
6995 * Put a character in the redo buffer, for when just after a CTRL-V.
6996 */
6997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006998redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
7000 char_u buf[10];
7001
7002 /* Only digits need special treatment. Translate them into a string of
7003 * three digits. */
7004 if (VIM_ISDIGIT(c))
7005 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007006 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 AppendToRedobuff(buf);
7008 }
7009 else
7010 AppendCharToRedobuff(c);
7011}
7012
7013/*
7014 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007015 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 */
7017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018start_arrow(
7019 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007021 start_arrow_common(end_insert_pos, TRUE);
7022}
7023
7024/*
7025 * Like start_arrow() but with end_change argument.
7026 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7027 */
7028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007029start_arrow_with_change(
7030 pos_T *end_insert_pos, /* can be NULL */
7031 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007032{
7033 start_arrow_common(end_insert_pos, end_change);
7034 if (!end_change)
7035 {
7036 AppendCharToRedobuff(Ctrl_G);
7037 AppendCharToRedobuff('U');
7038 }
7039}
7040
7041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042start_arrow_common(
7043 pos_T *end_insert_pos, /* can be NULL */
7044 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007045{
7046 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {
7048 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007049 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 arrow_used = TRUE; /* this means we stopped the current insert */
7051 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007052#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007053 check_spell_redraw();
7054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055}
7056
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007057#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007058/*
7059 * If we skipped highlighting word at cursor, do it now.
7060 * It may be skipped again, thus reset spell_redraw_lnum first.
7061 */
7062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007063check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007064{
7065 if (spell_redraw_lnum != 0)
7066 {
7067 linenr_T lnum = spell_redraw_lnum;
7068
7069 spell_redraw_lnum = 0;
Bram Moolenaar90a99792018-09-12 21:52:18 +02007070 redrawWinline(curwin, lnum, FALSE);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007071 }
7072}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007073
7074/*
7075 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7076 * spelled word, if there is one.
7077 */
7078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007080{
7081 pos_T tpos = curwin->w_cursor;
7082
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007083 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007084 if (curwin->w_cursor.col != tpos.col)
7085 start_arrow(&tpos);
7086}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007087#endif
7088
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089/*
7090 * stop_arrow() is called before a change is made in insert mode.
7091 * If an arrow key has been used, start a new insertion.
7092 * Returns FAIL if undo is impossible, shouldn't insert then.
7093 */
7094 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 if (arrow_used)
7098 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007099 Insstart = curwin->w_cursor; /* new insertion starts here */
7100 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7101 /* Don't update the original insert position when moved to the
7102 * right, except when nothing was inserted yet. */
7103 update_Insstart_orig = FALSE;
7104 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (u_save_cursor() == OK)
7107 {
7108 arrow_used = FALSE;
7109 ins_need_undo = FALSE;
7110 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (State & VREPLACE_FLAG)
7114 {
7115 orig_line_count = curbuf->b_ml.ml_line_count;
7116 vr_lines_changed = 1;
7117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 ResetRedobuff();
7119 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007120 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122 else if (ins_need_undo)
7123 {
7124 if (u_save_cursor() == OK)
7125 ins_need_undo = FALSE;
7126 }
7127
7128#ifdef FEAT_FOLDING
7129 /* Always open fold at the cursor line when inserting something. */
7130 foldOpenCursor();
7131#endif
7132
7133 return (arrow_used || ins_need_undo ? FAIL : OK);
7134}
7135
7136/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007137 * Do a few things to stop inserting.
7138 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7139 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 */
7141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007142stop_insert(
7143 pos_T *end_insert_pos,
7144 int esc, /* called by ins_esc() */
7145 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007147 int cc;
7148 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 stop_redo_ins();
7151 replace_flush(); /* abandon replace stack */
7152
7153 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007154 * Save the inserted text for later redo with ^@ and CTRL-A.
7155 * Don't do it when "restart_edit" was set and nothing was inserted,
7156 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007158 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007159 if (did_restart_edit == 0 || (ptr != NULL
7160 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007161 {
7162 vim_free(last_insert);
7163 last_insert = ptr;
7164 last_insert_skip = new_insert_skip;
7165 }
7166 else
7167 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
7171 /* Auto-format now. It may seem strange to do this when stopping an
7172 * insertion (or moving the cursor), but it's required when appending
7173 * a line and having it end in a space. But only do it when something
7174 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007175 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007177 pos_T tpos = curwin->w_cursor;
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 /* When the cursor is at the end of the line after a space the
7180 * formatting will move it to the following word. Avoid that by
7181 * moving the cursor onto the space. */
7182 cc = 'x';
7183 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7184 {
7185 dec_cursor();
7186 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007187 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007188 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 }
7190
7191 auto_format(TRUE, FALSE);
7192
Bram Moolenaar1c465442017-03-12 20:10:05 +01007193 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007194 {
7195 if (gchar_cursor() != NUL)
7196 inc_cursor();
7197#ifdef FEAT_VIRTUALEDIT
7198 /* If the cursor is still at the same character, also keep
7199 * the "coladd". */
7200 if (gchar_cursor() == NUL
7201 && curwin->w_cursor.lnum == tpos.lnum
7202 && curwin->w_cursor.col == tpos.col)
7203 curwin->w_cursor.coladd = tpos.coladd;
7204#endif
7205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207
7208 /* If a space was inserted for auto-formatting, remove it now. */
7209 check_auto_format(TRUE);
7210
7211 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007212 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007213 * Do this when ESC was used or moving the cursor up/down.
7214 * Check for the old position still being valid, just in case the text
7215 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007216 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007217 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7218 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007220 pos_T tpos = curwin->w_cursor;
7221
7222 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007223 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007224 for (;;)
7225 {
7226 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7227 --curwin->w_cursor.col;
7228 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007229 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007230 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007231 if (del_char(TRUE) == FAIL)
7232 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007233 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007234 if (curwin->w_cursor.lnum != tpos.lnum)
7235 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007236 else
7237 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007238 /* reset tpos, could have been invalidated in the loop above */
7239 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007240 tpos.col++;
7241 if (cc != NUL && gchar_pos(&tpos) == NUL)
7242 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 /* <C-S-Right> may have started Visual mode, adjust the position for
7246 * deleted characters. */
7247 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7248 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007249 int len = (int)STRLEN(ml_get_curline());
7250
7251 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007253 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007254#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260 }
7261 did_ai = FALSE;
7262#ifdef FEAT_SMARTINDENT
7263 did_si = FALSE;
7264 can_si = FALSE;
7265 can_si_back = FALSE;
7266#endif
7267
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007268 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7269 * now in a different buffer. */
7270 if (end_insert_pos != NULL)
7271 {
7272 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007273 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007274 curbuf->b_op_end = *end_insert_pos;
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278/*
7279 * Set the last inserted text to a single character.
7280 * Used for the replace command.
7281 */
7282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 char_u *s;
7286
7287 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (last_insert != NULL)
7290 {
7291 s = last_insert;
7292 /* Use the CTRL-V only when entering a special char */
7293 if (c < ' ' || c == DEL)
7294 *s++ = Ctrl_V;
7295 s = add_char2buf(c, s);
7296 *s++ = ESC;
7297 *s++ = NUL;
7298 last_insert_skip = 0;
7299 }
7300}
7301
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007302#if defined(EXITFREE) || defined(PROTO)
7303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007304free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007305{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007306 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007307# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007308 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007309# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007310}
7311#endif
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313/*
7314 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7315 * and CSI. Handle multi-byte characters.
7316 * Returns a pointer to after the added bytes.
7317 */
7318 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007319add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007322 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 int i;
7324 int len;
7325
7326 len = (*mb_char2bytes)(c, temp);
7327 for (i = 0; i < len; ++i)
7328 {
7329 c = temp[i];
7330#endif
7331 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7332 if (c == K_SPECIAL)
7333 {
7334 *s++ = K_SPECIAL;
7335 *s++ = KS_SPECIAL;
7336 *s++ = KE_FILLER;
7337 }
7338#ifdef FEAT_GUI
7339 else if (c == CSI)
7340 {
7341 *s++ = CSI;
7342 *s++ = KS_EXTRA;
7343 *s++ = (int)KE_CSI;
7344 }
7345#endif
7346 else
7347 *s++ = c;
7348#ifdef FEAT_MBYTE
7349 }
7350#endif
7351 return s;
7352}
7353
7354/*
7355 * move cursor to start of line
7356 * if flags & BL_WHITE move to first non-white
7357 * if flags & BL_SOL move to first non-white if startofline is set,
7358 * otherwise keep "curswant" column
7359 * if flags & BL_FIX don't leave the cursor on a NUL.
7360 */
7361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007362beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 if ((flags & BL_SOL) && !p_sol)
7365 coladvance(curwin->w_curswant);
7366 else
7367 {
7368 curwin->w_cursor.col = 0;
7369#ifdef FEAT_VIRTUALEDIT
7370 curwin->w_cursor.coladd = 0;
7371#endif
7372
7373 if (flags & (BL_WHITE | BL_SOL))
7374 {
7375 char_u *ptr;
7376
Bram Moolenaar1c465442017-03-12 20:10:05 +01007377 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7379 ++curwin->w_cursor.col;
7380 }
7381 curwin->w_set_curswant = TRUE;
7382 }
7383}
7384
7385/*
7386 * oneright oneleft cursor_down cursor_up
7387 *
7388 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007389 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 * Return OK when successful, FAIL when we hit a line of file boundary.
7391 */
7392
7393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399#ifdef FEAT_VIRTUALEDIT
7400 if (virtual_active())
7401 {
7402 pos_T prevpos = curwin->w_cursor;
7403
7404 /* Adjust for multi-wide char (excluding TAB) */
7405 ptr = ml_get_cursor();
7406 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007407# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 ))
7413 ? ptr2cells(ptr) : 1));
7414 curwin->w_set_curswant = TRUE;
7415 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7416 return (prevpos.col != curwin->w_cursor.col
7417 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7418 }
7419#endif
7420
7421 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007422 if (*ptr == NUL)
7423 return FAIL; /* already at the very end */
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007426 if (has_mbyte)
7427 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 else
7429#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007430 l = 1;
7431
7432 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7433 * contains "onemore". */
7434 if (ptr[l] == NUL
7435#ifdef FEAT_VIRTUALEDIT
7436 && (ve_flags & VE_ONEMORE) == 0
7437#endif
7438 )
7439 return FAIL;
7440 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
7442 curwin->w_set_curswant = TRUE;
7443 return OK;
7444}
7445
7446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007447oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449#ifdef FEAT_VIRTUALEDIT
7450 if (virtual_active())
7451 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007452# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 int v = getviscol();
7456
7457 if (v == 0)
7458 return FAIL;
7459
7460# ifdef FEAT_LINEBREAK
7461 /* We might get stuck on 'showbreak', skip over it. */
7462 width = 1;
7463 for (;;)
7464 {
7465 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007466 /* getviscol() is slow, skip it when 'showbreak' is empty,
7467 * 'breakindent' is not set and there are no multi-byte
7468 * characters */
7469 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470# ifdef FEAT_MBYTE
7471 && !has_mbyte
7472# endif
7473 ) || getviscol() < v)
7474 break;
7475 ++width;
7476 }
7477# else
7478 coladvance(v - 1);
7479# endif
7480
7481 if (curwin->w_cursor.coladd == 1)
7482 {
7483 char_u *ptr;
7484
7485 /* Adjust for multi-wide char (not a TAB) */
7486 ptr = ml_get_cursor();
7487 if (*ptr != TAB && vim_isprintc(
7488# ifdef FEAT_MBYTE
7489 (*mb_ptr2char)(ptr)
7490# else
7491 *ptr
7492# endif
7493 ) && ptr2cells(ptr) > 1)
7494 curwin->w_cursor.coladd = 0;
7495 }
7496
7497 curwin->w_set_curswant = TRUE;
7498 return OK;
7499 }
7500#endif
7501
7502 if (curwin->w_cursor.col == 0)
7503 return FAIL;
7504
7505 curwin->w_set_curswant = TRUE;
7506 --curwin->w_cursor.col;
7507
7508#ifdef FEAT_MBYTE
7509 /* if the character on the left of the current cursor is a multi-byte
7510 * character, move to its first byte */
7511 if (has_mbyte)
7512 mb_adjust_cursor();
7513#endif
7514 return OK;
7515}
7516
7517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007518cursor_up(
7519 long n,
7520 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 linenr_T lnum;
7523
7524 if (n > 0)
7525 {
7526 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007527 /* This fails if the cursor is already in the first line or the count
7528 * is larger than the line number and '-' is in 'cpoptions' */
7529 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 return FAIL;
7531 if (n >= lnum)
7532 lnum = 1;
7533 else
7534#ifdef FEAT_FOLDING
7535 if (hasAnyFolding(curwin))
7536 {
7537 /*
7538 * Count each sequence of folded lines as one logical line.
7539 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007540 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 (void)hasFolding(lnum, &lnum, NULL);
7542
7543 while (n--)
7544 {
7545 /* move up one line */
7546 --lnum;
7547 if (lnum <= 1)
7548 break;
7549 /* If we entered a fold, move to the beginning, unless in
7550 * Insert mode or when 'foldopen' contains "all": it will open
7551 * in a moment. */
7552 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7553 (void)hasFolding(lnum, &lnum, NULL);
7554 }
7555 if (lnum < 1)
7556 lnum = 1;
7557 }
7558 else
7559#endif
7560 lnum -= n;
7561 curwin->w_cursor.lnum = lnum;
7562 }
7563
7564 /* try to advance to the column we want to be at */
7565 coladvance(curwin->w_curswant);
7566
7567 if (upd_topline)
7568 update_topline(); /* make sure curwin->w_topline is valid */
7569
7570 return OK;
7571}
7572
7573/*
7574 * Cursor down a number of logical lines.
7575 */
7576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007577cursor_down(
7578 long n,
7579 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 linenr_T lnum;
7582
7583 if (n > 0)
7584 {
7585 lnum = curwin->w_cursor.lnum;
7586#ifdef FEAT_FOLDING
7587 /* Move to last line of fold, will fail if it's the end-of-file. */
7588 (void)hasFolding(lnum, NULL, &lnum);
7589#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007590 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007591 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007592 if (lnum >= curbuf->b_ml.ml_line_count
7593 || (lnum + n > curbuf->b_ml.ml_line_count
7594 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 return FAIL;
7596 if (lnum + n >= curbuf->b_ml.ml_line_count)
7597 lnum = curbuf->b_ml.ml_line_count;
7598 else
7599#ifdef FEAT_FOLDING
7600 if (hasAnyFolding(curwin))
7601 {
7602 linenr_T last;
7603
7604 /* count each sequence of folded lines as one logical line */
7605 while (n--)
7606 {
7607 if (hasFolding(lnum, NULL, &last))
7608 lnum = last + 1;
7609 else
7610 ++lnum;
7611 if (lnum >= curbuf->b_ml.ml_line_count)
7612 break;
7613 }
7614 if (lnum > curbuf->b_ml.ml_line_count)
7615 lnum = curbuf->b_ml.ml_line_count;
7616 }
7617 else
7618#endif
7619 lnum += n;
7620 curwin->w_cursor.lnum = lnum;
7621 }
7622
7623 /* try to advance to the column we want to be at */
7624 coladvance(curwin->w_curswant);
7625
7626 if (upd_topline)
7627 update_topline(); /* make sure curwin->w_topline is valid */
7628
7629 return OK;
7630}
7631
7632/*
7633 * Stuff the last inserted text in the read buffer.
7634 * Last_insert actually is a copy of the redo buffer, so we
7635 * first have to remove the command.
7636 */
7637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007638stuff_inserted(
7639 int c, /* Command character to be inserted */
7640 long count, /* Repeat this many times */
7641 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 char_u *esc_ptr;
7644 char_u *ptr;
7645 char_u *last_ptr;
7646 char_u last = NUL;
7647
7648 ptr = get_last_insert();
7649 if (ptr == NULL)
7650 {
7651 EMSG(_(e_noinstext));
7652 return FAIL;
7653 }
7654
7655 /* may want to stuff the command character, to start Insert mode */
7656 if (c != NUL)
7657 stuffcharReadbuff(c);
7658 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7659 *esc_ptr = NUL; /* remove the ESC */
7660
7661 /* when the last char is either "0" or "^" it will be quoted if no ESC
7662 * comes after it OR if it will inserted more than once and "ptr"
7663 * starts with ^D. -- Acevedo
7664 */
7665 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7666 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7667 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7668 {
7669 last = *last_ptr;
7670 *last_ptr = NUL;
7671 }
7672
7673 do
7674 {
7675 stuffReadbuff(ptr);
7676 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7677 if (last)
7678 stuffReadbuff((char_u *)(last == '0'
7679 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7680 : IF_EB("\026^", CTRL_V_STR "^")));
7681 }
7682 while (--count > 0);
7683
7684 if (last)
7685 *last_ptr = last;
7686
7687 if (esc_ptr != NULL)
7688 *esc_ptr = ESC; /* put the ESC back */
7689
7690 /* may want to stuff a trailing ESC, to get out of Insert mode */
7691 if (!no_esc)
7692 stuffcharReadbuff(ESC);
7693
7694 return OK;
7695}
7696
7697 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007698get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 if (last_insert == NULL)
7701 return NULL;
7702 return last_insert + last_insert_skip;
7703}
7704
7705/*
7706 * Get last inserted string, and remove trailing <Esc>.
7707 * Returns pointer to allocated memory (must be freed) or NULL.
7708 */
7709 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007710get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 char_u *s;
7713 int len;
7714
7715 if (last_insert == NULL)
7716 return NULL;
7717 s = vim_strsave(last_insert + last_insert_skip);
7718 if (s != NULL)
7719 {
7720 len = (int)STRLEN(s);
7721 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7722 s[len - 1] = NUL;
7723 }
7724 return s;
7725}
7726
7727/*
7728 * Check the word in front of the cursor for an abbreviation.
7729 * Called when the non-id character "c" has been entered.
7730 * When an abbreviation is recognized it is removed from the text and
7731 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7732 */
7733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007734echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 /* Don't check for abbreviation in paste mode, when disabled and just
7737 * after moving around with cursor keys. */
7738 if (p_paste || no_abbr || arrow_used)
7739 return FALSE;
7740
7741 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7742 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7743}
7744
7745/*
7746 * replace-stack functions
7747 *
7748 * When replacing characters, the replaced characters are remembered for each
7749 * new character. This is used to re-insert the old text when backspacing.
7750 *
7751 * There is a NUL headed list of characters for each character that is
7752 * currently in the file after the insertion point. When BS is used, one NUL
7753 * headed list is put back for the deleted character.
7754 *
7755 * For a newline, there are two NUL headed lists. One contains the characters
7756 * that the NL replaced. The extra one stores the characters after the cursor
7757 * that were deleted (always white space).
7758 *
7759 * Replace_offset is normally 0, in which case replace_push will add a new
7760 * character at the end of the stack. If replace_offset is not 0, that many
7761 * characters will be left on the stack above the newly inserted character.
7762 */
7763
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007764static char_u *replace_stack = NULL;
7765static long replace_stack_nr = 0; /* next entry in replace stack */
7766static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
7768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007769replace_push(
7770 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 char_u *p;
7773
7774 if (replace_stack_nr < replace_offset) /* nothing to do */
7775 return;
7776 if (replace_stack_len <= replace_stack_nr)
7777 {
7778 replace_stack_len += 50;
7779 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7780 if (p == NULL) /* out of memory */
7781 {
7782 replace_stack_len -= 50;
7783 return;
7784 }
7785 if (replace_stack != NULL)
7786 {
7787 mch_memmove(p, replace_stack,
7788 (size_t)(replace_stack_nr * sizeof(char_u)));
7789 vim_free(replace_stack);
7790 }
7791 replace_stack = p;
7792 }
7793 p = replace_stack + replace_stack_nr - replace_offset;
7794 if (replace_offset)
7795 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7796 *p = c;
7797 ++replace_stack_nr;
7798}
7799
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007800#if defined(FEAT_MBYTE) || defined(PROTO)
7801/*
7802 * Push a character onto the replace stack. Handles a multi-byte character in
7803 * reverse byte order, so that the first byte is popped off first.
7804 * Return the number of bytes done (includes composing characters).
7805 */
7806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007807replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007808{
7809 int l = (*mb_ptr2len)(p);
7810 int j;
7811
7812 for (j = l - 1; j >= 0; --j)
7813 replace_push(p[j]);
7814 return l;
7815}
7816#endif
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818/*
7819 * Pop one item from the replace stack.
7820 * return -1 if stack empty
7821 * return replaced character or NUL otherwise
7822 */
7823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 if (replace_stack_nr == 0)
7827 return -1;
7828 return (int)replace_stack[--replace_stack_nr];
7829}
7830
7831/*
7832 * Join the top two items on the replace stack. This removes to "off"'th NUL
7833 * encountered.
7834 */
7835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007836replace_join(
7837 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 int i;
7840
7841 for (i = replace_stack_nr; --i >= 0; )
7842 if (replace_stack[i] == NUL && off-- <= 0)
7843 {
7844 --replace_stack_nr;
7845 mch_memmove(replace_stack + i, replace_stack + i + 1,
7846 (size_t)(replace_stack_nr - i));
7847 return;
7848 }
7849}
7850
7851/*
7852 * Pop bytes from the replace stack until a NUL is found, and insert them
7853 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7854 */
7855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 int cc;
7859 int oldState = State;
7860
7861 State = NORMAL; /* don't want REPLACE here */
7862 while ((cc = replace_pop()) > 0)
7863 {
7864#ifdef FEAT_MBYTE
7865 mb_replace_pop_ins(cc);
7866#else
7867 ins_char(cc);
7868#endif
7869 dec_cursor();
7870 }
7871 State = oldState;
7872}
7873
7874#ifdef FEAT_MBYTE
7875/*
7876 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7877 * indicates a multi-byte char, pop the other bytes too.
7878 */
7879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007883 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 int i;
7885 int c;
7886
7887 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7888 {
7889 buf[0] = cc;
7890 for (i = 1; i < n; ++i)
7891 buf[i] = replace_pop();
7892 ins_bytes_len(buf, n);
7893 }
7894 else
7895 ins_char(cc);
7896
7897 if (enc_utf8)
7898 /* Handle composing chars. */
7899 for (;;)
7900 {
7901 c = replace_pop();
7902 if (c == -1) /* stack empty */
7903 break;
7904 if ((n = MB_BYTE2LEN(c)) == 1)
7905 {
7906 /* Not a multi-byte char, put it back. */
7907 replace_push(c);
7908 break;
7909 }
7910 else
7911 {
7912 buf[0] = c;
7913 for (i = 1; i < n; ++i)
7914 buf[i] = replace_pop();
7915 if (utf_iscomposing(utf_ptr2char(buf)))
7916 ins_bytes_len(buf, n);
7917 else
7918 {
7919 /* Not a composing char, put it back. */
7920 for (i = n - 1; i >= 0; --i)
7921 replace_push(buf[i]);
7922 break;
7923 }
7924 }
7925 }
7926}
7927#endif
7928
7929/*
7930 * make the replace stack empty
7931 * (called when exiting replace mode)
7932 */
7933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007934replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007936 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 replace_stack_len = 0;
7938 replace_stack_nr = 0;
7939}
7940
7941/*
7942 * Handle doing a BS for one character.
7943 * cc < 0: replace stack empty, just move cursor
7944 * cc == 0: character was inserted, delete it
7945 * cc > 0: character was replaced, put cc (first byte of original char) back
7946 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007947 * When "limit_col" is >= 0, don't delete before this column. Matters when
7948 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 */
7950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007951replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 int orig_len = 0;
7955 int ins_len;
7956 int orig_vcols = 0;
7957 colnr_T start_vcol;
7958 char_u *p;
7959 int i;
7960 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
7962 cc = replace_pop();
7963 if (cc > 0)
7964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (State & VREPLACE_FLAG)
7966 {
7967 /* Get the number of screen cells used by the character we are
7968 * going to delete. */
7969 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7970 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972#ifdef FEAT_MBYTE
7973 if (has_mbyte)
7974 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007975 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007977 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 replace_push(cc);
7979 }
7980 else
7981#endif
7982 {
7983 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007985 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987 replace_pop_ins();
7988
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (State & VREPLACE_FLAG)
7990 {
7991 /* Get the number of screen cells used by the inserted characters */
7992 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007993 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 vcol = start_vcol;
7995 for (i = 0; i < ins_len; ++i)
7996 {
7997 vcol += chartabsize(p + i, vcol);
7998#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007999 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000#endif
8001 }
8002 vcol -= start_vcol;
8003
8004 /* Delete spaces that were inserted after the cursor to keep the
8005 * text aligned. */
8006 curwin->w_cursor.col += ins_len;
8007 while (vcol > orig_vcols && gchar_cursor() == ' ')
8008 {
8009 del_char(FALSE);
8010 ++orig_vcols;
8011 }
8012 curwin->w_cursor.col -= ins_len;
8013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015 /* mark the buffer as changed and prepare for displaying */
8016 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8017 }
8018 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008019 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020}
8021
8022#ifdef FEAT_CINDENT
8023/*
8024 * Return TRUE if C-indenting is on.
8025 */
8026 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008027cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028{
8029 return (!p_paste && (curbuf->b_p_cin
8030# ifdef FEAT_EVAL
8031 || *curbuf->b_p_inde != NUL
8032# endif
8033 ));
8034}
8035#endif
8036
8037#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8038/*
8039 * Re-indent the current line, based on the current contents of it and the
8040 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8041 * confused what all the part that handles Control-T is doing that I'm not.
8042 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8043 */
8044
8045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008046fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008048 int amount = get_the_indent();
8049
8050 if (amount >= 0)
8051 {
8052 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8053 if (linewhite(curwin->w_cursor.lnum))
8054 did_ai = TRUE; /* delete the indent if the line stays empty */
8055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056}
8057
8058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008059fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 if (p_paste)
8062 return;
8063# ifdef FEAT_LISP
8064 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8065 fixthisline(get_lisp_indent);
8066# endif
8067# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8068 else
8069# endif
8070# ifdef FEAT_CINDENT
8071 if (cindent_on())
8072 do_c_expr_indent();
8073# endif
8074}
8075
8076#endif
8077
8078#ifdef FEAT_CINDENT
8079/*
8080 * return TRUE if 'cinkeys' contains the key "keytyped",
8081 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008082 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8084 *
8085 * "keytyped" can have a few special values:
8086 * KEY_OPEN_FORW
8087 * KEY_OPEN_BACK
8088 * KEY_COMPLETE just finished completion.
8089 *
8090 * If line_is_empty is TRUE accept keys with '0' before them.
8091 */
8092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008093in_cinkeys(
8094 int keytyped,
8095 int when,
8096 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 char_u *look;
8099 int try_match;
8100 int try_match_word;
8101 char_u *p;
8102 char_u *line;
8103 int icase;
8104 int i;
8105
Bram Moolenaar30848942009-12-24 14:46:12 +00008106 if (keytyped == NUL)
8107 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8108 return FALSE;
8109
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110#ifdef FEAT_EVAL
8111 if (*curbuf->b_p_inde != NUL)
8112 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8113 else
8114#endif
8115 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8116 while (*look)
8117 {
8118 /*
8119 * Find out if we want to try a match with this key, depending on
8120 * 'when' and a '*' or '!' before the key.
8121 */
8122 switch (when)
8123 {
8124 case '*': try_match = (*look == '*'); break;
8125 case '!': try_match = (*look == '!'); break;
8126 default: try_match = (*look != '*'); break;
8127 }
8128 if (*look == '*' || *look == '!')
8129 ++look;
8130
8131 /*
8132 * If there is a '0', only accept a match if the line is empty.
8133 * But may still match when typing last char of a word.
8134 */
8135 if (*look == '0')
8136 {
8137 try_match_word = try_match;
8138 if (!line_is_empty)
8139 try_match = FALSE;
8140 ++look;
8141 }
8142 else
8143 try_match_word = FALSE;
8144
8145 /*
8146 * does it look like a control character?
8147 */
8148 if (*look == '^'
8149#ifdef EBCDIC
8150 && (Ctrl_chr(look[1]) != 0)
8151#else
8152 && look[1] >= '?' && look[1] <= '_'
8153#endif
8154 )
8155 {
8156 if (try_match && keytyped == Ctrl_chr(look[1]))
8157 return TRUE;
8158 look += 2;
8159 }
8160 /*
8161 * 'o' means "o" command, open forward.
8162 * 'O' means "O" command, open backward.
8163 */
8164 else if (*look == 'o')
8165 {
8166 if (try_match && keytyped == KEY_OPEN_FORW)
8167 return TRUE;
8168 ++look;
8169 }
8170 else if (*look == 'O')
8171 {
8172 if (try_match && keytyped == KEY_OPEN_BACK)
8173 return TRUE;
8174 ++look;
8175 }
8176
8177 /*
8178 * 'e' means to check for "else" at start of line and just before the
8179 * cursor.
8180 */
8181 else if (*look == 'e')
8182 {
8183 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8184 {
8185 p = ml_get_curline();
8186 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8187 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8188 return TRUE;
8189 }
8190 ++look;
8191 }
8192
8193 /*
8194 * ':' only causes an indent if it is at the end of a label or case
8195 * statement, or when it was before typing the ':' (to fix
8196 * class::method for C++).
8197 */
8198 else if (*look == ':')
8199 {
8200 if (try_match && keytyped == ':')
8201 {
8202 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008203 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008205 /* Need to get the line again after cin_islabel(). */
8206 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 if (curwin->w_cursor.col > 2
8208 && p[curwin->w_cursor.col - 1] == ':'
8209 && p[curwin->w_cursor.col - 2] == ':')
8210 {
8211 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008212 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008213 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 p = ml_get_curline();
8215 p[curwin->w_cursor.col - 1] = ':';
8216 if (i)
8217 return TRUE;
8218 }
8219 }
8220 ++look;
8221 }
8222
8223
8224 /*
8225 * Is it a key in <>, maybe?
8226 */
8227 else if (*look == '<')
8228 {
8229 if (try_match)
8230 {
8231 /*
8232 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8233 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8234 * >, *, : and ! keys if they really really want to.
8235 */
8236 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8237 && keytyped == look[1])
8238 return TRUE;
8239
8240 if (keytyped == get_special_key_code(look + 1))
8241 return TRUE;
8242 }
8243 while (*look && *look != '>')
8244 look++;
8245 while (*look == '>')
8246 look++;
8247 }
8248
8249 /*
8250 * Is it a word: "=word"?
8251 */
8252 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8253 {
8254 ++look;
8255 if (*look == '~')
8256 {
8257 icase = TRUE;
8258 ++look;
8259 }
8260 else
8261 icase = FALSE;
8262 p = vim_strchr(look, ',');
8263 if (p == NULL)
8264 p = look + STRLEN(look);
8265 if ((try_match || try_match_word)
8266 && curwin->w_cursor.col >= (colnr_T)(p - look))
8267 {
8268 int match = FALSE;
8269
8270#ifdef FEAT_INS_EXPAND
8271 if (keytyped == KEY_COMPLETE)
8272 {
8273 char_u *s;
8274
8275 /* Just completed a word, check if it starts with "look".
8276 * search back for the start of a word. */
8277 line = ml_get_curline();
8278# ifdef FEAT_MBYTE
8279 if (has_mbyte)
8280 {
8281 char_u *n;
8282
8283 for (s = line + curwin->w_cursor.col; s > line; s = n)
8284 {
8285 n = mb_prevptr(line, s);
8286 if (!vim_iswordp(n))
8287 break;
8288 }
8289 }
8290 else
8291# endif
8292 for (s = line + curwin->w_cursor.col; s > line; --s)
8293 if (!vim_iswordc(s[-1]))
8294 break;
8295 if (s + (p - look) <= line + curwin->w_cursor.col
8296 && (icase
8297 ? MB_STRNICMP(s, look, p - look)
8298 : STRNCMP(s, look, p - look)) == 0)
8299 match = TRUE;
8300 }
8301 else
8302#endif
8303 /* TODO: multi-byte */
8304 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8305 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8306 {
8307 line = ml_get_cursor();
8308 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8309 || !vim_iswordc(line[-(p - look) - 1]))
8310 && (icase
8311 ? MB_STRNICMP(line - (p - look), look, p - look)
8312 : STRNCMP(line - (p - look), look, p - look))
8313 == 0)
8314 match = TRUE;
8315 }
8316 if (match && try_match_word && !try_match)
8317 {
8318 /* "0=word": Check if there are only blanks before the
8319 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008320 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 (int)(curwin->w_cursor.col - (p - look)))
8322 match = FALSE;
8323 }
8324 if (match)
8325 return TRUE;
8326 }
8327 look = p;
8328 }
8329
8330 /*
8331 * ok, it's a boring generic character.
8332 */
8333 else
8334 {
8335 if (try_match && *look == keytyped)
8336 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008337 if (*look != NUL)
8338 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
8340
8341 /*
8342 * Skip over ", ".
8343 */
8344 look = skip_to_option_part(look);
8345 }
8346 return FALSE;
8347}
8348#endif /* FEAT_CINDENT */
8349
8350#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8351/*
8352 * Map Hebrew keyboard when in hkmap mode.
8353 */
8354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008355hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8358 {
8359 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8360 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8361 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8362 static char_u map[26] =
8363 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8364 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8365 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8366 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8367 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8368 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8369 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8370 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8371 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8372
8373 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8374 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8375 /* '-1'='sofit' */
8376 else if (c == 'x')
8377 return 'X';
8378 else if (c == 'q')
8379 return '\''; /* {geresh}={'} */
8380 else if (c == 246)
8381 return ' '; /* \"o --> ' ' for a german keyboard */
8382 else if (c == 228)
8383 return ' '; /* \"a --> ' ' -- / -- */
8384 else if (c == 252)
8385 return ' '; /* \"u --> ' ' -- / -- */
8386#ifdef EBCDIC
8387 else if (islower(c))
8388#else
8389 /* NOTE: islower() does not do the right thing for us on Linux so we
8390 * do this the same was as 5.7 and previous, so it works correctly on
8391 * all systems. Specifically, the e.g. Delete and Arrow keys are
8392 * munged and won't work if e.g. searching for Hebrew text.
8393 */
8394 else if (c >= 'a' && c <= 'z')
8395#endif
8396 return (int)(map[CharOrdLow(c)] + p_aleph);
8397 else
8398 return c;
8399 }
8400 else
8401 {
8402 switch (c)
8403 {
8404 case '`': return ';';
8405 case '/': return '.';
8406 case '\'': return ',';
8407 case 'q': return '/';
8408 case 'w': return '\'';
8409
8410 /* Hebrew letters - set offset from 'a' */
8411 case ',': c = '{'; break;
8412 case '.': c = 'v'; break;
8413 case ';': c = 't'; break;
8414 default: {
8415 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8416
8417#ifdef EBCDIC
8418 /* see note about islower() above */
8419 if (!islower(c))
8420#else
8421 if (c < 'a' || c > 'z')
8422#endif
8423 return c;
8424 c = str[CharOrdLow(c)];
8425 break;
8426 }
8427 }
8428
8429 return (int)(CharOrdLow(c) + p_aleph);
8430 }
8431}
8432#endif
8433
8434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008435ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 int need_redraw = FALSE;
8438 int regname;
8439 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008440 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441
8442 /*
8443 * If we are going to wait for a character, show a '"'.
8444 */
8445 pc_status = PC_STATUS_UNSET;
8446 if (redrawing() && !char_avail())
8447 {
8448 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008449 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450
8451 edit_putchar('"', TRUE);
8452#ifdef FEAT_CMDL_INFO
8453 add_to_showcmd_c(Ctrl_R);
8454#endif
8455 }
8456
8457#ifdef USE_ON_FLY_SCROLL
8458 dont_scroll = TRUE; /* disallow scrolling here */
8459#endif
8460
8461 /*
8462 * Don't map the register name. This also prevents the mode message to be
8463 * deleted when ESC is hit.
8464 */
8465 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008466 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8469 {
8470 /* Get a third key for literal register insertion */
8471 literally = regname;
8472#ifdef FEAT_CMDL_INFO
8473 add_to_showcmd_c(literally);
8474#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008475 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 }
8478 --no_mapping;
8479
8480#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008481 /* Don't call u_sync() while typing the expression or giving an error
8482 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 ++no_u_sync;
8484 if (regname == '=')
8485 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008486# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008488# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008489 /* Sync undo when evaluating the expression calls setline() or
8490 * append(), so that it can be undone separately. */
8491 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008492
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008494# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 /* Restore the Input Method. */
8496 if (im_on)
8497 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008500 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8501 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008502 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 else
8506 {
8507#endif
8508 if (literally == Ctrl_O || literally == Ctrl_P)
8509 {
8510 /* Append the command to the redo buffer. */
8511 AppendCharToRedobuff(Ctrl_R);
8512 AppendCharToRedobuff(literally);
8513 AppendCharToRedobuff(regname);
8514
8515 do_put(regname, BACKWARD, 1L,
8516 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8517 }
8518 else if (insert_reg(regname, literally) == FAIL)
8519 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008520 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 need_redraw = TRUE; /* remove the '"' */
8522 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008523 else if (stop_insert_mode)
8524 /* When the '=' register was used and a function was invoked that
8525 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8526 * insert anything, need to remove the '"' */
8527 need_redraw = TRUE;
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529#ifdef FEAT_EVAL
8530 }
8531 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008532 if (u_sync_once == 1)
8533 ins_need_undo = TRUE;
8534 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535#endif
8536#ifdef FEAT_CMDL_INFO
8537 clear_showcmd();
8538#endif
8539
8540 /* If the inserted register is empty, we need to remove the '"' */
8541 if (need_redraw || stuff_empty())
8542 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008543
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008544 /* Disallow starting Visual mode here, would get a weird mode. */
8545 if (!vis_active && VIsual_active)
8546 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547}
8548
8549/*
8550 * CTRL-G commands in Insert mode.
8551 */
8552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008553ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 int c;
8556
8557#ifdef FEAT_INS_EXPAND
8558 /* Right after CTRL-X the cursor will be after the ruler. */
8559 setcursor();
8560#endif
8561
8562 /*
8563 * Don't map the second key. This also prevents the mode message to be
8564 * deleted when ESC is hit.
8565 */
8566 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008567 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 --no_mapping;
8569 switch (c)
8570 {
8571 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8572 case K_UP:
8573 case Ctrl_K:
8574 case 'k': ins_up(TRUE);
8575 break;
8576
8577 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8578 case K_DOWN:
8579 case Ctrl_J:
8580 case 'j': ins_down(TRUE);
8581 break;
8582
8583 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008584 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008586
8587 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008588 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008589 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008590 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 break;
8592
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008593 /* CTRL-G U: do not break undo with the next char */
8594 case 'U':
8595 /* Allow one left/right cursor movement with the next char,
8596 * without breaking undo. */
8597 dont_sync_undo = MAYBE;
8598 break;
8599
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008601 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603}
8604
8605/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008606 * CTRL-^ in Insert mode.
8607 */
8608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008609ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008610{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008611 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008612 {
8613 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8614 if (State & LANGMAP)
8615 {
8616 curbuf->b_p_iminsert = B_IMODE_NONE;
8617 State &= ~LANGMAP;
8618 }
8619 else
8620 {
8621 curbuf->b_p_iminsert = B_IMODE_LMAP;
8622 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008623#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008624 im_set_active(FALSE);
8625#endif
8626 }
8627 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008628#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008629 else
8630 {
8631 /* There are no ":lmap" mappings, toggle IM */
8632 if (im_get_status())
8633 {
8634 curbuf->b_p_iminsert = B_IMODE_NONE;
8635 im_set_active(FALSE);
8636 }
8637 else
8638 {
8639 curbuf->b_p_iminsert = B_IMODE_IM;
8640 State &= ~LANGMAP;
8641 im_set_active(TRUE);
8642 }
8643 }
8644#endif
8645 set_iminsert_global();
8646 showmode();
8647#ifdef FEAT_GUI
8648 /* may show different cursor shape or color */
8649 if (gui.in_use)
8650 gui_update_cursor(TRUE, FALSE);
8651#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008652#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008653 /* Show/unshow value of 'keymap' in status lines. */
8654 status_redraw_curbuf();
8655#endif
8656}
8657
8658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 * Handle ESC in insert mode.
8660 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8661 * insert.
8662 */
8663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008664ins_esc(
8665 long *count,
8666 int cmdchar,
8667 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int temp;
8670 static int disabled_redraw = FALSE;
8671
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008672#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008673 check_spell_redraw();
8674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675#if defined(FEAT_HANGULIN)
8676# if defined(ESC_CHG_TO_ENG_MODE)
8677 hangul_input_state_set(0);
8678# endif
8679 if (composing_hangul)
8680 {
8681 push_raw_key(composing_hangul_buffer, 2);
8682 composing_hangul = 0;
8683 }
8684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
8686 temp = curwin->w_cursor.col;
8687 if (disabled_redraw)
8688 {
8689 --RedrawingDisabled;
8690 disabled_redraw = FALSE;
8691 }
8692 if (!arrow_used)
8693 {
8694 /*
8695 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008696 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8697 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 */
8699 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008700 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702 /*
8703 * Repeating insert may take a long time. Check for
8704 * interrupt now and then.
8705 */
8706 if (*count > 0)
8707 {
8708 line_breakcheck();
8709 if (got_int)
8710 *count = 0;
8711 }
8712
8713 if (--*count > 0) /* repeat what was typed */
8714 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008715 /* Vi repeats the insert without replacing characters. */
8716 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8717 State &= ~REPLACE_FLAG;
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 (void)start_redo_ins();
8720 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008721 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 ++RedrawingDisabled;
8723 disabled_redraw = TRUE;
8724 return FALSE; /* repeat the insert */
8725 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008726 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 undisplay_dollar();
8728 }
8729
8730 /* When an autoindent was removed, curswant stays after the
8731 * indent */
8732 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8733 curwin->w_set_curswant = TRUE;
8734
8735 /* Remember the last Insert position in the '^ mark. */
8736 if (!cmdmod.keepjumps)
8737 curbuf->b_last_insert = curwin->w_cursor;
8738
8739 /*
8740 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008741 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008743 if (!nomove
8744 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#ifdef FEAT_VIRTUALEDIT
8746 || curwin->w_cursor.coladd > 0
8747#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008748 )
8749 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008750 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751#ifdef FEAT_RIGHTLEFT
8752 && !revins_on
8753#endif
8754 )
8755 {
8756#ifdef FEAT_VIRTUALEDIT
8757 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8758 {
8759 oneleft();
8760 if (restart_edit != NUL)
8761 ++curwin->w_cursor.coladd;
8762 }
8763 else
8764#endif
8765 {
8766 --curwin->w_cursor.col;
8767#ifdef FEAT_MBYTE
8768 /* Correct cursor for multi-byte character. */
8769 if (has_mbyte)
8770 mb_adjust_cursor();
8771#endif
8772 }
8773 }
8774
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008775#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 /* Disable IM to allow typing English directly for Normal mode commands.
8777 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8778 * well). */
8779 if (!(State & LANGMAP))
8780 im_save_status(&curbuf->b_p_iminsert);
8781 im_set_active(FALSE);
8782#endif
8783
8784 State = NORMAL;
8785 /* need to position cursor again (e.g. when on a TAB ) */
8786 changed_cline_bef_curs();
8787
8788#ifdef FEAT_MOUSE
8789 setmouse();
8790#endif
8791#ifdef CURSOR_SHAPE
8792 ui_cursor_shape(); /* may show different cursor shape */
8793#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008794 if (!p_ek)
8795 /* Re-enable bracketed paste mode. */
8796 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
8798 /*
8799 * When recording or for CTRL-O, need to display the new mode.
8800 * Otherwise remove the mode message.
8801 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008802 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 showmode();
8804 else if (p_smd)
8805 MSG("");
8806
8807 return TRUE; /* exit Insert mode */
8808}
8809
8810#ifdef FEAT_RIGHTLEFT
8811/*
8812 * Toggle language: hkmap and revins_on.
8813 * Move to end of reverse inserted text.
8814 */
8815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008816ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 if (revins_on && revins_chars && revins_scol >= 0)
8819 {
8820 while (gchar_cursor() != NUL && revins_chars--)
8821 ++curwin->w_cursor.col;
8822 }
8823 p_ri = !p_ri;
8824 revins_on = (State == INSERT && p_ri);
8825 if (revins_on)
8826 {
8827 revins_scol = curwin->w_cursor.col;
8828 revins_legal++;
8829 revins_chars = 0;
8830 undisplay_dollar();
8831 }
8832 else
8833 revins_scol = -1;
8834#ifdef FEAT_FKMAP
8835 if (p_altkeymap)
8836 {
8837 /*
8838 * to be consistent also for redo command, using '.'
8839 * set arrow_used to true and stop it - causing to redo
8840 * characters entered in one mode (normal/reverse insert).
8841 */
8842 arrow_used = TRUE;
8843 (void)stop_arrow();
8844 p_fkmap = curwin->w_p_rl ^ p_ri;
8845 if (p_fkmap && p_ri)
8846 State = INSERT;
8847 }
8848 else
8849#endif
8850 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8851 showmode();
8852}
8853#endif
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855/*
8856 * If 'keymodel' contains "startsel", may start selection.
8857 * Returns TRUE when a CTRL-O and other keys stuffed.
8858 */
8859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008860ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 if (km_startsel)
8863 switch (c)
8864 {
8865 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 case K_PAGEUP:
8868 case K_KPAGEUP:
8869 case K_PAGEDOWN:
8870 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008871# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 case K_LEFT:
8873 case K_RIGHT:
8874 case K_UP:
8875 case K_DOWN:
8876 case K_END:
8877 case K_HOME:
8878# endif
8879 if (!(mod_mask & MOD_MASK_SHIFT))
8880 break;
8881 /* FALLTHROUGH */
8882 case K_S_LEFT:
8883 case K_S_RIGHT:
8884 case K_S_UP:
8885 case K_S_DOWN:
8886 case K_S_END:
8887 case K_S_HOME:
8888 /* Start selection right away, the cursor can move with
8889 * CTRL-O when beyond the end of the line. */
8890 start_selection();
8891
8892 /* Execute the key in (insert) Select mode. */
8893 stuffcharReadbuff(Ctrl_O);
8894 if (mod_mask)
8895 {
8896 char_u buf[4];
8897
8898 buf[0] = K_SPECIAL;
8899 buf[1] = KS_MODIFIER;
8900 buf[2] = mod_mask;
8901 buf[3] = NUL;
8902 stuffReadbuff(buf);
8903 }
8904 stuffcharReadbuff(c);
8905 return TRUE;
8906 }
8907 return FALSE;
8908}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
8910/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008911 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008912 */
8913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008914ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008915{
8916#ifdef FEAT_FKMAP
8917 if (p_fkmap && p_ri)
8918 {
8919 beep_flush();
8920 EMSG(farsi_text_3); /* encoded in Farsi */
8921 return;
8922 }
8923#endif
8924
Bram Moolenaar1e015462005-09-25 22:16:38 +00008925# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008926 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008927 (char_u *)((State & REPLACE_FLAG) ? "i"
8928 : replaceState == VREPLACE ? "v"
8929 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008930# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008931 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008932 if (State & REPLACE_FLAG)
8933 State = INSERT | (State & LANGMAP);
8934 else
8935 State = replaceState | (State & LANGMAP);
8936 AppendCharToRedobuff(K_INS);
8937 showmode();
8938#ifdef CURSOR_SHAPE
8939 ui_cursor_shape(); /* may show different cursor shape */
8940#endif
8941}
8942
8943/*
8944 * Pressed CTRL-O in Insert mode.
8945 */
8946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008947ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008948{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008949 if (State & VREPLACE_FLAG)
8950 restart_edit = 'V';
8951 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008952 if (State & REPLACE_FLAG)
8953 restart_edit = 'R';
8954 else
8955 restart_edit = 'I';
8956#ifdef FEAT_VIRTUALEDIT
8957 if (virtual_active())
8958 ins_at_eol = FALSE; /* cursor always keeps its column */
8959 else
8960#endif
8961 ins_at_eol = (gchar_cursor() == NUL);
8962}
8963
8964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 * If the cursor is on an indent, ^T/^D insert/delete one
8966 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008967 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 * with vi. But vi only supports ^T and ^D after an
8969 * autoindent, we support it everywhere.
8970 */
8971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008972ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
8974 if (stop_arrow() == FAIL)
8975 return;
8976 AppendCharToRedobuff(c);
8977
8978 /*
8979 * 0^D and ^^D: remove all indent.
8980 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008981 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8982 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 {
8984 --curwin->w_cursor.col;
8985 (void)del_char(FALSE); /* delete the '^' or '0' */
8986 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8987 if (State & REPLACE_FLAG)
8988 replace_pop_ins();
8989 if (lastc == '^')
8990 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008991 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008994 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995
8996 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8997 did_ai = FALSE;
8998#ifdef FEAT_SMARTINDENT
8999 did_si = FALSE;
9000 can_si = FALSE;
9001 can_si_back = FALSE;
9002#endif
9003#ifdef FEAT_CINDENT
9004 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9005#endif
9006}
9007
9008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009009ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 int temp;
9012
9013 if (stop_arrow() == FAIL)
9014 return;
9015 if (gchar_cursor() == NUL) /* delete newline */
9016 {
9017 temp = curwin->w_cursor.col;
9018 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009019 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009020 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009024 /* Adjust orig_line_count in case more lines have been deleted than
9025 * have been added. That makes sure, that open_line() later
9026 * can access all buffer lines correctly */
9027 if (State & VREPLACE_FLAG &&
9028 orig_line_count > curbuf->b_ml.ml_line_count)
9029 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009032 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9033 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 did_ai = FALSE;
9035#ifdef FEAT_SMARTINDENT
9036 did_si = FALSE;
9037 can_si = FALSE;
9038 can_si_back = FALSE;
9039#endif
9040 AppendCharToRedobuff(K_DEL);
9041}
9042
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009043/*
9044 * Delete one character for ins_bs().
9045 */
9046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009047ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009048{
9049 dec_cursor();
9050 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9051 if (State & REPLACE_FLAG)
9052 {
9053 /* Don't delete characters before the insert point when in
9054 * Replace mode */
9055 if (curwin->w_cursor.lnum != Insstart.lnum
9056 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009057 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009058 }
9059 else
9060 (void)del_char(FALSE);
9061}
9062
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063/*
9064 * Handle Backspace, delete-word and delete-line in Insert mode.
9065 * Return TRUE when backspace was actually used.
9066 */
9067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068ins_bs(
9069 int c,
9070 int mode,
9071 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073 linenr_T lnum;
9074 int cc;
9075 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009076 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 colnr_T mincol;
9078 int did_backspace = FALSE;
9079 int in_indent;
9080 int oldState;
9081#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009082 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083#endif
9084
9085 /*
9086 * can't delete anything in an empty file
9087 * can't backup past first character in buffer
9088 * can't backup past starting point unless 'backspace' > 1
9089 * can backup to a previous line if 'backspace' == 0
9090 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009091 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 || (
9093#ifdef FEAT_RIGHTLEFT
9094 !revins_on &&
9095#endif
9096 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9097 || (!can_bs(BS_START)
9098 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009099 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9100 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9102 && curwin->w_cursor.col <= ai_col)
9103 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9104 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009105 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 return FALSE;
9107 }
9108
9109 if (stop_arrow() == FAIL)
9110 return FALSE;
9111 in_indent = inindent(0);
9112#ifdef FEAT_CINDENT
9113 if (in_indent)
9114 can_cindent = FALSE;
9115#endif
9116#ifdef FEAT_COMMENTS
9117 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9118#endif
9119#ifdef FEAT_RIGHTLEFT
9120 if (revins_on) /* put cursor after last inserted char */
9121 inc_cursor();
9122#endif
9123
9124#ifdef FEAT_VIRTUALEDIT
9125 /* Virtualedit:
9126 * BACKSPACE_CHAR eats a virtual space
9127 * BACKSPACE_WORD eats all coladd
9128 * BACKSPACE_LINE eats all coladd and keeps going
9129 */
9130 if (curwin->w_cursor.coladd > 0)
9131 {
9132 if (mode == BACKSPACE_CHAR)
9133 {
9134 --curwin->w_cursor.coladd;
9135 return TRUE;
9136 }
9137 if (mode == BACKSPACE_WORD)
9138 {
9139 curwin->w_cursor.coladd = 0;
9140 return TRUE;
9141 }
9142 curwin->w_cursor.coladd = 0;
9143 }
9144#endif
9145
9146 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009147 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 */
9149 if (curwin->w_cursor.col == 0)
9150 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009151 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009152 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153#ifdef FEAT_RIGHTLEFT
9154 || revins_on
9155#endif
9156 )
9157 {
9158 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9159 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9160 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009161 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009162 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 }
9164 /*
9165 * In replace mode:
9166 * cc < 0: NL was inserted, delete it
9167 * cc >= 0: NL was replaced, put original characters back
9168 */
9169 cc = -1;
9170 if (State & REPLACE_FLAG)
9171 cc = replace_pop(); /* returns -1 if NL was inserted */
9172 /*
9173 * In replace mode, in the line we started replacing, we only move the
9174 * cursor.
9175 */
9176 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9177 {
9178 dec_cursor();
9179 }
9180 else
9181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 if (!(State & VREPLACE_FLAG)
9183 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 {
9185 temp = gchar_cursor(); /* remember current char */
9186 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009187
9188 /* When "aw" is in 'formatoptions' we must delete the space at
9189 * the end of the line, otherwise the line will be broken
9190 * again when auto-formatting. */
9191 if (has_format_option(FO_AUTO)
9192 && has_format_option(FO_WHITE_PAR))
9193 {
9194 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9195 TRUE);
9196 int len;
9197
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009198 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009199 if (len > 0 && ptr[len - 1] == ' ')
9200 ptr[len - 1] = NUL;
9201 }
9202
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009203 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 if (temp == NUL && gchar_cursor() != NUL)
9205 inc_cursor();
9206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 else
9208 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
9210 /*
9211 * In REPLACE mode we have to put back the text that was replaced
9212 * by the NL. On the replace stack is first a NUL-terminated
9213 * sequence of characters that were deleted and then the
9214 * characters that NL replaced.
9215 */
9216 if (State & REPLACE_FLAG)
9217 {
9218 /*
9219 * Do the next ins_char() in NORMAL state, to
9220 * prevent ins_char() from replacing characters and
9221 * avoiding showmatch().
9222 */
9223 oldState = State;
9224 State = NORMAL;
9225 /*
9226 * restore characters (blanks) deleted after cursor
9227 */
9228 while (cc > 0)
9229 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009230 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231#ifdef FEAT_MBYTE
9232 mb_replace_pop_ins(cc);
9233#else
9234 ins_char(cc);
9235#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009236 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 cc = replace_pop();
9238 }
9239 /* restore the characters that NL replaced */
9240 replace_pop_ins();
9241 State = oldState;
9242 }
9243 }
9244 did_ai = FALSE;
9245 }
9246 else
9247 {
9248 /*
9249 * Delete character(s) before the cursor.
9250 */
9251#ifdef FEAT_RIGHTLEFT
9252 if (revins_on) /* put cursor on last inserted char */
9253 dec_cursor();
9254#endif
9255 mincol = 0;
9256 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009257 if (mode == BACKSPACE_LINE
9258 && (curbuf->b_p_ai
9259#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009260 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009261#endif
9262 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263#ifdef FEAT_RIGHTLEFT
9264 && !revins_on
9265#endif
9266 )
9267 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009268 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009270 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009272 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 }
9274
9275 /*
9276 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9277 */
9278 if ( mode == BACKSPACE_CHAR
9279 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009280 || ((get_sts_value() != 0
9281#ifdef FEAT_VARTABS
9282 || tabstop_count(curbuf->b_p_vsts_array)
9283#endif
9284 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009285 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 && (*(ml_get_cursor() - 1) == TAB
9287 || (*(ml_get_cursor() - 1) == ' '
9288 && (!*inserted_space_p
9289 || arrow_used))))))
9290 {
9291 int ts;
9292 colnr_T vcol;
9293 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009294 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
9296 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 /* Compute the virtual column where we want to be. Since
9298 * 'showbreak' may get in the way, need to get the last column of
9299 * the previous character. */
9300 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009301 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 dec_cursor();
9303 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9304 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009305#ifdef FEAT_VARTABS
9306 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009307 {
9308 ts = (int)get_sw_value(curbuf);
9309 want_vcol = (want_vcol / ts) * ts;
9310 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009311 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009312 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009313 curbuf->b_p_vsts_array);
9314#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009315 if (p_sta && in_indent)
9316 ts = (int)get_sw_value(curbuf);
9317 else
9318 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321
9322 /* delete characters until we are at or before want_vcol */
9323 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009324 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009325 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326
9327 /* insert extra spaces until we are at want_vcol */
9328 while (vcol < want_vcol)
9329 {
9330 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009331 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9332 && curwin->w_cursor.col < Insstart_orig.col)
9333 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (State & VREPLACE_FLAG)
9336 ins_char(' ');
9337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 {
9339 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009340 if ((State & REPLACE_FLAG))
9341 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 }
9343 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9344 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009345
9346 /* If we are now back where we started delete one character. Can
9347 * happen when using 'sts' and 'linebreak'. */
9348 if (vcol >= start_vcol)
9349 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 }
9351
9352 /*
9353 * Delete upto starting point, start of line or previous word.
9354 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009357#ifdef FEAT_MBYTE
9358 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009360 if (has_mbyte)
9361 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009363 do
9364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009366 if (!revins_on) /* put cursor on char to be deleted */
9367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009369
9370 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009372 /* look multi-byte character class */
9373 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009375 prev_cclass = cclass;
9376 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009379
9380 /* start of word? */
9381 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9382 {
9383 mode = BACKSPACE_WORD_NOT_SPACE;
9384 temp = vim_iswordc(cc);
9385 }
9386 /* end of word? */
9387 else if (mode == BACKSPACE_WORD_NOT_SPACE
9388 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9389#ifdef FEAT_MBYTE
9390 || prev_cclass != cclass
9391#endif
9392 ))
9393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009395 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009397 inc_cursor();
9398#ifdef FEAT_RIGHTLEFT
9399 else if (State & REPLACE_FLAG)
9400 dec_cursor();
9401#endif
9402 break;
9403 }
9404 if (State & REPLACE_FLAG)
9405 replace_do_bs(-1);
9406 else
9407 {
9408#ifdef FEAT_MBYTE
9409 if (enc_utf8 && p_deco)
9410 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9411#endif
9412 (void)del_char(FALSE);
9413#ifdef FEAT_MBYTE
9414 /*
9415 * If there are combining characters and 'delcombine' is set
9416 * move the cursor back. Don't back up before the base
9417 * character.
9418 */
9419 if (enc_utf8 && p_deco && cpc[0] != NUL)
9420 inc_cursor();
9421#endif
9422#ifdef FEAT_RIGHTLEFT
9423 if (revins_chars)
9424 {
9425 revins_chars--;
9426 revins_legal++;
9427 }
9428 if (revins_on && gchar_cursor() == NUL)
9429 break;
9430#endif
9431 }
9432 /* Just a single backspace?: */
9433 if (mode == BACKSPACE_CHAR)
9434 break;
9435 } while (
9436#ifdef FEAT_RIGHTLEFT
9437 revins_on ||
9438#endif
9439 (curwin->w_cursor.col > mincol
9440 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9441 || curwin->w_cursor.col != Insstart_orig.col)));
9442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 did_backspace = TRUE;
9444 }
9445#ifdef FEAT_SMARTINDENT
9446 did_si = FALSE;
9447 can_si = FALSE;
9448 can_si_back = FALSE;
9449#endif
9450 if (curwin->w_cursor.col <= 1)
9451 did_ai = FALSE;
9452 /*
9453 * It's a little strange to put backspaces into the redo
9454 * buffer, but it makes auto-indent a lot easier to deal
9455 * with.
9456 */
9457 AppendCharToRedobuff(c);
9458
9459 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009460 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009461 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009462 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463
9464 /* vi behaviour: the cursor moves backward but the character that
9465 * was there remains visible
9466 * Vim behaviour: the cursor moves backward and the character that
9467 * was there is erased from the screen.
9468 * We can emulate the vi behaviour by pretending there is a dollar
9469 * displayed even when there isn't.
9470 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009471 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 dollar_vcol = curwin->w_virtcol;
9473
Bram Moolenaarce3be472008-01-14 19:12:28 +00009474#ifdef FEAT_FOLDING
9475 /* When deleting a char the cursor line must never be in a closed fold.
9476 * E.g., when 'foldmethod' is indent and deleting the first non-white
9477 * char before a Tab. */
9478 if (did_backspace)
9479 foldOpenCursor();
9480#endif
9481
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 return did_backspace;
9483}
9484
9485#ifdef FEAT_MOUSE
9486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009490 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
9492# ifdef FEAT_GUI
9493 /* When GUI is active, also move/paste when 'mouse' is empty */
9494 if (!gui.in_use)
9495# endif
9496 if (!mouse_has(MOUSE_INSERT))
9497 return;
9498
9499 undisplay_dollar();
9500 tpos = curwin->w_cursor;
9501 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9502 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009503 win_T *new_curwin = curwin;
9504
9505 if (curwin != old_curwin && win_valid(old_curwin))
9506 {
9507 /* Mouse took us to another window. We need to go back to the
9508 * previous one to stop insert there properly. */
9509 curwin = old_curwin;
9510 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009511#ifdef FEAT_JOB_CHANNEL
9512 if (bt_prompt(curbuf))
9513 // Restart Insert mode when re-entering the prompt buffer.
9514 curbuf->b_prompt_insert = 'A';
9515#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009516 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009517 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009518 if (curwin != new_curwin && win_valid(new_curwin))
9519 {
9520 curwin = new_curwin;
9521 curbuf = curwin->w_buffer;
9522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523# ifdef FEAT_CINDENT
9524 can_cindent = TRUE;
9525# endif
9526 }
9527
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 /* redraw status lines (in case another window became active) */
9529 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531
9532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009536 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009537# ifdef FEAT_INS_EXPAND
9538 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539# endif
9540
9541 tpos = curwin->w_cursor;
9542
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009543 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 {
9545 int row, col;
9546
9547 row = mouse_row;
9548 col = mouse_col;
9549
9550 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009551 wp = mouse_find_win(&row, &col);
9552 if (wp == NULL)
9553 return;
9554 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 curbuf = curwin->w_buffer;
9556 }
9557 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 undisplay_dollar();
9559
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009560# ifdef FEAT_INS_EXPAND
9561 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009562 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009563# endif
9564 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009565 if (dir == MSCR_DOWN || dir == MSCR_UP)
9566 {
9567 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9568 scroll_redraw(dir,
9569 (long)(curwin->w_botline - curwin->w_topline));
9570 else
9571 scroll_redraw(dir, 3L);
9572 }
9573#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009574 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009575 {
9576 int val, step = 6;
9577
9578 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009579 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009580 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9581 if (val < 0)
9582 val = 0;
9583 gui_do_horiz_scroll(val, TRUE);
9584 }
9585#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009586# ifdef FEAT_INS_EXPAND
9587 did_scroll = TRUE;
9588# endif
9589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 curwin->w_redr_status = TRUE;
9592
9593 curwin = old_curwin;
9594 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009596# ifdef FEAT_INS_EXPAND
9597 /* The popup menu may overlay the window, need to redraw it.
9598 * TODO: Would be more efficient to only redraw the windows that are
9599 * overlapped by the popup menu. */
9600 if (pum_visible() && did_scroll)
9601 {
9602 redraw_all_later(NOT_VALID);
9603 ins_compl_show_pum();
9604 }
9605# endif
9606
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009607 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 {
9609 start_arrow(&tpos);
9610# ifdef FEAT_CINDENT
9611 can_cindent = TRUE;
9612# endif
9613 }
9614}
9615#endif
9616
Bram Moolenaarec2da362017-01-21 20:04:22 +01009617/*
9618 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9619 * P_PE literally.
9620 * When "drop" is TRUE then consume the text and drop it.
9621 */
9622 int
9623bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9624{
9625 int c;
9626 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9627 int idx = 0;
9628 char_u *end = find_termcode((char_u *)"PE");
9629 int ret_char = -1;
9630 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009631 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009632
9633 /* If the end code is too long we can't detect it, read everything. */
9634 if (STRLEN(end) >= NUMBUFLEN)
9635 end = NULL;
9636 ++no_mapping;
9637 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009638 if (!p_paste)
9639 // Also have the side effects of setting 'paste' to make it work much
9640 // faster.
9641 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009642
Bram Moolenaarec2da362017-01-21 20:04:22 +01009643 for (;;)
9644 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009645 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009646 if (end == NULL && vpeekc() == NUL)
9647 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009648 do
9649 {
9650 c = vgetc();
9651 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9652 if (c == NUL || got_int)
9653 // When CTRL-C was encountered the typeahead will be flushed and we
9654 // won't get the end sequence.
9655 break;
9656
Bram Moolenaarec2da362017-01-21 20:04:22 +01009657#ifdef FEAT_MBYTE
9658 if (has_mbyte)
9659 idx += (*mb_char2bytes)(c, buf + idx);
9660 else
9661#endif
9662 buf[idx++] = c;
9663 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009664 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009665 {
9666 if (end[idx] == NUL)
9667 break; /* Found the end of paste code. */
9668 continue;
9669 }
9670 if (!drop)
9671 {
9672 switch (mode)
9673 {
9674 case PASTE_CMDLINE:
9675 put_on_cmdline(buf, idx, TRUE);
9676 break;
9677
9678 case PASTE_EX:
9679 if (gap != NULL && ga_grow(gap, idx) == OK)
9680 {
9681 mch_memmove((char *)gap->ga_data + gap->ga_len,
9682 buf, (size_t)idx);
9683 gap->ga_len += idx;
9684 }
9685 break;
9686
9687 case PASTE_INSERT:
9688 if (stop_arrow() == OK)
9689 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009690 c = buf[0];
9691 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9692 ins_eol(c);
9693 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009694 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009695 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009696 AppendToRedobuffLit(buf, idx);
9697 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009698 }
9699 break;
9700
9701 case PASTE_ONE_CHAR:
9702 if (ret_char == -1)
9703 {
9704#ifdef FEAT_MBYTE
9705 if (has_mbyte)
9706 ret_char = (*mb_ptr2char)(buf);
9707 else
9708#endif
9709 ret_char = buf[0];
9710 }
9711 break;
9712 }
9713 }
9714 idx = 0;
9715 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009716
Bram Moolenaarec2da362017-01-21 20:04:22 +01009717 --no_mapping;
9718 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009719 if (!save_paste)
9720 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009721
9722 return ret_char;
9723}
9724
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009725#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009727ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009728{
9729 /* We will be leaving the current window, unless closing another tab. */
9730 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9731 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9732 {
9733 undisplay_dollar();
9734 start_arrow(&curwin->w_cursor);
9735# ifdef FEAT_CINDENT
9736 can_cindent = TRUE;
9737# endif
9738 }
9739
9740 if (c == K_TABLINE)
9741 goto_tabpage(current_tab);
9742 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009743 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009744 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009745 redraw_statuslines(); /* will redraw the tabline when needed */
9746 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009747}
9748#endif
9749
9750#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009752ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754 pos_T tpos;
9755
9756 undisplay_dollar();
9757 tpos = curwin->w_cursor;
9758 if (gui_do_scroll())
9759 {
9760 start_arrow(&tpos);
9761# ifdef FEAT_CINDENT
9762 can_cindent = TRUE;
9763# endif
9764 }
9765}
9766
9767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009768ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
9770 pos_T tpos;
9771
9772 undisplay_dollar();
9773 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009774 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 {
9776 start_arrow(&tpos);
9777# ifdef FEAT_CINDENT
9778 can_cindent = TRUE;
9779# endif
9780 }
9781}
9782#endif
9783
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785ins_left(
9786 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 pos_T tpos;
9789
9790#ifdef FEAT_FOLDING
9791 if ((fdo_flags & FDO_HOR) && KeyTyped)
9792 foldOpenCursor();
9793#endif
9794 undisplay_dollar();
9795 tpos = curwin->w_cursor;
9796 if (oneleft() == OK)
9797 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009798#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9799 /* Only call start_arrow() when not busy with preediting, it will
9800 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009801 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009802#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009803 {
9804 start_arrow_with_change(&tpos, end_change);
9805 if (!end_change)
9806 AppendCharToRedobuff(K_LEFT);
9807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#ifdef FEAT_RIGHTLEFT
9809 /* If exit reversed string, position is fixed */
9810 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9811 revins_legal++;
9812 revins_chars++;
9813#endif
9814 }
9815
9816 /*
9817 * if 'whichwrap' set for cursor in insert mode may go to
9818 * previous line
9819 */
9820 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9821 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009822 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 start_arrow(&tpos);
9824 --(curwin->w_cursor.lnum);
9825 coladvance((colnr_T)MAXCOL);
9826 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9827 }
9828 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009829 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009830 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831}
9832
9833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009834ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 pos_T tpos;
9837
9838#ifdef FEAT_FOLDING
9839 if ((fdo_flags & FDO_HOR) && KeyTyped)
9840 foldOpenCursor();
9841#endif
9842 undisplay_dollar();
9843 tpos = curwin->w_cursor;
9844 if (c == K_C_HOME)
9845 curwin->w_cursor.lnum = 1;
9846 curwin->w_cursor.col = 0;
9847#ifdef FEAT_VIRTUALEDIT
9848 curwin->w_cursor.coladd = 0;
9849#endif
9850 curwin->w_curswant = 0;
9851 start_arrow(&tpos);
9852}
9853
9854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009855ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857 pos_T tpos;
9858
9859#ifdef FEAT_FOLDING
9860 if ((fdo_flags & FDO_HOR) && KeyTyped)
9861 foldOpenCursor();
9862#endif
9863 undisplay_dollar();
9864 tpos = curwin->w_cursor;
9865 if (c == K_C_END)
9866 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9867 coladvance((colnr_T)MAXCOL);
9868 curwin->w_curswant = MAXCOL;
9869
9870 start_arrow(&tpos);
9871}
9872
9873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009874ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
9876#ifdef FEAT_FOLDING
9877 if ((fdo_flags & FDO_HOR) && KeyTyped)
9878 foldOpenCursor();
9879#endif
9880 undisplay_dollar();
9881 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9882 {
9883 start_arrow(&curwin->w_cursor);
9884 (void)bck_word(1L, FALSE, FALSE);
9885 curwin->w_set_curswant = TRUE;
9886 }
9887 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009888 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889}
9890
9891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009892ins_right(
9893 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895#ifdef FEAT_FOLDING
9896 if ((fdo_flags & FDO_HOR) && KeyTyped)
9897 foldOpenCursor();
9898#endif
9899 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009900 if (gchar_cursor() != NUL
9901#ifdef FEAT_VIRTUALEDIT
9902 || virtual_active()
9903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 )
9905 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009906 start_arrow_with_change(&curwin->w_cursor, end_change);
9907 if (!end_change)
9908 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 curwin->w_set_curswant = TRUE;
9910#ifdef FEAT_VIRTUALEDIT
9911 if (virtual_active())
9912 oneright();
9913 else
9914#endif
9915 {
9916#ifdef FEAT_MBYTE
9917 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009918 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 else
9920#endif
9921 ++curwin->w_cursor.col;
9922 }
9923
9924#ifdef FEAT_RIGHTLEFT
9925 revins_legal++;
9926 if (revins_chars)
9927 revins_chars--;
9928#endif
9929 }
9930 /* if 'whichwrap' set for cursor in insert mode, may move the
9931 * cursor to the next line */
9932 else if (vim_strchr(p_ww, ']') != NULL
9933 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9934 {
9935 start_arrow(&curwin->w_cursor);
9936 curwin->w_set_curswant = TRUE;
9937 ++curwin->w_cursor.lnum;
9938 curwin->w_cursor.col = 0;
9939 }
9940 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009941 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009942 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943}
9944
9945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009946ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948#ifdef FEAT_FOLDING
9949 if ((fdo_flags & FDO_HOR) && KeyTyped)
9950 foldOpenCursor();
9951#endif
9952 undisplay_dollar();
9953 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9954 || gchar_cursor() != NUL)
9955 {
9956 start_arrow(&curwin->w_cursor);
9957 (void)fwd_word(1L, FALSE, 0);
9958 curwin->w_set_curswant = TRUE;
9959 }
9960 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009961 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962}
9963
9964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009965ins_up(
9966 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968 pos_T tpos;
9969 linenr_T old_topline = curwin->w_topline;
9970#ifdef FEAT_DIFF
9971 int old_topfill = curwin->w_topfill;
9972#endif
9973
9974 undisplay_dollar();
9975 tpos = curwin->w_cursor;
9976 if (cursor_up(1L, TRUE) == OK)
9977 {
9978 if (startcol)
9979 coladvance(getvcol_nolist(&Insstart));
9980 if (old_topline != curwin->w_topline
9981#ifdef FEAT_DIFF
9982 || old_topfill != curwin->w_topfill
9983#endif
9984 )
9985 redraw_later(VALID);
9986 start_arrow(&tpos);
9987#ifdef FEAT_CINDENT
9988 can_cindent = TRUE;
9989#endif
9990 }
9991 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009992 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993}
9994
9995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009996ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 pos_T tpos;
9999
10000 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010001
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010002 if (mod_mask & MOD_MASK_CTRL)
10003 {
10004 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010005 if (first_tabpage->tp_next != NULL)
10006 {
10007 start_arrow(&curwin->w_cursor);
10008 goto_tabpage(-1);
10009 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010010 return;
10011 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010012
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 tpos = curwin->w_cursor;
10014 if (onepage(BACKWARD, 1L) == OK)
10015 {
10016 start_arrow(&tpos);
10017#ifdef FEAT_CINDENT
10018 can_cindent = TRUE;
10019#endif
10020 }
10021 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010022 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023}
10024
10025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010026ins_down(
10027 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029 pos_T tpos;
10030 linenr_T old_topline = curwin->w_topline;
10031#ifdef FEAT_DIFF
10032 int old_topfill = curwin->w_topfill;
10033#endif
10034
10035 undisplay_dollar();
10036 tpos = curwin->w_cursor;
10037 if (cursor_down(1L, TRUE) == OK)
10038 {
10039 if (startcol)
10040 coladvance(getvcol_nolist(&Insstart));
10041 if (old_topline != curwin->w_topline
10042#ifdef FEAT_DIFF
10043 || old_topfill != curwin->w_topfill
10044#endif
10045 )
10046 redraw_later(VALID);
10047 start_arrow(&tpos);
10048#ifdef FEAT_CINDENT
10049 can_cindent = TRUE;
10050#endif
10051 }
10052 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010053 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054}
10055
10056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010057ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 pos_T tpos;
10060
10061 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010062
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010063 if (mod_mask & MOD_MASK_CTRL)
10064 {
10065 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010066 if (first_tabpage->tp_next != NULL)
10067 {
10068 start_arrow(&curwin->w_cursor);
10069 goto_tabpage(0);
10070 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010071 return;
10072 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010073
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 tpos = curwin->w_cursor;
10075 if (onepage(FORWARD, 1L) == OK)
10076 {
10077 start_arrow(&tpos);
10078#ifdef FEAT_CINDENT
10079 can_cindent = TRUE;
10080#endif
10081 }
10082 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010083 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086#ifdef FEAT_DND
10087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010088ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089{
10090 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10091}
10092#endif
10093
10094/*
10095 * Handle TAB in Insert or Replace mode.
10096 * Return TRUE when the TAB needs to be inserted like a normal character.
10097 */
10098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 int ind;
10102 int i;
10103 int temp;
10104
10105 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10106 Insstart_blank_vcol = get_nolist_virtcol();
10107 if (echeck_abbr(TAB + ABBR_OFF))
10108 return FALSE;
10109
10110 ind = inindent(0);
10111#ifdef FEAT_CINDENT
10112 if (ind)
10113 can_cindent = FALSE;
10114#endif
10115
10116 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010117 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 */
10119 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010120#ifdef FEAT_VARTABS
10121 && !(p_sta && ind
10122 /* These five lines mean 'tabstop' != 'shiftwidth' */
10123 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10124 || (tabstop_count(curbuf->b_p_vts_array) == 1
10125 && tabstop_first(curbuf->b_p_vts_array)
10126 != get_sw_value(curbuf))
10127 || (tabstop_count(curbuf->b_p_vts_array) == 0
10128 && curbuf->b_p_ts != get_sw_value(curbuf))))
10129 && tabstop_count(curbuf->b_p_vsts_array) == 0
10130#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010131 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010132#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010133 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 return TRUE;
10135
10136 if (stop_arrow() == FAIL)
10137 return TRUE;
10138
10139 did_ai = FALSE;
10140#ifdef FEAT_SMARTINDENT
10141 did_si = FALSE;
10142 can_si = FALSE;
10143 can_si_back = FALSE;
10144#endif
10145 AppendToRedobuff((char_u *)"\t");
10146
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010147#ifdef FEAT_VARTABS
10148 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10149 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010150 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010151 temp -= get_nolist_virtcol() % temp;
10152 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010153 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010154 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010155 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010156 curbuf->b_p_vsts_array);
10157 else /* otherwise use 'tabstop' */
10158 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10159 curbuf->b_p_vts_array);
10160#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010162 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010163 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10164 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 else /* otherwise use 'tabstop' */
10166 temp = (int)curbuf->b_p_ts;
10167 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169
10170 /*
10171 * Insert the first space with ins_char(). It will delete one char in
10172 * replace mode. Insert the rest with ins_str(); it will not delete any
10173 * chars. For VREPLACE mode, we use ins_char() for all characters.
10174 */
10175 ins_char(' ');
10176 while (--temp > 0)
10177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (State & VREPLACE_FLAG)
10179 ins_char(' ');
10180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 {
10182 ins_str((char_u *)" ");
10183 if (State & REPLACE_FLAG) /* no char replaced */
10184 replace_push(NUL);
10185 }
10186 }
10187
10188 /*
10189 * When 'expandtab' not set: Replace spaces by TABs where possible.
10190 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010191#ifdef FEAT_VARTABS
10192 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10193 || get_sts_value() > 0
10194 || (p_sta && ind)))
10195#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010196 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
10199 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 char_u *saved_line = NULL; /* init for GCC */
10201 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 pos_T fpos;
10203 pos_T *cursor;
10204 colnr_T want_vcol, vcol;
10205 int change_col = -1;
10206 int save_list = curwin->w_p_list;
10207
10208 /*
10209 * Get the current line. For VREPLACE mode, don't make real changes
10210 * yet, just work on a copy of the line.
10211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (State & VREPLACE_FLAG)
10213 {
10214 pos = curwin->w_cursor;
10215 cursor = &pos;
10216 saved_line = vim_strsave(ml_get_curline());
10217 if (saved_line == NULL)
10218 return FALSE;
10219 ptr = saved_line + pos.col;
10220 }
10221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 {
10223 ptr = ml_get_cursor();
10224 cursor = &curwin->w_cursor;
10225 }
10226
10227 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10228 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10229 curwin->w_p_list = FALSE;
10230
10231 /* Find first white before the cursor */
10232 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010233 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 {
10235 --fpos.col;
10236 --ptr;
10237 }
10238
10239 /* In Replace mode, don't change characters before the insert point. */
10240 if ((State & REPLACE_FLAG)
10241 && fpos.lnum == Insstart.lnum
10242 && fpos.col < Insstart.col)
10243 {
10244 ptr += Insstart.col - fpos.col;
10245 fpos.col = Insstart.col;
10246 }
10247
10248 /* compute virtual column numbers of first white and cursor */
10249 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10250 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10251
Bram Moolenaar597a4222014-06-25 14:39:50 +020010252 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10253 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010254 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010256 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 if (vcol + i > want_vcol)
10258 break;
10259 if (*ptr != TAB)
10260 {
10261 *ptr = TAB;
10262 if (change_col < 0)
10263 {
10264 change_col = fpos.col; /* Column of first change */
10265 /* May have to adjust Insstart */
10266 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10267 Insstart.col = fpos.col;
10268 }
10269 }
10270 ++fpos.col;
10271 ++ptr;
10272 vcol += i;
10273 }
10274
10275 if (change_col >= 0)
10276 {
10277 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010278 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279
10280 /* Skip over the spaces we need. */
10281 while (vcol < want_vcol && *ptr == ' ')
10282 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010283 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 ++ptr;
10285 ++repl_off;
10286 }
10287 if (vcol > want_vcol)
10288 {
10289 /* Must have a char with 'showbreak' just before it. */
10290 --ptr;
10291 --repl_off;
10292 }
10293 fpos.col += repl_off;
10294
10295 /* Delete following spaces. */
10296 i = cursor->col - fpos.col;
10297 if (i > 0)
10298 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010299 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010301 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 for (temp = i; --temp >= 0; )
10303 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010304#ifdef FEAT_TEXT_PROP
10305 curbuf->b_ml.ml_line_len -= i;
10306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010308#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010309 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010310 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010311 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010312 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10313 (char_u *)"\t", 1);
10314 }
10315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 cursor->col -= i;
10317
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 /*
10319 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10320 * backspacing over the changed spacing and then inserting the new
10321 * spacing.
10322 */
10323 if (State & VREPLACE_FLAG)
10324 {
10325 /* Backspace from real cursor to change_col */
10326 backspace_until_column(change_col);
10327
10328 /* Insert each char in saved_line from changed_col to
10329 * ptr-cursor */
10330 ins_bytes_len(saved_line + change_col,
10331 cursor->col - change_col);
10332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 }
10334
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 if (State & VREPLACE_FLAG)
10336 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 curwin->w_p_list = save_list;
10338 }
10339
10340 return FALSE;
10341}
10342
10343/*
10344 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010345 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 */
10347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010348ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349{
10350 int i;
10351
10352 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010353 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 undisplay_dollar();
10357
10358 /*
10359 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10360 * character under the cursor. Only push a NUL on the replace stack,
10361 * nothing to put back when the NL is deleted.
10362 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010363 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 replace_push(NUL);
10365
10366 /*
10367 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10368 * replacing the next line, so we push all of the characters left on the
10369 * line onto the replace stack. This is not done here though, it is done
10370 * in open_line().
10371 */
10372
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010373#ifdef FEAT_VIRTUALEDIT
10374 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10375 * CTRL-O). */
10376 if (virtual_active() && curwin->w_cursor.coladd > 0)
10377 coladvance(getviscol());
10378#endif
10379
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380#ifdef FEAT_RIGHTLEFT
10381# ifdef FEAT_FKMAP
10382 if (p_altkeymap && p_fkmap)
10383 fkmap(NL);
10384# endif
10385 /* NL in reverse insert will always start in the end of
10386 * current line. */
10387 if (revins_on)
10388 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10389#endif
10390
10391 AppendToRedobuff(NL_STR);
10392 i = open_line(FORWARD,
10393#ifdef FEAT_COMMENTS
10394 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10395#endif
10396 0, old_indent);
10397 old_indent = 0;
10398#ifdef FEAT_CINDENT
10399 can_cindent = TRUE;
10400#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010401#ifdef FEAT_FOLDING
10402 /* When inserting a line the cursor line must never be in a closed fold. */
10403 foldOpenCursor();
10404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010406 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407}
10408
10409#ifdef FEAT_DIGRAPHS
10410/*
10411 * Handle digraph in insert mode.
10412 * Returns character still to be inserted, or NUL when nothing remaining to be
10413 * done.
10414 */
10415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010416ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417{
10418 int c;
10419 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010420 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421
10422 pc_status = PC_STATUS_UNSET;
10423 if (redrawing() && !char_avail())
10424 {
10425 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010426 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427
10428 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010429 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#ifdef FEAT_CMDL_INFO
10431 add_to_showcmd_c(Ctrl_K);
10432#endif
10433 }
10434
10435#ifdef USE_ON_FLY_SCROLL
10436 dont_scroll = TRUE; /* disallow scrolling here */
10437#endif
10438
10439 /* don't map the digraph chars. This also prevents the
10440 * mode message to be deleted when ESC is hit */
10441 ++no_mapping;
10442 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010443 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 --no_mapping;
10445 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010446 if (did_putchar)
10447 /* when the line fits in 'columns' the '?' is at the start of the next
10448 * line and will not be removed by the redraw */
10449 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010450
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 if (IS_SPECIAL(c) || mod_mask) /* special key */
10452 {
10453#ifdef FEAT_CMDL_INFO
10454 clear_showcmd();
10455#endif
10456 insert_special(c, TRUE, FALSE);
10457 return NUL;
10458 }
10459 if (c != ESC)
10460 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010461 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 if (redrawing() && !char_avail())
10463 {
10464 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010465 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466
10467 if (char2cells(c) == 1)
10468 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010469 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010471 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 }
10473#ifdef FEAT_CMDL_INFO
10474 add_to_showcmd_c(c);
10475#endif
10476 }
10477 ++no_mapping;
10478 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010479 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 --no_mapping;
10481 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010482 if (did_putchar)
10483 /* when the line fits in 'columns' the '?' is at the start of the
10484 * next line and will not be removed by a redraw */
10485 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 if (cc != ESC)
10487 {
10488 AppendToRedobuff((char_u *)CTRL_V_STR);
10489 c = getdigraph(c, cc, TRUE);
10490#ifdef FEAT_CMDL_INFO
10491 clear_showcmd();
10492#endif
10493 return c;
10494 }
10495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496#ifdef FEAT_CMDL_INFO
10497 clear_showcmd();
10498#endif
10499 return NUL;
10500}
10501#endif
10502
10503/*
10504 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10505 * Returns the char to be inserted, or NUL if none found.
10506 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010508ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 int c;
10511 int temp;
10512 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010513 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514
10515 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10516 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010517 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 return NUL;
10519 }
10520
10521 /* try to advance to the cursor column */
10522 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010523 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 prev_ptr = ptr;
10525 validate_virtcol();
10526 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10527 {
10528 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010529 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 }
10531 if ((colnr_T)temp > curwin->w_virtcol)
10532 ptr = prev_ptr;
10533
10534#ifdef FEAT_MBYTE
10535 c = (*mb_ptr2char)(ptr);
10536#else
10537 c = *ptr;
10538#endif
10539 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010540 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 return c;
10542}
10543
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010544/*
10545 * CTRL-Y or CTRL-E typed in Insert mode.
10546 */
10547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010548ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010549{
10550 int c = tc;
10551
10552#ifdef FEAT_INS_EXPAND
10553 if (ctrl_x_mode == CTRL_X_SCROLL)
10554 {
10555 if (c == Ctrl_Y)
10556 scrolldown_clamp();
10557 else
10558 scrollup_clamp();
10559 redraw_later(VALID);
10560 }
10561 else
10562#endif
10563 {
10564 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10565 if (c != NUL)
10566 {
10567 long tw_save;
10568
10569 /* The character must be taken literally, insert like it
10570 * was typed after a CTRL-V, and pretend 'textwidth'
10571 * wasn't set. Digits, 'o' and 'x' are special after a
10572 * CTRL-V, don't use it for these. */
10573 if (c < 256 && !isalnum(c))
10574 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10575 tw_save = curbuf->b_p_tw;
10576 curbuf->b_p_tw = -1;
10577 insert_special(c, TRUE, FALSE);
10578 curbuf->b_p_tw = tw_save;
10579#ifdef FEAT_RIGHTLEFT
10580 revins_chars++;
10581 revins_legal++;
10582#endif
10583 c = Ctrl_V; /* pretend CTRL-V is last character */
10584 auto_format(FALSE, TRUE);
10585 }
10586 }
10587 return c;
10588}
10589
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590#ifdef FEAT_SMARTINDENT
10591/*
10592 * Try to do some very smart auto-indenting.
10593 * Used when inserting a "normal" character.
10594 */
10595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010596ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597{
10598 pos_T *pos, old_pos;
10599 char_u *ptr;
10600 int i;
10601 int temp;
10602
10603 /*
10604 * do some very smart indenting when entering '{' or '}'
10605 */
10606 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10607 {
10608 /*
10609 * for '}' set indent equal to indent of line containing matching '{'
10610 */
10611 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10612 {
10613 old_pos = curwin->w_cursor;
10614 /*
10615 * If the matching '{' has a ')' immediately before it (ignoring
10616 * white-space), then line up with the start of the line
10617 * containing the matching '(' if there is one. This handles the
10618 * case where an "if (..\n..) {" statement continues over multiple
10619 * lines -- webb
10620 */
10621 ptr = ml_get(pos->lnum);
10622 i = pos->col;
10623 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010624 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 ;
10626 curwin->w_cursor.lnum = pos->lnum;
10627 curwin->w_cursor.col = i;
10628 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10629 curwin->w_cursor = *pos;
10630 i = get_indent();
10631 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010633 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 (void)set_indent(i, SIN_CHANGED);
10636 }
10637 else if (curwin->w_cursor.col > 0)
10638 {
10639 /*
10640 * when inserting '{' after "O" reduce indent, but not
10641 * more than indent of previous line
10642 */
10643 temp = TRUE;
10644 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10645 {
10646 old_pos = curwin->w_cursor;
10647 i = get_indent();
10648 while (curwin->w_cursor.lnum > 1)
10649 {
10650 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10651
10652 /* ignore empty lines and lines starting with '#'. */
10653 if (*ptr != '#' && *ptr != NUL)
10654 break;
10655 }
10656 if (get_indent() >= i)
10657 temp = FALSE;
10658 curwin->w_cursor = old_pos;
10659 }
10660 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010661 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663 }
10664
10665 /*
10666 * set indent of '#' always to 0
10667 */
10668 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10669 {
10670 /* remember current indent for next line */
10671 old_indent = get_indent();
10672 (void)set_indent(0, SIN_CHANGED);
10673 }
10674
10675 /* Adjust ai_col, the char at this position can be deleted. */
10676 if (ai_col > curwin->w_cursor.col)
10677 ai_col = curwin->w_cursor.col;
10678}
10679#endif
10680
10681/*
10682 * Get the value that w_virtcol would have when 'list' is off.
10683 * Unless 'cpo' contains the 'L' flag.
10684 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010685 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010688 // check validity of cursor in current buffer
10689 if (curwin->w_buffer == NULL
10690 || curwin->w_buffer->b_ml.ml_mfp == NULL
10691 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10692 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10694 return getvcol_nolist(&curwin->w_cursor);
10695 validate_virtcol();
10696 return curwin->w_virtcol;
10697}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010698
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010699#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010700/*
10701 * Handle the InsertCharPre autocommand.
10702 * "c" is the character that was typed.
10703 * Return a pointer to allocated memory with the replacement string.
10704 * Return NULL to continue inserting "c".
10705 */
10706 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010707do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010708{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010709 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010710 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010711
10712 /* Return quickly when there is nothing to do. */
10713 if (!has_insertcharpre())
10714 return NULL;
10715
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010716# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010717 if (has_mbyte)
10718 buf[(*mb_char2bytes)(c, buf)] = NUL;
10719 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010720# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010721 {
10722 buf[0] = c;
10723 buf[1] = NUL;
10724 }
10725
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010726 /* Lock the text to avoid weird things from happening. */
10727 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010728 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010729
Bram Moolenaar704984a2012-06-01 14:57:51 +020010730 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010731 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010732 {
10733 /* Get the value of v:char. It may be empty or more than one
10734 * character. Only use it when changed, otherwise continue with the
10735 * original character to avoid breaking autoindent. */
10736 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10737 res = vim_strsave(get_vim_var_str(VV_CHAR));
10738 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010739
10740 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10741 --textlock;
10742
10743 return res;
10744}
10745#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010746
10747/*
10748 * Trigger "event" and take care of fixing undo.
10749 */
10750 static int
10751ins_apply_autocmds(event_T event)
10752{
10753 varnumber_T tick = CHANGEDTICK(curbuf);
10754 int r;
10755
10756 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10757
10758 // If u_savesub() was called then we are not prepared to start
10759 // a new line. Call u_save() with no contents to fix that.
10760 if (tick != CHANGEDTICK(curbuf))
10761 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10762
10763 return r;
10764}