blob: 6c21eee4e68e7d87e6fc57a7554641efffa10302 [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);
158static int ins_compl_equal(compl_T *match, char_u *str, int len);
159static void ins_compl_longest_match(compl_T *match);
160static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
161static int ins_compl_make_cyclic(void);
162static void ins_compl_upd_pum(void);
163static void ins_compl_del_pum(void);
164static int pum_wanted(void);
165static int pum_enough_matches(void);
166static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
167static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
168static char_u *find_line_end(char_u *ptr);
169static void ins_compl_free(void);
170static void ins_compl_clear(void);
171static int ins_compl_bs(void);
172static int ins_compl_need_restart(void);
173static void ins_compl_new_leader(void);
174static void ins_compl_addleader(int c);
175static int ins_compl_len(void);
176static void ins_compl_restart(void);
177static void ins_compl_set_original_text(char_u *str);
178static void ins_compl_addfrommatch(void);
179static int ins_compl_prep(int c);
180static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
181static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100182# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_add_list(list_T *list);
184static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100185# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static int ins_compl_get_exp(pos_T *ini);
187static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200188static void ins_compl_insert(int in_compl_func);
189static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_key2dir(int c);
191static int ins_compl_pum_key(int c);
192static int ins_compl_key2count(int c);
193static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100194static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100195static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100196static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* FEAT_INS_EXPAND */
198
199#define BACKSPACE_CHAR 1
200#define BACKSPACE_WORD 2
201#define BACKSPACE_WORD_NOT_SPACE 3
202#define BACKSPACE_LINE 4
203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_redraw(int ready);
205static void ins_ctrl_v(void);
206static void undisplay_dollar(void);
207static void insert_special(int, int, int);
208static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
209static void check_auto_format(int);
210static void redo_literal(int c);
211static void start_arrow(pos_T *end_insert_pos);
212static void start_arrow_with_change(pos_T *end_insert_pos, int change);
213static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000214#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void check_spell_redraw(void);
216static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000217static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
220static int echeck_abbr(int);
221static int replace_pop(void);
222static void replace_join(int off);
223static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void replace_flush(void);
228static void replace_do_bs(int limit_col);
229static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100233static void ins_reg(void);
234static void ins_ctrl_g(void);
235static void ins_ctrl_hat(void);
236static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static int ins_start_select(int c);
241static void ins_insert(int replaceState);
242static void ins_ctrl_o(void);
243static void ins_shift(int c, int lastc);
244static void ins_del(void);
245static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_mouse(int c);
248static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000250#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000252#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ins_left(int end_change);
254static void ins_home(int c);
255static void ins_end(int c);
256static void ins_s_left(void);
257static void ins_right(int end_change);
258static void ins_s_right(void);
259static void ins_up(int startcol);
260static void ins_pageup(void);
261static void ins_down(int startcol);
262static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static int ins_tab(void);
267static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100273static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100276#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280static colnr_T Insstart_textlen; /* length of line when insert started */
281static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100282static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284static char_u *last_insert = NULL; /* the text of the previous insert,
285 K_SPECIAL and CSI are escaped */
286static int last_insert_skip; /* nr of chars in front of previous insert */
287static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000288static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
290#ifdef FEAT_CINDENT
291static int can_cindent; /* may do cindenting on this line */
292#endif
293
294static int old_indent = 0; /* for ^^D command in insert mode */
295
296#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000297static int revins_on; /* reverse insert mode on */
298static int revins_chars; /* how much to skip after edit */
299static int revins_legal; /* was the last char 'legal'? */
300static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static int ins_need_undo; /* call u_save() before inserting a
304 char. Set when edit() is called.
305 after that arrow_used is used. */
306
307static int did_add_space = FALSE; /* auto_format() added an extra space
308 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200309static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
310 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312/*
313 * edit(): Start inserting text.
314 *
315 * "cmdchar" can be:
316 * 'i' normal insert command
317 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100318 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 * 'R' replace command
320 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
321 * but still only one <CR> is inserted. The <Esc> is not used for redo.
322 * 'g' "gI" command.
323 * 'V' "gR" command for Virtual Replace mode.
324 * 'v' "gr" command for single character Virtual Replace mode.
325 *
326 * This function is not called recursively. For CTRL-O commands, it returns
327 * and lets the caller handle the Normal-mode command.
328 *
329 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
330 */
331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100332edit(
333 int cmdchar,
334 int startln, /* if set, insert at start of line */
335 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 int c = 0;
338 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100339 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000340 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 int i;
343 int did_backspace = TRUE; /* previous char was backspace */
344#ifdef FEAT_CINDENT
345 int line_is_white = FALSE; /* line is empty before insert */
346#endif
347 linenr_T old_topline = 0; /* topline before insertion */
348#ifdef FEAT_DIFF
349 int old_topfill = -1;
350#endif
351 int inserted_space = FALSE; /* just inserted a space */
352 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000353 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000355 /* Remember whether editing was restarted after CTRL-O. */
356 did_restart_edit = restart_edit;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* sleep before redrawing, needed for "CTRL-O :" that results in an
359 * error message */
360 check_for_delay(TRUE);
361
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100362 /* set Insstart_orig to Insstart */
363 update_Insstart_orig = TRUE;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365#ifdef HAVE_SANDBOX
366 /* Don't allow inserting in the sandbox. */
367 if (sandbox != 0)
368 {
369 EMSG(_(e_sandbox));
370 return FALSE;
371 }
372#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000373 /* Don't allow changes in the buffer while editing the cmdline. The
374 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000375 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000376 {
377 EMSG(_(e_secure));
378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
381#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000382 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000383 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000384 {
385 EMSG(_(e_secure));
386 return FALSE;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 ins_compl_clear(); /* clear stuff for CTRL-X mode */
389#endif
390
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391#ifdef FEAT_AUTOCMD
392 /*
393 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
394 */
395 if (cmdchar != 'r' && cmdchar != 'v')
396 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100397 pos_T save_cursor = curwin->w_cursor;
398
Bram Moolenaar1e015462005-09-25 22:16:38 +0000399# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000400 if (cmdchar == 'R')
401 ptr = (char_u *)"r";
402 else if (cmdchar == 'V')
403 ptr = (char_u *)"v";
404 else
405 ptr = (char_u *)"i";
406 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200407 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000408# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000409 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100410
Bram Moolenaar097c9922013-05-19 21:15:15 +0200411 /* Make sure the cursor didn't move. Do call check_cursor_col() in
412 * case the text was modified. Since Insert mode was not started yet
413 * a call to check_cursor_col() may move the cursor, especially with
414 * the "A" command, thus set State to avoid that. Also check that the
415 * line number is still valid (lines may have been deleted).
416 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100417 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaar097c9922013-05-19 21:15:15 +0200418# ifdef FEAT_EVAL
419 && *get_vim_var_str(VV_CHAR) == NUL
420# endif
421 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100422 {
423 int save_state = State;
424
425 curwin->w_cursor = save_cursor;
426 State = INSERT;
427 check_cursor_col();
428 State = save_state;
429 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000430 }
431#endif
432
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200433#ifdef FEAT_CONCEAL
434 /* Check if the cursor line needs redrawing before changing State. If
435 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200436 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#endif
438
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#ifdef FEAT_MOUSE
440 /*
441 * When doing a paste with the middle mouse button, Insstart is set to
442 * where the paste started.
443 */
444 if (where_paste_started.lnum != 0)
445 Insstart = where_paste_started;
446 else
447#endif
448 {
449 Insstart = curwin->w_cursor;
450 if (startln)
451 Insstart.col = 0;
452 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000453 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 Insstart_blank_vcol = MAXCOL;
455 if (!did_ai)
456 ai_col = 0;
457
458 if (cmdchar != NUL && restart_edit == 0)
459 {
460 ResetRedobuff();
461 AppendNumberToRedobuff(count);
462#ifdef FEAT_VREPLACE
463 if (cmdchar == 'V' || cmdchar == 'v')
464 {
465 /* "gR" or "gr" command */
466 AppendCharToRedobuff('g');
467 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
468 }
469 else
470#endif
471 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100472 if (cmdchar == K_PS)
473 AppendCharToRedobuff('a');
474 else
475 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (cmdchar == 'g') /* "gI" command */
477 AppendCharToRedobuff('I');
478 else if (cmdchar == 'r') /* "r<CR>" command */
479 count = 1; /* insert only one <CR> */
480 }
481 }
482
483 if (cmdchar == 'R')
484 {
485#ifdef FEAT_FKMAP
486 if (p_fkmap && p_ri)
487 {
488 beep_flush();
489 EMSG(farsi_text_3); /* encoded in Farsi */
490 State = INSERT;
491 }
492 else
493#endif
494 State = REPLACE;
495 }
496#ifdef FEAT_VREPLACE
497 else if (cmdchar == 'V' || cmdchar == 'v')
498 {
499 State = VREPLACE;
500 replaceState = VREPLACE;
501 orig_line_count = curbuf->b_ml.ml_line_count;
502 vr_lines_changed = 1;
503 }
504#endif
505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100524#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
712 /*
713 * If we inserted a character at the last position of the last line in
714 * the window, scroll the window one line up. This avoids an extra
715 * redraw.
716 * This is detected when the cursor column is smaller after inserting
717 * something.
718 * Don't do this when the topline changed already, it has
719 * already been adjusted (by insertchar() calling open_line())).
720 */
721 if (curbuf->b_mod_set
722 && curwin->w_p_wrap
723 && !did_backspace
724 && curwin->w_topline == old_topline
725#ifdef FEAT_DIFF
726 && curwin->w_topfill == old_topfill
727#endif
728 )
729 {
730 mincol = curwin->w_wcol;
731 validate_cursor_col();
732
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000733 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 && curwin->w_wrow == W_WINROW(curwin)
735 + curwin->w_height - 1 - p_so
736 && (curwin->w_cursor.lnum != curwin->w_topline
737#ifdef FEAT_DIFF
738 || curwin->w_topfill > 0
739#endif
740 ))
741 {
742#ifdef FEAT_DIFF
743 if (curwin->w_topfill > 0)
744 --curwin->w_topfill;
745 else
746#endif
747#ifdef FEAT_FOLDING
748 if (hasFolding(curwin->w_topline, NULL, &old_topline))
749 set_topline(curwin, old_topline + 1);
750 else
751#endif
752 set_topline(curwin, curwin->w_topline + 1);
753 }
754 }
755
756 /* May need to adjust w_topline to show the cursor. */
757 update_topline();
758
759 did_backspace = FALSE;
760
761 validate_cursor(); /* may set must_redraw */
762
763 /*
764 * Redraw the display when no characters are waiting.
765 * Also shows mode, ruler and positions cursor.
766 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000767 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769#ifdef FEAT_SCROLLBIND
770 if (curwin->w_p_scb)
771 do_check_scrollbind(TRUE);
772#endif
773
Bram Moolenaar860cae12010-06-05 23:22:07 +0200774#ifdef FEAT_CURSORBIND
775 if (curwin->w_p_crb)
776 do_check_cursorbind();
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 update_curswant();
779 old_topline = curwin->w_topline;
780#ifdef FEAT_DIFF
781 old_topfill = curwin->w_topfill;
782#endif
783
784#ifdef USE_ON_FLY_SCROLL
785 dont_scroll = FALSE; /* allow scrolling here */
786#endif
787
788 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100789 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100791 if (c != K_CURSORHOLD)
792 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200793
794 /* After using CTRL-G U the next cursor key will not break undo. */
795 if (dont_sync_undo == MAYBE)
796 dont_sync_undo = TRUE;
797 else
798 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100799 if (cmdchar == K_PS)
800 /* Got here from normal mode when bracketed paste started. */
801 c = K_PS;
802 else
803 do
804 {
805 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100806 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000808#ifdef FEAT_AUTOCMD
809 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
810 did_cursorhold = TRUE;
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_RIGHTLEFT
814 if (p_hkmap && KeyTyped)
815 c = hkmap(c); /* Hebrew mode mapping */
816#endif
817#ifdef FEAT_FKMAP
818 if (p_fkmap && KeyTyped)
819 c = fkmap(c); /* Farsi mode mapping */
820#endif
821
822#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000823 /*
824 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000825 * and the cursor is still in the completed word. Only when there is
826 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000828 if (compl_started
829 && pum_wanted()
830 && curwin->w_cursor.col >= compl_col
831 && (compl_shown_match == NULL
832 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000833 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* BS: Delete one character from "compl_leader". */
835 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000836 && curwin->w_cursor.col > compl_col
837 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 continue;
839
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /* When no match was selected or it was edited. */
841 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000843 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000844 * "compl_leader". Except when at the original match and
845 * there is nothing to add, CTRL-L works like CTRL-P then. */
846 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100847 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000848 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000849 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000850 {
851 ins_compl_addfrommatch();
852 continue;
853 }
854
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000855 /* A non-white character that fits in with the current
856 * completion: Add to "compl_leader". */
857 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000858 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100859#ifdef FEAT_AUTOCMD
860 /* Trigger InsertCharPre. */
861 char_u *str = do_insert_char_pre(c);
862 char_u *p;
863
864 if (str != NULL)
865 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100866 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100867 ins_compl_addleader(PTR2CHAR(p));
868 vim_free(str);
869 }
870 else
871#endif
872 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000873 continue;
874 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000876 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000877 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200878 if ((c == Ctrl_Y || (compl_enter_selects
879 && (c == CAR || c == K_KENTER || c == NL)))
880 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000881 {
882 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200883 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000885 }
886 }
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
889 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000890 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000891 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000892 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894
Bram Moolenaar488c6512005-08-11 20:09:58 +0000895 /* CTRL-\ CTRL-N goes to Normal mode,
896 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
897 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (c == Ctrl_BSL)
899 {
900 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000901 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ++no_mapping;
903 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000904 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 --no_mapping;
906 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000907 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 vungetc(c);
911 c = Ctrl_BSL;
912 }
913 else if (c == Ctrl_G && p_im)
914 continue;
915 else
916 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000917 if (c == Ctrl_O)
918 {
919 ins_ctrl_o();
920 ins_at_eol = FALSE; /* cursor keeps its column */
921 nomove = TRUE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 count = 0;
924 goto doESCkey;
925 }
926 }
927
928#ifdef FEAT_DIGRAPHS
929 c = do_digraph(c);
930#endif
931
932#ifdef FEAT_INS_EXPAND
933 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
934 goto docomplete;
935#endif
936 if (c == Ctrl_V || c == Ctrl_Q)
937 {
938 ins_ctrl_v();
939 c = Ctrl_V; /* pretend CTRL-V is last typed character */
940 continue;
941 }
942
943#ifdef FEAT_CINDENT
944 if (cindent_on()
945# ifdef FEAT_INS_EXPAND
946 && ctrl_x_mode == 0
947# endif
948 )
949 {
950 /* A key name preceded by a bang means this key is not to be
951 * inserted. Skip ahead to the re-indenting below.
952 * A key name preceded by a star means that indenting has to be
953 * done before inserting the key. */
954 line_is_white = inindent(0);
955 if (in_cinkeys(c, '!', line_is_white))
956 goto force_cindent;
957 if (can_cindent && in_cinkeys(c, '*', line_is_white)
958 && stop_arrow() == OK)
959 do_c_expr_indent();
960 }
961#endif
962
963#ifdef FEAT_RIGHTLEFT
964 if (curwin->w_p_rl)
965 switch (c)
966 {
967 case K_LEFT: c = K_RIGHT; break;
968 case K_S_LEFT: c = K_S_RIGHT; break;
969 case K_C_LEFT: c = K_C_RIGHT; break;
970 case K_RIGHT: c = K_LEFT; break;
971 case K_S_RIGHT: c = K_S_LEFT; break;
972 case K_C_RIGHT: c = K_C_LEFT; break;
973 }
974#endif
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /*
977 * If 'keymodel' contains "startsel", may start selection. If it
978 * does, a CTRL-O and c will be stuffed, we need to get these
979 * characters.
980 */
981 if (ins_start_select(c))
982 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * The big switch to handle a character in insert mode.
986 */
987 switch (c)
988 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (echeck_abbr(ESC + ABBR_OFF))
991 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200992 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_CMDWIN
996 if (c == Ctrl_C && cmdwin_type != 0)
997 {
998 /* Close the cmdline window. */
999 cmdwin_result = K_IGNORE;
1000 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001001 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 goto doESCkey;
1003 }
1004#endif
1005
1006#ifdef UNIX
1007do_intr:
1008#endif
1009 /* when 'insertmode' set, and not halfway a mapping, don't leave
1010 * Insert mode */
1011 if (goto_im())
1012 {
1013 if (got_int)
1014 {
1015 (void)vgetc(); /* flush all buffers */
1016 got_int = FALSE;
1017 }
1018 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001019 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021 }
1022doESCkey:
1023 /*
1024 * This is the ONLY return from edit()!
1025 */
1026 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1027 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001028 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 o_lnum = curwin->w_cursor.lnum;
1030
Bram Moolenaar488c6512005-08-11 20:09:58 +00001031 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032 {
1033#ifdef FEAT_AUTOCMD
1034 if (cmdchar != 'r' && cmdchar != 'v')
1035 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1036 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001037 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 continue;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case Ctrl_Z: /* suspend when 'insertmode' set */
1044 if (!p_im)
1045 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001046 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001047#ifdef CURSOR_SHAPE
1048 ui_cursor_shape(); /* may need to update cursor shape */
1049#endif
1050 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051
1052 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001053#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001054 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 goto docomplete;
1056#endif
1057 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1058 break;
1059 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001060
1061#ifdef FEAT_VIRTUALEDIT
1062 /* don't move the cursor left when 'virtualedit' has "onemore". */
1063 if (ve_flags & VE_ONEMORE)
1064 {
1065 ins_at_eol = FALSE;
1066 nomove = TRUE;
1067 }
1068#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 count = 0;
1070 goto doESCkey;
1071
Bram Moolenaar572cb562005-08-05 21:35:02 +00001072 case K_INS: /* toggle insert/replace mode */
1073 case K_KINS:
1074 ins_insert(replaceState);
1075 break;
1076
1077 case K_SELECT: /* end of Select mode mapping - ignore */
1078 break;
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_HELP: /* Help key works like <ESC> <Help> */
1081 case K_F1:
1082 case K_XF1:
1083 stuffcharReadbuff(K_HELP);
1084 if (p_im)
1085 need_start_insertmode = TRUE;
1086 goto doESCkey;
1087
1088#ifdef FEAT_NETBEANS_INTG
1089 case K_F21: /* NetBeans command */
1090 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001091 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 --no_mapping;
1093 netbeans_keycommand(i);
1094 break;
1095#endif
1096
1097 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case NUL:
1099 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 /* For ^@ the trailing ESC will end the insert, unless there is an
1101 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1103 && c != Ctrl_A && !p_im)
1104 goto doESCkey; /* quit insert mode */
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_reg();
1110 auto_format(FALSE, TRUE);
1111 inserted_space = FALSE;
1112 break;
1113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 ins_ctrl_g();
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_HAT: /* switch input mode and/or langmap */
1119 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
1122#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!p_ari)
1125 goto normalchar;
1126 ins_ctrl_();
1127 break;
1128#endif
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1132 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1133 goto docomplete;
1134#endif
1135 /* FALLTHROUGH */
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138# ifdef FEAT_INS_EXPAND
1139 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 if (has_compl_option(FALSE))
1142 goto docomplete;
1143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145# endif
1146 ins_shift(c, lastc);
1147 auto_format(FALSE, TRUE);
1148 inserted_space = FALSE;
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_KDEL:
1153 ins_del();
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case Ctrl_H:
1159 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1160 auto_format(FALSE, TRUE);
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1165 auto_format(FALSE, TRUE);
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001169# ifdef FEAT_COMPL_FUNC
1170 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001172 goto docomplete;
1173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
1179#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_LEFTMOUSE_NM:
1182 case K_LEFTDRAG:
1183 case K_LEFTRELEASE:
1184 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001185 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_MIDDLEMOUSE:
1187 case K_MIDDLEDRAG:
1188 case K_MIDDLERELEASE:
1189 case K_RIGHTMOUSE:
1190 case K_RIGHTDRAG:
1191 case K_RIGHTRELEASE:
1192 case K_X1MOUSE:
1193 case K_X1DRAG:
1194 case K_X1RELEASE:
1195 case K_X2MOUSE:
1196 case K_X2DRAG:
1197 case K_X2RELEASE:
1198 ins_mouse(c);
1199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001202 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001206 ins_mousescroll(MSCR_UP);
1207 break;
1208
1209 case K_MOUSELEFT: /* Scroll wheel left */
1210 ins_mousescroll(MSCR_LEFT);
1211 break;
1212
1213 case K_MOUSERIGHT: /* Scroll wheel right */
1214 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 break;
1216#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001217 case K_PS:
1218 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1219 if (cmdchar == K_PS)
1220 /* invoked from normal mode, bail out */
1221 goto doESCkey;
1222 break;
1223 case K_PE:
1224 /* Got K_PE without K_PS, ignore. */
1225 break;
1226
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001227#ifdef FEAT_GUI_TABLINE
1228 case K_TABLINE:
1229 case K_TABMENU:
1230 ins_tabline(c);
1231 break;
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar754b5602006-02-09 23:53:20 +00001237#ifdef FEAT_AUTOCMD
1238 case K_CURSORHOLD: /* Didn't type something for a while. */
1239 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1240 did_cursorhold = TRUE;
1241 break;
1242#endif
1243
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244#ifdef FEAT_GUI_W32
1245 /* On Win32 ignore <M-F4>, we get it when closing the window was
1246 * cancelled. */
1247 case K_F4:
1248 if (mod_mask != MOD_MASK_ALT)
1249 goto normalchar;
1250 break;
1251#endif
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#ifdef FEAT_GUI
1254 case K_VER_SCROLLBAR:
1255 ins_scroll();
1256 break;
1257
1258 case K_HOR_SCROLLBAR:
1259 ins_horscroll();
1260 break;
1261#endif
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_HOME:
1266 case K_C_HOME:
1267 ins_home(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 case K_S_END:
1273 case K_C_END:
1274 ins_end(c);
1275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001278 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1279 ins_s_left();
1280 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001281 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 case K_C_LEFT:
1286 ins_s_left();
1287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001290 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1291 ins_s_right();
1292 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001293 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 case K_C_RIGHT:
1298 ins_s_right();
1299 break;
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001302#ifdef FEAT_INS_EXPAND
1303 if (pum_visible())
1304 goto docomplete;
1305#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001306 if (mod_mask & MOD_MASK_SHIFT)
1307 ins_pageup();
1308 else
1309 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_PAGEUP:
1314 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001315#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001316 if (pum_visible())
1317 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 ins_pageup();
1320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001323#ifdef FEAT_INS_EXPAND
1324 if (pum_visible())
1325 goto docomplete;
1326#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001327 if (mod_mask & MOD_MASK_SHIFT)
1328 ins_pagedown();
1329 else
1330 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 break;
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case K_PAGEDOWN:
1335 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001336#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001337 if (pum_visible())
1338 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ins_pagedown();
1341 break;
1342
1343#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 ins_drop();
1346 break;
1347#endif
1348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 c = TAB;
1351 /* FALLTHROUGH */
1352
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001353 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1355 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1356 goto docomplete;
1357#endif
1358 inserted_space = FALSE;
1359 if (ins_tab())
1360 goto normalchar; /* insert TAB as a normal char */
1361 auto_format(FALSE, TRUE);
1362 break;
1363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 c = CAR;
1366 /* FALLTHROUGH */
1367 case CAR:
1368 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001369#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* In a quickfix window a <CR> jumps to the error under the
1371 * cursor. */
1372 if (bt_quickfix(curbuf) && c == CAR)
1373 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 if (curwin->w_llist_ref == NULL) /* quickfix window */
1375 do_cmdline_cmd((char_u *)".cc");
1376 else /* location list window */
1377 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 break;
1379 }
1380#endif
1381#ifdef FEAT_CMDWIN
1382 if (cmdwin_type != 0)
1383 {
1384 /* Execute the command in the cmdline window. */
1385 cmdwin_result = CAR;
1386 goto doESCkey;
1387 }
1388#endif
1389 if (ins_eol(c) && !p_im)
1390 goto doESCkey; /* out of memory */
1391 auto_format(FALSE, FALSE);
1392 inserted_space = FALSE;
1393 break;
1394
Bram Moolenaar860cae12010-06-05 23:22:07 +02001395#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001396 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397# ifdef FEAT_INS_EXPAND
1398 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1399 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001400 if (has_compl_option(TRUE))
1401 goto docomplete;
1402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404# endif
1405# ifdef FEAT_DIGRAPHS
1406 c = ins_digraph();
1407 if (c == NUL)
1408 break;
1409# endif
1410 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001414 case Ctrl_X: /* Enter CTRL-X mode */
1415 ins_ctrl_x();
1416 break;
1417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (ctrl_x_mode != CTRL_X_TAGS)
1420 goto normalchar;
1421 goto docomplete;
1422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (ctrl_x_mode != CTRL_X_FILES)
1425 goto normalchar;
1426 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001427
1428 case 's': /* Spelling completion after ^X */
1429 case Ctrl_S:
1430 if (ctrl_x_mode != CTRL_X_SPELL)
1431 goto normalchar;
1432 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#endif
1434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef FEAT_INS_EXPAND
1437 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1438#endif
1439 {
1440 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1441 if (p_im)
1442 {
1443 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1444 break;
1445 goto doESCkey;
1446 }
1447 goto normalchar;
1448 }
1449#ifdef FEAT_INS_EXPAND
1450 /* FALLTHROUGH */
1451
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 case Ctrl_N:
1454 /* if 'complete' is empty then plain ^P is no longer special,
1455 * but it is under other ^X modes */
1456 if (*curbuf->b_p_cpt == NUL
1457 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 goto normalchar;
1460
1461docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001462 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001463#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001464 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001465#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001466 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001467 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001468#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001469 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001470#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001471 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 break;
1473#endif /* FEAT_INS_EXPAND */
1474
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001475 case Ctrl_Y: /* copy from previous line or scroll down */
1476 case Ctrl_E: /* copy from next line or scroll up */
1477 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 break;
1479
1480 default:
1481#ifdef UNIX
1482 if (c == intr_char) /* special interrupt char */
1483 goto do_intr;
1484#endif
1485
Bram Moolenaare659c952011-05-19 17:25:41 +02001486normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001488 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001490#ifdef FEAT_AUTOCMD
1491 if (!p_paste)
1492 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001493 /* Trigger InsertCharPre. */
1494 char_u *str = do_insert_char_pre(c);
1495 char_u *p;
1496
1497 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001500 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001501 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001502 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001503 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001504 c = PTR2CHAR(p);
1505 if (c == CAR || c == K_KENTER || c == NL)
1506 ins_eol(c);
1507 else
1508 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001509 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001510 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001511 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001512 vim_free(str);
1513 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001514 }
1515
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001516 /* If the new value is already inserted or an empty string
1517 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001518 if (c == NUL)
1519 break;
1520 }
1521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522#ifdef FEAT_SMARTINDENT
1523 /* Try to perform smart-indenting. */
1524 ins_try_si(c);
1525#endif
1526
1527 if (c == ' ')
1528 {
1529 inserted_space = TRUE;
1530#ifdef FEAT_CINDENT
1531 if (inindent(0))
1532 can_cindent = FALSE;
1533#endif
1534 if (Insstart_blank_vcol == MAXCOL
1535 && curwin->w_cursor.lnum == Insstart.lnum)
1536 Insstart_blank_vcol = get_nolist_virtcol();
1537 }
1538
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001539 /* Insert a normal character and check for abbreviations on a
1540 * special character. Let CTRL-] expand abbreviations without
1541 * inserting it. */
1542 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#ifdef FEAT_MBYTE
1544 /* Add ABBR_OFF for characters above 0x100, this is
1545 * what check_abbr() expects. */
1546 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1547#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001548 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
1550 insert_special(c, FALSE, FALSE);
1551#ifdef FEAT_RIGHTLEFT
1552 revins_legal++;
1553 revins_chars++;
1554#endif
1555 }
1556
1557 auto_format(FALSE, TRUE);
1558
1559#ifdef FEAT_FOLDING
1560 /* When inserting a character the cursor line must never be in a
1561 * closed fold. */
1562 foldOpenCursor();
1563#endif
1564 break;
1565 } /* end of switch (c) */
1566
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001567#ifdef FEAT_AUTOCMD
1568 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001569 if (c != K_CURSORHOLD
1570# ifdef FEAT_COMPL_FUNC
1571 /* but not in CTRL-X mode, a script can't restore the state */
1572 && ctrl_x_mode == 0
1573# endif
1574 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001575 did_cursorhold = FALSE;
1576#endif
1577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 /* If the cursor was moved we didn't just insert a space */
1579 if (arrow_used)
1580 inserted_space = FALSE;
1581
1582#ifdef FEAT_CINDENT
1583 if (can_cindent && cindent_on()
1584# ifdef FEAT_INS_EXPAND
1585 && ctrl_x_mode == 0
1586# endif
1587 )
1588 {
1589force_cindent:
1590 /*
1591 * Indent now if a key was typed that is in 'cinkeys'.
1592 */
1593 if (in_cinkeys(c, ' ', line_is_white))
1594 {
1595 if (stop_arrow() == OK)
1596 /* re-indent the current line */
1597 do_c_expr_indent();
1598 }
1599 }
1600#endif /* FEAT_CINDENT */
1601
1602 } /* for (;;) */
1603 /* NOTREACHED */
1604}
1605
1606/*
1607 * Redraw for Insert mode.
1608 * This is postponed until getting the next character to make '$' in the 'cpo'
1609 * option work correctly.
1610 * Only redraw when there are no characters available. This speeds up
1611 * inserting sequences of characters (e.g., for CTRL-R).
1612 */
1613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614ins_redraw(
1615 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617#ifdef FEAT_CONCEAL
1618 linenr_T conceal_old_cursor_line = 0;
1619 linenr_T conceal_new_cursor_line = 0;
1620 int conceal_update_lines = FALSE;
1621#endif
1622
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001623 if (char_avail())
1624 return;
1625
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001626#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1628 * visible, the command might delete it. */
1629 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001630# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001631 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001632# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001633# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001634 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001635# endif
1636# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001637 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001638# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001639 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001640# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001641 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001642# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001643# ifdef FEAT_INS_EXPAND
1644 && !pum_visible()
1645# endif
1646 )
1647 {
1648# ifdef FEAT_SYN_HL
1649 /* Need to update the screen first, to make sure syntax
1650 * highlighting is correct after making a change (e.g., inserting
1651 * a "(". The autocommand may also require a redraw, so it's done
1652 * again below, unfortunately. */
1653 if (syntax_present(curwin) && must_redraw)
1654 update_screen(0);
1655# endif
1656# ifdef FEAT_AUTOCMD
1657 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001658 {
1659 /* Make sure curswant is correct, an autocommand may call
1660 * getcurpos(). */
1661 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001662 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001663 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664# endif
1665# ifdef FEAT_CONCEAL
1666 if (curwin->w_p_cole > 0)
1667 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001668# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 conceal_new_cursor_line = curwin->w_cursor.lnum;
1672 conceal_update_lines = TRUE;
1673 }
1674# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001675# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001676 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001677# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001678 }
1679#endif
1680
1681#ifdef FEAT_AUTOCMD
1682 /* Trigger TextChangedI if b_changedtick differs. */
1683 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001684 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001685# ifdef FEAT_INS_EXPAND
1686 && !pum_visible()
1687# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001688 )
1689 {
1690 if (last_changedtick_buf == curbuf)
1691 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1692 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001693 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001695#endif
1696
1697 if (must_redraw)
1698 update_screen(0);
1699 else if (clear_cmdline || redraw_cmdline)
1700 showmode(); /* clear cmdline and show mode */
1701# if defined(FEAT_CONCEAL)
1702 if ((conceal_update_lines
1703 && (conceal_old_cursor_line != conceal_new_cursor_line
1704 || conceal_cursor_line(curwin)))
1705 || need_cursor_line_redraw)
1706 {
1707 if (conceal_old_cursor_line != conceal_new_cursor_line)
1708 update_single_line(curwin, conceal_old_cursor_line);
1709 update_single_line(curwin, conceal_new_cursor_line == 0
1710 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1711 curwin->w_valid &= ~VALID_CROW;
1712 }
1713# endif
1714 showruler(FALSE);
1715 setcursor();
1716 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717}
1718
1719/*
1720 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1721 */
1722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001723ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001726 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001729 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001734 did_putchar = TRUE;
1735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1737
1738#ifdef FEAT_CMDL_INFO
1739 add_to_showcmd_c(Ctrl_V);
1740#endif
1741
1742 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001743 if (did_putchar)
1744 /* when the line fits in 'columns' the '^' is at the start of the next
1745 * line and will not removed by the redraw */
1746 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#ifdef FEAT_CMDL_INFO
1748 clear_showcmd();
1749#endif
1750 insert_special(c, FALSE, TRUE);
1751#ifdef FEAT_RIGHTLEFT
1752 revins_chars++;
1753 revins_legal++;
1754#endif
1755}
1756
1757/*
1758 * Put a character directly onto the screen. It's not stored in a buffer.
1759 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1760 */
1761static int pc_status;
1762#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1763#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1764#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1765#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767static int pc_attr;
1768static int pc_row;
1769static int pc_col;
1770
1771 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 int attr;
1775
1776 if (ScreenLines != NULL)
1777 {
1778 update_topline(); /* just in case w_topline isn't valid */
1779 validate_cursor();
1780 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001781 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 else
1783 attr = 0;
1784 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001785 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1787 pc_status = PC_STATUS_UNSET;
1788#endif
1789#ifdef FEAT_RIGHTLEFT
1790 if (curwin->w_p_rl)
1791 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001792 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793# ifdef FEAT_MBYTE
1794 if (has_mbyte)
1795 {
1796 int fix_col = mb_fix_col(pc_col, pc_row);
1797
1798 if (fix_col != pc_col)
1799 {
1800 screen_putchar(' ', pc_row, fix_col, attr);
1801 --curwin->w_wcol;
1802 pc_status = PC_STATUS_RIGHT;
1803 }
1804 }
1805# endif
1806 }
1807 else
1808#endif
1809 {
1810 pc_col += curwin->w_wcol;
1811#ifdef FEAT_MBYTE
1812 if (mb_lefthalve(pc_row, pc_col))
1813 pc_status = PC_STATUS_LEFT;
1814#endif
1815 }
1816
1817 /* save the character to be able to put it back */
1818#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1819 if (pc_status == PC_STATUS_UNSET)
1820#endif
1821 {
1822 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1823 pc_status = PC_STATUS_SET;
1824 }
1825 screen_putchar(c, pc_row, pc_col, attr);
1826 }
1827}
1828
1829/*
1830 * Undo the previous edit_putchar().
1831 */
1832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1836 {
1837#if defined(FEAT_MBYTE)
1838 if (pc_status == PC_STATUS_RIGHT)
1839 ++curwin->w_wcol;
1840 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1841 redrawWinline(curwin->w_cursor.lnum, FALSE);
1842 else
1843#endif
1844 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1845 }
1846}
1847
1848/*
1849 * Called when p_dollar is set: display a '$' at the end of the changed text
1850 * Only works when cursor is in the line that changes.
1851 */
1852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854{
1855 colnr_T save_col;
1856
1857 if (!redrawing())
1858 return;
1859
1860 cursor_off();
1861 save_col = curwin->w_cursor.col;
1862 curwin->w_cursor.col = col;
1863#ifdef FEAT_MBYTE
1864 if (has_mbyte)
1865 {
1866 char_u *p;
1867
1868 /* If on the last byte of a multi-byte move to the first byte. */
1869 p = ml_get_curline();
1870 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1871 }
1872#endif
1873 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001874 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 {
1876 edit_putchar('$', FALSE);
1877 dollar_vcol = curwin->w_virtcol;
1878 }
1879 curwin->w_cursor.col = save_col;
1880}
1881
1882/*
1883 * Call this function before moving the cursor from the normal insert position
1884 * in insert mode.
1885 */
1886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001889 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001891 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 redrawWinline(curwin->w_cursor.lnum, FALSE);
1893 }
1894}
1895
1896/*
1897 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1898 * Keep the cursor on the same character.
1899 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1900 * type == INDENT_DEC decrease indent (for CTRL-D)
1901 * type == INDENT_SET set indent to "amount"
1902 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1903 */
1904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905change_indent(
1906 int type,
1907 int amount,
1908 int round,
1909 int replaced, /* replaced character, put on replace stack */
1910 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 int vcol;
1913 int last_vcol;
1914 int insstart_less; /* reduction for Insstart.col */
1915 int new_cursor_col;
1916 int i;
1917 char_u *ptr;
1918 int save_p_list;
1919 int start_col;
1920 colnr_T vc;
1921#ifdef FEAT_VREPLACE
1922 colnr_T orig_col = 0; /* init for GCC */
1923 char_u *new_line, *orig_line = NULL; /* init for GCC */
1924
1925 /* VREPLACE mode needs to know what the line was like before changing */
1926 if (State & VREPLACE_FLAG)
1927 {
1928 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1929 orig_col = curwin->w_cursor.col;
1930 }
1931#endif
1932
1933 /* for the following tricks we don't want list mode */
1934 save_p_list = curwin->w_p_list;
1935 curwin->w_p_list = FALSE;
1936 vc = getvcol_nolist(&curwin->w_cursor);
1937 vcol = vc;
1938
1939 /*
1940 * For Replace mode we need to fix the replace stack later, which is only
1941 * possible when the cursor is in the indent. Remember the number of
1942 * characters before the cursor if it's possible.
1943 */
1944 start_col = curwin->w_cursor.col;
1945
1946 /* determine offset from first non-blank */
1947 new_cursor_col = curwin->w_cursor.col;
1948 beginline(BL_WHITE);
1949 new_cursor_col -= curwin->w_cursor.col;
1950
1951 insstart_less = curwin->w_cursor.col;
1952
1953 /*
1954 * If the cursor is in the indent, compute how many screen columns the
1955 * cursor is to the left of the first non-blank.
1956 */
1957 if (new_cursor_col < 0)
1958 vcol = get_indent() - vcol;
1959
1960 if (new_cursor_col > 0) /* can't fix replace stack */
1961 start_col = -1;
1962
1963 /*
1964 * Set the new indent. The cursor will be put on the first non-blank.
1965 */
1966 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001967 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
1969 {
1970#ifdef FEAT_VREPLACE
1971 int save_State = State;
1972
1973 /* Avoid being called recursively. */
1974 if (State & VREPLACE_FLAG)
1975 State = INSERT;
1976#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001977 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef FEAT_VREPLACE
1979 State = save_State;
1980#endif
1981 }
1982 insstart_less -= curwin->w_cursor.col;
1983
1984 /*
1985 * Try to put cursor on same character.
1986 * If the cursor is at or after the first non-blank in the line,
1987 * compute the cursor column relative to the column of the first
1988 * non-blank character.
1989 * If we are not in insert mode, leave the cursor on the first non-blank.
1990 * If the cursor is before the first non-blank, position it relative
1991 * to the first non-blank, counted in screen columns.
1992 */
1993 if (new_cursor_col >= 0)
1994 {
1995 /*
1996 * When changing the indent while the cursor is touching it, reset
1997 * Insstart_col to 0.
1998 */
1999 if (new_cursor_col == 0)
2000 insstart_less = MAXCOL;
2001 new_cursor_col += curwin->w_cursor.col;
2002 }
2003 else if (!(State & INSERT))
2004 new_cursor_col = curwin->w_cursor.col;
2005 else
2006 {
2007 /*
2008 * Compute the screen column where the cursor should be.
2009 */
2010 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002011 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 /*
2014 * Advance the cursor until we reach the right screen column.
2015 */
2016 vcol = last_vcol = 0;
2017 new_cursor_col = -1;
2018 ptr = ml_get_curline();
2019 while (vcol <= (int)curwin->w_virtcol)
2020 {
2021 last_vcol = vcol;
2022#ifdef FEAT_MBYTE
2023 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002024 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 else
2026#endif
2027 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002028 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030 vcol = last_vcol;
2031
2032 /*
2033 * May need to insert spaces to be able to position the cursor on
2034 * the right screen column.
2035 */
2036 if (vcol != (int)curwin->w_virtcol)
2037 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002038 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002040 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (ptr != NULL)
2042 {
2043 new_cursor_col += i;
2044 ptr[i] = NUL;
2045 while (--i >= 0)
2046 ptr[i] = ' ';
2047 ins_str(ptr);
2048 vim_free(ptr);
2049 }
2050 }
2051
2052 /*
2053 * When changing the indent while the cursor is in it, reset
2054 * Insstart_col to 0.
2055 */
2056 insstart_less = MAXCOL;
2057 }
2058
2059 curwin->w_p_list = save_p_list;
2060
2061 if (new_cursor_col <= 0)
2062 curwin->w_cursor.col = 0;
2063 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002064 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 curwin->w_set_curswant = TRUE;
2066 changed_cline_bef_curs();
2067
2068 /*
2069 * May have to adjust the start of the insert.
2070 */
2071 if (State & INSERT)
2072 {
2073 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2074 {
2075 if ((int)Insstart.col <= insstart_less)
2076 Insstart.col = 0;
2077 else
2078 Insstart.col -= insstart_less;
2079 }
2080 if ((int)ai_col <= insstart_less)
2081 ai_col = 0;
2082 else
2083 ai_col -= insstart_less;
2084 }
2085
2086 /*
2087 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2088 * If the number of characters before the cursor decreased, need to pop a
2089 * few characters from the replace stack.
2090 * If the number of characters before the cursor increased, need to push a
2091 * few NULs onto the replace stack.
2092 */
2093 if (REPLACE_NORMAL(State) && start_col >= 0)
2094 {
2095 while (start_col > (int)curwin->w_cursor.col)
2096 {
2097 replace_join(0); /* remove a NUL from the replace stack */
2098 --start_col;
2099 }
2100 while (start_col < (int)curwin->w_cursor.col || replaced)
2101 {
2102 replace_push(NUL);
2103 if (replaced)
2104 {
2105 replace_push(replaced);
2106 replaced = NUL;
2107 }
2108 ++start_col;
2109 }
2110 }
2111
2112#ifdef FEAT_VREPLACE
2113 /*
2114 * For VREPLACE mode, we also have to fix the replace stack. In this case
2115 * it is always possible because we backspace over the whole line and then
2116 * put it back again the way we wanted it.
2117 */
2118 if (State & VREPLACE_FLAG)
2119 {
2120 /* If orig_line didn't allocate, just return. At least we did the job,
2121 * even if you can't backspace. */
2122 if (orig_line == NULL)
2123 return;
2124
2125 /* Save new line */
2126 new_line = vim_strsave(ml_get_curline());
2127 if (new_line == NULL)
2128 return;
2129
2130 /* We only put back the new line up to the cursor */
2131 new_line[curwin->w_cursor.col] = NUL;
2132
2133 /* Put back original line */
2134 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2135 curwin->w_cursor.col = orig_col;
2136
2137 /* Backspace from cursor to start of line */
2138 backspace_until_column(0);
2139
2140 /* Insert new stuff into line again */
2141 ins_bytes(new_line);
2142
2143 vim_free(new_line);
2144 }
2145#endif
2146}
2147
2148/*
2149 * Truncate the space at the end of a line. This is to be used only in an
2150 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2151 * modes.
2152 */
2153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002154truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
2156 int i;
2157
2158 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002159 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 if (State & REPLACE_FLAG)
2162 replace_join(0); /* remove a NUL from the replace stack */
2163 }
2164 line[i + 1] = NUL;
2165}
2166
2167#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2168 || defined(FEAT_COMMENTS) || defined(PROTO)
2169/*
2170 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2171 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002172 * Will attempt not to go before "col" even when there is a composing
2173 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 */
2175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 while ((int)curwin->w_cursor.col > col)
2179 {
2180 curwin->w_cursor.col--;
2181 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002182 replace_do_bs(col);
2183 else if (!del_char_after_col(col))
2184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186}
2187#endif
2188
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002189/*
2190 * Like del_char(), but make sure not to go before column "limit_col".
2191 * Only matters when there are composing characters.
2192 * Return TRUE when something was deleted.
2193 */
2194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002196{
2197#ifdef FEAT_MBYTE
2198 if (enc_utf8 && limit_col >= 0)
2199 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002200 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002201
2202 /* Make sure the cursor is at the start of a character, but
2203 * skip forward again when going too far back because of a
2204 * composing character. */
2205 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002206 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002207 {
2208 int l = utf_ptr2len(ml_get_cursor());
2209
2210 if (l == 0) /* end of line */
2211 break;
2212 curwin->w_cursor.col += l;
2213 }
2214 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2215 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002216 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002217 }
2218 else
2219#endif
2220 (void)del_char(FALSE);
2221 return TRUE;
2222}
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2225/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002226 * CTRL-X pressed in Insert mode.
2227 */
2228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002229ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002230{
2231 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2232 * CTRL-V works like CTRL-N */
2233 if (ctrl_x_mode != CTRL_X_CMDLINE)
2234 {
2235 /* if the next ^X<> won't ADD nothing, then reset
2236 * compl_cont_status */
2237 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002238 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002239 else
2240 compl_cont_status = 0;
2241 /* We're not sure which CTRL-X mode it will be yet */
2242 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2243 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2244 edit_submode_pre = NULL;
2245 showmode();
2246 }
2247}
2248
2249/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002250 * Whether other than default completion has been selected.
2251 */
2252 int
2253ctrl_x_mode_not_default(void)
2254{
2255 return ctrl_x_mode != CTRL_X_NORMAL;
2256}
2257
2258/*
2259 * Whether CTRL-X was typed without a following character.
2260 */
2261 int
2262ctrl_x_mode_not_defined_yet(void)
2263{
2264 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2265}
2266
2267/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002268 * Return TRUE if the 'dict' or 'tsr' option can be used.
2269 */
2270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002272{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002273 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002274# ifdef FEAT_SPELL
2275 && !curwin->w_p_spell
2276# endif
2277 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002278 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2279 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002280 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002281 edit_submode = NULL;
2282 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2283 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002284 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002285 if (emsg_silent == 0)
2286 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002287 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 setcursor();
2289 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002290#ifdef FEAT_EVAL
2291 if (!get_vim_var_nr(VV_TESTING))
2292#endif
2293 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002294 }
2295 return FALSE;
2296 }
2297 return TRUE;
2298}
2299
2300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2302 * This depends on the current mode.
2303 */
2304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002305vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 /* Always allow ^R - let it's results then be checked */
2308 if (c == Ctrl_R)
2309 return TRUE;
2310
Bram Moolenaare3226be2005-12-18 22:10:00 +00002311 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002313 return TRUE;
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 switch (ctrl_x_mode)
2316 {
2317 case 0: /* Not in any CTRL-X mode */
2318 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2319 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002320 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2322 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2323 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002324 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002325 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 case CTRL_X_SCROLL:
2327 return (c == Ctrl_Y || c == Ctrl_E);
2328 case CTRL_X_WHOLE_LINE:
2329 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2330 case CTRL_X_FILES:
2331 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2332 case CTRL_X_DICTIONARY:
2333 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2334 case CTRL_X_THESAURUS:
2335 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2336 case CTRL_X_TAGS:
2337 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2338#ifdef FEAT_FIND_ID
2339 case CTRL_X_PATH_PATTERNS:
2340 return (c == Ctrl_P || c == Ctrl_N);
2341 case CTRL_X_PATH_DEFINES:
2342 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2343#endif
2344 case CTRL_X_CMDLINE:
2345 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2346 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002347#ifdef FEAT_COMPL_FUNC
2348 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002349 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002350 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002351 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002352#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002353 case CTRL_X_SPELL:
2354 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002355 case CTRL_X_EVAL:
2356 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002358 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return FALSE;
2360}
2361
2362/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002363 * Return TRUE when character "c" is part of the item currently being
2364 * completed. Used to decide whether to abandon complete mode when the menu
2365 * is visible.
2366 */
2367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002368ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002369{
2370 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2371 /* When expanding an identifier only accept identifier chars. */
2372 return vim_isIDc(c);
2373
2374 switch (ctrl_x_mode)
2375 {
2376 case CTRL_X_FILES:
2377 /* When expanding file name only accept file name chars. But not
2378 * path separators, so that "proto/<Tab>" expands files in
2379 * "proto", not "proto/" as a whole */
2380 return vim_isfilec(c) && !vim_ispathsep(c);
2381
2382 case CTRL_X_CMDLINE:
2383 case CTRL_X_OMNI:
2384 /* Command line and Omni completion can work with just about any
2385 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002386 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002387
2388 case CTRL_X_WHOLE_LINE:
2389 /* For while line completion a space can be part of the line. */
2390 return vim_isprintc(c);
2391 }
2392 return vim_iswordc(c);
2393}
2394
2395/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002396 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002398 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * the rest of the word to be in -- webb
2400 */
2401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002402ins_compl_add_infercase(
2403 char_u *str,
2404 int len,
2405 int icase,
2406 char_u *fname,
2407 int dir,
2408 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 char_u *p;
2411 int i, c;
2412 int actual_len; /* Take multi-byte characters */
2413 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002414 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002415 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 int has_lower = FALSE;
2417 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002419 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002421 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaara2993e12007-08-12 14:38:46 +00002423 /* Find actual length of completion. */
2424#ifdef FEAT_MBYTE
2425 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002427 p = str;
2428 actual_len = 0;
2429 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002431 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002432 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002435 else
2436#endif
2437 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaara2993e12007-08-12 14:38:46 +00002439 /* Find actual length of original text. */
2440#ifdef FEAT_MBYTE
2441 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002443 p = compl_orig_text;
2444 actual_compl_length = 0;
2445 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002447 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002448 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002451 else
2452#endif
2453 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002455 /* "actual_len" may be smaller than "actual_compl_length" when using
2456 * thesaurus, only use the minimum when comparing. */
2457 min_len = actual_len < actual_compl_length
2458 ? actual_len : actual_compl_length;
2459
Bram Moolenaara2993e12007-08-12 14:38:46 +00002460 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002461 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002462 if (wca != NULL)
2463 {
2464 p = str;
2465 for (i = 0; i < actual_len; ++i)
2466#ifdef FEAT_MBYTE
2467 if (has_mbyte)
2468 wca[i] = mb_ptr2char_adv(&p);
2469 else
2470#endif
2471 wca[i] = *(p++);
2472
2473 /* Rule 1: Were any chars converted to lower? */
2474 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002475 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002476 {
2477#ifdef FEAT_MBYTE
2478 if (has_mbyte)
2479 c = mb_ptr2char_adv(&p);
2480 else
2481#endif
2482 c = *(p++);
2483 if (MB_ISLOWER(c))
2484 {
2485 has_lower = TRUE;
2486 if (MB_ISUPPER(wca[i]))
2487 {
2488 /* Rule 1 is satisfied. */
2489 for (i = actual_compl_length; i < actual_len; ++i)
2490 wca[i] = MB_TOLOWER(wca[i]);
2491 break;
2492 }
2493 }
2494 }
2495
2496 /*
2497 * Rule 2: No lower case, 2nd consecutive letter converted to
2498 * upper case.
2499 */
2500 if (!has_lower)
2501 {
2502 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002503 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002504 {
2505#ifdef FEAT_MBYTE
2506 if (has_mbyte)
2507 c = mb_ptr2char_adv(&p);
2508 else
2509#endif
2510 c = *(p++);
2511 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2512 {
2513 /* Rule 2 is satisfied. */
2514 for (i = actual_compl_length; i < actual_len; ++i)
2515 wca[i] = MB_TOUPPER(wca[i]);
2516 break;
2517 }
2518 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2519 }
2520 }
2521
2522 /* Copy the original case of the part we typed. */
2523 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002524 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002525 {
2526#ifdef FEAT_MBYTE
2527 if (has_mbyte)
2528 c = mb_ptr2char_adv(&p);
2529 else
2530#endif
2531 c = *(p++);
2532 if (MB_ISLOWER(c))
2533 wca[i] = MB_TOLOWER(wca[i]);
2534 else if (MB_ISUPPER(c))
2535 wca[i] = MB_TOUPPER(wca[i]);
2536 }
2537
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002538 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002539 * Generate encoding specific output from wide character array.
2540 * Multi-byte characters can occupy up to five bytes more than
2541 * ASCII characters, and we also need one byte for NUL, so stay
2542 * six bytes away from the edge of IObuff.
2543 */
2544 p = IObuff;
2545 i = 0;
2546 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2547#ifdef FEAT_MBYTE
2548 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002549 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002550 else
2551#endif
2552 *(p++) = wca[i++];
2553 *p = NUL;
2554
2555 vim_free(wca);
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002558 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2559 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002561 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
2564/*
2565 * Add a match to the list of matches.
2566 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002567 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002568 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002571ins_compl_add(
2572 char_u *str,
2573 int len,
2574 int icase,
2575 char_u *fname,
2576 char_u **cptext, /* extra text for popup menu or NULL */
2577 int cdir,
2578 int flags,
2579 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002582 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 ui_breakcheck();
2585 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (len < 0)
2588 len = (int)STRLEN(str);
2589
2590 /*
2591 * If the same match is already present, don't add it.
2592 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002595 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 do
2597 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002598 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002599 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 && match->cp_str[len] == NUL)
2601 return NOTDONE;
2602 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002603 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002606 /* Remove any popup menu before changing the list of matches. */
2607 ins_compl_del_pum();
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 /*
2610 * Allocate a new match structure.
2611 * Copy the values to the new match structure.
2612 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002613 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002615 return FAIL;
2616 match->cp_number = -1;
2617 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002618 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002619 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002624 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2629 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002630 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002631 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002632 && compl_curr_match->cp_fname != NULL
2633 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002635 else if (fname != NULL)
2636 {
2637 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002638 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002641 match->cp_fname = NULL;
2642 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002643
2644 if (cptext != NULL)
2645 {
2646 int i;
2647
2648 for (i = 0; i < CPT_COUNT; ++i)
2649 if (cptext[i] != NULL && *cptext[i] != NUL)
2650 match->cp_text[i] = vim_strsave(cptext[i]);
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /*
2654 * Link the new match structure in the list of matches.
2655 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002656 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002657 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else if (dir == FORWARD)
2659 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002660 match->cp_next = compl_curr_match->cp_next;
2661 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 else /* BACKWARD */
2664 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002665 match->cp_next = compl_curr_match;
2666 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002668 if (match->cp_next)
2669 match->cp_next->cp_prev = match;
2670 if (match->cp_prev)
2671 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002673 compl_first_match = match;
2674 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002676 /*
2677 * Find the longest common string if still doing that.
2678 */
2679 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2680 ins_compl_longest_match(match);
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return OK;
2683}
2684
2685/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002686 * Return TRUE if "str[len]" matches with match->cp_str, considering
2687 * match->cp_icase.
2688 */
2689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002690ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002691{
2692 if (match->cp_icase)
2693 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2694 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2695}
2696
2697/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002698 * Reduce the longest common string for match "match".
2699 */
2700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002701ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002702{
2703 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002704 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002705 int had_match;
2706
2707 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002708 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002709 /* First match, use it as a whole. */
2710 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002711 if (compl_leader != NULL)
2712 {
2713 had_match = (curwin->w_cursor.col > compl_col);
2714 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002715 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002716 ins_redraw(FALSE);
2717
2718 /* When the match isn't there (to avoid matching itself) remove it
2719 * again after redrawing. */
2720 if (!had_match)
2721 ins_compl_delete();
2722 compl_used_match = FALSE;
2723 }
2724 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002725 else
2726 {
2727 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002728 p = compl_leader;
2729 s = match->cp_str;
2730 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002731 {
2732#ifdef FEAT_MBYTE
2733 if (has_mbyte)
2734 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002735 c1 = mb_ptr2char(p);
2736 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002737 }
2738 else
2739#endif
2740 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002741 c1 = *p;
2742 c2 = *s;
2743 }
2744 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2745 : (c1 != c2))
2746 break;
2747#ifdef FEAT_MBYTE
2748 if (has_mbyte)
2749 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002750 MB_PTR_ADV(p);
2751 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002752 }
2753 else
2754#endif
2755 {
2756 ++p;
2757 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002758 }
2759 }
2760
2761 if (*p != NUL)
2762 {
2763 /* Leader was shortened, need to change the inserted text. */
2764 *p = NUL;
2765 had_match = (curwin->w_cursor.col > compl_col);
2766 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002767 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002768 ins_redraw(FALSE);
2769
2770 /* When the match isn't there (to avoid matching itself) remove it
2771 * again after redrawing. */
2772 if (!had_match)
2773 ins_compl_delete();
2774 }
2775
2776 compl_used_match = FALSE;
2777 }
2778}
2779
2780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 * Add an array of matches to the list of matches.
2782 * Frees matches[].
2783 */
2784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002785ins_compl_add_matches(
2786 int num_matches,
2787 char_u **matches,
2788 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 int i;
2791 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002792 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar572cb562005-08-05 21:35:02 +00002794 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002796 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002798 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 FreeWild(num_matches, matches);
2800}
2801
2802/* Make the completion list cyclic.
2803 * Return the number of matches (excluding the original).
2804 */
2805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002806ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002808 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 int count = 0;
2810
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002811 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
2813 /*
2814 * Find the end of the list.
2815 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002816 match = compl_first_match;
2817 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002818 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002820 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ++count;
2822 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002823 match->cp_next = compl_first_match;
2824 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826 return count;
2827}
2828
Bram Moolenaara94bc432006-03-10 21:42:59 +00002829/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002830 * Set variables that store noselect and noinsert behavior from the
2831 * 'completeopt' value.
2832 */
2833 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002834completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002835{
2836 compl_no_insert = FALSE;
2837 compl_no_select = FALSE;
2838 if (strstr((char *)p_cot, "noselect") != NULL)
2839 compl_no_select = TRUE;
2840 if (strstr((char *)p_cot, "noinsert") != NULL)
2841 compl_no_insert = TRUE;
2842}
2843
2844/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002845 * Start completion for the complete() function.
2846 * "startcol" is where the matched text starts (1 is first column).
2847 * "list" is the list of matches.
2848 */
2849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002850set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002851{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002852 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002853 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002854
Bram Moolenaara94bc432006-03-10 21:42:59 +00002855 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002856 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002857 ins_compl_prep(' ');
2858 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002859 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002860
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002861 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002862 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002863 startcol = curwin->w_cursor.col;
2864 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002865 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002866 /* compl_pattern doesn't need to be set */
2867 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2868 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002869 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002870 return;
2871
Bram Moolenaare4214502015-03-05 18:08:43 +01002872 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002873
2874 ins_compl_add_list(list);
2875 compl_matches = ins_compl_make_cyclic();
2876 compl_started = TRUE;
2877 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002878 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002879
2880 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002881 if (compl_no_insert || compl_no_select)
2882 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002883 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002884 if (compl_no_select)
2885 /* Down/Up has no real effect. */
2886 ins_complete(K_UP, FALSE);
2887 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002888 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002889 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002890 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002891
2892 /* Lazily show the popup menu, unless we got interrupted. */
2893 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002894 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002895 out_flush();
2896}
2897
2898
Bram Moolenaar9372a112005-12-06 19:59:18 +00002899/* "compl_match_array" points the currently displayed list of entries in the
2900 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002902static int compl_match_arraysize;
2903
2904/*
2905 * Update the screen and when there is any scrolling remove the popup menu.
2906 */
2907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002908ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002909{
2910 int h;
2911
2912 if (compl_match_array != NULL)
2913 {
2914 h = curwin->w_cline_height;
2915 update_screen(0);
2916 if (h != curwin->w_cline_height)
2917 ins_compl_del_pum();
2918 }
2919}
2920
2921/*
2922 * Remove any popup menu.
2923 */
2924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002926{
2927 if (compl_match_array != NULL)
2928 {
2929 pum_undisplay();
2930 vim_free(compl_match_array);
2931 compl_match_array = NULL;
2932 }
2933}
2934
2935/*
2936 * Return TRUE if the popup menu should be displayed.
2937 */
2938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002941 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002942 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002943 return FALSE;
2944
2945 /* The display looks bad on a B&W display. */
2946 if (t_colors < 8
2947#ifdef FEAT_GUI
2948 && !gui.in_use
2949#endif
2950 )
2951 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002952 return TRUE;
2953}
2954
2955/*
2956 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002957 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002958 */
2959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002960pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002961{
2962 compl_T *compl;
2963 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964
2965 /* Don't display the popup menu if there are no matches or there is only
2966 * one (ignoring the original text). */
2967 compl = compl_first_match;
2968 i = 0;
2969 do
2970 {
2971 if (compl == NULL
2972 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2973 break;
2974 compl = compl->cp_next;
2975 } while (compl != compl_first_match);
2976
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002977 if (strstr((char *)p_cot, "menuone") != NULL)
2978 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002979 return (i >= 2);
2980}
2981
2982/*
2983 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002984 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002987ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002988{
2989 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002990 compl_T *shown_compl = NULL;
2991 int did_find_shown_match = FALSE;
2992 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 int i;
2994 int cur = -1;
2995 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002996 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002997
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002998 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999 return;
3000
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003001#if defined(FEAT_EVAL)
3002 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3003 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3004#endif
3005
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 /* Update the screen before drawing the popup menu over it. */
3007 update_screen(0);
3008
3009 if (compl_match_array == NULL)
3010 {
3011 /* Need to build the popup menu list. */
3012 compl_match_arraysize = 0;
3013 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003014 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003015 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003016 do
3017 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003018 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3019 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003020 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003021 ++compl_match_arraysize;
3022 compl = compl->cp_next;
3023 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003024 if (compl_match_arraysize == 0)
3025 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003026 compl_match_array = (pumitem_T *)alloc_clear(
3027 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003028 * compl_match_arraysize));
3029 if (compl_match_array != NULL)
3030 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003031 /* If the current match is the original text don't find the first
3032 * match after it, don't highlight anything. */
3033 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3034 shown_match_ok = TRUE;
3035
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003036 i = 0;
3037 compl = compl_first_match;
3038 do
3039 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003040 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3041 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003042 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003043 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003044 if (!shown_match_ok)
3045 {
3046 if (compl == compl_shown_match || did_find_shown_match)
3047 {
3048 /* This item is the shown match or this is the
3049 * first displayed item after the shown match. */
3050 compl_shown_match = compl;
3051 did_find_shown_match = TRUE;
3052 shown_match_ok = TRUE;
3053 }
3054 else
3055 /* Remember this displayed match for when the
3056 * shown match is just below it. */
3057 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003058 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003059 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003060
3061 if (compl->cp_text[CPT_ABBR] != NULL)
3062 compl_match_array[i].pum_text =
3063 compl->cp_text[CPT_ABBR];
3064 else
3065 compl_match_array[i].pum_text = compl->cp_str;
3066 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3067 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3068 if (compl->cp_text[CPT_MENU] != NULL)
3069 compl_match_array[i++].pum_extra =
3070 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003071 else
3072 compl_match_array[i++].pum_extra = compl->cp_fname;
3073 }
3074
3075 if (compl == compl_shown_match)
3076 {
3077 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003078
3079 /* When the original text is the shown match don't set
3080 * compl_shown_match. */
3081 if (compl->cp_flags & ORIGINAL_TEXT)
3082 shown_match_ok = TRUE;
3083
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003084 if (!shown_match_ok && shown_compl != NULL)
3085 {
3086 /* The shown match isn't displayed, set it to the
3087 * previously displayed match. */
3088 compl_shown_match = shown_compl;
3089 shown_match_ok = TRUE;
3090 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 }
3092 compl = compl->cp_next;
3093 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003094
3095 if (!shown_match_ok) /* no displayed match at all */
3096 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003097 }
3098 }
3099 else
3100 {
3101 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003102 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003103 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3104 || compl_match_array[i].pum_text
3105 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003106 {
3107 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003108 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003109 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003110 }
3111
3112 if (compl_match_array != NULL)
3113 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003114 /* In Replace mode when a $ is displayed at the end of the line only
3115 * part of the screen would be updated. We do need to redraw here. */
3116 dollar_vcol = -1;
3117
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003118 /* Compute the screen column of the start of the completed text.
3119 * Use the cursor to get all wrapping and other settings right. */
3120 col = curwin->w_cursor.col;
3121 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003122 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003123 curwin->w_cursor.col = col;
3124 }
3125}
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#define DICT_FIRST (1) /* use just first element in "dict" */
3128#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 * Add any identifiers that match the given pattern in the list of dictionary
3132 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 */
3134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003135ins_compl_dictionaries(
3136 char_u *dict_start,
3137 char_u *pat,
3138 int flags, /* DICT_FIRST and/or DICT_EXACT */
3139 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *ptr;
3143 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 char_u **files;
3146 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003148 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
Bram Moolenaar0b238792006-03-02 22:49:12 +00003150 if (*dict == NUL)
3151 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003152#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003153 /* When 'dictionary' is empty and spell checking is enabled use
3154 * "spell". */
3155 if (!thesaurus && curwin->w_p_spell)
3156 dict = (char_u *)"spell";
3157 else
3158#endif
3159 return;
3160 }
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163 if (buf == NULL)
3164 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003165 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 /* If 'infercase' is set, don't use 'smartcase' here */
3168 save_p_scs = p_scs;
3169 if (curbuf->b_p_inf)
3170 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003171
3172 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3173 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003174 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003175 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003176 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003177 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003178 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003179
3180 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003181 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003182 len = STRLEN(pat_esc) + 10;
3183 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003185 {
3186 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003187 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003188 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003189 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003190 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3191 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003192 vim_free(ptr);
3193 }
3194 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003195 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003196 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 if (regmatch.regprog == NULL)
3198 goto theend;
3199 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3202 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003203 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 /* copy one dictionary file name into buf */
3206 if (flags == DICT_EXACT)
3207 {
3208 count = 1;
3209 files = &dict;
3210 }
3211 else
3212 {
3213 /* Expand wildcards in the dictionary name, but do not allow
3214 * backticks (for security, the 'dict' option may have been set in
3215 * a modeline). */
3216 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003217# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 if (!thesaurus && STRCMP(buf, "spell") == 0)
3219 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003220 else
3221# endif
3222 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 || expand_wildcards(1, &buf, &count, &files,
3224 EW_FILE|EW_SILENT) != OK)
3225 count = 0;
3226 }
3227
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003228# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003229 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003231 /* Complete from active spelling. Skip "\<" in the pattern, we
3232 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003233 if (pat[0] == '\\' && pat[1] == '<')
3234 ptr = pat + 2;
3235 else
3236 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003237 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003240# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003241 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003242 {
3243 ins_compl_files(count, files, thesaurus, flags,
3244 &regmatch, buf, &dir);
3245 if (flags != DICT_EXACT)
3246 FreeWild(count, files);
3247 }
3248 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 break;
3250 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003251
3252theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003254 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 vim_free(buf);
3256}
3257
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259ins_compl_files(
3260 int count,
3261 char_u **files,
3262 int thesaurus,
3263 int flags,
3264 regmatch_T *regmatch,
3265 char_u *buf,
3266 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003267{
3268 char_u *ptr;
3269 int i;
3270 FILE *fp;
3271 int add_r;
3272
3273 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3274 {
3275 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3276 if (flags != DICT_EXACT)
3277 {
3278 vim_snprintf((char *)IObuff, IOSIZE,
3279 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003280 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003281 }
3282
3283 if (fp != NULL)
3284 {
3285 /*
3286 * Read dictionary file line by line.
3287 * Check each line for a match.
3288 */
3289 while (!got_int && !compl_interrupted
3290 && !vim_fgets(buf, LSIZE, fp))
3291 {
3292 ptr = buf;
3293 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3294 {
3295 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003296 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003297 ptr = find_line_end(ptr);
3298 else
3299 ptr = find_word_end(ptr);
3300 add_r = ins_compl_add_infercase(regmatch->startp[0],
3301 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003302 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 if (thesaurus)
3304 {
3305 char_u *wstart;
3306
3307 /*
3308 * Add the other matches on the line
3309 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003310 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 while (!got_int)
3312 {
3313 /* Find start of the next word. Skip white
3314 * space and punctuation. */
3315 ptr = find_word_start(ptr);
3316 if (*ptr == NUL || *ptr == NL)
3317 break;
3318 wstart = ptr;
3319
Bram Moolenaara2993e12007-08-12 14:38:46 +00003320 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003321#ifdef FEAT_MBYTE
3322 if (has_mbyte)
3323 /* Japanese words may have characters in
3324 * different classes, only separate words
3325 * with single-byte non-word characters. */
3326 while (*ptr != NUL)
3327 {
3328 int l = (*mb_ptr2len)(ptr);
3329
3330 if (l < 2 && !vim_iswordc(*ptr))
3331 break;
3332 ptr += l;
3333 }
3334 else
3335#endif
3336 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003337
3338 /* Add the word. Skip the regexp match. */
3339 if (wstart != regmatch->startp[0])
3340 add_r = ins_compl_add_infercase(wstart,
3341 (int)(ptr - wstart),
3342 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003343 }
3344 }
3345 if (add_r == OK)
3346 /* if dir was BACKWARD then honor it just once */
3347 *dir = FORWARD;
3348 else if (add_r == FAIL)
3349 break;
3350 /* avoid expensive call to vim_regexec() when at end
3351 * of line */
3352 if (*ptr == '\n' || got_int)
3353 break;
3354 }
3355 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003356 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003357 }
3358 fclose(fp);
3359 }
3360 }
3361}
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363/*
3364 * Find the start of the next word.
3365 * Returns a pointer to the first char of the word. Also stops at a NUL.
3366 */
3367 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003368find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370#ifdef FEAT_MBYTE
3371 if (has_mbyte)
3372 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003373 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
3375#endif
3376 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3377 ++ptr;
3378 return ptr;
3379}
3380
3381/*
3382 * Find the end of the word. Assumes it starts inside a word.
3383 * Returns a pointer to just after the word.
3384 */
3385 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003386find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388#ifdef FEAT_MBYTE
3389 int start_class;
3390
3391 if (has_mbyte)
3392 {
3393 start_class = mb_get_class(ptr);
3394 if (start_class > 1)
3395 while (*ptr != NUL)
3396 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003397 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (mb_get_class(ptr) != start_class)
3399 break;
3400 }
3401 }
3402 else
3403#endif
3404 while (vim_iswordc(*ptr))
3405 ++ptr;
3406 return ptr;
3407}
3408
3409/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003410 * Find the end of the line, omitting CR and NL at the end.
3411 * Returns a pointer to just after the line.
3412 */
3413 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415{
3416 char_u *s;
3417
3418 s = ptr + STRLEN(ptr);
3419 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3420 --s;
3421 return s;
3422}
3423
3424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 * Free the list of completions
3426 */
3427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003428ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003430 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003431 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003433 vim_free(compl_pattern);
3434 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003435 vim_free(compl_leader);
3436 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003438 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003440
3441 ins_compl_del_pum();
3442 pum_clear();
3443
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003444 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 do
3446 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003447 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003448 compl_curr_match = compl_curr_match->cp_next;
3449 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003451 if (match->cp_flags & FREE_FNAME)
3452 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003453 for (i = 0; i < CPT_COUNT; ++i)
3454 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003456 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3457 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003458 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003459 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460}
3461
3462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003463ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003465 compl_cont_status = 0;
3466 compl_started = FALSE;
3467 compl_matches = 0;
3468 vim_free(compl_pattern);
3469 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003470 vim_free(compl_leader);
3471 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003473 vim_free(compl_orig_text);
3474 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003475 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003476 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003477 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478}
3479
3480/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003481 * Return TRUE when Insert completion is active.
3482 */
3483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003485{
3486 return compl_started;
3487}
3488
3489/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003490 * Delete one character before the cursor and show the subset of the matches
3491 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003492 * Returns the character to be used, NUL if the work is done and another char
3493 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003494 */
3495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003496ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003497{
3498 char_u *line;
3499 char_u *p;
3500
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003501 line = ml_get_curline();
3502 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003503 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003504
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003505 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003506 * allow the word to be deleted, we won't match everything.
3507 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003508 if ((int)(p - line) - (int)compl_col < 0
3509 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003510 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3511 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3512 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003513 return K_BS;
3514
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003515 /* Deleted more than what was used to find matches or didn't finish
3516 * finding all matches: need to look for matches all over again. */
3517 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003518 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003519 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003520
Bram Moolenaara6557602006-02-04 22:43:20 +00003521 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003523 if (compl_leader != NULL)
3524 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003525 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003526 if (compl_shown_match != NULL)
3527 /* Make sure current match is not a hidden item. */
3528 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529 return NUL;
3530 }
3531 return K_BS;
3532}
Bram Moolenaara6557602006-02-04 22:43:20 +00003533
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003534/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003535 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3536 * be called.
3537 */
3538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003539ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003540{
3541 /* Return TRUE if we didn't complete finding matches or when the
3542 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3543 return compl_was_interrupted
3544 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3545 && compl_opt_refresh_always);
3546}
3547
3548/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003549 * Called after changing "compl_leader".
3550 * Show the popup menu with a different set of matches.
3551 * May also search for matches again if the previous search was interrupted.
3552 */
3553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003555{
3556 ins_compl_del_pum();
3557 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003558 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003559 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003560
3561 if (compl_started)
3562 ins_compl_set_original_text(compl_leader);
3563 else
3564 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003565#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003566 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003567#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003568 /*
3569 * Matches were cleared, need to search for them now. First display
3570 * the changed text before the cursor. Set "compl_restarting" to
3571 * avoid that the first match is inserted.
3572 */
3573 update_screen(0);
3574#ifdef FEAT_GUI
3575 if (gui.in_use)
3576 {
3577 /* Show the cursor after the match, not after the redrawn text. */
3578 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003579 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003580 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003581#endif
3582 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003583 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003584 compl_cont_status = 0;
3585 compl_restarting = FALSE;
3586 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003587
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003588 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003589
3590 /* Show the popup menu with a different set of matches. */
3591 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003592
3593 /* Don't let Enter select the original text when there is no popup menu. */
3594 if (compl_match_array == NULL)
3595 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003596}
3597
3598/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003599 * Return the length of the completion, from the completion start column to
3600 * the cursor column. Making sure it never goes below zero.
3601 */
3602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003603ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003604{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003605 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003606
3607 if (off < 0)
3608 return 0;
3609 return off;
3610}
3611
3612/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003613 * Append one character to the match leader. May reduce the number of
3614 * matches.
3615 */
3616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003618{
3619#ifdef FEAT_MBYTE
3620 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003621#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003622
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003623 if (stop_arrow() == FAIL)
3624 return;
3625#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003626 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3627 {
3628 char_u buf[MB_MAXBYTES + 1];
3629
3630 (*mb_char2bytes)(c, buf);
3631 buf[cc] = NUL;
3632 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003633 if (compl_opt_refresh_always)
3634 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003635 }
3636 else
3637#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003638 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003639 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003640 if (compl_opt_refresh_always)
3641 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003642 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003643
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003644 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003645 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003646 ins_compl_restart();
3647
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003648 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003649 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003650 * break redo. */
3651 if (!compl_opt_refresh_always)
3652 {
3653 vim_free(compl_leader);
3654 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003655 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003656 if (compl_leader != NULL)
3657 ins_compl_new_leader();
3658 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003659}
3660
3661/*
3662 * Setup for finding completions again without leaving CTRL-X mode. Used when
3663 * BS or a key was typed while still searching for matches.
3664 */
3665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003666ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003667{
3668 ins_compl_free();
3669 compl_started = FALSE;
3670 compl_matches = 0;
3671 compl_cont_status = 0;
3672 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003673}
3674
3675/*
3676 * Set the first match, the original text.
3677 */
3678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003679ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003680{
3681 char_u *p;
3682
3683 /* Replace the original text entry. */
3684 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3685 {
3686 p = vim_strsave(str);
3687 if (p != NULL)
3688 {
3689 vim_free(compl_first_match->cp_str);
3690 compl_first_match->cp_str = p;
3691 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003692 }
3693}
3694
3695/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003696 * Append one character to the match leader. May reduce the number of
3697 * matches.
3698 */
3699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003700ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003701{
3702 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003703 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003704 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003705 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003706
3707 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003708 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003709 {
3710 /* When still at the original match use the first entry that matches
3711 * the leader. */
3712 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3713 {
3714 p = NULL;
3715 for (cp = compl_shown_match->cp_next; cp != NULL
3716 && cp != compl_first_match; cp = cp->cp_next)
3717 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003718 if (compl_leader == NULL
3719 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003720 (int)STRLEN(compl_leader)))
3721 {
3722 p = cp->cp_str;
3723 break;
3724 }
3725 }
3726 if (p == NULL || (int)STRLEN(p) <= len)
3727 return;
3728 }
3729 else
3730 return;
3731 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003732 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003733 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003734 ins_compl_addleader(c);
3735}
3736
3737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003739 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003740 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003743ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003747 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 /* Forget any previous 'special' messages if this is actually
3750 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3751 */
3752 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3753 edit_submode_extra = NULL;
3754
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003755 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003756 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3757 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003758 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003760 /* Set "compl_get_longest" when finding the first matches. */
3761 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003762 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003763 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003764 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003765 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003766
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003767 }
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3770 {
3771 /*
3772 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3773 * it will be yet. Now we decide.
3774 */
3775 switch (c)
3776 {
3777 case Ctrl_E:
3778 case Ctrl_Y:
3779 ctrl_x_mode = CTRL_X_SCROLL;
3780 if (!(State & REPLACE_FLAG))
3781 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3782 else
3783 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3784 edit_submode_pre = NULL;
3785 showmode();
3786 break;
3787 case Ctrl_L:
3788 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3789 break;
3790 case Ctrl_F:
3791 ctrl_x_mode = CTRL_X_FILES;
3792 break;
3793 case Ctrl_K:
3794 ctrl_x_mode = CTRL_X_DICTIONARY;
3795 break;
3796 case Ctrl_R:
3797 /* Simply allow ^R to happen without affecting ^X mode */
3798 break;
3799 case Ctrl_T:
3800 ctrl_x_mode = CTRL_X_THESAURUS;
3801 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003802#ifdef FEAT_COMPL_FUNC
3803 case Ctrl_U:
3804 ctrl_x_mode = CTRL_X_FUNCTION;
3805 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003806 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003807 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003809#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003810 case 's':
3811 case Ctrl_S:
3812 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003813#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003814 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003815 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003816 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003817#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case Ctrl_RSB:
3820 ctrl_x_mode = CTRL_X_TAGS;
3821 break;
3822#ifdef FEAT_FIND_ID
3823 case Ctrl_I:
3824 case K_S_TAB:
3825 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3826 break;
3827 case Ctrl_D:
3828 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3829 break;
3830#endif
3831 case Ctrl_V:
3832 case Ctrl_Q:
3833 ctrl_x_mode = CTRL_X_CMDLINE;
3834 break;
3835 case Ctrl_P:
3836 case Ctrl_N:
3837 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3838 * just started ^X mode, or there were enough ^X's to cancel
3839 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3840 * do normal expansion when interrupting a different mode (say
3841 * ^X^F^X^P or ^P^X^X^P, see below)
3842 * nothing changes if interrupting mode 0, (eg, the flag
3843 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 if (!(compl_cont_status & CONT_INTRPT))
3845 compl_cont_status |= CONT_LOCAL;
3846 else if (compl_cont_mode != 0)
3847 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 /* FALLTHROUGH */
3849 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003850 /* If we have typed at least 2 ^X's... for modes != 0, we set
3851 * compl_cont_status = 0 (eg, as if we had just started ^X
3852 * mode).
3853 * For mode 0, we set "compl_cont_mode" to an impossible
3854 * value, in both cases ^X^X can be used to restart the same
3855 * mode (avoiding ADDING mode).
3856 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3857 * 'complete' and local ^P expansions respectively.
3858 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3859 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (c == Ctrl_X)
3861 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003862 if (compl_cont_mode != 0)
3863 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003865 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003867 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 edit_submode = NULL;
3869 showmode();
3870 break;
3871 }
3872 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003873 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 /* We're already in CTRL-X mode, do we stay in it? */
3876 if (!vim_is_ctrl_x_key(c))
3877 {
3878 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003879 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 else
3881 ctrl_x_mode = CTRL_X_FINISHED;
3882 edit_submode = NULL;
3883 }
3884 showmode();
3885 }
3886
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
3889 /* Show error message from attempted keyword completion (probably
3890 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003891 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003893 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3894 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 || ctrl_x_mode == CTRL_X_FINISHED)
3896 {
3897 /* Get here when we have finished typing a sequence of ^N and
3898 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003900 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
3902 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003903 * If any of the original typed text has been changed, eg when
3904 * ignorecase is set, we must add back-spaces to the redo
3905 * buffer. We add as few as necessary to delete just the part
3906 * of the original text that has changed.
3907 * When using the longest match, edited the match or used
3908 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003910 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3911 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003912 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003913 ptr = NULL;
3914 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916
3917#ifdef FEAT_CINDENT
3918 want_cindent = (can_cindent && cindent_on());
3919#endif
3920 /*
3921 * When completing whole lines: fix indent for 'cindent'.
3922 * Otherwise, break line if it's too long.
3923 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
3926#ifdef FEAT_CINDENT
3927 /* re-indent the current line */
3928 if (want_cindent)
3929 {
3930 do_c_expr_indent();
3931 want_cindent = FALSE; /* don't do it again */
3932 }
3933#endif
3934 }
3935 else
3936 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003937 int prev_col = curwin->w_cursor.col;
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003940 if (prev_col > 0)
3941 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003942 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003943 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003945 if (prev_col > 0
3946 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3947 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003950 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003951 * the selection without inserting anything. When
3952 * compl_enter_selects is set the Enter key does the same. */
3953 if ((c == Ctrl_Y || (compl_enter_selects
3954 && (c == CAR || c == K_KENTER || c == NL)))
3955 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003956 retval = TRUE;
3957
Bram Moolenaar47247282016-08-02 22:36:02 +02003958 /* CTRL-E means completion is Ended, go back to the typed text.
3959 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003960 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003961 {
3962 ins_compl_delete();
3963 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003964 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003965 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003966 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003967 retval = TRUE;
3968 }
3969
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003970 auto_format(FALSE, TRUE);
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 compl_started = FALSE;
3974 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003975 if (!shortmess(SHM_COMPLETIONMENU))
3976 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003977 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003978 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (edit_submode != NULL)
3980 {
3981 edit_submode = NULL;
3982 showmode();
3983 }
3984
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003985#ifdef FEAT_CMDWIN
3986 if (c == Ctrl_C && cmdwin_type != 0)
3987 /* Avoid the popup menu remains displayed when leaving the
3988 * command line window. */
3989 update_screen(0);
3990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#ifdef FEAT_CINDENT
3992 /*
3993 * Indent now if a key was typed that is in 'cinkeys'.
3994 */
3995 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3996 do_c_expr_indent();
3997#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003998#ifdef FEAT_AUTOCMD
3999 /* Trigger the CompleteDone event to give scripts a chance to act
4000 * upon the completion. */
4001 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004005#ifdef FEAT_AUTOCMD
4006 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4007 /* Trigger the CompleteDone event to give scripts a chance to act
4008 * upon the (possibly failed) completion. */
4009 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
4012 /* reset continue_* if we left expansion-mode, if we stay they'll be
4013 * (re)set properly in ins_complete() */
4014 if (!vim_is_ctrl_x_key(c))
4015 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 compl_cont_status = 0;
4017 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004019
4020 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021}
4022
4023/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004024 * Fix the redo buffer for the completion leader replacing some of the typed
4025 * text. This inserts backspaces and appends the changed text.
4026 * "ptr" is the known leader text or NUL.
4027 */
4028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004029ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004030{
4031 int len;
4032 char_u *p;
4033 char_u *ptr = ptr_arg;
4034
4035 if (ptr == NULL)
4036 {
4037 if (compl_leader != NULL)
4038 ptr = compl_leader;
4039 else
4040 return; /* nothing to do */
4041 }
4042 if (compl_orig_text != NULL)
4043 {
4044 p = compl_orig_text;
4045 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4046 ;
4047#ifdef FEAT_MBYTE
4048 if (len > 0)
4049 len -= (*mb_head_off)(p, p + len);
4050#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004051 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004052 AppendCharToRedobuff(K_BS);
4053 }
4054 else
4055 len = 0;
4056 if (ptr != NULL)
4057 AppendToRedobuffLit(ptr + len, -1);
4058}
4059
4060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4062 * (depending on flag) starting from buf and looking for a non-scanned
4063 * buffer (other than curbuf). curbuf is special, if it is called with
4064 * buf=curbuf then it has to be the first call for a given flag/expansion.
4065 *
4066 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4067 */
4068 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004069ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 if (flag == 'w') /* just windows */
4074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 if (buf == curbuf) /* first call for this flag/expansion */
4076 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004077 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 && wp->w_buffer->b_scanned)
4079 ;
4080 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 else
4083 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4084 * (unlisted buffers)
4085 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004086 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 && ((flag == 'U'
4088 ? buf->b_p_bl
4089 : (!buf->b_p_bl
4090 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004091 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 ;
4093 return buf;
4094}
4095
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004096#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004097/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004098 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004099 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004100 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004102expand_by_function(
4103 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4104 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004105{
Bram Moolenaar82139082011-09-14 16:52:09 +02004106 list_T *matchlist = NULL;
4107 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004108 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004109 char_u *funcname;
4110 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004111 win_T *curwin_save;
4112 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004113 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004114
Bram Moolenaare344bea2005-09-01 20:46:49 +00004115 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4116 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004117 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004118
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004119 /* Call 'completefunc' to obtain the list of matches. */
4120 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004121 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004122
Bram Moolenaare344bea2005-09-01 20:46:49 +00004123 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004124 curwin_save = curwin;
4125 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004126
4127 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004128 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004129 {
4130 switch (rettv.v_type)
4131 {
4132 case VAR_LIST:
4133 matchlist = rettv.vval.v_list;
4134 break;
4135 case VAR_DICT:
4136 matchdict = rettv.vval.v_dict;
4137 break;
4138 default:
4139 /* TODO: Give error message? */
4140 clear_tv(&rettv);
4141 break;
4142 }
4143 }
4144
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004145 if (curwin_save != curwin || curbuf_save != curbuf)
4146 {
4147 EMSG(_(e_complwin));
4148 goto theend;
4149 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004150 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004151 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004152 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004153 {
4154 EMSG(_(e_compldel));
4155 goto theend;
4156 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004157
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004158 if (matchlist != NULL)
4159 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004160 else if (matchdict != NULL)
4161 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004162
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004163theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004164 if (matchdict != NULL)
4165 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004166 if (matchlist != NULL)
4167 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004168}
4169#endif /* FEAT_COMPL_FUNC */
4170
Bram Moolenaar39f05632006-03-19 22:15:26 +00004171#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004172/*
4173 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004174 */
4175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004177{
4178 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004179 int dir = compl_direction;
4180
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004181 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004182 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004183 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004184 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4185 /* if dir was BACKWARD then honor it just once */
4186 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004187 else if (did_emsg)
4188 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004189 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004190}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004191
4192/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004193 * Add completions from a dict.
4194 */
4195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004196ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004197{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004198 dictitem_T *di_refresh;
4199 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004200
4201 /* Check for optional "refresh" item. */
4202 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004203 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4204 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004205 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004206 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004207
4208 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4209 compl_opt_refresh_always = TRUE;
4210 }
4211
4212 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004213 di_words = dict_find(dict, (char_u *)"words", 5);
4214 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4215 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004216}
4217
4218/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004219 * Add a match to the list of matches from a typeval_T.
4220 * If the given string is already in the list of completions, then return
4221 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4222 * maybe because alloc() returns NULL, then FAIL is returned.
4223 */
4224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004225ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004226{
4227 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004228 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004229 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004230 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004231 char_u *(cptext[CPT_COUNT]);
4232
4233 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4234 {
4235 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4236 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4237 (char_u *)"abbr", FALSE);
4238 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4239 (char_u *)"menu", FALSE);
4240 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4241 (char_u *)"kind", FALSE);
4242 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4243 (char_u *)"info", FALSE);
4244 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4245 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004246 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004247 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004248 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4249 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004250 }
4251 else
4252 {
4253 word = get_tv_string_chk(tv);
4254 vim_memset(cptext, 0, sizeof(cptext));
4255 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004256 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004257 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004258 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004259}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004260#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004261
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262/*
4263 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004264 * The search starts at position "ini" in curbuf and in the direction
4265 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004266 * When "compl_started" is FALSE start at that position, otherwise continue
4267 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 * This may return before finding all the matches.
4269 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 */
4271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004272ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
4274 static pos_T first_match_pos;
4275 static pos_T last_match_pos;
4276 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277 static int found_all = FALSE; /* Found all matches of a
4278 certain type. */
4279 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar572cb562005-08-05 21:35:02 +00004281 pos_T *pos;
4282 char_u **matches;
4283 int save_p_scs;
4284 int save_p_ws;
4285 int save_p_ic;
4286 int i;
4287 int num_matches;
4288 int len;
4289 int found_new_match;
4290 int type = ctrl_x_mode;
4291 char_u *ptr;
4292 char_u *dict = NULL;
4293 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004294 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004298 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 ins_buf->b_scanned = 0;
4300 found_all = FALSE;
4301 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004303 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 last_match_pos = first_match_pos = *ini;
4305 }
4306
Bram Moolenaar4475b622017-05-01 20:46:52 +02004307 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004308 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4310 for (;;)
4311 {
4312 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004313 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 * or if found_all says this entry is done. For ^X^L only use the
4317 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004318 if ((ctrl_x_mode == CTRL_X_NORMAL
4319 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 found_all = FALSE;
4323 while (*e_cpt == ',' || *e_cpt == ' ')
4324 e_cpt++;
4325 if (*e_cpt == '.' && !curbuf->b_scanned)
4326 {
4327 ins_buf = curbuf;
4328 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004329 /* Move the cursor back one character so that ^N can match the
4330 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004331 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004332 {
4333 /* Move the cursor to after the last character in the
4334 * buffer, so that word at start of buffer is found
4335 * correctly. */
4336 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4337 first_match_pos.col =
4338 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 last_match_pos = first_match_pos;
4341 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004342
4343 /* Remember the first match so that the loop stops when we
4344 * wrap and come back there a second time. */
4345 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4348 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4349 {
4350 /* Scan a buffer, but not the current one. */
4351 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4352 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 first_match_pos.col = last_match_pos.col = 0;
4355 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4356 last_match_pos.lnum = 0;
4357 type = 0;
4358 }
4359 else /* unloaded buffer, scan like dictionary */
4360 {
4361 found_all = TRUE;
4362 if (ins_buf->b_fname == NULL)
4363 continue;
4364 type = CTRL_X_DICTIONARY;
4365 dict = ins_buf->b_fname;
4366 dict_f = DICT_EXACT;
4367 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004368 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 ins_buf->b_fname == NULL
4370 ? buf_spname(ins_buf)
4371 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004372 ? ins_buf->b_fname
4373 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004374 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 else if (*e_cpt == NUL)
4377 break;
4378 else
4379 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004380 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 type = -1;
4382 else if (*e_cpt == 'k' || *e_cpt == 's')
4383 {
4384 if (*e_cpt == 'k')
4385 type = CTRL_X_DICTIONARY;
4386 else
4387 type = CTRL_X_THESAURUS;
4388 if (*++e_cpt != ',' && *e_cpt != NUL)
4389 {
4390 dict = e_cpt;
4391 dict_f = DICT_FIRST;
4392 }
4393 }
4394#ifdef FEAT_FIND_ID
4395 else if (*e_cpt == 'i')
4396 type = CTRL_X_PATH_PATTERNS;
4397 else if (*e_cpt == 'd')
4398 type = CTRL_X_PATH_DEFINES;
4399#endif
4400 else if (*e_cpt == ']' || *e_cpt == 't')
4401 {
4402 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004403 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004404 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 else
4407 type = -1;
4408
4409 /* in any case e_cpt is advanced to the next entry */
4410 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4411
4412 found_all = TRUE;
4413 if (type == -1)
4414 continue;
4415 }
4416 }
4417
Bram Moolenaar4475b622017-05-01 20:46:52 +02004418 /* If complete() was called then compl_pattern has been reset. The
4419 * following won't work then, bail out. */
4420 if (compl_pattern == NULL)
4421 break;
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 switch (type)
4424 {
4425 case -1:
4426 break;
4427#ifdef FEAT_FIND_ID
4428 case CTRL_X_PATH_PATTERNS:
4429 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004430 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4435 (linenr_T)1, (linenr_T)MAXLNUM);
4436 break;
4437#endif
4438
4439 case CTRL_X_DICTIONARY:
4440 case CTRL_X_THESAURUS:
4441 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004442 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 : (type == CTRL_X_THESAURUS
4444 ? (*curbuf->b_p_tsr == NUL
4445 ? p_tsr
4446 : curbuf->b_p_tsr)
4447 : (*curbuf->b_p_dict == NUL
4448 ? p_dict
4449 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004450 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004451 dict != NULL ? dict_f
4452 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 dict = NULL;
4454 break;
4455
4456 case CTRL_X_TAGS:
4457 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4458 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004461 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 * of matches is found when compl_pattern is empty */
4463 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004464 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4465 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4467 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004468 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 p_ic = save_p_ic;
4471 break;
4472
4473 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4476 {
4477
4478 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004480 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482 break;
4483
4484 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 if (expand_cmdline(&compl_xp, compl_pattern,
4486 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004488 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 break;
4490
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004491#ifdef FEAT_COMPL_FUNC
4492 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004493 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004494 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004495 break;
4496#endif
4497
Bram Moolenaar488c6512005-08-11 20:09:58 +00004498 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004499#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004500 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004501 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004502 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004503 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004504#endif
4505 break;
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 default: /* normal ^P/^N and ^X^L */
4508 /*
4509 * If 'infercase' is set, don't use 'smartcase' here
4510 */
4511 save_p_scs = p_scs;
4512 if (ins_buf->b_p_inf)
4513 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514
Bram Moolenaar361aa502014-01-23 22:45:58 +01004515 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 * end but never from the middle, thus setting nowrapscan in this
4517 * buffers is a good idea, on the other hand, we always set
4518 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4519 save_p_ws = p_ws;
4520 if (ins_buf != curbuf)
4521 p_ws = FALSE;
4522 else if (*e_cpt == '.')
4523 p_ws = TRUE;
4524 for (;;)
4525 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004526 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004528 ++msg_silent; /* Don't want messages for wrapscan. */
4529
Bram Moolenaare4214502015-03-05 18:08:43 +01004530 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4531 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004532 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004533 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004536 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004538 found_new_match = searchit(NULL, ins_buf, pos,
4539 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004541 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004542 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004543 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004545 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 first_match_pos = *pos;
4548 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004549 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004552 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 found_new_match = FAIL;
4554 if (found_new_match == FAIL)
4555 {
4556 if (ins_buf == curbuf)
4557 found_all = TRUE;
4558 break;
4559 }
4560
4561 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 && ini->lnum == pos->lnum
4564 && ini->col == pos->col)
4565 continue;
4566 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004567 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004569 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4572 continue;
4573 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4574 if (!p_paste)
4575 ptr = skipwhite(ptr);
4576 }
4577 len = (int)STRLEN(ptr);
4578 }
4579 else
4580 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 char_u *tmp_ptr = ptr;
4582
4583 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /* Skip if already inside a word. */
4587 if (vim_iswordp(tmp_ptr))
4588 continue;
4589 /* Find start of next word. */
4590 tmp_ptr = find_word_start(tmp_ptr);
4591 }
4592 /* Find end of this word. */
4593 tmp_ptr = find_word_end(tmp_ptr);
4594 len = (int)(tmp_ptr - ptr);
4595
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 if ((compl_cont_status & CONT_ADDING)
4597 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4600 {
4601 /* Try next line, if any. the new word will be
4602 * "join" as if the normal command "J" was used.
4603 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 * works -- Acevedo */
4606 STRNCPY(IObuff, ptr, len);
4607 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4608 tmp_ptr = ptr = skipwhite(ptr);
4609 /* Find start of next word. */
4610 tmp_ptr = find_word_start(tmp_ptr);
4611 /* Find end of next word. */
4612 tmp_ptr = find_word_end(tmp_ptr);
4613 if (tmp_ptr > ptr)
4614 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004615 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004617 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 IObuff[len++] = ' ';
4619 /* IObuf =~ "\k.* ", thus len >= 2 */
4620 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004621 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 || (vim_strchr(p_cpo, CPO_JOINSP)
4623 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004624 && (IObuff[len - 2] == '?'
4625 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 IObuff[len++] = ' ';
4627 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004628 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (tmp_ptr - ptr >= IOSIZE - len)
4630 tmp_ptr = ptr + IOSIZE - len - 1;
4631 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4632 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004633 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 IObuff[len] = NUL;
4636 ptr = IObuff;
4637 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 continue;
4640 }
4641 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004642 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004643 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004644 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 found_new_match = OK;
4647 break;
4648 }
4649 }
4650 p_scs = save_p_scs;
4651 p_ws = save_p_ws;
4652 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004653
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004655 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004656 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 found_new_match = OK;
4658
4659 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004660 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4661 * match */
4662 if ((ctrl_x_mode != CTRL_X_NORMAL
4663 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004664 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004665 {
4666 if (got_int)
4667 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004668 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004669 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004670 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004671
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004672 if ((ctrl_x_mode != CTRL_X_NORMAL
4673 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004674 || compl_interrupted)
4675 break;
4676 compl_started = TRUE;
4677 }
4678 else
4679 {
4680 /* Mark a buffer scanned when it has been scanned completely */
4681 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4682 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004684 compl_started = FALSE;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004689 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 && *e_cpt == NUL) /* Got to end of 'complete' */
4691 found_new_match = FAIL;
4692
4693 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004694 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4695 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 i = ins_compl_make_cyclic();
4697
Bram Moolenaar4475b622017-05-01 20:46:52 +02004698 if (compl_old_match != NULL)
4699 {
4700 /* If several matches were added (FORWARD) or the search failed and has
4701 * just been made cyclic then we have to move compl_curr_match to the
4702 * next or previous entry (if any) -- Acevedo */
4703 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4704 : compl_old_match->cp_prev;
4705 if (compl_curr_match == NULL)
4706 compl_curr_match = compl_old_match;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return i;
4709}
4710
4711/* Delete the old text being completed. */
4712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004713ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004715 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 /*
4718 * In insert mode: Delete the typed part.
4719 * In replace mode: Put the old characters back, if any.
4720 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004721 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4722 if ((int)curwin->w_cursor.col > col)
4723 {
4724 if (stop_arrow() == FAIL)
4725 return;
4726 backspace_until_column(col);
4727 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004728
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004729 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4730 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004732 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004733 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734}
4735
Bram Moolenaar472e8592016-10-15 17:06:47 +02004736/*
4737 * Insert the new text being completed.
4738 * "in_compl_func" is TRUE when called from complete_check().
4739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004741ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004743 dict_T *dict;
4744
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004745 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004746 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4747 compl_used_match = FALSE;
4748 else
4749 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004750
4751 /* Set completed item. */
4752 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004753 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004754 if (dict != NULL)
4755 {
4756 dict_add_nr_str(dict, "word", 0L,
4757 EMPTY_IF_NULL(compl_shown_match->cp_str));
4758 dict_add_nr_str(dict, "abbr", 0L,
4759 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4760 dict_add_nr_str(dict, "menu", 0L,
4761 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4762 dict_add_nr_str(dict, "kind", 0L,
4763 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4764 dict_add_nr_str(dict, "info", 0L,
4765 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4766 }
4767 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004768 if (!in_compl_func)
4769 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770}
4771
4772/*
4773 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004774 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4775 * get more completions. If it is FALSE, then we just do nothing when there
4776 * are no more completions in a given direction. The latter case is used when
4777 * we are still in the middle of finding completions, to allow browsing
4778 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * Return the total number of matches, or -1 if still unknown -- webb.
4780 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4782 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 *
4784 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004785 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4786 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 */
4788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004789ins_compl_next(
4790 int allow_get_expansion,
4791 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004792 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004793 int insert_match, /* Insert the newly selected match */
4794 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004797 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004798 compl_T *found_compl = NULL;
4799 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004800 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004801 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004803 /* When user complete function return -1 for findstart which is next
4804 * time of 'always', compl_shown_match become NULL. */
4805 if (compl_shown_match == NULL)
4806 return -1;
4807
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004808 if (compl_leader != NULL
4809 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004811 /* Set "compl_shown_match" to the actually shown match, it may differ
4812 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004813 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004814 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004815 && compl_shown_match->cp_next != NULL
4816 && compl_shown_match->cp_next != compl_first_match)
4817 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004818
4819 /* If we didn't find it searching forward, and compl_shows_dir is
4820 * backward, find the last match. */
4821 if (compl_shows_dir == BACKWARD
4822 && !ins_compl_equal(compl_shown_match,
4823 compl_leader, (int)STRLEN(compl_leader))
4824 && (compl_shown_match->cp_next == NULL
4825 || compl_shown_match->cp_next == compl_first_match))
4826 {
4827 while (!ins_compl_equal(compl_shown_match,
4828 compl_leader, (int)STRLEN(compl_leader))
4829 && compl_shown_match->cp_prev != NULL
4830 && compl_shown_match->cp_prev != compl_first_match)
4831 compl_shown_match = compl_shown_match->cp_prev;
4832 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004833 }
4834
4835 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004836 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 /* Delete old text to be replaced */
4838 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004839
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004840 /* When finding the longest common text we stick at the original text,
4841 * don't let CTRL-N or CTRL-P move to the first match. */
4842 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4843
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004844 /* When restarting the search don't insert the first match either. */
4845 if (compl_restarting)
4846 {
4847 advance = FALSE;
4848 compl_restarting = FALSE;
4849 }
4850
Bram Moolenaare3226be2005-12-18 22:10:00 +00004851 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4852 * around. */
4853 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004855 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004857 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004858 found_end = (compl_first_match != NULL
4859 && (compl_shown_match->cp_next == compl_first_match
4860 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004861 }
4862 else if (compl_shows_dir == BACKWARD
4863 && compl_shown_match->cp_prev != NULL)
4864 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004865 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004866 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004867 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004870 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004871 if (!allow_get_expansion)
4872 {
4873 if (advance)
4874 {
4875 if (compl_shows_dir == BACKWARD)
4876 compl_pending -= todo + 1;
4877 else
4878 compl_pending += todo + 1;
4879 }
4880 return -1;
4881 }
4882
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004883 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004884 {
4885 if (compl_shows_dir == BACKWARD)
4886 --compl_pending;
4887 else
4888 ++compl_pending;
4889 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004890
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004891 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004892 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004893
4894 /* handle any pending completions */
4895 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004896 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004897 {
4898 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4899 {
4900 compl_shown_match = compl_shown_match->cp_next;
4901 --compl_pending;
4902 }
4903 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4904 {
4905 compl_shown_match = compl_shown_match->cp_prev;
4906 ++compl_pending;
4907 }
4908 else
4909 break;
4910 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004911 found_end = FALSE;
4912 }
4913 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4914 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004915 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004916 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004917 ++todo;
4918 else
4919 /* Remember a matching item. */
4920 found_compl = compl_shown_match;
4921
4922 /* Stop at the end of the list when we found a usable match. */
4923 if (found_end)
4924 {
4925 if (found_compl != NULL)
4926 {
4927 compl_shown_match = found_compl;
4928 break;
4929 }
4930 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004934 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004935 if (compl_no_insert && !started)
4936 {
4937 ins_bytes(compl_orig_text + ins_compl_len());
4938 compl_used_match = FALSE;
4939 }
4940 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004941 {
4942 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004943 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004944 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004945 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004946 }
4947 else
4948 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 if (!allow_get_expansion)
4951 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004952 /* may undisplay the popup menu first */
4953 ins_compl_upd_pum();
4954
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004955 /* redraw to show the user what was inserted */
4956 update_screen(0);
4957
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004958 /* display the updated popup menu */
4959 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004960#ifdef FEAT_GUI
4961 if (gui.in_use)
4962 {
4963 /* Show the cursor after the match, not after the redrawn text. */
4964 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004965 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004966 }
4967#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 /* Delete old text to be replaced, since we're still searching and
4970 * don't want to match ourselves! */
4971 ins_compl_delete();
4972 }
4973
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004974 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004975 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004976 if (compl_no_insert && !started)
4977 compl_enter_selects = TRUE;
4978 else
4979 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /*
4982 * Show the file name for the match (if any)
4983 * Truncate the file name to avoid a wait for return.
4984 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004985 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004987 char *lead = _("match in file");
4988 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4989 char_u *s;
4990 char_u *e;
4991
4992 if (space > 0)
4993 {
4994 /* We need the tail that fits. With double-byte encoding going
4995 * back from the end is very slow, thus go from the start and keep
4996 * the text that fits in "space" between "s" and "e". */
4997 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4998 {
4999 space -= ptr2cells(e);
5000 while (space < 0)
5001 {
5002 space += ptr2cells(s);
5003 MB_PTR_ADV(s);
5004 }
5005 }
5006 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5007 s > compl_shown_match->cp_fname ? "<" : "", s);
5008 msg(IObuff);
5009 redraw_cmdline = FALSE; /* don't overwrite! */
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012
5013 return num_matches;
5014}
5015
5016/*
5017 * Call this while finding completions, to check whether the user has hit a key
5018 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005019 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005021 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005022 * "in_compl_func" is TRUE when called from complete_check(), don't set
5023 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 */
5025 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005026ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 static int count = 0;
5029
5030 int c;
5031
5032 /* Don't check when reading keys from a script. That would break the test
5033 * scripts */
5034 if (using_script())
5035 return;
5036
5037 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005038 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 return;
5040 count = 0;
5041
Bram Moolenaara260a972006-06-23 19:36:29 +00005042 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5043 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (c != NUL)
5046 {
5047 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5048 {
5049 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005050 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005051 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005052 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005054 else
5055 {
5056 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005057 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005058 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005059 if (c != K_IGNORE)
5060 {
5061 /* Don't interrupt completion when the character wasn't typed,
5062 * e.g., when doing @q to replay keys. */
5063 if (c != Ctrl_R && KeyTyped)
5064 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005065
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005066 vungetc(c);
5067 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005070 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005071 {
5072 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5073
5074 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005075 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005076 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005077}
5078
5079/*
5080 * Decide the direction of Insert mode complete from the key typed.
5081 * Returns BACKWARD or FORWARD.
5082 */
5083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005084ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005085{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005086 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005087 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005088 return BACKWARD;
5089 return FORWARD;
5090}
5091
5092/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005093 * Return TRUE for keys that are used for completion only when the popup menu
5094 * is visible.
5095 */
5096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005098{
5099 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005100 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5101 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005102}
5103
5104/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005105 * Decide the number of completions to move forward.
5106 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5107 */
5108 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005109ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005110{
5111 int h;
5112
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005113 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005114 {
5115 h = pum_get_height();
5116 if (h > 3)
5117 h -= 2; /* keep some context */
5118 return h;
5119 }
5120 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121}
5122
5123/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005124 * Return TRUE if completion with "c" should insert the match, FALSE if only
5125 * to change the currently selected completion.
5126 */
5127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005128ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005129{
5130 switch (c)
5131 {
5132 case K_UP:
5133 case K_DOWN:
5134 case K_PAGEDOWN:
5135 case K_KPAGEDOWN:
5136 case K_S_DOWN:
5137 case K_PAGEUP:
5138 case K_KPAGEUP:
5139 case K_S_UP:
5140 return FALSE;
5141 }
5142 return TRUE;
5143}
5144
5145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * Do Insert mode completion.
5147 * Called when character "c" was typed, which has a meaning for completion.
5148 * Returns OK if completion was done, FAIL if something failed (out of mem).
5149 */
5150 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005151ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 char_u *line;
5154 int startcol = 0; /* column where searched text starts */
5155 colnr_T curs_col; /* cursor column */
5156 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005157 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005158 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005159 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005160 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
Bram Moolenaare3226be2005-12-18 22:10:00 +00005162 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005163 insert_match = ins_compl_use_match(c);
5164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005165 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 /* First time we hit ^N or ^P (in a row, I mean) */
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 did_ai = FALSE;
5170#ifdef FEAT_SMARTINDENT
5171 did_si = FALSE;
5172 can_si = FALSE;
5173 can_si_back = FALSE;
5174#endif
5175 if (stop_arrow() == FAIL)
5176 return FAIL;
5177
5178 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005180 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005182 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 * "compl_startpos" to the cursor as a pattern to add a new word
5184 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005185 * "compl_startpos" is not in the same line as the cursor then fix it
5186 * (the line has been split because it was longer than 'tw'). if SOL
5187 * is set then skip the previous pattern, a word at the beginning of
5188 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005189 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5190 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192 /*
5193 * it is a continued search
5194 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005196 if (ctrl_x_mode == CTRL_X_NORMAL
5197 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5198 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 /* line (probably) wrapped, set compl_startpos to the
5203 * first non_blank in the line, if it is not a wordchar
5204 * include it to get a better pattern, but then we don't
5205 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005206 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005207 compl_startpos.col = compl_col;
5208 compl_startpos.lnum = curwin->w_cursor.lnum;
5209 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211 else
5212 {
5213 /* S_IPOS was set when we inserted a word that was at the
5214 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 * mode but first we need to redefine compl_startpos */
5216 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 compl_cont_status |= CONT_SOL;
5219 compl_startpos.col = (colnr_T)(skipwhite(
5220 line + compl_length
5221 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005226 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005227 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 compl_cont_status &= ~CONT_SOL;
5232 compl_length = (IOSIZE - MIN_SPACE);
5233 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5236 if (compl_length < 1)
5237 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005239 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005245 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005250 if (ctrl_x_mode != CTRL_X_NORMAL)
5251 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 compl_cont_status = 0;
5253 compl_cont_status |= CONT_N_ADDS;
5254 compl_startpos = curwin->w_cursor;
5255 startcol = (int)curs_col;
5256 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258
5259 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005260 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5264 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269 compl_col += ++startcol;
5270 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 compl_pattern = str_foldcase(line + compl_col,
5274 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 compl_pattern = vim_strnsave(line + compl_col,
5277 compl_length);
5278 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 return FAIL;
5280 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 char_u *prefix = (char_u *)"\\<";
5284
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005285 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005287 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 if (!vim_iswordp(line + compl_col)
5291 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 && (
5293#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#endif
5298 )))
5299 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 STRCPY((char *)compl_pattern, prefix);
5301 (void)quote_meta(compl_pattern + STRLEN(prefix),
5302 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309#endif
5310 )
5311 {
5312 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5314 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 compl_col += curs_col;
5317 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319 else
5320 {
5321#ifdef FEAT_MBYTE
5322 /* Search the point of change class of multibyte character
5323 * or not a word single byte character backward. */
5324 if (has_mbyte)
5325 {
5326 int base_class;
5327 int head_off;
5328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005329 startcol -= (*mb_head_off)(line, line + startcol);
5330 base_class = mb_get_class(line + startcol);
5331 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 head_off = (*mb_head_off)(line, line + startcol);
5334 if (base_class != mb_get_class(line + startcol
5335 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339 }
5340 else
5341#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_col += ++startcol;
5345 compl_length = (int)curs_col - startcol;
5346 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 /* Only match word with at least two chars -- webb
5349 * there's no need to call quote_meta,
5350 * alloc(7) is enough -- Acevedo
5351 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 compl_pattern = alloc(7);
5353 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 STRCPY((char *)compl_pattern, "\\<");
5356 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5357 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 else
5360 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005362 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 STRCPY((char *)compl_pattern, "\\<");
5366 (void)quote_meta(compl_pattern + 2, line + compl_col,
5367 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 }
5370 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005371 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005373 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 compl_length = (int)curs_col - (int)compl_col;
5375 if (compl_length < 0) /* cursor in indent: empty pattern */
5376 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 compl_pattern = str_foldcase(line + compl_col, compl_length,
5379 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005381 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5382 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 return FAIL;
5384 }
5385 else if (ctrl_x_mode == CTRL_X_FILES)
5386 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005387 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005388 if (startcol > 0)
5389 {
5390 char_u *p = line + startcol;
5391
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005392 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005393 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005394 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005395 if (p == line && vim_isfilec(PTR2CHAR(p)))
5396 startcol = 0;
5397 else
5398 startcol = (int)(p - line) + 1;
5399 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005400
Bram Moolenaar0300e462013-09-08 16:03:45 +02005401 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005402 compl_length = (int)curs_col - startcol;
5403 compl_pattern = addstar(line + compl_col, compl_length,
5404 EXPAND_FILES);
5405 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return FAIL;
5407 }
5408 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5409 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005410 compl_pattern = vim_strnsave(line, curs_col);
5411 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005414 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5416 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005417 /* No completion possible, use an empty pattern to get a
5418 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005419 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005420 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005421 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5422 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005424 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005425 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005426#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005427 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005428 * Call user defined function 'completefunc' with "a:findstart"
5429 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005430 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005431 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005432 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005433 char_u *funcname;
5434 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005435 win_T *curwin_save;
5436 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005437
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005438 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005439 * string */
5440 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5441 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5442 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005443 {
5444 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5445 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005446 /* restore did_ai, so that adding comment leader works */
5447 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005448 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005449 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005450
5451 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005452 args[1] = NULL;
5453 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005454 curwin_save = curwin;
5455 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005456 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005457 if (curwin_save != curwin || curbuf_save != curbuf)
5458 {
5459 EMSG(_(e_complwin));
5460 return FAIL;
5461 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005462 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005463 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005464 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005465 {
5466 EMSG(_(e_compldel));
5467 return FAIL;
5468 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005469
Bram Moolenaar6110a002012-01-26 18:58:38 +01005470 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005471 * cancel the complete without an error.
5472 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005473 if (col == -2)
5474 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005475 if (col == -3)
5476 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005477 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005478 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005479 if (!shortmess(SHM_COMPLETIONMENU))
5480 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005481 return FAIL;
5482 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005483
Bram Moolenaar82139082011-09-14 16:52:09 +02005484 /*
5485 * Reset extended parameters of completion, when start new
5486 * completion.
5487 */
5488 compl_opt_refresh_always = FALSE;
5489
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005490 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005491 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005492 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005493 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005494 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005495
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005496 /* Setup variables for completion. Need to obtain "line" again,
5497 * it may have become invalid. */
5498 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005499 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5501 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005502#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 return FAIL;
5504 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005505 else if (ctrl_x_mode == CTRL_X_SPELL)
5506 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005507#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005508 if (spell_bad_len > 0)
5509 compl_col = curs_col - spell_bad_len;
5510 else
5511 compl_col = spell_word_start(startcol);
5512 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005513 {
5514 compl_length = 0;
5515 compl_col = curs_col;
5516 }
5517 else
5518 {
5519 spell_expand_check_cap(compl_col);
5520 compl_length = (int)curs_col - compl_col;
5521 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005522 /* Need to obtain "line" again, it may have become invalid. */
5523 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005524 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5525 if (compl_pattern == NULL)
5526#endif
5527 return FAIL;
5528 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 else
5530 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005531 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005532 return FAIL;
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005538 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 /* Insert a new line, keep indentation but ignore 'comments' */
5541#ifdef FEAT_COMMENTS
5542 char_u *old = curbuf->b_p_com;
5543
5544 curbuf->b_p_com = (char_u *)"";
5545#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005546 compl_startpos.lnum = curwin->w_cursor.lnum;
5547 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 ins_eol('\r');
5549#ifdef FEAT_COMMENTS
5550 curbuf->b_p_com = old;
5551#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005552 compl_length = 0;
5553 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555 }
5556 else
5557 {
5558 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005559 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005562 if (compl_cont_status & CONT_LOCAL)
5563 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 else
5565 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5566
Bram Moolenaar62951b12011-09-21 18:23:05 +02005567 /* If any of the original typed text has been changed we need to fix
5568 * the redo buffer. */
5569 ins_compl_fixRedoBufForLeader(NULL);
5570
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005571 /* Always add completion for the original text. */
5572 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005573 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5574 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005575 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005577 vim_free(compl_pattern);
5578 compl_pattern = NULL;
5579 vim_free(compl_orig_text);
5580 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return FAIL;
5582 }
5583
5584 /* showmode might reset the internal line pointers, so it must
5585 * be called before line = ml_get(), or when this address is no
5586 * longer needed. -- Acevedo.
5587 */
5588 edit_submode_extra = (char_u *)_("-- Searching...");
5589 edit_submode_highl = HLF_COUNT;
5590 showmode();
5591 edit_submode_extra = NULL;
5592 out_flush();
5593 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005594 else if (insert_match && stop_arrow() == FAIL)
5595 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005597 compl_shown_match = compl_curr_match;
5598 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
5600 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005601 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005603 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005604 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005605 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005607 /* may undisplay the popup menu */
5608 ins_compl_upd_pum();
5609
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005610 if (n > 1) /* all matches have been found */
5611 compl_matches = n;
5612 compl_curr_match = compl_shown_match;
5613 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaard68071d2006-05-02 22:08:30 +00005615 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5616 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (got_int && !global_busy)
5618 {
5619 (void)vgetc();
5620 got_int = FALSE;
5621 }
5622
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005623 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005624 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005626 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5627 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5629 edit_submode_highl = HLF_E;
5630 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5631 * because we couldn't expand anything at first place, but if we used
5632 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5633 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 if ( compl_length > 1
5635 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005636 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5638 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005639 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641
Bram Moolenaar572cb562005-08-05 21:35:02 +00005642 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005643 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005645 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 if (edit_submode_extra == NULL)
5648 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005649 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
5651 edit_submode_extra = (char_u *)_("Back at original");
5652 edit_submode_highl = HLF_W;
5653 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005654 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 edit_submode_extra = (char_u *)_("Word from other line");
5657 edit_submode_highl = HLF_COUNT;
5658 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005659 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 edit_submode_extra = (char_u *)_("The only match");
5662 edit_submode_highl = HLF_COUNT;
5663 }
5664 else
5665 {
5666 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005667 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005669 int number = 0;
5670 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005672 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 /* search backwards for the first valid (!= -1) number.
5675 * This should normally succeed already at the first loop
5676 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005677 for (match = compl_curr_match->cp_prev; match != NULL
5678 && match != compl_first_match;
5679 match = match->cp_prev)
5680 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005682 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 break;
5684 }
5685 if (match != NULL)
5686 /* go up and assign all numbers which are not assigned
5687 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005688 for (match = match->cp_next;
5689 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005690 match = match->cp_next)
5691 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 else /* BACKWARD */
5694 {
5695 /* search forwards (upwards) for the first valid (!= -1)
5696 * number. This should normally succeed already at the
5697 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005698 for (match = compl_curr_match->cp_next; match != NULL
5699 && match != compl_first_match;
5700 match = match->cp_next)
5701 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005703 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 break;
5705 }
5706 if (match != NULL)
5707 /* go down and assign all numbers which are not
5708 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005709 for (match = match->cp_prev; match
5710 && match->cp_number == -1;
5711 match = match->cp_prev)
5712 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 }
5715
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005716 /* The match should always have a sequence number now, this is
5717 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005718 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005720 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5721 * Translations may need more than twice that. */
5722 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005724 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005725 vim_snprintf((char *)match_ref, sizeof(match_ref),
5726 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005727 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005729 vim_snprintf((char *)match_ref, sizeof(match_ref),
5730 _("match %d"),
5731 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 edit_submode_extra = match_ref;
5733 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005734 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 curs_columns(FALSE);
5736 }
5737 }
5738 }
5739
5740 /* Show a message about what (completion) mode we're in. */
5741 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005742 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005744 if (edit_submode_extra != NULL)
5745 {
5746 if (!p_smd)
5747 msg_attr(edit_submode_extra,
5748 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005749 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005750 }
5751 else
5752 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaard68071d2006-05-02 22:08:30 +00005755 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005756 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005757 show_pum(save_w_wrow, save_w_leftcol);
5758
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005759 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005760 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 return OK;
5763}
5764
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005765 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005766show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005767{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005768 /* RedrawingDisabled may be set when invoked through complete(). */
5769 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005770
Bram Moolenaarcb036422017-03-01 12:29:10 +01005771 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005772
Bram Moolenaarcb036422017-03-01 12:29:10 +01005773 /* If the cursor moved or the display scrolled we need to remove the pum
5774 * first. */
5775 setcursor();
5776 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5777 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005778
Bram Moolenaarcb036422017-03-01 12:29:10 +01005779 ins_compl_show_pum();
5780 setcursor();
5781 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005782}
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784/*
5785 * Looks in the first "len" chars. of "src" for search-metachars.
5786 * If dest is not NULL the chars. are copied there quoting (with
5787 * a backslash) the metachars, and dest would be NUL terminated.
5788 * Returns the length (needed) of dest
5789 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005790 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005791quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005793 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005795 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
5797 switch (*src)
5798 {
5799 case '.':
5800 case '*':
5801 case '[':
5802 if (ctrl_x_mode == CTRL_X_DICTIONARY
5803 || ctrl_x_mode == CTRL_X_THESAURUS)
5804 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005805 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 case '~':
5807 if (!p_magic) /* quote these only if magic is set */
5808 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005809 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 case '\\':
5811 if (ctrl_x_mode == CTRL_X_DICTIONARY
5812 || ctrl_x_mode == CTRL_X_THESAURUS)
5813 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005814 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 case '^': /* currently it's not needed. */
5816 case '$':
5817 m++;
5818 if (dest != NULL)
5819 *dest++ = '\\';
5820 break;
5821 }
5822 if (dest != NULL)
5823 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005824# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 /* Copy remaining bytes of a multibyte character. */
5826 if (has_mbyte)
5827 {
5828 int i, mb_len;
5829
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005830 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (mb_len > 0 && len >= mb_len)
5832 for (i = 0; i < mb_len; ++i)
5833 {
5834 --len;
5835 ++src;
5836 if (dest != NULL)
5837 *dest++ = *src;
5838 }
5839 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
5842 if (dest != NULL)
5843 *dest = NUL;
5844
5845 return m;
5846}
5847#endif /* FEAT_INS_EXPAND */
5848
5849/*
5850 * Next character is interpreted literally.
5851 * A one, two or three digit decimal number is interpreted as its byte value.
5852 * If one or two digits are entered, the next character is given to vungetc().
5853 * For Unicode a character > 255 may be returned.
5854 */
5855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005856get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 int cc;
5859 int nc;
5860 int i;
5861 int hex = FALSE;
5862 int octal = FALSE;
5863#ifdef FEAT_MBYTE
5864 int unicode = 0;
5865#endif
5866
5867 if (got_int)
5868 return Ctrl_C;
5869
5870#ifdef FEAT_GUI
5871 /*
5872 * In GUI there is no point inserting the internal code for a special key.
5873 * It is more useful to insert the string "<KEY>" instead. This would
5874 * probably be useful in a text window too, but it would not be
5875 * vi-compatible (maybe there should be an option for it?) -- webb
5876 */
5877 if (gui.in_use)
5878 ++allow_keys;
5879#endif
5880#ifdef USE_ON_FLY_SCROLL
5881 dont_scroll = TRUE; /* disallow scrolling here */
5882#endif
5883 ++no_mapping; /* don't map the next key hits */
5884 cc = 0;
5885 i = 0;
5886 for (;;)
5887 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005888 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#ifdef FEAT_CMDL_INFO
5890 if (!(State & CMDLINE)
5891# ifdef FEAT_MBYTE
5892 && MB_BYTE2LEN_CHECK(nc) == 1
5893# endif
5894 )
5895 add_to_showcmd(nc);
5896#endif
5897 if (nc == 'x' || nc == 'X')
5898 hex = TRUE;
5899 else if (nc == 'o' || nc == 'O')
5900 octal = TRUE;
5901#ifdef FEAT_MBYTE
5902 else if (nc == 'u' || nc == 'U')
5903 unicode = nc;
5904#endif
5905 else
5906 {
5907 if (hex
5908#ifdef FEAT_MBYTE
5909 || unicode != 0
5910#endif
5911 )
5912 {
5913 if (!vim_isxdigit(nc))
5914 break;
5915 cc = cc * 16 + hex2nr(nc);
5916 }
5917 else if (octal)
5918 {
5919 if (nc < '0' || nc > '7')
5920 break;
5921 cc = cc * 8 + nc - '0';
5922 }
5923 else
5924 {
5925 if (!VIM_ISDIGIT(nc))
5926 break;
5927 cc = cc * 10 + nc - '0';
5928 }
5929
5930 ++i;
5931 }
5932
5933 if (cc > 255
5934#ifdef FEAT_MBYTE
5935 && unicode == 0
5936#endif
5937 )
5938 cc = 255; /* limit range to 0-255 */
5939 nc = 0;
5940
5941 if (hex) /* hex: up to two chars */
5942 {
5943 if (i >= 2)
5944 break;
5945 }
5946#ifdef FEAT_MBYTE
5947 else if (unicode) /* Unicode: up to four or eight chars */
5948 {
5949 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5950 break;
5951 }
5952#endif
5953 else if (i >= 3) /* decimal or octal: up to three chars */
5954 break;
5955 }
5956 if (i == 0) /* no number entered */
5957 {
5958 if (nc == K_ZERO) /* NUL is stored as NL */
5959 {
5960 cc = '\n';
5961 nc = 0;
5962 }
5963 else
5964 {
5965 cc = nc;
5966 nc = 0;
5967 }
5968 }
5969
5970 if (cc == 0) /* NUL is stored as NL */
5971 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005972#ifdef FEAT_MBYTE
5973 if (enc_dbcs && (cc & 0xff) == 0)
5974 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5975 second byte will cause trouble! */
5976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 --no_mapping;
5979#ifdef FEAT_GUI
5980 if (gui.in_use)
5981 --allow_keys;
5982#endif
5983 if (nc)
5984 vungetc(nc);
5985 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5986 return cc;
5987}
5988
5989/*
5990 * Insert character, taking care of special keys and mod_mask
5991 */
5992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993insert_special(
5994 int c,
5995 int allow_modmask,
5996 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 char_u *p;
5999 int len;
6000
6001 /*
6002 * Special function key, translate into "<Key>". Up to the last '>' is
6003 * inserted with ins_str(), so as not to replace characters in replace
6004 * mode.
6005 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6006 * unless 'allow_modmask' is TRUE.
6007 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006008#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 /* Command-key never produces a normal key */
6010 if (mod_mask & MOD_MASK_CMD)
6011 allow_modmask = TRUE;
6012#endif
6013 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6014 {
6015 p = get_special_key_name(c, mod_mask);
6016 len = (int)STRLEN(p);
6017 c = p[len - 1];
6018 if (len > 2)
6019 {
6020 if (stop_arrow() == FAIL)
6021 return;
6022 p[len - 1] = NUL;
6023 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006024 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 ctrlv = FALSE;
6026 }
6027 }
6028 if (stop_arrow() == OK)
6029 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6030}
6031
6032/*
6033 * Special characters in this context are those that need processing other
6034 * than the simple insertion that can be performed here. This includes ESC
6035 * which terminates the insert, and CR/NL which need special processing to
6036 * open up a new line. This routine tries to optimize insertions performed by
6037 * the "redo", "undo" or "put" commands, so it needs to know when it should
6038 * stop and defer processing to the "normal" mechanism.
6039 * '0' and '^' are special, because they can be followed by CTRL-D.
6040 */
6041#ifdef EBCDIC
6042# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6043#else
6044# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6045#endif
6046
6047#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006048# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006050# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif
6052
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006053/*
6054 * "flags": INSCHAR_FORMAT - force formatting
6055 * INSCHAR_CTRLV - char typed just after CTRL-V
6056 * INSCHAR_NO_FEX - don't use 'formatexpr'
6057 *
6058 * NOTE: passes the flags value straight through to internal_format() which,
6059 * beside INSCHAR_FORMAT (above), is also looking for these:
6060 * INSCHAR_DO_COM - format comments
6061 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064insertchar(
6065 int c, /* character to insert or NUL */
6066 int flags, /* INSCHAR_FORMAT, etc. */
6067 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 int textwidth;
6070#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006074 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006076 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078
6079 /*
6080 * Try to break the line in two or more pieces when:
6081 * - Always do this if we have been called to do formatting only.
6082 * - Always do this when 'formatoptions' has the 'a' flag and the line
6083 * ends in white space.
6084 * - Otherwise:
6085 * - Don't do this if inserting a blank
6086 * - Don't do this if an existing character is being replaced, unless
6087 * we're in VREPLACE mode.
6088 * - Do this if the cursor is not on the line where insert started
6089 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6090 * before the insert.
6091 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6092 * before 'textwidth'
6093 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006094 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006095 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006096 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 && !((State & REPLACE_FLAG)
6098#ifdef FEAT_VREPLACE
6099 && !(State & VREPLACE_FLAG)
6100#endif
6101 && *ml_get_cursor() != NUL)
6102 && (curwin->w_cursor.lnum != Insstart.lnum
6103 || ((!has_format_option(FO_INS_LONG)
6104 || Insstart_textlen <= (colnr_T)textwidth)
6105 && (!fo_ins_blank
6106 || Insstart_blank_vcol <= (colnr_T)textwidth
6107 ))))))
6108 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006109 /* Format with 'formatexpr' when it's set. Use internal formatting
6110 * when 'formatexpr' isn't set or it returns non-zero. */
6111#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006112 int do_internal = TRUE;
6113 colnr_T virtcol = get_nolist_virtcol()
6114 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006115
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006116 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6117 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006118 {
6119 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6120 /* It may be required to save for undo again, e.g. when setline()
6121 * was called. */
6122 ins_need_undo = TRUE;
6123 }
6124 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006126 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (c == NUL) /* only formatting was wanted */
6130 return;
6131
6132#ifdef FEAT_COMMENTS
6133 /* Check whether this character should end a comment. */
6134 if (did_ai && (int)c == end_comment_pending)
6135 {
6136 char_u *line;
6137 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6138 int middle_len, end_len;
6139 int i;
6140
6141 /*
6142 * Need to remove existing (middle) comment leader and insert end
6143 * comment leader. First, check what comment leader we can find.
6144 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006145 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6147 {
6148 /* Skip middle-comment string */
6149 while (*p && p[-1] != ':') /* find end of middle flags */
6150 ++p;
6151 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6152 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006153 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 --middle_len;
6155
6156 /* Find the end-comment string */
6157 while (*p && p[-1] != ':') /* find end of end flags */
6158 ++p;
6159 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6160
6161 /* Skip white space before the cursor */
6162 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006163 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 ;
6165 i++;
6166
6167 /* Skip to before the middle leader */
6168 i -= middle_len;
6169
6170 /* Check some expected things before we go on */
6171 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6172 {
6173 /* Backspace over all the stuff we want to replace */
6174 backspace_until_column(i);
6175
6176 /*
6177 * Insert the end-comment string, except for the last
6178 * character, which will get inserted as normal later.
6179 */
6180 ins_bytes_len(lead_end, end_len - 1);
6181 }
6182 }
6183 }
6184 end_comment_pending = NUL;
6185#endif
6186
6187 did_ai = FALSE;
6188#ifdef FEAT_SMARTINDENT
6189 did_si = FALSE;
6190 can_si = FALSE;
6191 can_si_back = FALSE;
6192#endif
6193
6194 /*
6195 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6196 * This speeds up normal text input considerably.
6197 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6198 * need to re-indent at a ':', or any other character (but not what
6199 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006200 * Don't do this when there an InsertCharPre autocommand is defined,
6201 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 */
6203#ifdef USE_ON_FLY_SCROLL
6204 dont_scroll = FALSE; /* allow scrolling here */
6205#endif
6206
6207 if ( !ISSPECIAL(c)
6208#ifdef FEAT_MBYTE
6209 && (!has_mbyte || (*mb_char2len)(c) == 1)
6210#endif
6211 && vpeekc() != NUL
6212 && !(State & REPLACE_FLAG)
6213#ifdef FEAT_CINDENT
6214 && !cindent_on()
6215#endif
6216#ifdef FEAT_RIGHTLEFT
6217 && !p_ri
6218#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006219#ifdef FEAT_AUTOCMD
6220 && !has_insertcharpre()
6221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 )
6223 {
6224#define INPUT_BUFLEN 100
6225 char_u buf[INPUT_BUFLEN + 1];
6226 int i;
6227 colnr_T virtcol = 0;
6228
6229 buf[0] = c;
6230 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006231 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 virtcol = get_nolist_virtcol();
6233 /*
6234 * Stop the string when:
6235 * - no more chars available
6236 * - finding a special character (command key)
6237 * - buffer is full
6238 * - running into the 'textwidth' boundary
6239 * - need to check for abbreviation: A non-word char after a word-char
6240 */
6241 while ( (c = vpeekc()) != NUL
6242 && !ISSPECIAL(c)
6243#ifdef FEAT_MBYTE
6244 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6245#endif
6246 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006247# ifdef FEAT_FKMAP
6248 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 && (textwidth == 0
6251 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6252 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6253 {
6254#ifdef FEAT_RIGHTLEFT
6255 c = vgetc();
6256 if (p_hkmap && KeyTyped)
6257 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 buf[i++] = c;
6259#else
6260 buf[i++] = vgetc();
6261#endif
6262 }
6263
6264#ifdef FEAT_DIGRAPHS
6265 do_digraph(-1); /* clear digraphs */
6266 do_digraph(buf[i-1]); /* may be the start of a digraph */
6267#endif
6268 buf[i] = NUL;
6269 ins_str(buf);
6270 if (flags & INSCHAR_CTRLV)
6271 {
6272 redo_literal(*buf);
6273 i = 1;
6274 }
6275 else
6276 i = 0;
6277 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006278 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 else
6281 {
6282#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006283 int cc;
6284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6286 {
6287 char_u buf[MB_MAXBYTES + 1];
6288
6289 (*mb_char2bytes)(c, buf);
6290 buf[cc] = NUL;
6291 ins_char_bytes(buf, cc);
6292 AppendCharToRedobuff(c);
6293 }
6294 else
6295#endif
6296 {
6297 ins_char(c);
6298 if (flags & INSCHAR_CTRLV)
6299 redo_literal(c);
6300 else
6301 AppendCharToRedobuff(c);
6302 }
6303 }
6304}
6305
6306/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006307 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006308 *
6309 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6310 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006311 */
6312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006313internal_format(
6314 int textwidth,
6315 int second_indent,
6316 int flags,
6317 int format_only,
6318 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319{
6320 int cc;
6321 int save_char = NUL;
6322 int haveto_redraw = FALSE;
6323 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6324#ifdef FEAT_MBYTE
6325 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6326#endif
6327 int fo_white_par = has_format_option(FO_WHITE_PAR);
6328 int first_line = TRUE;
6329#ifdef FEAT_COMMENTS
6330 colnr_T leader_len;
6331 int no_leader = FALSE;
6332 int do_comments = (flags & INSCHAR_DO_COM);
6333#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006334#ifdef FEAT_LINEBREAK
6335 int has_lbr = curwin->w_p_lbr;
6336
6337 /* make sure win_lbr_chartabsize() counts correctly */
6338 curwin->w_p_lbr = FALSE;
6339#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340
6341 /*
6342 * When 'ai' is off we don't want a space under the cursor to be
6343 * deleted. Replace it with an 'x' temporarily.
6344 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006345 if (!curbuf->b_p_ai
6346#ifdef FEAT_VREPLACE
6347 && !(State & VREPLACE_FLAG)
6348#endif
6349 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350 {
6351 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006352 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006353 {
6354 save_char = cc;
6355 pchar_cursor('x');
6356 }
6357 }
6358
6359 /*
6360 * Repeat breaking lines, until the current line is not too long.
6361 */
6362 while (!got_int)
6363 {
6364 int startcol; /* Cursor column at entry */
6365 int wantcol; /* column at textwidth border */
6366 int foundcol; /* column for start of spaces */
6367 int end_foundcol = 0; /* column for start of word */
6368 colnr_T len;
6369 colnr_T virtcol;
6370#ifdef FEAT_VREPLACE
6371 int orig_col = 0;
6372 char_u *saved_text = NULL;
6373#endif
6374 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006375 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376
Bram Moolenaar97b98102009-11-17 16:41:01 +00006377 virtcol = get_nolist_virtcol()
6378 + char2cells(c != NUL ? c : gchar_cursor());
6379 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380 break;
6381
6382#ifdef FEAT_COMMENTS
6383 if (no_leader)
6384 do_comments = FALSE;
6385 else if (!(flags & INSCHAR_FORMAT)
6386 && has_format_option(FO_WRAP_COMS))
6387 do_comments = TRUE;
6388
6389 /* Don't break until after the comment leader */
6390 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006391 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 else
6393 leader_len = 0;
6394
6395 /* If the line doesn't start with a comment leader, then don't
6396 * start one in a following broken line. Avoids that a %word
6397 * moved to the start of the next line causes all following lines
6398 * to start with %. */
6399 if (leader_len == 0)
6400 no_leader = TRUE;
6401#endif
6402 if (!(flags & INSCHAR_FORMAT)
6403#ifdef FEAT_COMMENTS
6404 && leader_len == 0
6405#endif
6406 && !has_format_option(FO_WRAP))
6407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409 if ((startcol = curwin->w_cursor.col) == 0)
6410 break;
6411
6412 /* find column of textwidth border */
6413 coladvance((colnr_T)textwidth);
6414 wantcol = curwin->w_cursor.col;
6415
Bram Moolenaar97b98102009-11-17 16:41:01 +00006416 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006417 foundcol = 0;
6418
6419 /*
6420 * Find position to break at.
6421 * Stop at first entered white when 'formatoptions' has 'v'
6422 */
6423 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006424 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425 || curwin->w_cursor.lnum != Insstart.lnum
6426 || curwin->w_cursor.col >= Insstart.col)
6427 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006428 if (curwin->w_cursor.col == startcol && c != NUL)
6429 cc = c;
6430 else
6431 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006432 if (WHITECHAR(cc))
6433 {
6434 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006435 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436
6437 /* find start of sequence of blanks */
6438 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6439 {
6440 dec_cursor();
6441 cc = gchar_cursor();
6442 }
6443 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6444 break; /* only spaces in front of text */
6445#ifdef FEAT_COMMENTS
6446 /* Don't break until after the comment leader */
6447 if (curwin->w_cursor.col < leader_len)
6448 break;
6449#endif
6450 if (has_format_option(FO_ONE_LETTER))
6451 {
6452 /* do not break after one-letter words */
6453 if (curwin->w_cursor.col == 0)
6454 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006455#ifdef FEAT_COMMENTS
6456 /* do not break "#a b" when 'tw' is 2 */
6457 if (curwin->w_cursor.col <= leader_len)
6458 break;
6459#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 col = curwin->w_cursor.col;
6461 dec_cursor();
6462 cc = gchar_cursor();
6463
6464 if (WHITECHAR(cc))
6465 continue; /* one-letter, continue */
6466 curwin->w_cursor.col = col;
6467 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006468
6469 inc_cursor();
6470
6471 end_foundcol = end_col + 1;
6472 foundcol = curwin->w_cursor.col;
6473 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006474 break;
6475 }
6476#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006477 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006478 {
6479 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006480 if (curwin->w_cursor.col != startcol)
6481 {
6482#ifdef FEAT_COMMENTS
6483 /* Don't break until after the comment leader */
6484 if (curwin->w_cursor.col < leader_len)
6485 break;
6486#endif
6487 col = curwin->w_cursor.col;
6488 inc_cursor();
6489 /* Don't change end_foundcol if already set. */
6490 if (foundcol != curwin->w_cursor.col)
6491 {
6492 foundcol = curwin->w_cursor.col;
6493 end_foundcol = foundcol;
6494 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6495 break;
6496 }
6497 curwin->w_cursor.col = col;
6498 }
6499
6500 if (curwin->w_cursor.col == 0)
6501 break;
6502
6503 col = curwin->w_cursor.col;
6504
6505 dec_cursor();
6506 cc = gchar_cursor();
6507
6508 if (WHITECHAR(cc))
6509 continue; /* break with space */
6510#ifdef FEAT_COMMENTS
6511 /* Don't break until after the comment leader */
6512 if (curwin->w_cursor.col < leader_len)
6513 break;
6514#endif
6515
6516 curwin->w_cursor.col = col;
6517
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006520 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6521 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522 }
6523#endif
6524 if (curwin->w_cursor.col == 0)
6525 break;
6526 dec_cursor();
6527 }
6528
6529 if (foundcol == 0) /* no spaces, cannot break line */
6530 {
6531 curwin->w_cursor.col = startcol;
6532 break;
6533 }
6534
6535 /* Going to break the line, remove any "$" now. */
6536 undisplay_dollar();
6537
6538 /*
6539 * Offset between cursor position and line break is used by replace
6540 * stack functions. VREPLACE does not use this, and backspaces
6541 * over the text instead.
6542 */
6543#ifdef FEAT_VREPLACE
6544 if (State & VREPLACE_FLAG)
6545 orig_col = startcol; /* Will start backspacing from here */
6546 else
6547#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006548 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006549
6550 /*
6551 * adjust startcol for spaces that will be deleted and
6552 * characters that will remain on top line
6553 */
6554 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006555 while ((cc = gchar_cursor(), WHITECHAR(cc))
6556 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557 inc_cursor();
6558 startcol -= curwin->w_cursor.col;
6559 if (startcol < 0)
6560 startcol = 0;
6561
6562#ifdef FEAT_VREPLACE
6563 if (State & VREPLACE_FLAG)
6564 {
6565 /*
6566 * In VREPLACE mode, we will backspace over the text to be
6567 * wrapped, so save a copy now to put on the next line.
6568 */
6569 saved_text = vim_strsave(ml_get_cursor());
6570 curwin->w_cursor.col = orig_col;
6571 if (saved_text == NULL)
6572 break; /* Can't do it, out of memory */
6573 saved_text[startcol] = NUL;
6574
6575 /* Backspace over characters that will move to the next line */
6576 if (!fo_white_par)
6577 backspace_until_column(foundcol);
6578 }
6579 else
6580#endif
6581 {
6582 /* put cursor after pos. to break line */
6583 if (!fo_white_par)
6584 curwin->w_cursor.col = foundcol;
6585 }
6586
6587 /*
6588 * Split the line just before the margin.
6589 * Only insert/delete lines, but don't really redraw the window.
6590 */
6591 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6592 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6593#ifdef FEAT_COMMENTS
6594 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006595 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006596#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006597 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6598 if (!(flags & INSCHAR_COM_LIST))
6599 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006600
6601 replace_offset = 0;
6602 if (first_line)
6603 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006604 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006605 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006606 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006607 * This section is for auto-wrap of numeric lists. When not
6608 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6609 * flag will be set and open_line() will handle it (as seen
6610 * above). The code here (and in get_number_indent()) will
6611 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006612 */
6613 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006614 second_indent =
6615 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006616 if (second_indent >= 0)
6617 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006618#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006619 if (State & VREPLACE_FLAG)
6620 change_indent(INDENT_SET, second_indent,
6621 FALSE, NUL, TRUE);
6622 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006623#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006624#ifdef FEAT_COMMENTS
6625 if (leader_len > 0 && second_indent - leader_len > 0)
6626 {
6627 int i;
6628 int padding = second_indent - leader_len;
6629
6630 /* We started at the first_line of a numbered list
6631 * that has a comment. the open_line() function has
6632 * inserted the proper comment leader and positioned
6633 * the cursor at the end of the split line. Now we
6634 * add the additional whitespace needed after the
6635 * comment leader for the numbered list. */
6636 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006637 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006638 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006639 }
6640 else
6641 {
6642#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006643 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006644#ifdef FEAT_COMMENTS
6645 }
6646#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006647 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006648 }
6649 first_line = FALSE;
6650 }
6651
6652#ifdef FEAT_VREPLACE
6653 if (State & VREPLACE_FLAG)
6654 {
6655 /*
6656 * In VREPLACE mode we have backspaced over the text to be
6657 * moved, now we re-insert it into the new line.
6658 */
6659 ins_bytes(saved_text);
6660 vim_free(saved_text);
6661 }
6662 else
6663#endif
6664 {
6665 /*
6666 * Check if cursor is not past the NUL off the line, cindent
6667 * may have added or removed indent.
6668 */
6669 curwin->w_cursor.col += startcol;
6670 len = (colnr_T)STRLEN(ml_get_curline());
6671 if (curwin->w_cursor.col > len)
6672 curwin->w_cursor.col = len;
6673 }
6674
6675 haveto_redraw = TRUE;
6676#ifdef FEAT_CINDENT
6677 can_cindent = TRUE;
6678#endif
6679 /* moved the cursor, don't autoindent or cindent now */
6680 did_ai = FALSE;
6681#ifdef FEAT_SMARTINDENT
6682 did_si = FALSE;
6683 can_si = FALSE;
6684 can_si_back = FALSE;
6685#endif
6686 line_breakcheck();
6687 }
6688
6689 if (save_char != NUL) /* put back space after cursor */
6690 pchar_cursor(save_char);
6691
Bram Moolenaar0026d472014-09-09 16:32:39 +02006692#ifdef FEAT_LINEBREAK
6693 curwin->w_p_lbr = has_lbr;
6694#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006695 if (!format_only && haveto_redraw)
6696 {
6697 update_topline();
6698 redraw_curbuf_later(VALID);
6699 }
6700}
6701
6702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 * Called after inserting or deleting text: When 'formatoptions' includes the
6704 * 'a' flag format from the current line until the end of the paragraph.
6705 * Keep the cursor at the same position relative to the text.
6706 * The caller must have saved the cursor line for undo, following ones will be
6707 * saved here.
6708 */
6709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710auto_format(
6711 int trailblank, /* when TRUE also format with trailing blank */
6712 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 pos_T pos;
6715 colnr_T len;
6716 char_u *old;
6717 char_u *new, *pnew;
6718 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006719 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 if (!has_format_option(FO_AUTO))
6722 return;
6723
6724 pos = curwin->w_cursor;
6725 old = ml_get_curline();
6726
6727 /* may remove added space */
6728 check_auto_format(FALSE);
6729
6730 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6731 * user might insert normal text next. Also skip formatting when "1" is
6732 * in 'formatoptions' and there is a single character before the cursor.
6733 * Otherwise the line would be broken and when typing another non-white
6734 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006735 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (*old != NUL && !trailblank && wasatend)
6737 {
6738 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006739 cc = gchar_cursor();
6740 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6741 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006743 cc = gchar_cursor();
6744 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {
6746 curwin->w_cursor = pos;
6747 return;
6748 }
6749 curwin->w_cursor = pos;
6750 }
6751
6752#ifdef FEAT_COMMENTS
6753 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6754 * comments. */
6755 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006756 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 return;
6758#endif
6759
6760 /*
6761 * May start formatting in a previous line, so that after "x" a word is
6762 * moved to the previous line if it fits there now. Only when this is not
6763 * the start of a paragraph.
6764 */
6765 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6766 {
6767 --curwin->w_cursor.lnum;
6768 if (u_save_cursor() == FAIL)
6769 return;
6770 }
6771
6772 /*
6773 * Do the formatting and restore the cursor position. "saved_cursor" will
6774 * be adjusted for the text formatting.
6775 */
6776 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006777 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 curwin->w_cursor = saved_cursor;
6779 saved_cursor.lnum = 0;
6780
6781 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6782 {
6783 /* "cannot happen" */
6784 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6785 coladvance((colnr_T)MAXCOL);
6786 }
6787 else
6788 check_cursor_col();
6789
6790 /* Insert mode: If the cursor is now after the end of the line while it
6791 * previously wasn't, the line was broken. Because of the rule above we
6792 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6793 * formatted. */
6794 if (!wasatend && has_format_option(FO_WHITE_PAR))
6795 {
6796 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006797 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (curwin->w_cursor.col == len)
6799 {
6800 pnew = vim_strnsave(new, len + 2);
6801 pnew[len] = ' ';
6802 pnew[len + 1] = NUL;
6803 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6804 /* remove the space later */
6805 did_add_space = TRUE;
6806 }
6807 else
6808 /* may remove added space */
6809 check_auto_format(FALSE);
6810 }
6811
6812 check_cursor();
6813}
6814
6815/*
6816 * When an extra space was added to continue a paragraph for auto-formatting,
6817 * delete it now. The space must be under the cursor, just after the insert
6818 * position.
6819 */
6820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821check_auto_format(
6822 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006825 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827 if (did_add_space)
6828 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006829 cc = gchar_cursor();
6830 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 /* Somehow the space was removed already. */
6832 did_add_space = FALSE;
6833 else
6834 {
6835 if (!end_insert)
6836 {
6837 inc_cursor();
6838 c = gchar_cursor();
6839 dec_cursor();
6840 }
6841 if (c != NUL)
6842 {
6843 /* The space is no longer at the end of the line, delete it. */
6844 del_char(FALSE);
6845 did_add_space = FALSE;
6846 }
6847 }
6848 }
6849}
6850
6851/*
6852 * Find out textwidth to be used for formatting:
6853 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006854 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 * if invalid value, use 0.
6856 * Set default to window width (maximum 79) for "gq" operator.
6857 */
6858 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006859comp_textwidth(
6860 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int textwidth;
6863
6864 textwidth = curbuf->b_p_tw;
6865 if (textwidth == 0 && curbuf->b_p_wm)
6866 {
6867 /* The width is the window width minus 'wrapmargin' minus all the
6868 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006869 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870#ifdef FEAT_CMDWIN
6871 if (cmdwin_type != 0)
6872 textwidth -= 1;
6873#endif
6874#ifdef FEAT_FOLDING
6875 textwidth -= curwin->w_p_fdc;
6876#endif
6877#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006878 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 textwidth -= 1;
6880#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006881 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 textwidth -= 8;
6883 }
6884 if (textwidth < 0)
6885 textwidth = 0;
6886 if (ff && textwidth == 0)
6887 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006888 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 if (textwidth > 79)
6890 textwidth = 79;
6891 }
6892 return textwidth;
6893}
6894
6895/*
6896 * Put a character in the redo buffer, for when just after a CTRL-V.
6897 */
6898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006899redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
6901 char_u buf[10];
6902
6903 /* Only digits need special treatment. Translate them into a string of
6904 * three digits. */
6905 if (VIM_ISDIGIT(c))
6906 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006907 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 AppendToRedobuff(buf);
6909 }
6910 else
6911 AppendCharToRedobuff(c);
6912}
6913
6914/*
6915 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006916 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 */
6918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919start_arrow(
6920 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006922 start_arrow_common(end_insert_pos, TRUE);
6923}
6924
6925/*
6926 * Like start_arrow() but with end_change argument.
6927 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6928 */
6929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006930start_arrow_with_change(
6931 pos_T *end_insert_pos, /* can be NULL */
6932 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006933{
6934 start_arrow_common(end_insert_pos, end_change);
6935 if (!end_change)
6936 {
6937 AppendCharToRedobuff(Ctrl_G);
6938 AppendCharToRedobuff('U');
6939 }
6940}
6941
6942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006943start_arrow_common(
6944 pos_T *end_insert_pos, /* can be NULL */
6945 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006946{
6947 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
6949 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006950 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 arrow_used = TRUE; /* this means we stopped the current insert */
6952 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006953#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006954 check_spell_redraw();
6955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956}
6957
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006958#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006959/*
6960 * If we skipped highlighting word at cursor, do it now.
6961 * It may be skipped again, thus reset spell_redraw_lnum first.
6962 */
6963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006964check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006965{
6966 if (spell_redraw_lnum != 0)
6967 {
6968 linenr_T lnum = spell_redraw_lnum;
6969
6970 spell_redraw_lnum = 0;
6971 redrawWinline(lnum, FALSE);
6972 }
6973}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006974
6975/*
6976 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6977 * spelled word, if there is one.
6978 */
6979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006980spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006981{
6982 pos_T tpos = curwin->w_cursor;
6983
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006984 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006985 if (curwin->w_cursor.col != tpos.col)
6986 start_arrow(&tpos);
6987}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006988#endif
6989
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990/*
6991 * stop_arrow() is called before a change is made in insert mode.
6992 * If an arrow key has been used, start a new insertion.
6993 * Returns FAIL if undo is impossible, shouldn't insert then.
6994 */
6995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006996stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 if (arrow_used)
6999 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007000 Insstart = curwin->w_cursor; /* new insertion starts here */
7001 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7002 /* Don't update the original insert position when moved to the
7003 * right, except when nothing was inserted yet. */
7004 update_Insstart_orig = FALSE;
7005 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (u_save_cursor() == OK)
7008 {
7009 arrow_used = FALSE;
7010 ins_need_undo = FALSE;
7011 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 ai_col = 0;
7014#ifdef FEAT_VREPLACE
7015 if (State & VREPLACE_FLAG)
7016 {
7017 orig_line_count = curbuf->b_ml.ml_line_count;
7018 vr_lines_changed = 1;
7019 }
7020#endif
7021 ResetRedobuff();
7022 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007023 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
7025 else if (ins_need_undo)
7026 {
7027 if (u_save_cursor() == OK)
7028 ins_need_undo = FALSE;
7029 }
7030
7031#ifdef FEAT_FOLDING
7032 /* Always open fold at the cursor line when inserting something. */
7033 foldOpenCursor();
7034#endif
7035
7036 return (arrow_used || ins_need_undo ? FAIL : OK);
7037}
7038
7039/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007040 * Do a few things to stop inserting.
7041 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7042 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 */
7044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045stop_insert(
7046 pos_T *end_insert_pos,
7047 int esc, /* called by ins_esc() */
7048 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007050 int cc;
7051 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
7053 stop_redo_ins();
7054 replace_flush(); /* abandon replace stack */
7055
7056 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007057 * Save the inserted text for later redo with ^@ and CTRL-A.
7058 * Don't do it when "restart_edit" was set and nothing was inserted,
7059 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007061 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007062 if (did_restart_edit == 0 || (ptr != NULL
7063 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007064 {
7065 vim_free(last_insert);
7066 last_insert = ptr;
7067 last_insert_skip = new_insert_skip;
7068 }
7069 else
7070 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007072 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
7074 /* Auto-format now. It may seem strange to do this when stopping an
7075 * insertion (or moving the cursor), but it's required when appending
7076 * a line and having it end in a space. But only do it when something
7077 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007078 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007080 pos_T tpos = curwin->w_cursor;
7081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 /* When the cursor is at the end of the line after a space the
7083 * formatting will move it to the following word. Avoid that by
7084 * moving the cursor onto the space. */
7085 cc = 'x';
7086 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7087 {
7088 dec_cursor();
7089 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007090 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007091 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093
7094 auto_format(TRUE, FALSE);
7095
Bram Moolenaar1c465442017-03-12 20:10:05 +01007096 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007097 {
7098 if (gchar_cursor() != NUL)
7099 inc_cursor();
7100#ifdef FEAT_VIRTUALEDIT
7101 /* If the cursor is still at the same character, also keep
7102 * the "coladd". */
7103 if (gchar_cursor() == NUL
7104 && curwin->w_cursor.lnum == tpos.lnum
7105 && curwin->w_cursor.col == tpos.col)
7106 curwin->w_cursor.coladd = tpos.coladd;
7107#endif
7108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110
7111 /* If a space was inserted for auto-formatting, remove it now. */
7112 check_auto_format(TRUE);
7113
7114 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007115 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007116 * Do this when ESC was used or moving the cursor up/down.
7117 * Check for the old position still being valid, just in case the text
7118 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007119 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007120 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7121 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007123 pos_T tpos = curwin->w_cursor;
7124
7125 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007126 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007127 for (;;)
7128 {
7129 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7130 --curwin->w_cursor.col;
7131 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007132 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007133 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007134 if (del_char(TRUE) == FAIL)
7135 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007136 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007137 if (curwin->w_cursor.lnum != tpos.lnum)
7138 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007139 else
7140 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007141 /* reset tpos, could have been invalidated in the loop above */
7142 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007143 tpos.col++;
7144 if (cc != NUL && gchar_pos(&tpos) == NUL)
7145 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 /* <C-S-Right> may have started Visual mode, adjust the position for
7149 * deleted characters. */
7150 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7151 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007152 int len = (int)STRLEN(ml_get_curline());
7153
7154 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007156 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007157#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
7161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 }
7163 }
7164 did_ai = FALSE;
7165#ifdef FEAT_SMARTINDENT
7166 did_si = FALSE;
7167 can_si = FALSE;
7168 can_si_back = FALSE;
7169#endif
7170
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007171 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7172 * now in a different buffer. */
7173 if (end_insert_pos != NULL)
7174 {
7175 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007176 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007177 curbuf->b_op_end = *end_insert_pos;
7178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
7181/*
7182 * Set the last inserted text to a single character.
7183 * Used for the replace command.
7184 */
7185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007186set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 char_u *s;
7189
7190 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (last_insert != NULL)
7193 {
7194 s = last_insert;
7195 /* Use the CTRL-V only when entering a special char */
7196 if (c < ' ' || c == DEL)
7197 *s++ = Ctrl_V;
7198 s = add_char2buf(c, s);
7199 *s++ = ESC;
7200 *s++ = NUL;
7201 last_insert_skip = 0;
7202 }
7203}
7204
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007205#if defined(EXITFREE) || defined(PROTO)
7206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007208{
7209 vim_free(last_insert);
7210 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007211# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007212 vim_free(compl_orig_text);
7213 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007214# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007215}
7216#endif
7217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218/*
7219 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7220 * and CSI. Handle multi-byte characters.
7221 * Returns a pointer to after the added bytes.
7222 */
7223 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007224add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007227 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 int i;
7229 int len;
7230
7231 len = (*mb_char2bytes)(c, temp);
7232 for (i = 0; i < len; ++i)
7233 {
7234 c = temp[i];
7235#endif
7236 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7237 if (c == K_SPECIAL)
7238 {
7239 *s++ = K_SPECIAL;
7240 *s++ = KS_SPECIAL;
7241 *s++ = KE_FILLER;
7242 }
7243#ifdef FEAT_GUI
7244 else if (c == CSI)
7245 {
7246 *s++ = CSI;
7247 *s++ = KS_EXTRA;
7248 *s++ = (int)KE_CSI;
7249 }
7250#endif
7251 else
7252 *s++ = c;
7253#ifdef FEAT_MBYTE
7254 }
7255#endif
7256 return s;
7257}
7258
7259/*
7260 * move cursor to start of line
7261 * if flags & BL_WHITE move to first non-white
7262 * if flags & BL_SOL move to first non-white if startofline is set,
7263 * otherwise keep "curswant" column
7264 * if flags & BL_FIX don't leave the cursor on a NUL.
7265 */
7266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 if ((flags & BL_SOL) && !p_sol)
7270 coladvance(curwin->w_curswant);
7271 else
7272 {
7273 curwin->w_cursor.col = 0;
7274#ifdef FEAT_VIRTUALEDIT
7275 curwin->w_cursor.coladd = 0;
7276#endif
7277
7278 if (flags & (BL_WHITE | BL_SOL))
7279 {
7280 char_u *ptr;
7281
Bram Moolenaar1c465442017-03-12 20:10:05 +01007282 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7284 ++curwin->w_cursor.col;
7285 }
7286 curwin->w_set_curswant = TRUE;
7287 }
7288}
7289
7290/*
7291 * oneright oneleft cursor_down cursor_up
7292 *
7293 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007294 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 * Return OK when successful, FAIL when we hit a line of file boundary.
7296 */
7297
7298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304#ifdef FEAT_VIRTUALEDIT
7305 if (virtual_active())
7306 {
7307 pos_T prevpos = curwin->w_cursor;
7308
7309 /* Adjust for multi-wide char (excluding TAB) */
7310 ptr = ml_get_cursor();
7311 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007312# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007314# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 ))
7318 ? ptr2cells(ptr) : 1));
7319 curwin->w_set_curswant = TRUE;
7320 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7321 return (prevpos.col != curwin->w_cursor.col
7322 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7323 }
7324#endif
7325
7326 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007327 if (*ptr == NUL)
7328 return FAIL; /* already at the very end */
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007331 if (has_mbyte)
7332 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 else
7334#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007335 l = 1;
7336
7337 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7338 * contains "onemore". */
7339 if (ptr[l] == NUL
7340#ifdef FEAT_VIRTUALEDIT
7341 && (ve_flags & VE_ONEMORE) == 0
7342#endif
7343 )
7344 return FAIL;
7345 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
7347 curwin->w_set_curswant = TRUE;
7348 return OK;
7349}
7350
7351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007352oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
7354#ifdef FEAT_VIRTUALEDIT
7355 if (virtual_active())
7356 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007357# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 int v = getviscol();
7361
7362 if (v == 0)
7363 return FAIL;
7364
7365# ifdef FEAT_LINEBREAK
7366 /* We might get stuck on 'showbreak', skip over it. */
7367 width = 1;
7368 for (;;)
7369 {
7370 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007371 /* getviscol() is slow, skip it when 'showbreak' is empty,
7372 * 'breakindent' is not set and there are no multi-byte
7373 * characters */
7374 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375# ifdef FEAT_MBYTE
7376 && !has_mbyte
7377# endif
7378 ) || getviscol() < v)
7379 break;
7380 ++width;
7381 }
7382# else
7383 coladvance(v - 1);
7384# endif
7385
7386 if (curwin->w_cursor.coladd == 1)
7387 {
7388 char_u *ptr;
7389
7390 /* Adjust for multi-wide char (not a TAB) */
7391 ptr = ml_get_cursor();
7392 if (*ptr != TAB && vim_isprintc(
7393# ifdef FEAT_MBYTE
7394 (*mb_ptr2char)(ptr)
7395# else
7396 *ptr
7397# endif
7398 ) && ptr2cells(ptr) > 1)
7399 curwin->w_cursor.coladd = 0;
7400 }
7401
7402 curwin->w_set_curswant = TRUE;
7403 return OK;
7404 }
7405#endif
7406
7407 if (curwin->w_cursor.col == 0)
7408 return FAIL;
7409
7410 curwin->w_set_curswant = TRUE;
7411 --curwin->w_cursor.col;
7412
7413#ifdef FEAT_MBYTE
7414 /* if the character on the left of the current cursor is a multi-byte
7415 * character, move to its first byte */
7416 if (has_mbyte)
7417 mb_adjust_cursor();
7418#endif
7419 return OK;
7420}
7421
7422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423cursor_up(
7424 long n,
7425 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
7427 linenr_T lnum;
7428
7429 if (n > 0)
7430 {
7431 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007432 /* This fails if the cursor is already in the first line or the count
7433 * is larger than the line number and '-' is in 'cpoptions' */
7434 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 return FAIL;
7436 if (n >= lnum)
7437 lnum = 1;
7438 else
7439#ifdef FEAT_FOLDING
7440 if (hasAnyFolding(curwin))
7441 {
7442 /*
7443 * Count each sequence of folded lines as one logical line.
7444 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007445 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 (void)hasFolding(lnum, &lnum, NULL);
7447
7448 while (n--)
7449 {
7450 /* move up one line */
7451 --lnum;
7452 if (lnum <= 1)
7453 break;
7454 /* If we entered a fold, move to the beginning, unless in
7455 * Insert mode or when 'foldopen' contains "all": it will open
7456 * in a moment. */
7457 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7458 (void)hasFolding(lnum, &lnum, NULL);
7459 }
7460 if (lnum < 1)
7461 lnum = 1;
7462 }
7463 else
7464#endif
7465 lnum -= n;
7466 curwin->w_cursor.lnum = lnum;
7467 }
7468
7469 /* try to advance to the column we want to be at */
7470 coladvance(curwin->w_curswant);
7471
7472 if (upd_topline)
7473 update_topline(); /* make sure curwin->w_topline is valid */
7474
7475 return OK;
7476}
7477
7478/*
7479 * Cursor down a number of logical lines.
7480 */
7481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482cursor_down(
7483 long n,
7484 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 linenr_T lnum;
7487
7488 if (n > 0)
7489 {
7490 lnum = curwin->w_cursor.lnum;
7491#ifdef FEAT_FOLDING
7492 /* Move to last line of fold, will fail if it's the end-of-file. */
7493 (void)hasFolding(lnum, NULL, &lnum);
7494#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007495 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007496 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007497 if (lnum >= curbuf->b_ml.ml_line_count
7498 || (lnum + n > curbuf->b_ml.ml_line_count
7499 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 return FAIL;
7501 if (lnum + n >= curbuf->b_ml.ml_line_count)
7502 lnum = curbuf->b_ml.ml_line_count;
7503 else
7504#ifdef FEAT_FOLDING
7505 if (hasAnyFolding(curwin))
7506 {
7507 linenr_T last;
7508
7509 /* count each sequence of folded lines as one logical line */
7510 while (n--)
7511 {
7512 if (hasFolding(lnum, NULL, &last))
7513 lnum = last + 1;
7514 else
7515 ++lnum;
7516 if (lnum >= curbuf->b_ml.ml_line_count)
7517 break;
7518 }
7519 if (lnum > curbuf->b_ml.ml_line_count)
7520 lnum = curbuf->b_ml.ml_line_count;
7521 }
7522 else
7523#endif
7524 lnum += n;
7525 curwin->w_cursor.lnum = lnum;
7526 }
7527
7528 /* try to advance to the column we want to be at */
7529 coladvance(curwin->w_curswant);
7530
7531 if (upd_topline)
7532 update_topline(); /* make sure curwin->w_topline is valid */
7533
7534 return OK;
7535}
7536
7537/*
7538 * Stuff the last inserted text in the read buffer.
7539 * Last_insert actually is a copy of the redo buffer, so we
7540 * first have to remove the command.
7541 */
7542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543stuff_inserted(
7544 int c, /* Command character to be inserted */
7545 long count, /* Repeat this many times */
7546 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
7548 char_u *esc_ptr;
7549 char_u *ptr;
7550 char_u *last_ptr;
7551 char_u last = NUL;
7552
7553 ptr = get_last_insert();
7554 if (ptr == NULL)
7555 {
7556 EMSG(_(e_noinstext));
7557 return FAIL;
7558 }
7559
7560 /* may want to stuff the command character, to start Insert mode */
7561 if (c != NUL)
7562 stuffcharReadbuff(c);
7563 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7564 *esc_ptr = NUL; /* remove the ESC */
7565
7566 /* when the last char is either "0" or "^" it will be quoted if no ESC
7567 * comes after it OR if it will inserted more than once and "ptr"
7568 * starts with ^D. -- Acevedo
7569 */
7570 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7571 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7572 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7573 {
7574 last = *last_ptr;
7575 *last_ptr = NUL;
7576 }
7577
7578 do
7579 {
7580 stuffReadbuff(ptr);
7581 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7582 if (last)
7583 stuffReadbuff((char_u *)(last == '0'
7584 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7585 : IF_EB("\026^", CTRL_V_STR "^")));
7586 }
7587 while (--count > 0);
7588
7589 if (last)
7590 *last_ptr = last;
7591
7592 if (esc_ptr != NULL)
7593 *esc_ptr = ESC; /* put the ESC back */
7594
7595 /* may want to stuff a trailing ESC, to get out of Insert mode */
7596 if (!no_esc)
7597 stuffcharReadbuff(ESC);
7598
7599 return OK;
7600}
7601
7602 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 if (last_insert == NULL)
7606 return NULL;
7607 return last_insert + last_insert_skip;
7608}
7609
7610/*
7611 * Get last inserted string, and remove trailing <Esc>.
7612 * Returns pointer to allocated memory (must be freed) or NULL.
7613 */
7614 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007615get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617 char_u *s;
7618 int len;
7619
7620 if (last_insert == NULL)
7621 return NULL;
7622 s = vim_strsave(last_insert + last_insert_skip);
7623 if (s != NULL)
7624 {
7625 len = (int)STRLEN(s);
7626 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7627 s[len - 1] = NUL;
7628 }
7629 return s;
7630}
7631
7632/*
7633 * Check the word in front of the cursor for an abbreviation.
7634 * Called when the non-id character "c" has been entered.
7635 * When an abbreviation is recognized it is removed from the text and
7636 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7637 */
7638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 /* Don't check for abbreviation in paste mode, when disabled and just
7642 * after moving around with cursor keys. */
7643 if (p_paste || no_abbr || arrow_used)
7644 return FALSE;
7645
7646 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7647 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7648}
7649
7650/*
7651 * replace-stack functions
7652 *
7653 * When replacing characters, the replaced characters are remembered for each
7654 * new character. This is used to re-insert the old text when backspacing.
7655 *
7656 * There is a NUL headed list of characters for each character that is
7657 * currently in the file after the insertion point. When BS is used, one NUL
7658 * headed list is put back for the deleted character.
7659 *
7660 * For a newline, there are two NUL headed lists. One contains the characters
7661 * that the NL replaced. The extra one stores the characters after the cursor
7662 * that were deleted (always white space).
7663 *
7664 * Replace_offset is normally 0, in which case replace_push will add a new
7665 * character at the end of the stack. If replace_offset is not 0, that many
7666 * characters will be left on the stack above the newly inserted character.
7667 */
7668
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007669static char_u *replace_stack = NULL;
7670static long replace_stack_nr = 0; /* next entry in replace stack */
7671static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007674replace_push(
7675 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 char_u *p;
7678
7679 if (replace_stack_nr < replace_offset) /* nothing to do */
7680 return;
7681 if (replace_stack_len <= replace_stack_nr)
7682 {
7683 replace_stack_len += 50;
7684 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7685 if (p == NULL) /* out of memory */
7686 {
7687 replace_stack_len -= 50;
7688 return;
7689 }
7690 if (replace_stack != NULL)
7691 {
7692 mch_memmove(p, replace_stack,
7693 (size_t)(replace_stack_nr * sizeof(char_u)));
7694 vim_free(replace_stack);
7695 }
7696 replace_stack = p;
7697 }
7698 p = replace_stack + replace_stack_nr - replace_offset;
7699 if (replace_offset)
7700 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7701 *p = c;
7702 ++replace_stack_nr;
7703}
7704
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007705#if defined(FEAT_MBYTE) || defined(PROTO)
7706/*
7707 * Push a character onto the replace stack. Handles a multi-byte character in
7708 * reverse byte order, so that the first byte is popped off first.
7709 * Return the number of bytes done (includes composing characters).
7710 */
7711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007713{
7714 int l = (*mb_ptr2len)(p);
7715 int j;
7716
7717 for (j = l - 1; j >= 0; --j)
7718 replace_push(p[j]);
7719 return l;
7720}
7721#endif
7722
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723/*
7724 * Pop one item from the replace stack.
7725 * return -1 if stack empty
7726 * return replaced character or NUL otherwise
7727 */
7728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007729replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 if (replace_stack_nr == 0)
7732 return -1;
7733 return (int)replace_stack[--replace_stack_nr];
7734}
7735
7736/*
7737 * Join the top two items on the replace stack. This removes to "off"'th NUL
7738 * encountered.
7739 */
7740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007741replace_join(
7742 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 int i;
7745
7746 for (i = replace_stack_nr; --i >= 0; )
7747 if (replace_stack[i] == NUL && off-- <= 0)
7748 {
7749 --replace_stack_nr;
7750 mch_memmove(replace_stack + i, replace_stack + i + 1,
7751 (size_t)(replace_stack_nr - i));
7752 return;
7753 }
7754}
7755
7756/*
7757 * Pop bytes from the replace stack until a NUL is found, and insert them
7758 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7759 */
7760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007761replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 int cc;
7764 int oldState = State;
7765
7766 State = NORMAL; /* don't want REPLACE here */
7767 while ((cc = replace_pop()) > 0)
7768 {
7769#ifdef FEAT_MBYTE
7770 mb_replace_pop_ins(cc);
7771#else
7772 ins_char(cc);
7773#endif
7774 dec_cursor();
7775 }
7776 State = oldState;
7777}
7778
7779#ifdef FEAT_MBYTE
7780/*
7781 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7782 * indicates a multi-byte char, pop the other bytes too.
7783 */
7784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007785mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007788 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 int i;
7790 int c;
7791
7792 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7793 {
7794 buf[0] = cc;
7795 for (i = 1; i < n; ++i)
7796 buf[i] = replace_pop();
7797 ins_bytes_len(buf, n);
7798 }
7799 else
7800 ins_char(cc);
7801
7802 if (enc_utf8)
7803 /* Handle composing chars. */
7804 for (;;)
7805 {
7806 c = replace_pop();
7807 if (c == -1) /* stack empty */
7808 break;
7809 if ((n = MB_BYTE2LEN(c)) == 1)
7810 {
7811 /* Not a multi-byte char, put it back. */
7812 replace_push(c);
7813 break;
7814 }
7815 else
7816 {
7817 buf[0] = c;
7818 for (i = 1; i < n; ++i)
7819 buf[i] = replace_pop();
7820 if (utf_iscomposing(utf_ptr2char(buf)))
7821 ins_bytes_len(buf, n);
7822 else
7823 {
7824 /* Not a composing char, put it back. */
7825 for (i = n - 1; i >= 0; --i)
7826 replace_push(buf[i]);
7827 break;
7828 }
7829 }
7830 }
7831}
7832#endif
7833
7834/*
7835 * make the replace stack empty
7836 * (called when exiting replace mode)
7837 */
7838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 vim_free(replace_stack);
7842 replace_stack = NULL;
7843 replace_stack_len = 0;
7844 replace_stack_nr = 0;
7845}
7846
7847/*
7848 * Handle doing a BS for one character.
7849 * cc < 0: replace stack empty, just move cursor
7850 * cc == 0: character was inserted, delete it
7851 * cc > 0: character was replaced, put cc (first byte of original char) back
7852 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007853 * When "limit_col" is >= 0, don't delete before this column. Matters when
7854 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 */
7856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007857replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 int cc;
7860#ifdef FEAT_VREPLACE
7861 int orig_len = 0;
7862 int ins_len;
7863 int orig_vcols = 0;
7864 colnr_T start_vcol;
7865 char_u *p;
7866 int i;
7867 int vcol;
7868#endif
7869
7870 cc = replace_pop();
7871 if (cc > 0)
7872 {
7873#ifdef FEAT_VREPLACE
7874 if (State & VREPLACE_FLAG)
7875 {
7876 /* Get the number of screen cells used by the character we are
7877 * going to delete. */
7878 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7879 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7880 }
7881#endif
7882#ifdef FEAT_MBYTE
7883 if (has_mbyte)
7884 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007885 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886# ifdef FEAT_VREPLACE
7887 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007888 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889# endif
7890 replace_push(cc);
7891 }
7892 else
7893#endif
7894 {
7895 pchar_cursor(cc);
7896#ifdef FEAT_VREPLACE
7897 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007898 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899#endif
7900 }
7901 replace_pop_ins();
7902
7903#ifdef FEAT_VREPLACE
7904 if (State & VREPLACE_FLAG)
7905 {
7906 /* Get the number of screen cells used by the inserted characters */
7907 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007908 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 vcol = start_vcol;
7910 for (i = 0; i < ins_len; ++i)
7911 {
7912 vcol += chartabsize(p + i, vcol);
7913#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007914 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915#endif
7916 }
7917 vcol -= start_vcol;
7918
7919 /* Delete spaces that were inserted after the cursor to keep the
7920 * text aligned. */
7921 curwin->w_cursor.col += ins_len;
7922 while (vcol > orig_vcols && gchar_cursor() == ' ')
7923 {
7924 del_char(FALSE);
7925 ++orig_vcols;
7926 }
7927 curwin->w_cursor.col -= ins_len;
7928 }
7929#endif
7930
7931 /* mark the buffer as changed and prepare for displaying */
7932 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7933 }
7934 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007935 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
7938#ifdef FEAT_CINDENT
7939/*
7940 * Return TRUE if C-indenting is on.
7941 */
7942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007943cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 return (!p_paste && (curbuf->b_p_cin
7946# ifdef FEAT_EVAL
7947 || *curbuf->b_p_inde != NUL
7948# endif
7949 ));
7950}
7951#endif
7952
7953#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7954/*
7955 * Re-indent the current line, based on the current contents of it and the
7956 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7957 * confused what all the part that handles Control-T is doing that I'm not.
7958 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7959 */
7960
7961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007962fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007964 int amount = get_the_indent();
7965
7966 if (amount >= 0)
7967 {
7968 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7969 if (linewhite(curwin->w_cursor.lnum))
7970 did_ai = TRUE; /* delete the indent if the line stays empty */
7971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972}
7973
7974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007975fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 if (p_paste)
7978 return;
7979# ifdef FEAT_LISP
7980 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7981 fixthisline(get_lisp_indent);
7982# endif
7983# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7984 else
7985# endif
7986# ifdef FEAT_CINDENT
7987 if (cindent_on())
7988 do_c_expr_indent();
7989# endif
7990}
7991
7992#endif
7993
7994#ifdef FEAT_CINDENT
7995/*
7996 * return TRUE if 'cinkeys' contains the key "keytyped",
7997 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007998 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8000 *
8001 * "keytyped" can have a few special values:
8002 * KEY_OPEN_FORW
8003 * KEY_OPEN_BACK
8004 * KEY_COMPLETE just finished completion.
8005 *
8006 * If line_is_empty is TRUE accept keys with '0' before them.
8007 */
8008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008009in_cinkeys(
8010 int keytyped,
8011 int when,
8012 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
8014 char_u *look;
8015 int try_match;
8016 int try_match_word;
8017 char_u *p;
8018 char_u *line;
8019 int icase;
8020 int i;
8021
Bram Moolenaar30848942009-12-24 14:46:12 +00008022 if (keytyped == NUL)
8023 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8024 return FALSE;
8025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026#ifdef FEAT_EVAL
8027 if (*curbuf->b_p_inde != NUL)
8028 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8029 else
8030#endif
8031 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8032 while (*look)
8033 {
8034 /*
8035 * Find out if we want to try a match with this key, depending on
8036 * 'when' and a '*' or '!' before the key.
8037 */
8038 switch (when)
8039 {
8040 case '*': try_match = (*look == '*'); break;
8041 case '!': try_match = (*look == '!'); break;
8042 default: try_match = (*look != '*'); break;
8043 }
8044 if (*look == '*' || *look == '!')
8045 ++look;
8046
8047 /*
8048 * If there is a '0', only accept a match if the line is empty.
8049 * But may still match when typing last char of a word.
8050 */
8051 if (*look == '0')
8052 {
8053 try_match_word = try_match;
8054 if (!line_is_empty)
8055 try_match = FALSE;
8056 ++look;
8057 }
8058 else
8059 try_match_word = FALSE;
8060
8061 /*
8062 * does it look like a control character?
8063 */
8064 if (*look == '^'
8065#ifdef EBCDIC
8066 && (Ctrl_chr(look[1]) != 0)
8067#else
8068 && look[1] >= '?' && look[1] <= '_'
8069#endif
8070 )
8071 {
8072 if (try_match && keytyped == Ctrl_chr(look[1]))
8073 return TRUE;
8074 look += 2;
8075 }
8076 /*
8077 * 'o' means "o" command, open forward.
8078 * 'O' means "O" command, open backward.
8079 */
8080 else if (*look == 'o')
8081 {
8082 if (try_match && keytyped == KEY_OPEN_FORW)
8083 return TRUE;
8084 ++look;
8085 }
8086 else if (*look == 'O')
8087 {
8088 if (try_match && keytyped == KEY_OPEN_BACK)
8089 return TRUE;
8090 ++look;
8091 }
8092
8093 /*
8094 * 'e' means to check for "else" at start of line and just before the
8095 * cursor.
8096 */
8097 else if (*look == 'e')
8098 {
8099 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8100 {
8101 p = ml_get_curline();
8102 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8103 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8104 return TRUE;
8105 }
8106 ++look;
8107 }
8108
8109 /*
8110 * ':' only causes an indent if it is at the end of a label or case
8111 * statement, or when it was before typing the ':' (to fix
8112 * class::method for C++).
8113 */
8114 else if (*look == ':')
8115 {
8116 if (try_match && keytyped == ':')
8117 {
8118 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008119 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008121 /* Need to get the line again after cin_islabel(). */
8122 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 if (curwin->w_cursor.col > 2
8124 && p[curwin->w_cursor.col - 1] == ':'
8125 && p[curwin->w_cursor.col - 2] == ':')
8126 {
8127 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008128 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008129 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 p = ml_get_curline();
8131 p[curwin->w_cursor.col - 1] = ':';
8132 if (i)
8133 return TRUE;
8134 }
8135 }
8136 ++look;
8137 }
8138
8139
8140 /*
8141 * Is it a key in <>, maybe?
8142 */
8143 else if (*look == '<')
8144 {
8145 if (try_match)
8146 {
8147 /*
8148 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8149 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8150 * >, *, : and ! keys if they really really want to.
8151 */
8152 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8153 && keytyped == look[1])
8154 return TRUE;
8155
8156 if (keytyped == get_special_key_code(look + 1))
8157 return TRUE;
8158 }
8159 while (*look && *look != '>')
8160 look++;
8161 while (*look == '>')
8162 look++;
8163 }
8164
8165 /*
8166 * Is it a word: "=word"?
8167 */
8168 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8169 {
8170 ++look;
8171 if (*look == '~')
8172 {
8173 icase = TRUE;
8174 ++look;
8175 }
8176 else
8177 icase = FALSE;
8178 p = vim_strchr(look, ',');
8179 if (p == NULL)
8180 p = look + STRLEN(look);
8181 if ((try_match || try_match_word)
8182 && curwin->w_cursor.col >= (colnr_T)(p - look))
8183 {
8184 int match = FALSE;
8185
8186#ifdef FEAT_INS_EXPAND
8187 if (keytyped == KEY_COMPLETE)
8188 {
8189 char_u *s;
8190
8191 /* Just completed a word, check if it starts with "look".
8192 * search back for the start of a word. */
8193 line = ml_get_curline();
8194# ifdef FEAT_MBYTE
8195 if (has_mbyte)
8196 {
8197 char_u *n;
8198
8199 for (s = line + curwin->w_cursor.col; s > line; s = n)
8200 {
8201 n = mb_prevptr(line, s);
8202 if (!vim_iswordp(n))
8203 break;
8204 }
8205 }
8206 else
8207# endif
8208 for (s = line + curwin->w_cursor.col; s > line; --s)
8209 if (!vim_iswordc(s[-1]))
8210 break;
8211 if (s + (p - look) <= line + curwin->w_cursor.col
8212 && (icase
8213 ? MB_STRNICMP(s, look, p - look)
8214 : STRNCMP(s, look, p - look)) == 0)
8215 match = TRUE;
8216 }
8217 else
8218#endif
8219 /* TODO: multi-byte */
8220 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8221 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8222 {
8223 line = ml_get_cursor();
8224 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8225 || !vim_iswordc(line[-(p - look) - 1]))
8226 && (icase
8227 ? MB_STRNICMP(line - (p - look), look, p - look)
8228 : STRNCMP(line - (p - look), look, p - look))
8229 == 0)
8230 match = TRUE;
8231 }
8232 if (match && try_match_word && !try_match)
8233 {
8234 /* "0=word": Check if there are only blanks before the
8235 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008236 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 (int)(curwin->w_cursor.col - (p - look)))
8238 match = FALSE;
8239 }
8240 if (match)
8241 return TRUE;
8242 }
8243 look = p;
8244 }
8245
8246 /*
8247 * ok, it's a boring generic character.
8248 */
8249 else
8250 {
8251 if (try_match && *look == keytyped)
8252 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008253 if (*look != NUL)
8254 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 }
8256
8257 /*
8258 * Skip over ", ".
8259 */
8260 look = skip_to_option_part(look);
8261 }
8262 return FALSE;
8263}
8264#endif /* FEAT_CINDENT */
8265
8266#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8267/*
8268 * Map Hebrew keyboard when in hkmap mode.
8269 */
8270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008271hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8274 {
8275 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8276 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8277 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8278 static char_u map[26] =
8279 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8280 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8281 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8282 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8283 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8284 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8285 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8286 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8287 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8288
8289 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8290 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8291 /* '-1'='sofit' */
8292 else if (c == 'x')
8293 return 'X';
8294 else if (c == 'q')
8295 return '\''; /* {geresh}={'} */
8296 else if (c == 246)
8297 return ' '; /* \"o --> ' ' for a german keyboard */
8298 else if (c == 228)
8299 return ' '; /* \"a --> ' ' -- / -- */
8300 else if (c == 252)
8301 return ' '; /* \"u --> ' ' -- / -- */
8302#ifdef EBCDIC
8303 else if (islower(c))
8304#else
8305 /* NOTE: islower() does not do the right thing for us on Linux so we
8306 * do this the same was as 5.7 and previous, so it works correctly on
8307 * all systems. Specifically, the e.g. Delete and Arrow keys are
8308 * munged and won't work if e.g. searching for Hebrew text.
8309 */
8310 else if (c >= 'a' && c <= 'z')
8311#endif
8312 return (int)(map[CharOrdLow(c)] + p_aleph);
8313 else
8314 return c;
8315 }
8316 else
8317 {
8318 switch (c)
8319 {
8320 case '`': return ';';
8321 case '/': return '.';
8322 case '\'': return ',';
8323 case 'q': return '/';
8324 case 'w': return '\'';
8325
8326 /* Hebrew letters - set offset from 'a' */
8327 case ',': c = '{'; break;
8328 case '.': c = 'v'; break;
8329 case ';': c = 't'; break;
8330 default: {
8331 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8332
8333#ifdef EBCDIC
8334 /* see note about islower() above */
8335 if (!islower(c))
8336#else
8337 if (c < 'a' || c > 'z')
8338#endif
8339 return c;
8340 c = str[CharOrdLow(c)];
8341 break;
8342 }
8343 }
8344
8345 return (int)(CharOrdLow(c) + p_aleph);
8346 }
8347}
8348#endif
8349
8350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008351ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 int need_redraw = FALSE;
8354 int regname;
8355 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008356 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
8358 /*
8359 * If we are going to wait for a character, show a '"'.
8360 */
8361 pc_status = PC_STATUS_UNSET;
8362 if (redrawing() && !char_avail())
8363 {
8364 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008365 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
8367 edit_putchar('"', TRUE);
8368#ifdef FEAT_CMDL_INFO
8369 add_to_showcmd_c(Ctrl_R);
8370#endif
8371 }
8372
8373#ifdef USE_ON_FLY_SCROLL
8374 dont_scroll = TRUE; /* disallow scrolling here */
8375#endif
8376
8377 /*
8378 * Don't map the register name. This also prevents the mode message to be
8379 * deleted when ESC is hit.
8380 */
8381 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008382 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8385 {
8386 /* Get a third key for literal register insertion */
8387 literally = regname;
8388#ifdef FEAT_CMDL_INFO
8389 add_to_showcmd_c(literally);
8390#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008391 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 }
8394 --no_mapping;
8395
8396#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008397 /* Don't call u_sync() while typing the expression or giving an error
8398 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 ++no_u_sync;
8400 if (regname == '=')
8401 {
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008402# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008404# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008405 /* Sync undo when evaluating the expression calls setline() or
8406 * append(), so that it can be undone separately. */
8407 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 regname = get_expr_register();
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008410# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 /* Restore the Input Method. */
8412 if (im_on)
8413 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008416 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8417 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008418 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 else
8422 {
8423#endif
8424 if (literally == Ctrl_O || literally == Ctrl_P)
8425 {
8426 /* Append the command to the redo buffer. */
8427 AppendCharToRedobuff(Ctrl_R);
8428 AppendCharToRedobuff(literally);
8429 AppendCharToRedobuff(regname);
8430
8431 do_put(regname, BACKWARD, 1L,
8432 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8433 }
8434 else if (insert_reg(regname, literally) == FAIL)
8435 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008436 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 need_redraw = TRUE; /* remove the '"' */
8438 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008439 else if (stop_insert_mode)
8440 /* When the '=' register was used and a function was invoked that
8441 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8442 * insert anything, need to remove the '"' */
8443 need_redraw = TRUE;
8444
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#ifdef FEAT_EVAL
8446 }
8447 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008448 if (u_sync_once == 1)
8449 ins_need_undo = TRUE;
8450 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451#endif
8452#ifdef FEAT_CMDL_INFO
8453 clear_showcmd();
8454#endif
8455
8456 /* If the inserted register is empty, we need to remove the '"' */
8457 if (need_redraw || stuff_empty())
8458 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008459
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008460 /* Disallow starting Visual mode here, would get a weird mode. */
8461 if (!vis_active && VIsual_active)
8462 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463}
8464
8465/*
8466 * CTRL-G commands in Insert mode.
8467 */
8468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008469ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int c;
8472
8473#ifdef FEAT_INS_EXPAND
8474 /* Right after CTRL-X the cursor will be after the ruler. */
8475 setcursor();
8476#endif
8477
8478 /*
8479 * Don't map the second key. This also prevents the mode message to be
8480 * deleted when ESC is hit.
8481 */
8482 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008483 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 --no_mapping;
8485 switch (c)
8486 {
8487 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8488 case K_UP:
8489 case Ctrl_K:
8490 case 'k': ins_up(TRUE);
8491 break;
8492
8493 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8494 case K_DOWN:
8495 case Ctrl_J:
8496 case 'j': ins_down(TRUE);
8497 break;
8498
8499 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008500 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008502
8503 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008504 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008505 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008506 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 break;
8508
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008509 /* CTRL-G U: do not break undo with the next char */
8510 case 'U':
8511 /* Allow one left/right cursor movement with the next char,
8512 * without breaking undo. */
8513 dont_sync_undo = MAYBE;
8514 break;
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008517 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 }
8519}
8520
8521/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008522 * CTRL-^ in Insert mode.
8523 */
8524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008525ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008526{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008527 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008528 {
8529 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8530 if (State & LANGMAP)
8531 {
8532 curbuf->b_p_iminsert = B_IMODE_NONE;
8533 State &= ~LANGMAP;
8534 }
8535 else
8536 {
8537 curbuf->b_p_iminsert = B_IMODE_LMAP;
8538 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008539#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008540 im_set_active(FALSE);
8541#endif
8542 }
8543 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008544#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008545 else
8546 {
8547 /* There are no ":lmap" mappings, toggle IM */
8548 if (im_get_status())
8549 {
8550 curbuf->b_p_iminsert = B_IMODE_NONE;
8551 im_set_active(FALSE);
8552 }
8553 else
8554 {
8555 curbuf->b_p_iminsert = B_IMODE_IM;
8556 State &= ~LANGMAP;
8557 im_set_active(TRUE);
8558 }
8559 }
8560#endif
8561 set_iminsert_global();
8562 showmode();
8563#ifdef FEAT_GUI
8564 /* may show different cursor shape or color */
8565 if (gui.in_use)
8566 gui_update_cursor(TRUE, FALSE);
8567#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008568#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008569 /* Show/unshow value of 'keymap' in status lines. */
8570 status_redraw_curbuf();
8571#endif
8572}
8573
8574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 * Handle ESC in insert mode.
8576 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8577 * insert.
8578 */
8579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008580ins_esc(
8581 long *count,
8582 int cmdchar,
8583 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
8585 int temp;
8586 static int disabled_redraw = FALSE;
8587
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008588#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008589 check_spell_redraw();
8590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#if defined(FEAT_HANGULIN)
8592# if defined(ESC_CHG_TO_ENG_MODE)
8593 hangul_input_state_set(0);
8594# endif
8595 if (composing_hangul)
8596 {
8597 push_raw_key(composing_hangul_buffer, 2);
8598 composing_hangul = 0;
8599 }
8600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 temp = curwin->w_cursor.col;
8603 if (disabled_redraw)
8604 {
8605 --RedrawingDisabled;
8606 disabled_redraw = FALSE;
8607 }
8608 if (!arrow_used)
8609 {
8610 /*
8611 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008612 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8613 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 */
8615 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008616 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
8618 /*
8619 * Repeating insert may take a long time. Check for
8620 * interrupt now and then.
8621 */
8622 if (*count > 0)
8623 {
8624 line_breakcheck();
8625 if (got_int)
8626 *count = 0;
8627 }
8628
8629 if (--*count > 0) /* repeat what was typed */
8630 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008631 /* Vi repeats the insert without replacing characters. */
8632 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8633 State &= ~REPLACE_FLAG;
8634
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 (void)start_redo_ins();
8636 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008637 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 ++RedrawingDisabled;
8639 disabled_redraw = TRUE;
8640 return FALSE; /* repeat the insert */
8641 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008642 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 undisplay_dollar();
8644 }
8645
8646 /* When an autoindent was removed, curswant stays after the
8647 * indent */
8648 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8649 curwin->w_set_curswant = TRUE;
8650
8651 /* Remember the last Insert position in the '^ mark. */
8652 if (!cmdmod.keepjumps)
8653 curbuf->b_last_insert = curwin->w_cursor;
8654
8655 /*
8656 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008657 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008659 if (!nomove
8660 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661#ifdef FEAT_VIRTUALEDIT
8662 || curwin->w_cursor.coladd > 0
8663#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008664 )
8665 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008666 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667#ifdef FEAT_RIGHTLEFT
8668 && !revins_on
8669#endif
8670 )
8671 {
8672#ifdef FEAT_VIRTUALEDIT
8673 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8674 {
8675 oneleft();
8676 if (restart_edit != NUL)
8677 ++curwin->w_cursor.coladd;
8678 }
8679 else
8680#endif
8681 {
8682 --curwin->w_cursor.col;
8683#ifdef FEAT_MBYTE
8684 /* Correct cursor for multi-byte character. */
8685 if (has_mbyte)
8686 mb_adjust_cursor();
8687#endif
8688 }
8689 }
8690
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008691#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 /* Disable IM to allow typing English directly for Normal mode commands.
8693 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8694 * well). */
8695 if (!(State & LANGMAP))
8696 im_save_status(&curbuf->b_p_iminsert);
8697 im_set_active(FALSE);
8698#endif
8699
8700 State = NORMAL;
8701 /* need to position cursor again (e.g. when on a TAB ) */
8702 changed_cline_bef_curs();
8703
8704#ifdef FEAT_MOUSE
8705 setmouse();
8706#endif
8707#ifdef CURSOR_SHAPE
8708 ui_cursor_shape(); /* may show different cursor shape */
8709#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008710 if (!p_ek)
8711 /* Re-enable bracketed paste mode. */
8712 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713
8714 /*
8715 * When recording or for CTRL-O, need to display the new mode.
8716 * Otherwise remove the mode message.
8717 */
8718 if (Recording || restart_edit != NUL)
8719 showmode();
8720 else if (p_smd)
8721 MSG("");
8722
8723 return TRUE; /* exit Insert mode */
8724}
8725
8726#ifdef FEAT_RIGHTLEFT
8727/*
8728 * Toggle language: hkmap and revins_on.
8729 * Move to end of reverse inserted text.
8730 */
8731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008732ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 if (revins_on && revins_chars && revins_scol >= 0)
8735 {
8736 while (gchar_cursor() != NUL && revins_chars--)
8737 ++curwin->w_cursor.col;
8738 }
8739 p_ri = !p_ri;
8740 revins_on = (State == INSERT && p_ri);
8741 if (revins_on)
8742 {
8743 revins_scol = curwin->w_cursor.col;
8744 revins_legal++;
8745 revins_chars = 0;
8746 undisplay_dollar();
8747 }
8748 else
8749 revins_scol = -1;
8750#ifdef FEAT_FKMAP
8751 if (p_altkeymap)
8752 {
8753 /*
8754 * to be consistent also for redo command, using '.'
8755 * set arrow_used to true and stop it - causing to redo
8756 * characters entered in one mode (normal/reverse insert).
8757 */
8758 arrow_used = TRUE;
8759 (void)stop_arrow();
8760 p_fkmap = curwin->w_p_rl ^ p_ri;
8761 if (p_fkmap && p_ri)
8762 State = INSERT;
8763 }
8764 else
8765#endif
8766 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8767 showmode();
8768}
8769#endif
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771/*
8772 * If 'keymodel' contains "startsel", may start selection.
8773 * Returns TRUE when a CTRL-O and other keys stuffed.
8774 */
8775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008776ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778 if (km_startsel)
8779 switch (c)
8780 {
8781 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 case K_PAGEUP:
8784 case K_KPAGEUP:
8785 case K_PAGEDOWN:
8786 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008787# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 case K_LEFT:
8789 case K_RIGHT:
8790 case K_UP:
8791 case K_DOWN:
8792 case K_END:
8793 case K_HOME:
8794# endif
8795 if (!(mod_mask & MOD_MASK_SHIFT))
8796 break;
8797 /* FALLTHROUGH */
8798 case K_S_LEFT:
8799 case K_S_RIGHT:
8800 case K_S_UP:
8801 case K_S_DOWN:
8802 case K_S_END:
8803 case K_S_HOME:
8804 /* Start selection right away, the cursor can move with
8805 * CTRL-O when beyond the end of the line. */
8806 start_selection();
8807
8808 /* Execute the key in (insert) Select mode. */
8809 stuffcharReadbuff(Ctrl_O);
8810 if (mod_mask)
8811 {
8812 char_u buf[4];
8813
8814 buf[0] = K_SPECIAL;
8815 buf[1] = KS_MODIFIER;
8816 buf[2] = mod_mask;
8817 buf[3] = NUL;
8818 stuffReadbuff(buf);
8819 }
8820 stuffcharReadbuff(c);
8821 return TRUE;
8822 }
8823 return FALSE;
8824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008827 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008828 */
8829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008830ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008831{
8832#ifdef FEAT_FKMAP
8833 if (p_fkmap && p_ri)
8834 {
8835 beep_flush();
8836 EMSG(farsi_text_3); /* encoded in Farsi */
8837 return;
8838 }
8839#endif
8840
8841#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008842# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008843 set_vim_var_string(VV_INSERTMODE,
8844 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008845# ifdef FEAT_VREPLACE
8846 replaceState == VREPLACE ? "v" :
8847# endif
8848 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008849# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008850 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8851#endif
8852 if (State & REPLACE_FLAG)
8853 State = INSERT | (State & LANGMAP);
8854 else
8855 State = replaceState | (State & LANGMAP);
8856 AppendCharToRedobuff(K_INS);
8857 showmode();
8858#ifdef CURSOR_SHAPE
8859 ui_cursor_shape(); /* may show different cursor shape */
8860#endif
8861}
8862
8863/*
8864 * Pressed CTRL-O in Insert mode.
8865 */
8866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008867ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008868{
8869#ifdef FEAT_VREPLACE
8870 if (State & VREPLACE_FLAG)
8871 restart_edit = 'V';
8872 else
8873#endif
8874 if (State & REPLACE_FLAG)
8875 restart_edit = 'R';
8876 else
8877 restart_edit = 'I';
8878#ifdef FEAT_VIRTUALEDIT
8879 if (virtual_active())
8880 ins_at_eol = FALSE; /* cursor always keeps its column */
8881 else
8882#endif
8883 ins_at_eol = (gchar_cursor() == NUL);
8884}
8885
8886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 * If the cursor is on an indent, ^T/^D insert/delete one
8888 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008889 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 * with vi. But vi only supports ^T and ^D after an
8891 * autoindent, we support it everywhere.
8892 */
8893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 if (stop_arrow() == FAIL)
8897 return;
8898 AppendCharToRedobuff(c);
8899
8900 /*
8901 * 0^D and ^^D: remove all indent.
8902 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008903 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8904 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 {
8906 --curwin->w_cursor.col;
8907 (void)del_char(FALSE); /* delete the '^' or '0' */
8908 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8909 if (State & REPLACE_FLAG)
8910 replace_pop_ins();
8911 if (lastc == '^')
8912 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008913 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008916 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
8918 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8919 did_ai = FALSE;
8920#ifdef FEAT_SMARTINDENT
8921 did_si = FALSE;
8922 can_si = FALSE;
8923 can_si_back = FALSE;
8924#endif
8925#ifdef FEAT_CINDENT
8926 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8927#endif
8928}
8929
8930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008931ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 int temp;
8934
8935 if (stop_arrow() == FAIL)
8936 return;
8937 if (gchar_cursor() == NUL) /* delete newline */
8938 {
8939 temp = curwin->w_cursor.col;
8940 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008941 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008942 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 else
8944 curwin->w_cursor.col = temp;
8945 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008946 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8947 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 did_ai = FALSE;
8949#ifdef FEAT_SMARTINDENT
8950 did_si = FALSE;
8951 can_si = FALSE;
8952 can_si_back = FALSE;
8953#endif
8954 AppendCharToRedobuff(K_DEL);
8955}
8956
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008957static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008958
8959/*
8960 * Delete one character for ins_bs().
8961 */
8962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008963ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008964{
8965 dec_cursor();
8966 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8967 if (State & REPLACE_FLAG)
8968 {
8969 /* Don't delete characters before the insert point when in
8970 * Replace mode */
8971 if (curwin->w_cursor.lnum != Insstart.lnum
8972 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008973 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008974 }
8975 else
8976 (void)del_char(FALSE);
8977}
8978
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979/*
8980 * Handle Backspace, delete-word and delete-line in Insert mode.
8981 * Return TRUE when backspace was actually used.
8982 */
8983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008984ins_bs(
8985 int c,
8986 int mode,
8987 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989 linenr_T lnum;
8990 int cc;
8991 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008992 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 colnr_T mincol;
8994 int did_backspace = FALSE;
8995 int in_indent;
8996 int oldState;
8997#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008998 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999#endif
9000
9001 /*
9002 * can't delete anything in an empty file
9003 * can't backup past first character in buffer
9004 * can't backup past starting point unless 'backspace' > 1
9005 * can backup to a previous line if 'backspace' == 0
9006 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009007 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 || (
9009#ifdef FEAT_RIGHTLEFT
9010 !revins_on &&
9011#endif
9012 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9013 || (!can_bs(BS_START)
9014 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009015 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9016 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9018 && curwin->w_cursor.col <= ai_col)
9019 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9020 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009021 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 return FALSE;
9023 }
9024
9025 if (stop_arrow() == FAIL)
9026 return FALSE;
9027 in_indent = inindent(0);
9028#ifdef FEAT_CINDENT
9029 if (in_indent)
9030 can_cindent = FALSE;
9031#endif
9032#ifdef FEAT_COMMENTS
9033 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9034#endif
9035#ifdef FEAT_RIGHTLEFT
9036 if (revins_on) /* put cursor after last inserted char */
9037 inc_cursor();
9038#endif
9039
9040#ifdef FEAT_VIRTUALEDIT
9041 /* Virtualedit:
9042 * BACKSPACE_CHAR eats a virtual space
9043 * BACKSPACE_WORD eats all coladd
9044 * BACKSPACE_LINE eats all coladd and keeps going
9045 */
9046 if (curwin->w_cursor.coladd > 0)
9047 {
9048 if (mode == BACKSPACE_CHAR)
9049 {
9050 --curwin->w_cursor.coladd;
9051 return TRUE;
9052 }
9053 if (mode == BACKSPACE_WORD)
9054 {
9055 curwin->w_cursor.coladd = 0;
9056 return TRUE;
9057 }
9058 curwin->w_cursor.coladd = 0;
9059 }
9060#endif
9061
9062 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009063 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 */
9065 if (curwin->w_cursor.col == 0)
9066 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009067 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009068 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069#ifdef FEAT_RIGHTLEFT
9070 || revins_on
9071#endif
9072 )
9073 {
9074 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9075 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9076 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009077 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009078 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 }
9080 /*
9081 * In replace mode:
9082 * cc < 0: NL was inserted, delete it
9083 * cc >= 0: NL was replaced, put original characters back
9084 */
9085 cc = -1;
9086 if (State & REPLACE_FLAG)
9087 cc = replace_pop(); /* returns -1 if NL was inserted */
9088 /*
9089 * In replace mode, in the line we started replacing, we only move the
9090 * cursor.
9091 */
9092 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9093 {
9094 dec_cursor();
9095 }
9096 else
9097 {
9098#ifdef FEAT_VREPLACE
9099 if (!(State & VREPLACE_FLAG)
9100 || curwin->w_cursor.lnum > orig_line_count)
9101#endif
9102 {
9103 temp = gchar_cursor(); /* remember current char */
9104 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009105
9106 /* When "aw" is in 'formatoptions' we must delete the space at
9107 * the end of the line, otherwise the line will be broken
9108 * again when auto-formatting. */
9109 if (has_format_option(FO_AUTO)
9110 && has_format_option(FO_WHITE_PAR))
9111 {
9112 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9113 TRUE);
9114 int len;
9115
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009116 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009117 if (len > 0 && ptr[len - 1] == ' ')
9118 ptr[len - 1] = NUL;
9119 }
9120
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009121 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 if (temp == NUL && gchar_cursor() != NUL)
9123 inc_cursor();
9124 }
9125#ifdef FEAT_VREPLACE
9126 else
9127 dec_cursor();
9128#endif
9129
9130 /*
9131 * In REPLACE mode we have to put back the text that was replaced
9132 * by the NL. On the replace stack is first a NUL-terminated
9133 * sequence of characters that were deleted and then the
9134 * characters that NL replaced.
9135 */
9136 if (State & REPLACE_FLAG)
9137 {
9138 /*
9139 * Do the next ins_char() in NORMAL state, to
9140 * prevent ins_char() from replacing characters and
9141 * avoiding showmatch().
9142 */
9143 oldState = State;
9144 State = NORMAL;
9145 /*
9146 * restore characters (blanks) deleted after cursor
9147 */
9148 while (cc > 0)
9149 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009150 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#ifdef FEAT_MBYTE
9152 mb_replace_pop_ins(cc);
9153#else
9154 ins_char(cc);
9155#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009156 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 cc = replace_pop();
9158 }
9159 /* restore the characters that NL replaced */
9160 replace_pop_ins();
9161 State = oldState;
9162 }
9163 }
9164 did_ai = FALSE;
9165 }
9166 else
9167 {
9168 /*
9169 * Delete character(s) before the cursor.
9170 */
9171#ifdef FEAT_RIGHTLEFT
9172 if (revins_on) /* put cursor on last inserted char */
9173 dec_cursor();
9174#endif
9175 mincol = 0;
9176 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009177 if (mode == BACKSPACE_LINE
9178 && (curbuf->b_p_ai
9179#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009180 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009181#endif
9182 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#ifdef FEAT_RIGHTLEFT
9184 && !revins_on
9185#endif
9186 )
9187 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009188 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009190 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009192 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 }
9194
9195 /*
9196 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9197 */
9198 if ( mode == BACKSPACE_CHAR
9199 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009200 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009201 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 && (*(ml_get_cursor() - 1) == TAB
9203 || (*(ml_get_cursor() - 1) == ' '
9204 && (!*inserted_space_p
9205 || arrow_used))))))
9206 {
9207 int ts;
9208 colnr_T vcol;
9209 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009210 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211
9212 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009213 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009214 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009216 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 /* Compute the virtual column where we want to be. Since
9218 * 'showbreak' may get in the way, need to get the last column of
9219 * the previous character. */
9220 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009221 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 dec_cursor();
9223 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9224 inc_cursor();
9225 want_vcol = (want_vcol / ts) * ts;
9226
9227 /* delete characters until we are at or before want_vcol */
9228 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009229 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009230 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231
9232 /* insert extra spaces until we are at want_vcol */
9233 while (vcol < want_vcol)
9234 {
9235 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009236 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9237 && curwin->w_cursor.col < Insstart_orig.col)
9238 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240#ifdef FEAT_VREPLACE
9241 if (State & VREPLACE_FLAG)
9242 ins_char(' ');
9243 else
9244#endif
9245 {
9246 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009247 if ((State & REPLACE_FLAG))
9248 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9251 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009252
9253 /* If we are now back where we started delete one character. Can
9254 * happen when using 'sts' and 'linebreak'. */
9255 if (vcol >= start_vcol)
9256 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 }
9258
9259 /*
9260 * Delete upto starting point, start of line or previous word.
9261 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009264#ifdef FEAT_MBYTE
9265 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009267 if (has_mbyte)
9268 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009270 do
9271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009273 if (!revins_on) /* put cursor on char to be deleted */
9274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009276
9277 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009279 /* look multi-byte character class */
9280 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009282 prev_cclass = cclass;
9283 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009286
9287 /* start of word? */
9288 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9289 {
9290 mode = BACKSPACE_WORD_NOT_SPACE;
9291 temp = vim_iswordc(cc);
9292 }
9293 /* end of word? */
9294 else if (mode == BACKSPACE_WORD_NOT_SPACE
9295 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9296#ifdef FEAT_MBYTE
9297 || prev_cclass != cclass
9298#endif
9299 ))
9300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009302 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009304 inc_cursor();
9305#ifdef FEAT_RIGHTLEFT
9306 else if (State & REPLACE_FLAG)
9307 dec_cursor();
9308#endif
9309 break;
9310 }
9311 if (State & REPLACE_FLAG)
9312 replace_do_bs(-1);
9313 else
9314 {
9315#ifdef FEAT_MBYTE
9316 if (enc_utf8 && p_deco)
9317 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9318#endif
9319 (void)del_char(FALSE);
9320#ifdef FEAT_MBYTE
9321 /*
9322 * If there are combining characters and 'delcombine' is set
9323 * move the cursor back. Don't back up before the base
9324 * character.
9325 */
9326 if (enc_utf8 && p_deco && cpc[0] != NUL)
9327 inc_cursor();
9328#endif
9329#ifdef FEAT_RIGHTLEFT
9330 if (revins_chars)
9331 {
9332 revins_chars--;
9333 revins_legal++;
9334 }
9335 if (revins_on && gchar_cursor() == NUL)
9336 break;
9337#endif
9338 }
9339 /* Just a single backspace?: */
9340 if (mode == BACKSPACE_CHAR)
9341 break;
9342 } while (
9343#ifdef FEAT_RIGHTLEFT
9344 revins_on ||
9345#endif
9346 (curwin->w_cursor.col > mincol
9347 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9348 || curwin->w_cursor.col != Insstart_orig.col)));
9349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 did_backspace = TRUE;
9351 }
9352#ifdef FEAT_SMARTINDENT
9353 did_si = FALSE;
9354 can_si = FALSE;
9355 can_si_back = FALSE;
9356#endif
9357 if (curwin->w_cursor.col <= 1)
9358 did_ai = FALSE;
9359 /*
9360 * It's a little strange to put backspaces into the redo
9361 * buffer, but it makes auto-indent a lot easier to deal
9362 * with.
9363 */
9364 AppendCharToRedobuff(c);
9365
9366 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009367 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9368 && curwin->w_cursor.col < Insstart_orig.col)
9369 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
9371 /* vi behaviour: the cursor moves backward but the character that
9372 * was there remains visible
9373 * Vim behaviour: the cursor moves backward and the character that
9374 * was there is erased from the screen.
9375 * We can emulate the vi behaviour by pretending there is a dollar
9376 * displayed even when there isn't.
9377 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009378 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 dollar_vcol = curwin->w_virtcol;
9380
Bram Moolenaarce3be472008-01-14 19:12:28 +00009381#ifdef FEAT_FOLDING
9382 /* When deleting a char the cursor line must never be in a closed fold.
9383 * E.g., when 'foldmethod' is indent and deleting the first non-white
9384 * char before a Tab. */
9385 if (did_backspace)
9386 foldOpenCursor();
9387#endif
9388
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 return did_backspace;
9390}
9391
9392#ifdef FEAT_MOUSE
9393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009394ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009397 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
9399# ifdef FEAT_GUI
9400 /* When GUI is active, also move/paste when 'mouse' is empty */
9401 if (!gui.in_use)
9402# endif
9403 if (!mouse_has(MOUSE_INSERT))
9404 return;
9405
9406 undisplay_dollar();
9407 tpos = curwin->w_cursor;
9408 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9409 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009410 win_T *new_curwin = curwin;
9411
9412 if (curwin != old_curwin && win_valid(old_curwin))
9413 {
9414 /* Mouse took us to another window. We need to go back to the
9415 * previous one to stop insert there properly. */
9416 curwin = old_curwin;
9417 curbuf = curwin->w_buffer;
9418 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009419 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009420 if (curwin != new_curwin && win_valid(new_curwin))
9421 {
9422 curwin = new_curwin;
9423 curbuf = curwin->w_buffer;
9424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425# ifdef FEAT_CINDENT
9426 can_cindent = TRUE;
9427# endif
9428 }
9429
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 /* redraw status lines (in case another window became active) */
9431 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432}
9433
9434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009435ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009438 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009439# ifdef FEAT_INS_EXPAND
9440 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441# endif
9442
9443 tpos = curwin->w_cursor;
9444
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009445 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 int row, col;
9448
9449 row = mouse_row;
9450 col = mouse_col;
9451
9452 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009453 wp = mouse_find_win(&row, &col);
9454 if (wp == NULL)
9455 return;
9456 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 curbuf = curwin->w_buffer;
9458 }
9459 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 undisplay_dollar();
9461
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009462# ifdef FEAT_INS_EXPAND
9463 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009464 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009465# endif
9466 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009467 if (dir == MSCR_DOWN || dir == MSCR_UP)
9468 {
9469 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9470 scroll_redraw(dir,
9471 (long)(curwin->w_botline - curwin->w_topline));
9472 else
9473 scroll_redraw(dir, 3L);
9474 }
9475#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009476 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009477 {
9478 int val, step = 6;
9479
9480 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009481 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009482 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9483 if (val < 0)
9484 val = 0;
9485 gui_do_horiz_scroll(val, TRUE);
9486 }
9487#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009488# ifdef FEAT_INS_EXPAND
9489 did_scroll = TRUE;
9490# endif
9491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 curwin->w_redr_status = TRUE;
9494
9495 curwin = old_curwin;
9496 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009498# ifdef FEAT_INS_EXPAND
9499 /* The popup menu may overlay the window, need to redraw it.
9500 * TODO: Would be more efficient to only redraw the windows that are
9501 * overlapped by the popup menu. */
9502 if (pum_visible() && did_scroll)
9503 {
9504 redraw_all_later(NOT_VALID);
9505 ins_compl_show_pum();
9506 }
9507# endif
9508
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009509 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 {
9511 start_arrow(&tpos);
9512# ifdef FEAT_CINDENT
9513 can_cindent = TRUE;
9514# endif
9515 }
9516}
9517#endif
9518
Bram Moolenaarec2da362017-01-21 20:04:22 +01009519/*
9520 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9521 * P_PE literally.
9522 * When "drop" is TRUE then consume the text and drop it.
9523 */
9524 int
9525bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9526{
9527 int c;
9528 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9529 int idx = 0;
9530 char_u *end = find_termcode((char_u *)"PE");
9531 int ret_char = -1;
9532 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009533 int save_paste = p_paste;
9534 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009535
9536 /* If the end code is too long we can't detect it, read everything. */
9537 if (STRLEN(end) >= NUMBUFLEN)
9538 end = NULL;
9539 ++no_mapping;
9540 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009541 p_paste = TRUE;
9542 curbuf->b_p_ai = FALSE;
9543
Bram Moolenaarec2da362017-01-21 20:04:22 +01009544 for (;;)
9545 {
9546 /* When the end is not defined read everything. */
9547 if (end == NULL && vpeekc() == NUL)
9548 break;
9549 c = plain_vgetc();
9550#ifdef FEAT_MBYTE
9551 if (has_mbyte)
9552 idx += (*mb_char2bytes)(c, buf + idx);
9553 else
9554#endif
9555 buf[idx++] = c;
9556 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009557 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009558 {
9559 if (end[idx] == NUL)
9560 break; /* Found the end of paste code. */
9561 continue;
9562 }
9563 if (!drop)
9564 {
9565 switch (mode)
9566 {
9567 case PASTE_CMDLINE:
9568 put_on_cmdline(buf, idx, TRUE);
9569 break;
9570
9571 case PASTE_EX:
9572 if (gap != NULL && ga_grow(gap, idx) == OK)
9573 {
9574 mch_memmove((char *)gap->ga_data + gap->ga_len,
9575 buf, (size_t)idx);
9576 gap->ga_len += idx;
9577 }
9578 break;
9579
9580 case PASTE_INSERT:
9581 if (stop_arrow() == OK)
9582 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009583 c = buf[0];
9584 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9585 ins_eol(c);
9586 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009587 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009588 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009589 AppendToRedobuffLit(buf, idx);
9590 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009591 }
9592 break;
9593
9594 case PASTE_ONE_CHAR:
9595 if (ret_char == -1)
9596 {
9597#ifdef FEAT_MBYTE
9598 if (has_mbyte)
9599 ret_char = (*mb_ptr2char)(buf);
9600 else
9601#endif
9602 ret_char = buf[0];
9603 }
9604 break;
9605 }
9606 }
9607 idx = 0;
9608 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009609
Bram Moolenaarec2da362017-01-21 20:04:22 +01009610 --no_mapping;
9611 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009612 p_paste = save_paste;
9613 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009614
9615 return ret_char;
9616}
9617
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009618#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009620ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009621{
9622 /* We will be leaving the current window, unless closing another tab. */
9623 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9624 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9625 {
9626 undisplay_dollar();
9627 start_arrow(&curwin->w_cursor);
9628# ifdef FEAT_CINDENT
9629 can_cindent = TRUE;
9630# endif
9631 }
9632
9633 if (c == K_TABLINE)
9634 goto_tabpage(current_tab);
9635 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009636 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009637 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009638 redraw_statuslines(); /* will redraw the tabline when needed */
9639 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009640}
9641#endif
9642
9643#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
9647 pos_T tpos;
9648
9649 undisplay_dollar();
9650 tpos = curwin->w_cursor;
9651 if (gui_do_scroll())
9652 {
9653 start_arrow(&tpos);
9654# ifdef FEAT_CINDENT
9655 can_cindent = TRUE;
9656# endif
9657 }
9658}
9659
9660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009661ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 pos_T tpos;
9664
9665 undisplay_dollar();
9666 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009667 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 {
9669 start_arrow(&tpos);
9670# ifdef FEAT_CINDENT
9671 can_cindent = TRUE;
9672# endif
9673 }
9674}
9675#endif
9676
9677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009678ins_left(
9679 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 pos_T tpos;
9682
9683#ifdef FEAT_FOLDING
9684 if ((fdo_flags & FDO_HOR) && KeyTyped)
9685 foldOpenCursor();
9686#endif
9687 undisplay_dollar();
9688 tpos = curwin->w_cursor;
9689 if (oneleft() == OK)
9690 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009691#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9692 /* Only call start_arrow() when not busy with preediting, it will
9693 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009694 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009695#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009696 {
9697 start_arrow_with_change(&tpos, end_change);
9698 if (!end_change)
9699 AppendCharToRedobuff(K_LEFT);
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#ifdef FEAT_RIGHTLEFT
9702 /* If exit reversed string, position is fixed */
9703 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9704 revins_legal++;
9705 revins_chars++;
9706#endif
9707 }
9708
9709 /*
9710 * if 'whichwrap' set for cursor in insert mode may go to
9711 * previous line
9712 */
9713 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9714 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009715 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 start_arrow(&tpos);
9717 --(curwin->w_cursor.lnum);
9718 coladvance((colnr_T)MAXCOL);
9719 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9720 }
9721 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009722 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009723 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724}
9725
9726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009727ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
9729 pos_T tpos;
9730
9731#ifdef FEAT_FOLDING
9732 if ((fdo_flags & FDO_HOR) && KeyTyped)
9733 foldOpenCursor();
9734#endif
9735 undisplay_dollar();
9736 tpos = curwin->w_cursor;
9737 if (c == K_C_HOME)
9738 curwin->w_cursor.lnum = 1;
9739 curwin->w_cursor.col = 0;
9740#ifdef FEAT_VIRTUALEDIT
9741 curwin->w_cursor.coladd = 0;
9742#endif
9743 curwin->w_curswant = 0;
9744 start_arrow(&tpos);
9745}
9746
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 pos_T tpos;
9751
9752#ifdef FEAT_FOLDING
9753 if ((fdo_flags & FDO_HOR) && KeyTyped)
9754 foldOpenCursor();
9755#endif
9756 undisplay_dollar();
9757 tpos = curwin->w_cursor;
9758 if (c == K_C_END)
9759 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9760 coladvance((colnr_T)MAXCOL);
9761 curwin->w_curswant = MAXCOL;
9762
9763 start_arrow(&tpos);
9764}
9765
9766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009767ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
9769#ifdef FEAT_FOLDING
9770 if ((fdo_flags & FDO_HOR) && KeyTyped)
9771 foldOpenCursor();
9772#endif
9773 undisplay_dollar();
9774 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9775 {
9776 start_arrow(&curwin->w_cursor);
9777 (void)bck_word(1L, FALSE, FALSE);
9778 curwin->w_set_curswant = TRUE;
9779 }
9780 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009781 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782}
9783
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785ins_right(
9786 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788#ifdef FEAT_FOLDING
9789 if ((fdo_flags & FDO_HOR) && KeyTyped)
9790 foldOpenCursor();
9791#endif
9792 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009793 if (gchar_cursor() != NUL
9794#ifdef FEAT_VIRTUALEDIT
9795 || virtual_active()
9796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 )
9798 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009799 start_arrow_with_change(&curwin->w_cursor, end_change);
9800 if (!end_change)
9801 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 curwin->w_set_curswant = TRUE;
9803#ifdef FEAT_VIRTUALEDIT
9804 if (virtual_active())
9805 oneright();
9806 else
9807#endif
9808 {
9809#ifdef FEAT_MBYTE
9810 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009811 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 else
9813#endif
9814 ++curwin->w_cursor.col;
9815 }
9816
9817#ifdef FEAT_RIGHTLEFT
9818 revins_legal++;
9819 if (revins_chars)
9820 revins_chars--;
9821#endif
9822 }
9823 /* if 'whichwrap' set for cursor in insert mode, may move the
9824 * cursor to the next line */
9825 else if (vim_strchr(p_ww, ']') != NULL
9826 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9827 {
9828 start_arrow(&curwin->w_cursor);
9829 curwin->w_set_curswant = TRUE;
9830 ++curwin->w_cursor.lnum;
9831 curwin->w_cursor.col = 0;
9832 }
9833 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009834 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009835 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836}
9837
9838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840{
9841#ifdef FEAT_FOLDING
9842 if ((fdo_flags & FDO_HOR) && KeyTyped)
9843 foldOpenCursor();
9844#endif
9845 undisplay_dollar();
9846 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9847 || gchar_cursor() != NUL)
9848 {
9849 start_arrow(&curwin->w_cursor);
9850 (void)fwd_word(1L, FALSE, 0);
9851 curwin->w_set_curswant = TRUE;
9852 }
9853 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009854 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855}
9856
9857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009858ins_up(
9859 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861 pos_T tpos;
9862 linenr_T old_topline = curwin->w_topline;
9863#ifdef FEAT_DIFF
9864 int old_topfill = curwin->w_topfill;
9865#endif
9866
9867 undisplay_dollar();
9868 tpos = curwin->w_cursor;
9869 if (cursor_up(1L, TRUE) == OK)
9870 {
9871 if (startcol)
9872 coladvance(getvcol_nolist(&Insstart));
9873 if (old_topline != curwin->w_topline
9874#ifdef FEAT_DIFF
9875 || old_topfill != curwin->w_topfill
9876#endif
9877 )
9878 redraw_later(VALID);
9879 start_arrow(&tpos);
9880#ifdef FEAT_CINDENT
9881 can_cindent = TRUE;
9882#endif
9883 }
9884 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009885 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886}
9887
9888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009889ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
9891 pos_T tpos;
9892
9893 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009894
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009895 if (mod_mask & MOD_MASK_CTRL)
9896 {
9897 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009898 if (first_tabpage->tp_next != NULL)
9899 {
9900 start_arrow(&curwin->w_cursor);
9901 goto_tabpage(-1);
9902 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009903 return;
9904 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009905
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 tpos = curwin->w_cursor;
9907 if (onepage(BACKWARD, 1L) == OK)
9908 {
9909 start_arrow(&tpos);
9910#ifdef FEAT_CINDENT
9911 can_cindent = TRUE;
9912#endif
9913 }
9914 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009915 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916}
9917
9918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009919ins_down(
9920 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 pos_T tpos;
9923 linenr_T old_topline = curwin->w_topline;
9924#ifdef FEAT_DIFF
9925 int old_topfill = curwin->w_topfill;
9926#endif
9927
9928 undisplay_dollar();
9929 tpos = curwin->w_cursor;
9930 if (cursor_down(1L, TRUE) == OK)
9931 {
9932 if (startcol)
9933 coladvance(getvcol_nolist(&Insstart));
9934 if (old_topline != curwin->w_topline
9935#ifdef FEAT_DIFF
9936 || old_topfill != curwin->w_topfill
9937#endif
9938 )
9939 redraw_later(VALID);
9940 start_arrow(&tpos);
9941#ifdef FEAT_CINDENT
9942 can_cindent = TRUE;
9943#endif
9944 }
9945 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009946 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947}
9948
9949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009950ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952 pos_T tpos;
9953
9954 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009955
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009956 if (mod_mask & MOD_MASK_CTRL)
9957 {
9958 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009959 if (first_tabpage->tp_next != NULL)
9960 {
9961 start_arrow(&curwin->w_cursor);
9962 goto_tabpage(0);
9963 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009964 return;
9965 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009966
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 tpos = curwin->w_cursor;
9968 if (onepage(FORWARD, 1L) == OK)
9969 {
9970 start_arrow(&tpos);
9971#ifdef FEAT_CINDENT
9972 can_cindent = TRUE;
9973#endif
9974 }
9975 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009976 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979#ifdef FEAT_DND
9980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009981ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
9983 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9984}
9985#endif
9986
9987/*
9988 * Handle TAB in Insert or Replace mode.
9989 * Return TRUE when the TAB needs to be inserted like a normal character.
9990 */
9991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009992ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 int ind;
9995 int i;
9996 int temp;
9997
9998 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9999 Insstart_blank_vcol = get_nolist_virtcol();
10000 if (echeck_abbr(TAB + ABBR_OFF))
10001 return FALSE;
10002
10003 ind = inindent(0);
10004#ifdef FEAT_CINDENT
10005 if (ind)
10006 can_cindent = FALSE;
10007#endif
10008
10009 /*
10010 * When nothing special, insert TAB like a normal character
10011 */
10012 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010013 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010014 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 return TRUE;
10016
10017 if (stop_arrow() == FAIL)
10018 return TRUE;
10019
10020 did_ai = FALSE;
10021#ifdef FEAT_SMARTINDENT
10022 did_si = FALSE;
10023 can_si = FALSE;
10024 can_si_back = FALSE;
10025#endif
10026 AppendToRedobuff((char_u *)"\t");
10027
10028 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010029 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010030 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10031 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else /* otherwise use 'tabstop' */
10033 temp = (int)curbuf->b_p_ts;
10034 temp -= get_nolist_virtcol() % temp;
10035
10036 /*
10037 * Insert the first space with ins_char(). It will delete one char in
10038 * replace mode. Insert the rest with ins_str(); it will not delete any
10039 * chars. For VREPLACE mode, we use ins_char() for all characters.
10040 */
10041 ins_char(' ');
10042 while (--temp > 0)
10043 {
10044#ifdef FEAT_VREPLACE
10045 if (State & VREPLACE_FLAG)
10046 ins_char(' ');
10047 else
10048#endif
10049 {
10050 ins_str((char_u *)" ");
10051 if (State & REPLACE_FLAG) /* no char replaced */
10052 replace_push(NUL);
10053 }
10054 }
10055
10056 /*
10057 * When 'expandtab' not set: Replace spaces by TABs where possible.
10058 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010059 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
10061 char_u *ptr;
10062#ifdef FEAT_VREPLACE
10063 char_u *saved_line = NULL; /* init for GCC */
10064 pos_T pos;
10065#endif
10066 pos_T fpos;
10067 pos_T *cursor;
10068 colnr_T want_vcol, vcol;
10069 int change_col = -1;
10070 int save_list = curwin->w_p_list;
10071
10072 /*
10073 * Get the current line. For VREPLACE mode, don't make real changes
10074 * yet, just work on a copy of the line.
10075 */
10076#ifdef FEAT_VREPLACE
10077 if (State & VREPLACE_FLAG)
10078 {
10079 pos = curwin->w_cursor;
10080 cursor = &pos;
10081 saved_line = vim_strsave(ml_get_curline());
10082 if (saved_line == NULL)
10083 return FALSE;
10084 ptr = saved_line + pos.col;
10085 }
10086 else
10087#endif
10088 {
10089 ptr = ml_get_cursor();
10090 cursor = &curwin->w_cursor;
10091 }
10092
10093 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10094 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10095 curwin->w_p_list = FALSE;
10096
10097 /* Find first white before the cursor */
10098 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010099 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 {
10101 --fpos.col;
10102 --ptr;
10103 }
10104
10105 /* In Replace mode, don't change characters before the insert point. */
10106 if ((State & REPLACE_FLAG)
10107 && fpos.lnum == Insstart.lnum
10108 && fpos.col < Insstart.col)
10109 {
10110 ptr += Insstart.col - fpos.col;
10111 fpos.col = Insstart.col;
10112 }
10113
10114 /* compute virtual column numbers of first white and cursor */
10115 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10116 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10117
Bram Moolenaar597a4222014-06-25 14:39:50 +020010118 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10119 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010120 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010122 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 if (vcol + i > want_vcol)
10124 break;
10125 if (*ptr != TAB)
10126 {
10127 *ptr = TAB;
10128 if (change_col < 0)
10129 {
10130 change_col = fpos.col; /* Column of first change */
10131 /* May have to adjust Insstart */
10132 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10133 Insstart.col = fpos.col;
10134 }
10135 }
10136 ++fpos.col;
10137 ++ptr;
10138 vcol += i;
10139 }
10140
10141 if (change_col >= 0)
10142 {
10143 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010144 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
10146 /* Skip over the spaces we need. */
10147 while (vcol < want_vcol && *ptr == ' ')
10148 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010149 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 ++ptr;
10151 ++repl_off;
10152 }
10153 if (vcol > want_vcol)
10154 {
10155 /* Must have a char with 'showbreak' just before it. */
10156 --ptr;
10157 --repl_off;
10158 }
10159 fpos.col += repl_off;
10160
10161 /* Delete following spaces. */
10162 i = cursor->col - fpos.col;
10163 if (i > 0)
10164 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010165 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 /* correct replace stack. */
10167 if ((State & REPLACE_FLAG)
10168#ifdef FEAT_VREPLACE
10169 && !(State & VREPLACE_FLAG)
10170#endif
10171 )
10172 for (temp = i; --temp >= 0; )
10173 replace_join(repl_off);
10174 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010175#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010176 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010177 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010178 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010179 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10180 (char_u *)"\t", 1);
10181 }
10182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 cursor->col -= i;
10184
10185#ifdef FEAT_VREPLACE
10186 /*
10187 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10188 * backspacing over the changed spacing and then inserting the new
10189 * spacing.
10190 */
10191 if (State & VREPLACE_FLAG)
10192 {
10193 /* Backspace from real cursor to change_col */
10194 backspace_until_column(change_col);
10195
10196 /* Insert each char in saved_line from changed_col to
10197 * ptr-cursor */
10198 ins_bytes_len(saved_line + change_col,
10199 cursor->col - change_col);
10200 }
10201#endif
10202 }
10203
10204#ifdef FEAT_VREPLACE
10205 if (State & VREPLACE_FLAG)
10206 vim_free(saved_line);
10207#endif
10208 curwin->w_p_list = save_list;
10209 }
10210
10211 return FALSE;
10212}
10213
10214/*
10215 * Handle CR or NL in insert mode.
10216 * Return TRUE when out of memory or can't undo.
10217 */
10218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010219ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221 int i;
10222
10223 if (echeck_abbr(c + ABBR_OFF))
10224 return FALSE;
10225 if (stop_arrow() == FAIL)
10226 return TRUE;
10227 undisplay_dollar();
10228
10229 /*
10230 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10231 * character under the cursor. Only push a NUL on the replace stack,
10232 * nothing to put back when the NL is deleted.
10233 */
10234 if ((State & REPLACE_FLAG)
10235#ifdef FEAT_VREPLACE
10236 && !(State & VREPLACE_FLAG)
10237#endif
10238 )
10239 replace_push(NUL);
10240
10241 /*
10242 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10243 * replacing the next line, so we push all of the characters left on the
10244 * line onto the replace stack. This is not done here though, it is done
10245 * in open_line().
10246 */
10247
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010248#ifdef FEAT_VIRTUALEDIT
10249 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10250 * CTRL-O). */
10251 if (virtual_active() && curwin->w_cursor.coladd > 0)
10252 coladvance(getviscol());
10253#endif
10254
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255#ifdef FEAT_RIGHTLEFT
10256# ifdef FEAT_FKMAP
10257 if (p_altkeymap && p_fkmap)
10258 fkmap(NL);
10259# endif
10260 /* NL in reverse insert will always start in the end of
10261 * current line. */
10262 if (revins_on)
10263 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10264#endif
10265
10266 AppendToRedobuff(NL_STR);
10267 i = open_line(FORWARD,
10268#ifdef FEAT_COMMENTS
10269 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10270#endif
10271 0, old_indent);
10272 old_indent = 0;
10273#ifdef FEAT_CINDENT
10274 can_cindent = TRUE;
10275#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010276#ifdef FEAT_FOLDING
10277 /* When inserting a line the cursor line must never be in a closed fold. */
10278 foldOpenCursor();
10279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280
10281 return (!i);
10282}
10283
10284#ifdef FEAT_DIGRAPHS
10285/*
10286 * Handle digraph in insert mode.
10287 * Returns character still to be inserted, or NUL when nothing remaining to be
10288 * done.
10289 */
10290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010291ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
10293 int c;
10294 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010295 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296
10297 pc_status = PC_STATUS_UNSET;
10298 if (redrawing() && !char_avail())
10299 {
10300 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010301 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
10303 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010304 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#ifdef FEAT_CMDL_INFO
10306 add_to_showcmd_c(Ctrl_K);
10307#endif
10308 }
10309
10310#ifdef USE_ON_FLY_SCROLL
10311 dont_scroll = TRUE; /* disallow scrolling here */
10312#endif
10313
10314 /* don't map the digraph chars. This also prevents the
10315 * mode message to be deleted when ESC is hit */
10316 ++no_mapping;
10317 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010318 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 --no_mapping;
10320 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010321 if (did_putchar)
10322 /* when the line fits in 'columns' the '?' is at the start of the next
10323 * line and will not be removed by the redraw */
10324 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010325
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (IS_SPECIAL(c) || mod_mask) /* special key */
10327 {
10328#ifdef FEAT_CMDL_INFO
10329 clear_showcmd();
10330#endif
10331 insert_special(c, TRUE, FALSE);
10332 return NUL;
10333 }
10334 if (c != ESC)
10335 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010336 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 if (redrawing() && !char_avail())
10338 {
10339 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010340 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341
10342 if (char2cells(c) == 1)
10343 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010344 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010346 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 }
10348#ifdef FEAT_CMDL_INFO
10349 add_to_showcmd_c(c);
10350#endif
10351 }
10352 ++no_mapping;
10353 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010354 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 --no_mapping;
10356 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010357 if (did_putchar)
10358 /* when the line fits in 'columns' the '?' is at the start of the
10359 * next line and will not be removed by a redraw */
10360 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 if (cc != ESC)
10362 {
10363 AppendToRedobuff((char_u *)CTRL_V_STR);
10364 c = getdigraph(c, cc, TRUE);
10365#ifdef FEAT_CMDL_INFO
10366 clear_showcmd();
10367#endif
10368 return c;
10369 }
10370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371#ifdef FEAT_CMDL_INFO
10372 clear_showcmd();
10373#endif
10374 return NUL;
10375}
10376#endif
10377
10378/*
10379 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10380 * Returns the char to be inserted, or NUL if none found.
10381 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010383ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384{
10385 int c;
10386 int temp;
10387 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010388 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389
10390 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10391 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010392 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 return NUL;
10394 }
10395
10396 /* try to advance to the cursor column */
10397 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010398 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 prev_ptr = ptr;
10400 validate_virtcol();
10401 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10402 {
10403 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010404 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 }
10406 if ((colnr_T)temp > curwin->w_virtcol)
10407 ptr = prev_ptr;
10408
10409#ifdef FEAT_MBYTE
10410 c = (*mb_ptr2char)(ptr);
10411#else
10412 c = *ptr;
10413#endif
10414 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010415 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 return c;
10417}
10418
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010419/*
10420 * CTRL-Y or CTRL-E typed in Insert mode.
10421 */
10422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010423ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010424{
10425 int c = tc;
10426
10427#ifdef FEAT_INS_EXPAND
10428 if (ctrl_x_mode == CTRL_X_SCROLL)
10429 {
10430 if (c == Ctrl_Y)
10431 scrolldown_clamp();
10432 else
10433 scrollup_clamp();
10434 redraw_later(VALID);
10435 }
10436 else
10437#endif
10438 {
10439 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10440 if (c != NUL)
10441 {
10442 long tw_save;
10443
10444 /* The character must be taken literally, insert like it
10445 * was typed after a CTRL-V, and pretend 'textwidth'
10446 * wasn't set. Digits, 'o' and 'x' are special after a
10447 * CTRL-V, don't use it for these. */
10448 if (c < 256 && !isalnum(c))
10449 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10450 tw_save = curbuf->b_p_tw;
10451 curbuf->b_p_tw = -1;
10452 insert_special(c, TRUE, FALSE);
10453 curbuf->b_p_tw = tw_save;
10454#ifdef FEAT_RIGHTLEFT
10455 revins_chars++;
10456 revins_legal++;
10457#endif
10458 c = Ctrl_V; /* pretend CTRL-V is last character */
10459 auto_format(FALSE, TRUE);
10460 }
10461 }
10462 return c;
10463}
10464
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465#ifdef FEAT_SMARTINDENT
10466/*
10467 * Try to do some very smart auto-indenting.
10468 * Used when inserting a "normal" character.
10469 */
10470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010471ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 pos_T *pos, old_pos;
10474 char_u *ptr;
10475 int i;
10476 int temp;
10477
10478 /*
10479 * do some very smart indenting when entering '{' or '}'
10480 */
10481 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10482 {
10483 /*
10484 * for '}' set indent equal to indent of line containing matching '{'
10485 */
10486 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10487 {
10488 old_pos = curwin->w_cursor;
10489 /*
10490 * If the matching '{' has a ')' immediately before it (ignoring
10491 * white-space), then line up with the start of the line
10492 * containing the matching '(' if there is one. This handles the
10493 * case where an "if (..\n..) {" statement continues over multiple
10494 * lines -- webb
10495 */
10496 ptr = ml_get(pos->lnum);
10497 i = pos->col;
10498 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010499 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 ;
10501 curwin->w_cursor.lnum = pos->lnum;
10502 curwin->w_cursor.col = i;
10503 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10504 curwin->w_cursor = *pos;
10505 i = get_indent();
10506 curwin->w_cursor = old_pos;
10507#ifdef FEAT_VREPLACE
10508 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010509 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 else
10511#endif
10512 (void)set_indent(i, SIN_CHANGED);
10513 }
10514 else if (curwin->w_cursor.col > 0)
10515 {
10516 /*
10517 * when inserting '{' after "O" reduce indent, but not
10518 * more than indent of previous line
10519 */
10520 temp = TRUE;
10521 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10522 {
10523 old_pos = curwin->w_cursor;
10524 i = get_indent();
10525 while (curwin->w_cursor.lnum > 1)
10526 {
10527 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10528
10529 /* ignore empty lines and lines starting with '#'. */
10530 if (*ptr != '#' && *ptr != NUL)
10531 break;
10532 }
10533 if (get_indent() >= i)
10534 temp = FALSE;
10535 curwin->w_cursor = old_pos;
10536 }
10537 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010538 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540 }
10541
10542 /*
10543 * set indent of '#' always to 0
10544 */
10545 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10546 {
10547 /* remember current indent for next line */
10548 old_indent = get_indent();
10549 (void)set_indent(0, SIN_CHANGED);
10550 }
10551
10552 /* Adjust ai_col, the char at this position can be deleted. */
10553 if (ai_col > curwin->w_cursor.col)
10554 ai_col = curwin->w_cursor.col;
10555}
10556#endif
10557
10558/*
10559 * Get the value that w_virtcol would have when 'list' is off.
10560 * Unless 'cpo' contains the 'L' flag.
10561 */
10562 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010563get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10566 return getvcol_nolist(&curwin->w_cursor);
10567 validate_virtcol();
10568 return curwin->w_virtcol;
10569}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010570
10571#ifdef FEAT_AUTOCMD
10572/*
10573 * Handle the InsertCharPre autocommand.
10574 * "c" is the character that was typed.
10575 * Return a pointer to allocated memory with the replacement string.
10576 * Return NULL to continue inserting "c".
10577 */
10578 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010579do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010580{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010581 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010582 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010583
10584 /* Return quickly when there is nothing to do. */
10585 if (!has_insertcharpre())
10586 return NULL;
10587
Bram Moolenaar704984a2012-06-01 14:57:51 +020010588#ifdef FEAT_MBYTE
10589 if (has_mbyte)
10590 buf[(*mb_char2bytes)(c, buf)] = NUL;
10591 else
10592#endif
10593 {
10594 buf[0] = c;
10595 buf[1] = NUL;
10596 }
10597
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010598 /* Lock the text to avoid weird things from happening. */
10599 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010600 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010601
Bram Moolenaar704984a2012-06-01 14:57:51 +020010602 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010603 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010604 {
10605 /* Get the value of v:char. It may be empty or more than one
10606 * character. Only use it when changed, otherwise continue with the
10607 * original character to avoid breaking autoindent. */
10608 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10609 res = vim_strsave(get_vim_var_str(VV_CHAR));
10610 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010611
10612 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10613 --textlock;
10614
10615 return res;
10616}
10617#endif