blob: a9e6343a806c3c932f5459db8c780d7b1c9ecd93 [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
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001457 && (ctrl_x_mode == CTRL_X_NORMAL
1458 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001459 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 goto normalchar;
1461
1462docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001463 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001464#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001465 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001466#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001467 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001469#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001470 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001471#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001472 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 break;
1474#endif /* FEAT_INS_EXPAND */
1475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001476 case Ctrl_Y: /* copy from previous line or scroll down */
1477 case Ctrl_E: /* copy from next line or scroll up */
1478 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 break;
1480
1481 default:
1482#ifdef UNIX
1483 if (c == intr_char) /* special interrupt char */
1484 goto do_intr;
1485#endif
1486
Bram Moolenaare659c952011-05-19 17:25:41 +02001487normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001489 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001491#ifdef FEAT_AUTOCMD
1492 if (!p_paste)
1493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 /* Trigger InsertCharPre. */
1495 char_u *str = do_insert_char_pre(c);
1496 char_u *p;
1497
1498 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001499 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001500 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001502 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001503 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 c = PTR2CHAR(p);
1506 if (c == CAR || c == K_KENTER || c == NL)
1507 ins_eol(c);
1508 else
1509 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001510 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001511 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001512 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001513 vim_free(str);
1514 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001515 }
1516
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001517 /* If the new value is already inserted or an empty string
1518 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001519 if (c == NUL)
1520 break;
1521 }
1522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523#ifdef FEAT_SMARTINDENT
1524 /* Try to perform smart-indenting. */
1525 ins_try_si(c);
1526#endif
1527
1528 if (c == ' ')
1529 {
1530 inserted_space = TRUE;
1531#ifdef FEAT_CINDENT
1532 if (inindent(0))
1533 can_cindent = FALSE;
1534#endif
1535 if (Insstart_blank_vcol == MAXCOL
1536 && curwin->w_cursor.lnum == Insstart.lnum)
1537 Insstart_blank_vcol = get_nolist_virtcol();
1538 }
1539
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001540 /* Insert a normal character and check for abbreviations on a
1541 * special character. Let CTRL-] expand abbreviations without
1542 * inserting it. */
1543 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#ifdef FEAT_MBYTE
1545 /* Add ABBR_OFF for characters above 0x100, this is
1546 * what check_abbr() expects. */
1547 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1548#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001549 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 insert_special(c, FALSE, FALSE);
1552#ifdef FEAT_RIGHTLEFT
1553 revins_legal++;
1554 revins_chars++;
1555#endif
1556 }
1557
1558 auto_format(FALSE, TRUE);
1559
1560#ifdef FEAT_FOLDING
1561 /* When inserting a character the cursor line must never be in a
1562 * closed fold. */
1563 foldOpenCursor();
1564#endif
1565 break;
1566 } /* end of switch (c) */
1567
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001568#ifdef FEAT_AUTOCMD
1569 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001570 if (c != K_CURSORHOLD
1571# ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001572 /* but not in CTRL-X mode, a script can't restore the state */
1573 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar245c4102016-04-20 17:37:41 +02001574# endif
1575 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001576 did_cursorhold = FALSE;
1577#endif
1578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /* If the cursor was moved we didn't just insert a space */
1580 if (arrow_used)
1581 inserted_space = FALSE;
1582
1583#ifdef FEAT_CINDENT
1584 if (can_cindent && cindent_on()
1585# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001586 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587# endif
1588 )
1589 {
1590force_cindent:
1591 /*
1592 * Indent now if a key was typed that is in 'cinkeys'.
1593 */
1594 if (in_cinkeys(c, ' ', line_is_white))
1595 {
1596 if (stop_arrow() == OK)
1597 /* re-indent the current line */
1598 do_c_expr_indent();
1599 }
1600 }
1601#endif /* FEAT_CINDENT */
1602
1603 } /* for (;;) */
1604 /* NOTREACHED */
1605}
1606
1607/*
1608 * Redraw for Insert mode.
1609 * This is postponed until getting the next character to make '$' in the 'cpo'
1610 * option work correctly.
1611 * Only redraw when there are no characters available. This speeds up
1612 * inserting sequences of characters (e.g., for CTRL-R).
1613 */
1614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615ins_redraw(
1616 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001618#ifdef FEAT_CONCEAL
1619 linenr_T conceal_old_cursor_line = 0;
1620 linenr_T conceal_new_cursor_line = 0;
1621 int conceal_update_lines = FALSE;
1622#endif
1623
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 if (char_avail())
1625 return;
1626
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001627#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001628 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1629 * visible, the command might delete it. */
1630 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001631# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001633# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001634# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001635 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001636# endif
1637# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001638 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001639# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001640 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001641# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001642 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001643# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001644# ifdef FEAT_INS_EXPAND
1645 && !pum_visible()
1646# endif
1647 )
1648 {
1649# ifdef FEAT_SYN_HL
1650 /* Need to update the screen first, to make sure syntax
1651 * highlighting is correct after making a change (e.g., inserting
1652 * a "(". The autocommand may also require a redraw, so it's done
1653 * again below, unfortunately. */
1654 if (syntax_present(curwin) && must_redraw)
1655 update_screen(0);
1656# endif
1657# ifdef FEAT_AUTOCMD
1658 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001659 {
1660 /* Make sure curswant is correct, an autocommand may call
1661 * getcurpos(). */
1662 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001663 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001664 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665# endif
1666# ifdef FEAT_CONCEAL
1667 if (curwin->w_p_cole > 0)
1668 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001669# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001671# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672 conceal_new_cursor_line = curwin->w_cursor.lnum;
1673 conceal_update_lines = TRUE;
1674 }
1675# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001676# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001677 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001678# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001679 }
1680#endif
1681
1682#ifdef FEAT_AUTOCMD
1683 /* Trigger TextChangedI if b_changedtick differs. */
1684 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001685 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001686# ifdef FEAT_INS_EXPAND
1687 && !pum_visible()
1688# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001689 )
1690 {
1691 if (last_changedtick_buf == curbuf)
1692 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1693 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001694 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001696#endif
1697
1698 if (must_redraw)
1699 update_screen(0);
1700 else if (clear_cmdline || redraw_cmdline)
1701 showmode(); /* clear cmdline and show mode */
1702# if defined(FEAT_CONCEAL)
1703 if ((conceal_update_lines
1704 && (conceal_old_cursor_line != conceal_new_cursor_line
1705 || conceal_cursor_line(curwin)))
1706 || need_cursor_line_redraw)
1707 {
1708 if (conceal_old_cursor_line != conceal_new_cursor_line)
1709 update_single_line(curwin, conceal_old_cursor_line);
1710 update_single_line(curwin, conceal_new_cursor_line == 0
1711 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1712 curwin->w_valid &= ~VALID_CROW;
1713 }
1714# endif
1715 showruler(FALSE);
1716 setcursor();
1717 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718}
1719
1720/*
1721 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1722 */
1723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001727 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001730 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001735 did_putchar = TRUE;
1736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1738
1739#ifdef FEAT_CMDL_INFO
1740 add_to_showcmd_c(Ctrl_V);
1741#endif
1742
1743 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001744 if (did_putchar)
1745 /* when the line fits in 'columns' the '^' is at the start of the next
1746 * line and will not removed by the redraw */
1747 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_CMDL_INFO
1749 clear_showcmd();
1750#endif
1751 insert_special(c, FALSE, TRUE);
1752#ifdef FEAT_RIGHTLEFT
1753 revins_chars++;
1754 revins_legal++;
1755#endif
1756}
1757
1758/*
1759 * Put a character directly onto the screen. It's not stored in a buffer.
1760 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1761 */
1762static int pc_status;
1763#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1764#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1765#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1766#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768static int pc_attr;
1769static int pc_row;
1770static int pc_col;
1771
1772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int attr;
1776
1777 if (ScreenLines != NULL)
1778 {
1779 update_topline(); /* just in case w_topline isn't valid */
1780 validate_cursor();
1781 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001782 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 else
1784 attr = 0;
1785 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001786 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1788 pc_status = PC_STATUS_UNSET;
1789#endif
1790#ifdef FEAT_RIGHTLEFT
1791 if (curwin->w_p_rl)
1792 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001793 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794# ifdef FEAT_MBYTE
1795 if (has_mbyte)
1796 {
1797 int fix_col = mb_fix_col(pc_col, pc_row);
1798
1799 if (fix_col != pc_col)
1800 {
1801 screen_putchar(' ', pc_row, fix_col, attr);
1802 --curwin->w_wcol;
1803 pc_status = PC_STATUS_RIGHT;
1804 }
1805 }
1806# endif
1807 }
1808 else
1809#endif
1810 {
1811 pc_col += curwin->w_wcol;
1812#ifdef FEAT_MBYTE
1813 if (mb_lefthalve(pc_row, pc_col))
1814 pc_status = PC_STATUS_LEFT;
1815#endif
1816 }
1817
1818 /* save the character to be able to put it back */
1819#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1820 if (pc_status == PC_STATUS_UNSET)
1821#endif
1822 {
1823 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1824 pc_status = PC_STATUS_SET;
1825 }
1826 screen_putchar(c, pc_row, pc_col, attr);
1827 }
1828}
1829
1830/*
1831 * Undo the previous edit_putchar().
1832 */
1833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1837 {
1838#if defined(FEAT_MBYTE)
1839 if (pc_status == PC_STATUS_RIGHT)
1840 ++curwin->w_wcol;
1841 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1842 redrawWinline(curwin->w_cursor.lnum, FALSE);
1843 else
1844#endif
1845 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1846 }
1847}
1848
1849/*
1850 * Called when p_dollar is set: display a '$' at the end of the changed text
1851 * Only works when cursor is in the line that changes.
1852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 colnr_T save_col;
1857
1858 if (!redrawing())
1859 return;
1860
1861 cursor_off();
1862 save_col = curwin->w_cursor.col;
1863 curwin->w_cursor.col = col;
1864#ifdef FEAT_MBYTE
1865 if (has_mbyte)
1866 {
1867 char_u *p;
1868
1869 /* If on the last byte of a multi-byte move to the first byte. */
1870 p = ml_get_curline();
1871 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1872 }
1873#endif
1874 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001875 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 edit_putchar('$', FALSE);
1878 dollar_vcol = curwin->w_virtcol;
1879 }
1880 curwin->w_cursor.col = save_col;
1881}
1882
1883/*
1884 * Call this function before moving the cursor from the normal insert position
1885 * in insert mode.
1886 */
1887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001890 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001892 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 redrawWinline(curwin->w_cursor.lnum, FALSE);
1894 }
1895}
1896
1897/*
1898 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1899 * Keep the cursor on the same character.
1900 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1901 * type == INDENT_DEC decrease indent (for CTRL-D)
1902 * type == INDENT_SET set indent to "amount"
1903 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1904 */
1905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001906change_indent(
1907 int type,
1908 int amount,
1909 int round,
1910 int replaced, /* replaced character, put on replace stack */
1911 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913 int vcol;
1914 int last_vcol;
1915 int insstart_less; /* reduction for Insstart.col */
1916 int new_cursor_col;
1917 int i;
1918 char_u *ptr;
1919 int save_p_list;
1920 int start_col;
1921 colnr_T vc;
1922#ifdef FEAT_VREPLACE
1923 colnr_T orig_col = 0; /* init for GCC */
1924 char_u *new_line, *orig_line = NULL; /* init for GCC */
1925
1926 /* VREPLACE mode needs to know what the line was like before changing */
1927 if (State & VREPLACE_FLAG)
1928 {
1929 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1930 orig_col = curwin->w_cursor.col;
1931 }
1932#endif
1933
1934 /* for the following tricks we don't want list mode */
1935 save_p_list = curwin->w_p_list;
1936 curwin->w_p_list = FALSE;
1937 vc = getvcol_nolist(&curwin->w_cursor);
1938 vcol = vc;
1939
1940 /*
1941 * For Replace mode we need to fix the replace stack later, which is only
1942 * possible when the cursor is in the indent. Remember the number of
1943 * characters before the cursor if it's possible.
1944 */
1945 start_col = curwin->w_cursor.col;
1946
1947 /* determine offset from first non-blank */
1948 new_cursor_col = curwin->w_cursor.col;
1949 beginline(BL_WHITE);
1950 new_cursor_col -= curwin->w_cursor.col;
1951
1952 insstart_less = curwin->w_cursor.col;
1953
1954 /*
1955 * If the cursor is in the indent, compute how many screen columns the
1956 * cursor is to the left of the first non-blank.
1957 */
1958 if (new_cursor_col < 0)
1959 vcol = get_indent() - vcol;
1960
1961 if (new_cursor_col > 0) /* can't fix replace stack */
1962 start_col = -1;
1963
1964 /*
1965 * Set the new indent. The cursor will be put on the first non-blank.
1966 */
1967 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001968 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 else
1970 {
1971#ifdef FEAT_VREPLACE
1972 int save_State = State;
1973
1974 /* Avoid being called recursively. */
1975 if (State & VREPLACE_FLAG)
1976 State = INSERT;
1977#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001978 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef FEAT_VREPLACE
1980 State = save_State;
1981#endif
1982 }
1983 insstart_less -= curwin->w_cursor.col;
1984
1985 /*
1986 * Try to put cursor on same character.
1987 * If the cursor is at or after the first non-blank in the line,
1988 * compute the cursor column relative to the column of the first
1989 * non-blank character.
1990 * If we are not in insert mode, leave the cursor on the first non-blank.
1991 * If the cursor is before the first non-blank, position it relative
1992 * to the first non-blank, counted in screen columns.
1993 */
1994 if (new_cursor_col >= 0)
1995 {
1996 /*
1997 * When changing the indent while the cursor is touching it, reset
1998 * Insstart_col to 0.
1999 */
2000 if (new_cursor_col == 0)
2001 insstart_less = MAXCOL;
2002 new_cursor_col += curwin->w_cursor.col;
2003 }
2004 else if (!(State & INSERT))
2005 new_cursor_col = curwin->w_cursor.col;
2006 else
2007 {
2008 /*
2009 * Compute the screen column where the cursor should be.
2010 */
2011 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002012 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 /*
2015 * Advance the cursor until we reach the right screen column.
2016 */
2017 vcol = last_vcol = 0;
2018 new_cursor_col = -1;
2019 ptr = ml_get_curline();
2020 while (vcol <= (int)curwin->w_virtcol)
2021 {
2022 last_vcol = vcol;
2023#ifdef FEAT_MBYTE
2024 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002025 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 else
2027#endif
2028 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002029 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 vcol = last_vcol;
2032
2033 /*
2034 * May need to insert spaces to be able to position the cursor on
2035 * the right screen column.
2036 */
2037 if (vcol != (int)curwin->w_virtcol)
2038 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002039 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002041 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (ptr != NULL)
2043 {
2044 new_cursor_col += i;
2045 ptr[i] = NUL;
2046 while (--i >= 0)
2047 ptr[i] = ' ';
2048 ins_str(ptr);
2049 vim_free(ptr);
2050 }
2051 }
2052
2053 /*
2054 * When changing the indent while the cursor is in it, reset
2055 * Insstart_col to 0.
2056 */
2057 insstart_less = MAXCOL;
2058 }
2059
2060 curwin->w_p_list = save_p_list;
2061
2062 if (new_cursor_col <= 0)
2063 curwin->w_cursor.col = 0;
2064 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002065 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 curwin->w_set_curswant = TRUE;
2067 changed_cline_bef_curs();
2068
2069 /*
2070 * May have to adjust the start of the insert.
2071 */
2072 if (State & INSERT)
2073 {
2074 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2075 {
2076 if ((int)Insstart.col <= insstart_less)
2077 Insstart.col = 0;
2078 else
2079 Insstart.col -= insstart_less;
2080 }
2081 if ((int)ai_col <= insstart_less)
2082 ai_col = 0;
2083 else
2084 ai_col -= insstart_less;
2085 }
2086
2087 /*
2088 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2089 * If the number of characters before the cursor decreased, need to pop a
2090 * few characters from the replace stack.
2091 * If the number of characters before the cursor increased, need to push a
2092 * few NULs onto the replace stack.
2093 */
2094 if (REPLACE_NORMAL(State) && start_col >= 0)
2095 {
2096 while (start_col > (int)curwin->w_cursor.col)
2097 {
2098 replace_join(0); /* remove a NUL from the replace stack */
2099 --start_col;
2100 }
2101 while (start_col < (int)curwin->w_cursor.col || replaced)
2102 {
2103 replace_push(NUL);
2104 if (replaced)
2105 {
2106 replace_push(replaced);
2107 replaced = NUL;
2108 }
2109 ++start_col;
2110 }
2111 }
2112
2113#ifdef FEAT_VREPLACE
2114 /*
2115 * For VREPLACE mode, we also have to fix the replace stack. In this case
2116 * it is always possible because we backspace over the whole line and then
2117 * put it back again the way we wanted it.
2118 */
2119 if (State & VREPLACE_FLAG)
2120 {
2121 /* If orig_line didn't allocate, just return. At least we did the job,
2122 * even if you can't backspace. */
2123 if (orig_line == NULL)
2124 return;
2125
2126 /* Save new line */
2127 new_line = vim_strsave(ml_get_curline());
2128 if (new_line == NULL)
2129 return;
2130
2131 /* We only put back the new line up to the cursor */
2132 new_line[curwin->w_cursor.col] = NUL;
2133
2134 /* Put back original line */
2135 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2136 curwin->w_cursor.col = orig_col;
2137
2138 /* Backspace from cursor to start of line */
2139 backspace_until_column(0);
2140
2141 /* Insert new stuff into line again */
2142 ins_bytes(new_line);
2143
2144 vim_free(new_line);
2145 }
2146#endif
2147}
2148
2149/*
2150 * Truncate the space at the end of a line. This is to be used only in an
2151 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2152 * modes.
2153 */
2154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 int i;
2158
2159 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002160 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 if (State & REPLACE_FLAG)
2163 replace_join(0); /* remove a NUL from the replace stack */
2164 }
2165 line[i + 1] = NUL;
2166}
2167
2168#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2169 || defined(FEAT_COMMENTS) || defined(PROTO)
2170/*
2171 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2172 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002173 * Will attempt not to go before "col" even when there is a composing
2174 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
2176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 while ((int)curwin->w_cursor.col > col)
2180 {
2181 curwin->w_cursor.col--;
2182 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002183 replace_do_bs(col);
2184 else if (!del_char_after_col(col))
2185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187}
2188#endif
2189
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002190/*
2191 * Like del_char(), but make sure not to go before column "limit_col".
2192 * Only matters when there are composing characters.
2193 * Return TRUE when something was deleted.
2194 */
2195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002196del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002197{
2198#ifdef FEAT_MBYTE
2199 if (enc_utf8 && limit_col >= 0)
2200 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002201 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002202
2203 /* Make sure the cursor is at the start of a character, but
2204 * skip forward again when going too far back because of a
2205 * composing character. */
2206 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002207 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002208 {
2209 int l = utf_ptr2len(ml_get_cursor());
2210
2211 if (l == 0) /* end of line */
2212 break;
2213 curwin->w_cursor.col += l;
2214 }
2215 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2216 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002217 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002218 }
2219 else
2220#endif
2221 (void)del_char(FALSE);
2222 return TRUE;
2223}
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2226/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002227 * CTRL-X pressed in Insert mode.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002231{
2232 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2233 * CTRL-V works like CTRL-N */
2234 if (ctrl_x_mode != CTRL_X_CMDLINE)
2235 {
2236 /* if the next ^X<> won't ADD nothing, then reset
2237 * compl_cont_status */
2238 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002239 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002240 else
2241 compl_cont_status = 0;
2242 /* We're not sure which CTRL-X mode it will be yet */
2243 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2244 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2245 edit_submode_pre = NULL;
2246 showmode();
2247 }
2248}
2249
2250/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002251 * Whether other than default completion has been selected.
2252 */
2253 int
2254ctrl_x_mode_not_default(void)
2255{
2256 return ctrl_x_mode != CTRL_X_NORMAL;
2257}
2258
2259/*
2260 * Whether CTRL-X was typed without a following character.
2261 */
2262 int
2263ctrl_x_mode_not_defined_yet(void)
2264{
2265 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2266}
2267
2268/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002269 * Return TRUE if the 'dict' or 'tsr' option can be used.
2270 */
2271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002272has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002273{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002274 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002275# ifdef FEAT_SPELL
2276 && !curwin->w_p_spell
2277# endif
2278 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002279 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2280 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002281 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002282 edit_submode = NULL;
2283 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2284 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002285 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002286 if (emsg_silent == 0)
2287 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002288 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002289 setcursor();
2290 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002291#ifdef FEAT_EVAL
2292 if (!get_vim_var_nr(VV_TESTING))
2293#endif
2294 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 }
2296 return FALSE;
2297 }
2298 return TRUE;
2299}
2300
2301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2303 * This depends on the current mode.
2304 */
2305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002306vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 /* Always allow ^R - let it's results then be checked */
2309 if (c == Ctrl_R)
2310 return TRUE;
2311
Bram Moolenaare3226be2005-12-18 22:10:00 +00002312 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002314 return TRUE;
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 switch (ctrl_x_mode)
2317 {
2318 case 0: /* Not in any CTRL-X mode */
2319 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2320 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002321 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2323 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2324 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002325 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002326 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 case CTRL_X_SCROLL:
2328 return (c == Ctrl_Y || c == Ctrl_E);
2329 case CTRL_X_WHOLE_LINE:
2330 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2331 case CTRL_X_FILES:
2332 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2333 case CTRL_X_DICTIONARY:
2334 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2335 case CTRL_X_THESAURUS:
2336 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2337 case CTRL_X_TAGS:
2338 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2339#ifdef FEAT_FIND_ID
2340 case CTRL_X_PATH_PATTERNS:
2341 return (c == Ctrl_P || c == Ctrl_N);
2342 case CTRL_X_PATH_DEFINES:
2343 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2344#endif
2345 case CTRL_X_CMDLINE:
2346 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2347 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002348#ifdef FEAT_COMPL_FUNC
2349 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002350 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002351 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002352 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002353#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002354 case CTRL_X_SPELL:
2355 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002356 case CTRL_X_EVAL:
2357 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002359 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 return FALSE;
2361}
2362
2363/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002364 * Return TRUE when character "c" is part of the item currently being
2365 * completed. Used to decide whether to abandon complete mode when the menu
2366 * is visible.
2367 */
2368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002370{
2371 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2372 /* When expanding an identifier only accept identifier chars. */
2373 return vim_isIDc(c);
2374
2375 switch (ctrl_x_mode)
2376 {
2377 case CTRL_X_FILES:
2378 /* When expanding file name only accept file name chars. But not
2379 * path separators, so that "proto/<Tab>" expands files in
2380 * "proto", not "proto/" as a whole */
2381 return vim_isfilec(c) && !vim_ispathsep(c);
2382
2383 case CTRL_X_CMDLINE:
2384 case CTRL_X_OMNI:
2385 /* Command line and Omni completion can work with just about any
2386 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002387 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002388
2389 case CTRL_X_WHOLE_LINE:
2390 /* For while line completion a space can be part of the line. */
2391 return vim_isprintc(c);
2392 }
2393 return vim_iswordc(c);
2394}
2395
2396/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002397 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002399 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * the rest of the word to be in -- webb
2401 */
2402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002403ins_compl_add_infercase(
2404 char_u *str,
2405 int len,
2406 int icase,
2407 char_u *fname,
2408 int dir,
2409 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 char_u *p;
2412 int i, c;
2413 int actual_len; /* Take multi-byte characters */
2414 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002415 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002416 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 int has_lower = FALSE;
2418 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002420 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002422 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaara2993e12007-08-12 14:38:46 +00002424 /* Find actual length of completion. */
2425#ifdef FEAT_MBYTE
2426 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002428 p = str;
2429 actual_len = 0;
2430 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002432 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002433 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002436 else
2437#endif
2438 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
Bram Moolenaara2993e12007-08-12 14:38:46 +00002440 /* Find actual length of original text. */
2441#ifdef FEAT_MBYTE
2442 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002444 p = compl_orig_text;
2445 actual_compl_length = 0;
2446 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002448 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002449 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002452 else
2453#endif
2454 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002456 /* "actual_len" may be smaller than "actual_compl_length" when using
2457 * thesaurus, only use the minimum when comparing. */
2458 min_len = actual_len < actual_compl_length
2459 ? actual_len : actual_compl_length;
2460
Bram Moolenaara2993e12007-08-12 14:38:46 +00002461 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002462 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002463 if (wca != NULL)
2464 {
2465 p = str;
2466 for (i = 0; i < actual_len; ++i)
2467#ifdef FEAT_MBYTE
2468 if (has_mbyte)
2469 wca[i] = mb_ptr2char_adv(&p);
2470 else
2471#endif
2472 wca[i] = *(p++);
2473
2474 /* Rule 1: Were any chars converted to lower? */
2475 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002476 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002477 {
2478#ifdef FEAT_MBYTE
2479 if (has_mbyte)
2480 c = mb_ptr2char_adv(&p);
2481 else
2482#endif
2483 c = *(p++);
2484 if (MB_ISLOWER(c))
2485 {
2486 has_lower = TRUE;
2487 if (MB_ISUPPER(wca[i]))
2488 {
2489 /* Rule 1 is satisfied. */
2490 for (i = actual_compl_length; i < actual_len; ++i)
2491 wca[i] = MB_TOLOWER(wca[i]);
2492 break;
2493 }
2494 }
2495 }
2496
2497 /*
2498 * Rule 2: No lower case, 2nd consecutive letter converted to
2499 * upper case.
2500 */
2501 if (!has_lower)
2502 {
2503 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002504 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 {
2506#ifdef FEAT_MBYTE
2507 if (has_mbyte)
2508 c = mb_ptr2char_adv(&p);
2509 else
2510#endif
2511 c = *(p++);
2512 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2513 {
2514 /* Rule 2 is satisfied. */
2515 for (i = actual_compl_length; i < actual_len; ++i)
2516 wca[i] = MB_TOUPPER(wca[i]);
2517 break;
2518 }
2519 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2520 }
2521 }
2522
2523 /* Copy the original case of the part we typed. */
2524 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002525 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 {
2527#ifdef FEAT_MBYTE
2528 if (has_mbyte)
2529 c = mb_ptr2char_adv(&p);
2530 else
2531#endif
2532 c = *(p++);
2533 if (MB_ISLOWER(c))
2534 wca[i] = MB_TOLOWER(wca[i]);
2535 else if (MB_ISUPPER(c))
2536 wca[i] = MB_TOUPPER(wca[i]);
2537 }
2538
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002539 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 * Generate encoding specific output from wide character array.
2541 * Multi-byte characters can occupy up to five bytes more than
2542 * ASCII characters, and we also need one byte for NUL, so stay
2543 * six bytes away from the edge of IObuff.
2544 */
2545 p = IObuff;
2546 i = 0;
2547 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002550 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 else
2552#endif
2553 *(p++) = wca[i++];
2554 *p = NUL;
2555
2556 vim_free(wca);
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002559 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2560 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002562 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563}
2564
2565/*
2566 * Add a match to the list of matches.
2567 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002569 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572ins_compl_add(
2573 char_u *str,
2574 int len,
2575 int icase,
2576 char_u *fname,
2577 char_u **cptext, /* extra text for popup menu or NULL */
2578 int cdir,
2579 int flags,
2580 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002582 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002583 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 ui_breakcheck();
2586 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002587 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (len < 0)
2589 len = (int)STRLEN(str);
2590
2591 /*
2592 * If the same match is already present, don't add it.
2593 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002596 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 do
2598 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002600 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002601 && match->cp_str[len] == NUL)
2602 return NOTDONE;
2603 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002604 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002607 /* Remove any popup menu before changing the list of matches. */
2608 ins_compl_del_pum();
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 /*
2611 * Allocate a new match structure.
2612 * Copy the values to the new match structure.
2613 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002614 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002616 return FAIL;
2617 match->cp_number = -1;
2618 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002619 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002620 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
2622 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002628 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2630 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002631 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002632 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002633 && compl_curr_match->cp_fname != NULL
2634 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002635 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002636 else if (fname != NULL)
2637 {
2638 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002639 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002642 match->cp_fname = NULL;
2643 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002644
2645 if (cptext != NULL)
2646 {
2647 int i;
2648
2649 for (i = 0; i < CPT_COUNT; ++i)
2650 if (cptext[i] != NULL && *cptext[i] != NUL)
2651 match->cp_text[i] = vim_strsave(cptext[i]);
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /*
2655 * Link the new match structure in the list of matches.
2656 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002657 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002658 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 else if (dir == FORWARD)
2660 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002661 match->cp_next = compl_curr_match->cp_next;
2662 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 else /* BACKWARD */
2665 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002666 match->cp_next = compl_curr_match;
2667 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002669 if (match->cp_next)
2670 match->cp_next->cp_prev = match;
2671 if (match->cp_prev)
2672 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002674 compl_first_match = match;
2675 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002677 /*
2678 * Find the longest common string if still doing that.
2679 */
2680 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2681 ins_compl_longest_match(match);
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 return OK;
2684}
2685
2686/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002687 * Return TRUE if "str[len]" matches with match->cp_str, considering
2688 * match->cp_icase.
2689 */
2690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002691ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002692{
2693 if (match->cp_icase)
2694 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2695 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2696}
2697
2698/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002699 * Reduce the longest common string for match "match".
2700 */
2701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002702ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002703{
2704 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002705 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002706 int had_match;
2707
2708 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002709 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002710 /* First match, use it as a whole. */
2711 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002712 if (compl_leader != NULL)
2713 {
2714 had_match = (curwin->w_cursor.col > compl_col);
2715 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002716 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002717 ins_redraw(FALSE);
2718
2719 /* When the match isn't there (to avoid matching itself) remove it
2720 * again after redrawing. */
2721 if (!had_match)
2722 ins_compl_delete();
2723 compl_used_match = FALSE;
2724 }
2725 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002726 else
2727 {
2728 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002729 p = compl_leader;
2730 s = match->cp_str;
2731 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002732 {
2733#ifdef FEAT_MBYTE
2734 if (has_mbyte)
2735 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002736 c1 = mb_ptr2char(p);
2737 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002738 }
2739 else
2740#endif
2741 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002742 c1 = *p;
2743 c2 = *s;
2744 }
2745 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2746 : (c1 != c2))
2747 break;
2748#ifdef FEAT_MBYTE
2749 if (has_mbyte)
2750 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002751 MB_PTR_ADV(p);
2752 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002753 }
2754 else
2755#endif
2756 {
2757 ++p;
2758 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002759 }
2760 }
2761
2762 if (*p != NUL)
2763 {
2764 /* Leader was shortened, need to change the inserted text. */
2765 *p = NUL;
2766 had_match = (curwin->w_cursor.col > compl_col);
2767 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002768 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002769 ins_redraw(FALSE);
2770
2771 /* When the match isn't there (to avoid matching itself) remove it
2772 * again after redrawing. */
2773 if (!had_match)
2774 ins_compl_delete();
2775 }
2776
2777 compl_used_match = FALSE;
2778 }
2779}
2780
2781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 * Add an array of matches to the list of matches.
2783 * Frees matches[].
2784 */
2785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002786ins_compl_add_matches(
2787 int num_matches,
2788 char_u **matches,
2789 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 int i;
2792 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002793 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar572cb562005-08-05 21:35:02 +00002795 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002796 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002797 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002799 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 FreeWild(num_matches, matches);
2801}
2802
2803/* Make the completion list cyclic.
2804 * Return the number of matches (excluding the original).
2805 */
2806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002809 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int count = 0;
2811
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002812 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 /*
2815 * Find the end of the list.
2816 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002817 match = compl_first_match;
2818 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002819 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002821 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 ++count;
2823 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002824 match->cp_next = compl_first_match;
2825 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827 return count;
2828}
2829
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002831 * Set variables that store noselect and noinsert behavior from the
2832 * 'completeopt' value.
2833 */
2834 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002835completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002836{
2837 compl_no_insert = FALSE;
2838 compl_no_select = FALSE;
2839 if (strstr((char *)p_cot, "noselect") != NULL)
2840 compl_no_select = TRUE;
2841 if (strstr((char *)p_cot, "noinsert") != NULL)
2842 compl_no_insert = TRUE;
2843}
2844
2845/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002846 * Start completion for the complete() function.
2847 * "startcol" is where the matched text starts (1 is first column).
2848 * "list" is the list of matches.
2849 */
2850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002851set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002852{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002853 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002854 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002855
Bram Moolenaara94bc432006-03-10 21:42:59 +00002856 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002857 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002858 ins_compl_prep(' ');
2859 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002860 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002861
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002862 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002863 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002864 startcol = curwin->w_cursor.col;
2865 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002866 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002867 /* compl_pattern doesn't need to be set */
2868 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2869 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002870 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002871 return;
2872
Bram Moolenaare4214502015-03-05 18:08:43 +01002873 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002874
2875 ins_compl_add_list(list);
2876 compl_matches = ins_compl_make_cyclic();
2877 compl_started = TRUE;
2878 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002879 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002880
2881 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002882 if (compl_no_insert || compl_no_select)
2883 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002884 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002885 if (compl_no_select)
2886 /* Down/Up has no real effect. */
2887 ins_complete(K_UP, FALSE);
2888 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002889 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002890 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002891 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002892
2893 /* Lazily show the popup menu, unless we got interrupted. */
2894 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002895 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002896 out_flush();
2897}
2898
2899
Bram Moolenaar9372a112005-12-06 19:59:18 +00002900/* "compl_match_array" points the currently displayed list of entries in the
2901 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002902static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903static int compl_match_arraysize;
2904
2905/*
2906 * Update the screen and when there is any scrolling remove the popup menu.
2907 */
2908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002909ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002910{
2911 int h;
2912
2913 if (compl_match_array != NULL)
2914 {
2915 h = curwin->w_cline_height;
2916 update_screen(0);
2917 if (h != curwin->w_cline_height)
2918 ins_compl_del_pum();
2919 }
2920}
2921
2922/*
2923 * Remove any popup menu.
2924 */
2925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002927{
2928 if (compl_match_array != NULL)
2929 {
2930 pum_undisplay();
Bram Moolenaar15675582018-02-09 12:29:56 +01002931 vim_clear((void **)&compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932 }
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 Moolenaar15675582018-02-09 12:29:56 +01003433 vim_clear((void **)&compl_pattern);
3434 vim_clear((void **)&compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003438
3439 ins_compl_del_pum();
3440 pum_clear();
3441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003442 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 do
3444 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003445 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003446 compl_curr_match = compl_curr_match->cp_next;
3447 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003449 if (match->cp_flags & FREE_FNAME)
3450 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003451 for (i = 0; i < CPT_COUNT; ++i)
3452 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003454 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3455 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003456 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003457 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459
3460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003463 compl_cont_status = 0;
3464 compl_started = FALSE;
3465 compl_matches = 0;
Bram Moolenaar15675582018-02-09 12:29:56 +01003466 vim_clear((void **)&compl_pattern);
3467 vim_clear((void **)&compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 edit_submode_extra = NULL;
Bram Moolenaar15675582018-02-09 12:29:56 +01003469 vim_clear((void **)&compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003470 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003471 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003472 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473}
3474
3475/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003476 * Return TRUE when Insert completion is active.
3477 */
3478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003480{
3481 return compl_started;
3482}
3483
3484/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003485 * Delete one character before the cursor and show the subset of the matches
3486 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003487 * Returns the character to be used, NUL if the work is done and another char
3488 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003489 */
3490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003491ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003492{
3493 char_u *line;
3494 char_u *p;
3495
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003496 line = ml_get_curline();
3497 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003498 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003499
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003500 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003501 * allow the word to be deleted, we won't match everything.
3502 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003503 if ((int)(p - line) - (int)compl_col < 0
3504 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003505 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3506 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3507 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003508 return K_BS;
3509
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003510 /* Deleted more than what was used to find matches or didn't finish
3511 * finding all matches: need to look for matches all over again. */
3512 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003513 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003514 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003515
Bram Moolenaara6557602006-02-04 22:43:20 +00003516 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003517 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003518 if (compl_leader != NULL)
3519 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003521 if (compl_shown_match != NULL)
3522 /* Make sure current match is not a hidden item. */
3523 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524 return NUL;
3525 }
3526 return K_BS;
3527}
Bram Moolenaara6557602006-02-04 22:43:20 +00003528
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003530 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3531 * be called.
3532 */
3533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003535{
3536 /* Return TRUE if we didn't complete finding matches or when the
3537 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3538 return compl_was_interrupted
3539 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3540 && compl_opt_refresh_always);
3541}
3542
3543/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003544 * Called after changing "compl_leader".
3545 * Show the popup menu with a different set of matches.
3546 * May also search for matches again if the previous search was interrupted.
3547 */
3548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003549ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003550{
3551 ins_compl_del_pum();
3552 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003553 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003554 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003555
3556 if (compl_started)
3557 ins_compl_set_original_text(compl_leader);
3558 else
3559 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003560#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003561 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003562#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003563 /*
3564 * Matches were cleared, need to search for them now. First display
3565 * the changed text before the cursor. Set "compl_restarting" to
3566 * avoid that the first match is inserted.
3567 */
3568 update_screen(0);
3569#ifdef FEAT_GUI
3570 if (gui.in_use)
3571 {
3572 /* Show the cursor after the match, not after the redrawn text. */
3573 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003574 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003576#endif
3577 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003578 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003579 compl_cont_status = 0;
3580 compl_restarting = FALSE;
3581 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003582
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003583 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003584
3585 /* Show the popup menu with a different set of matches. */
3586 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003587
3588 /* Don't let Enter select the original text when there is no popup menu. */
3589 if (compl_match_array == NULL)
3590 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003591}
3592
3593/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003594 * Return the length of the completion, from the completion start column to
3595 * the cursor column. Making sure it never goes below zero.
3596 */
3597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003599{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003600 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003601
3602 if (off < 0)
3603 return 0;
3604 return off;
3605}
3606
3607/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003608 * Append one character to the match leader. May reduce the number of
3609 * matches.
3610 */
3611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003613{
3614#ifdef FEAT_MBYTE
3615 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003616#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003617
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003618 if (stop_arrow() == FAIL)
3619 return;
3620#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003621 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3622 {
3623 char_u buf[MB_MAXBYTES + 1];
3624
3625 (*mb_char2bytes)(c, buf);
3626 buf[cc] = NUL;
3627 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003628 if (compl_opt_refresh_always)
3629 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003630 }
3631 else
3632#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003633 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003634 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003635 if (compl_opt_refresh_always)
3636 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003637 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003638
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003639 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003640 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003641 ins_compl_restart();
3642
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003643 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003644 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003645 * break redo. */
3646 if (!compl_opt_refresh_always)
3647 {
3648 vim_free(compl_leader);
3649 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003650 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003651 if (compl_leader != NULL)
3652 ins_compl_new_leader();
3653 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003654}
3655
3656/*
3657 * Setup for finding completions again without leaving CTRL-X mode. Used when
3658 * BS or a key was typed while still searching for matches.
3659 */
3660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003662{
3663 ins_compl_free();
3664 compl_started = FALSE;
3665 compl_matches = 0;
3666 compl_cont_status = 0;
3667 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003668}
3669
3670/*
3671 * Set the first match, the original text.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003675{
3676 char_u *p;
3677
3678 /* Replace the original text entry. */
3679 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3680 {
3681 p = vim_strsave(str);
3682 if (p != NULL)
3683 {
3684 vim_free(compl_first_match->cp_str);
3685 compl_first_match->cp_str = p;
3686 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003687 }
3688}
3689
3690/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003691 * Append one character to the match leader. May reduce the number of
3692 * matches.
3693 */
3694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003695ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003696{
3697 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003698 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003700 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003701
3702 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003703 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003704 {
3705 /* When still at the original match use the first entry that matches
3706 * the leader. */
3707 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3708 {
3709 p = NULL;
3710 for (cp = compl_shown_match->cp_next; cp != NULL
3711 && cp != compl_first_match; cp = cp->cp_next)
3712 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003713 if (compl_leader == NULL
3714 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003715 (int)STRLEN(compl_leader)))
3716 {
3717 p = cp->cp_str;
3718 break;
3719 }
3720 }
3721 if (p == NULL || (int)STRLEN(p) <= len)
3722 return;
3723 }
3724 else
3725 return;
3726 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003727 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003728 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003729 ins_compl_addleader(c);
3730}
3731
3732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003734 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003735 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003738ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003742 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 /* Forget any previous 'special' messages if this is actually
3745 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3746 */
3747 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3748 edit_submode_extra = NULL;
3749
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003750 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003751 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3752 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003753 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003755 /* Set "compl_get_longest" when finding the first matches. */
3756 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003757 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003758 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003759 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003760 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003761
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003762 }
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3765 {
3766 /*
3767 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3768 * it will be yet. Now we decide.
3769 */
3770 switch (c)
3771 {
3772 case Ctrl_E:
3773 case Ctrl_Y:
3774 ctrl_x_mode = CTRL_X_SCROLL;
3775 if (!(State & REPLACE_FLAG))
3776 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3777 else
3778 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3779 edit_submode_pre = NULL;
3780 showmode();
3781 break;
3782 case Ctrl_L:
3783 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3784 break;
3785 case Ctrl_F:
3786 ctrl_x_mode = CTRL_X_FILES;
3787 break;
3788 case Ctrl_K:
3789 ctrl_x_mode = CTRL_X_DICTIONARY;
3790 break;
3791 case Ctrl_R:
3792 /* Simply allow ^R to happen without affecting ^X mode */
3793 break;
3794 case Ctrl_T:
3795 ctrl_x_mode = CTRL_X_THESAURUS;
3796 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003797#ifdef FEAT_COMPL_FUNC
3798 case Ctrl_U:
3799 ctrl_x_mode = CTRL_X_FUNCTION;
3800 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003802 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003803 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003804#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003805 case 's':
3806 case Ctrl_S:
3807 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003808#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003809 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003810 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003811 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003812#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 case Ctrl_RSB:
3815 ctrl_x_mode = CTRL_X_TAGS;
3816 break;
3817#ifdef FEAT_FIND_ID
3818 case Ctrl_I:
3819 case K_S_TAB:
3820 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3821 break;
3822 case Ctrl_D:
3823 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3824 break;
3825#endif
3826 case Ctrl_V:
3827 case Ctrl_Q:
3828 ctrl_x_mode = CTRL_X_CMDLINE;
3829 break;
3830 case Ctrl_P:
3831 case Ctrl_N:
3832 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3833 * just started ^X mode, or there were enough ^X's to cancel
3834 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3835 * do normal expansion when interrupting a different mode (say
3836 * ^X^F^X^P or ^P^X^X^P, see below)
3837 * nothing changes if interrupting mode 0, (eg, the flag
3838 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 if (!(compl_cont_status & CONT_INTRPT))
3840 compl_cont_status |= CONT_LOCAL;
3841 else if (compl_cont_mode != 0)
3842 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 /* FALLTHROUGH */
3844 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 /* If we have typed at least 2 ^X's... for modes != 0, we set
3846 * compl_cont_status = 0 (eg, as if we had just started ^X
3847 * mode).
3848 * For mode 0, we set "compl_cont_mode" to an impossible
3849 * value, in both cases ^X^X can be used to restart the same
3850 * mode (avoiding ADDING mode).
3851 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3852 * 'complete' and local ^P expansions respectively.
3853 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3854 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (c == Ctrl_X)
3856 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 if (compl_cont_mode != 0)
3858 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003862 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 edit_submode = NULL;
3864 showmode();
3865 break;
3866 }
3867 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003868 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 /* We're already in CTRL-X mode, do we stay in it? */
3871 if (!vim_is_ctrl_x_key(c))
3872 {
3873 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003874 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 else
3876 ctrl_x_mode = CTRL_X_FINISHED;
3877 edit_submode = NULL;
3878 }
3879 showmode();
3880 }
3881
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 /* Show error message from attempted keyword completion (probably
3885 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003886 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003888 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3889 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 || ctrl_x_mode == CTRL_X_FINISHED)
3891 {
3892 /* Get here when we have finished typing a sequence of ^N and
3893 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003894 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003895 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003898 * If any of the original typed text has been changed, eg when
3899 * ignorecase is set, we must add back-spaces to the redo
3900 * buffer. We add as few as necessary to delete just the part
3901 * of the original text that has changed.
3902 * When using the longest match, edited the match or used
3903 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003905 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3906 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003907 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003908 ptr = NULL;
3909 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911
3912#ifdef FEAT_CINDENT
3913 want_cindent = (can_cindent && cindent_on());
3914#endif
3915 /*
3916 * When completing whole lines: fix indent for 'cindent'.
3917 * Otherwise, break line if it's too long.
3918 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921#ifdef FEAT_CINDENT
3922 /* re-indent the current line */
3923 if (want_cindent)
3924 {
3925 do_c_expr_indent();
3926 want_cindent = FALSE; /* don't do it again */
3927 }
3928#endif
3929 }
3930 else
3931 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003932 int prev_col = curwin->w_cursor.col;
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003935 if (prev_col > 0)
3936 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003937 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003938 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003940 if (prev_col > 0
3941 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3942 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003945 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003946 * the selection without inserting anything. When
3947 * compl_enter_selects is set the Enter key does the same. */
3948 if ((c == Ctrl_Y || (compl_enter_selects
3949 && (c == CAR || c == K_KENTER || c == NL)))
3950 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003951 retval = TRUE;
3952
Bram Moolenaar47247282016-08-02 22:36:02 +02003953 /* CTRL-E means completion is Ended, go back to the typed text.
3954 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003955 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003956 {
3957 ins_compl_delete();
3958 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003959 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003960 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003961 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003962 retval = TRUE;
3963 }
3964
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003965 auto_format(FALSE, TRUE);
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003968 compl_started = FALSE;
3969 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003970 if (!shortmess(SHM_COMPLETIONMENU))
3971 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003972 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003973 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (edit_submode != NULL)
3975 {
3976 edit_submode = NULL;
3977 showmode();
3978 }
3979
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003980#ifdef FEAT_CMDWIN
3981 if (c == Ctrl_C && cmdwin_type != 0)
3982 /* Avoid the popup menu remains displayed when leaving the
3983 * command line window. */
3984 update_screen(0);
3985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#ifdef FEAT_CINDENT
3987 /*
3988 * Indent now if a key was typed that is in 'cinkeys'.
3989 */
3990 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3991 do_c_expr_indent();
3992#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003993#ifdef FEAT_AUTOCMD
3994 /* Trigger the CompleteDone event to give scripts a chance to act
3995 * upon the completion. */
3996 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004000#ifdef FEAT_AUTOCMD
4001 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4002 /* Trigger the CompleteDone event to give scripts a chance to act
4003 * upon the (possibly failed) completion. */
4004 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 /* reset continue_* if we left expansion-mode, if we stay they'll be
4008 * (re)set properly in ins_complete() */
4009 if (!vim_is_ctrl_x_key(c))
4010 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004011 compl_cont_status = 0;
4012 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004014
4015 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016}
4017
4018/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004019 * Fix the redo buffer for the completion leader replacing some of the typed
4020 * text. This inserts backspaces and appends the changed text.
4021 * "ptr" is the known leader text or NUL.
4022 */
4023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004025{
4026 int len;
4027 char_u *p;
4028 char_u *ptr = ptr_arg;
4029
4030 if (ptr == NULL)
4031 {
4032 if (compl_leader != NULL)
4033 ptr = compl_leader;
4034 else
4035 return; /* nothing to do */
4036 }
4037 if (compl_orig_text != NULL)
4038 {
4039 p = compl_orig_text;
4040 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4041 ;
4042#ifdef FEAT_MBYTE
4043 if (len > 0)
4044 len -= (*mb_head_off)(p, p + len);
4045#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004046 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004047 AppendCharToRedobuff(K_BS);
4048 }
4049 else
4050 len = 0;
4051 if (ptr != NULL)
4052 AppendToRedobuffLit(ptr + len, -1);
4053}
4054
4055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4057 * (depending on flag) starting from buf and looking for a non-scanned
4058 * buffer (other than curbuf). curbuf is special, if it is called with
4059 * buf=curbuf then it has to be the first call for a given flag/expansion.
4060 *
4061 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4062 */
4063 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004064ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 if (flag == 'w') /* just windows */
4069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (buf == curbuf) /* first call for this flag/expansion */
4071 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004072 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 && wp->w_buffer->b_scanned)
4074 ;
4075 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 else
4078 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4079 * (unlisted buffers)
4080 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004081 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 && ((flag == 'U'
4083 ? buf->b_p_bl
4084 : (!buf->b_p_bl
4085 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004086 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ;
4088 return buf;
4089}
4090
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004091#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004093 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004094 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004095 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004097expand_by_function(
4098 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4099 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004100{
Bram Moolenaar82139082011-09-14 16:52:09 +02004101 list_T *matchlist = NULL;
4102 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004103 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004104 char_u *funcname;
4105 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004106 win_T *curwin_save;
4107 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004108 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004109
Bram Moolenaare344bea2005-09-01 20:46:49 +00004110 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4111 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004112 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004113
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004114 /* Call 'completefunc' to obtain the list of matches. */
4115 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004116 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004117
Bram Moolenaare344bea2005-09-01 20:46:49 +00004118 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004119 curwin_save = curwin;
4120 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004121
4122 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004123 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004124 {
4125 switch (rettv.v_type)
4126 {
4127 case VAR_LIST:
4128 matchlist = rettv.vval.v_list;
4129 break;
4130 case VAR_DICT:
4131 matchdict = rettv.vval.v_dict;
4132 break;
4133 default:
4134 /* TODO: Give error message? */
4135 clear_tv(&rettv);
4136 break;
4137 }
4138 }
4139
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004140 if (curwin_save != curwin || curbuf_save != curbuf)
4141 {
4142 EMSG(_(e_complwin));
4143 goto theend;
4144 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004145 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004146 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004147 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004148 {
4149 EMSG(_(e_compldel));
4150 goto theend;
4151 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004152
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004153 if (matchlist != NULL)
4154 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004155 else if (matchdict != NULL)
4156 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004157
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004158theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004159 if (matchdict != NULL)
4160 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004161 if (matchlist != NULL)
4162 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004163}
4164#endif /* FEAT_COMPL_FUNC */
4165
Bram Moolenaar39f05632006-03-19 22:15:26 +00004166#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004167/*
4168 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004169 */
4170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004171ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004172{
4173 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004174 int dir = compl_direction;
4175
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004176 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004177 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004178 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004179 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4180 /* if dir was BACKWARD then honor it just once */
4181 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004182 else if (did_emsg)
4183 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004184 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004185}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004186
4187/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004188 * Add completions from a dict.
4189 */
4190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004191ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004192{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004193 dictitem_T *di_refresh;
4194 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004195
4196 /* Check for optional "refresh" item. */
4197 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004198 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4199 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004200 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004201 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004202
4203 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4204 compl_opt_refresh_always = TRUE;
4205 }
4206
4207 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004208 di_words = dict_find(dict, (char_u *)"words", 5);
4209 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4210 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004211}
4212
4213/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004214 * Add a match to the list of matches from a typeval_T.
4215 * If the given string is already in the list of completions, then return
4216 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4217 * maybe because alloc() returns NULL, then FAIL is returned.
4218 */
4219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004220ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004221{
4222 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004223 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004224 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004225 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004226 char_u *(cptext[CPT_COUNT]);
4227
4228 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4229 {
4230 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4231 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4232 (char_u *)"abbr", FALSE);
4233 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4234 (char_u *)"menu", FALSE);
4235 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4236 (char_u *)"kind", FALSE);
4237 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4238 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004239 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4240 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004241 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4242 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004243 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004244 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004245 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4246 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004247 }
4248 else
4249 {
4250 word = get_tv_string_chk(tv);
4251 vim_memset(cptext, 0, sizeof(cptext));
4252 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004253 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004254 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004255 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004256}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004257#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004258
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259/*
4260 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004261 * The search starts at position "ini" in curbuf and in the direction
4262 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004263 * When "compl_started" is FALSE start at that position, otherwise continue
4264 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004265 * This may return before finding all the matches.
4266 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 */
4268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004269ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
4271 static pos_T first_match_pos;
4272 static pos_T last_match_pos;
4273 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004274 static int found_all = FALSE; /* Found all matches of a
4275 certain type. */
4276 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar572cb562005-08-05 21:35:02 +00004278 pos_T *pos;
4279 char_u **matches;
4280 int save_p_scs;
4281 int save_p_ws;
4282 int save_p_ic;
4283 int i;
4284 int num_matches;
4285 int len;
4286 int found_new_match;
4287 int type = ctrl_x_mode;
4288 char_u *ptr;
4289 char_u *dict = NULL;
4290 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004291 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004295 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 ins_buf->b_scanned = 0;
4297 found_all = FALSE;
4298 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004300 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 last_match_pos = first_match_pos = *ini;
4302 }
4303
Bram Moolenaar4475b622017-05-01 20:46:52 +02004304 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004305 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4307 for (;;)
4308 {
4309 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004310 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004312 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 * or if found_all says this entry is done. For ^X^L only use the
4314 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004315 if ((ctrl_x_mode == CTRL_X_NORMAL
4316 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 found_all = FALSE;
4320 while (*e_cpt == ',' || *e_cpt == ' ')
4321 e_cpt++;
4322 if (*e_cpt == '.' && !curbuf->b_scanned)
4323 {
4324 ins_buf = curbuf;
4325 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004326 /* Move the cursor back one character so that ^N can match the
4327 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004328 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004329 {
4330 /* Move the cursor to after the last character in the
4331 * buffer, so that word at start of buffer is found
4332 * correctly. */
4333 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4334 first_match_pos.col =
4335 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 last_match_pos = first_match_pos;
4338 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004339
4340 /* Remember the first match so that the loop stops when we
4341 * wrap and come back there a second time. */
4342 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4345 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4346 {
4347 /* Scan a buffer, but not the current one. */
4348 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4349 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004350 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 first_match_pos.col = last_match_pos.col = 0;
4352 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4353 last_match_pos.lnum = 0;
4354 type = 0;
4355 }
4356 else /* unloaded buffer, scan like dictionary */
4357 {
4358 found_all = TRUE;
4359 if (ins_buf->b_fname == NULL)
4360 continue;
4361 type = CTRL_X_DICTIONARY;
4362 dict = ins_buf->b_fname;
4363 dict_f = DICT_EXACT;
4364 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004365 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 ins_buf->b_fname == NULL
4367 ? buf_spname(ins_buf)
4368 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004369 ? ins_buf->b_fname
4370 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004371 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 else if (*e_cpt == NUL)
4374 break;
4375 else
4376 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004377 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 type = -1;
4379 else if (*e_cpt == 'k' || *e_cpt == 's')
4380 {
4381 if (*e_cpt == 'k')
4382 type = CTRL_X_DICTIONARY;
4383 else
4384 type = CTRL_X_THESAURUS;
4385 if (*++e_cpt != ',' && *e_cpt != NUL)
4386 {
4387 dict = e_cpt;
4388 dict_f = DICT_FIRST;
4389 }
4390 }
4391#ifdef FEAT_FIND_ID
4392 else if (*e_cpt == 'i')
4393 type = CTRL_X_PATH_PATTERNS;
4394 else if (*e_cpt == 'd')
4395 type = CTRL_X_PATH_DEFINES;
4396#endif
4397 else if (*e_cpt == ']' || *e_cpt == 't')
4398 {
4399 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004400 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004401 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 else
4404 type = -1;
4405
4406 /* in any case e_cpt is advanced to the next entry */
4407 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4408
4409 found_all = TRUE;
4410 if (type == -1)
4411 continue;
4412 }
4413 }
4414
Bram Moolenaar4475b622017-05-01 20:46:52 +02004415 /* If complete() was called then compl_pattern has been reset. The
4416 * following won't work then, bail out. */
4417 if (compl_pattern == NULL)
4418 break;
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 switch (type)
4421 {
4422 case -1:
4423 break;
4424#ifdef FEAT_FIND_ID
4425 case CTRL_X_PATH_PATTERNS:
4426 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004427 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4432 (linenr_T)1, (linenr_T)MAXLNUM);
4433 break;
4434#endif
4435
4436 case CTRL_X_DICTIONARY:
4437 case CTRL_X_THESAURUS:
4438 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004439 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 : (type == CTRL_X_THESAURUS
4441 ? (*curbuf->b_p_tsr == NUL
4442 ? p_tsr
4443 : curbuf->b_p_tsr)
4444 : (*curbuf->b_p_dict == NUL
4445 ? p_dict
4446 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004447 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004448 dict != NULL ? dict_f
4449 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 dict = NULL;
4451 break;
4452
4453 case CTRL_X_TAGS:
4454 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4455 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004458 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 * of matches is found when compl_pattern is empty */
4460 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004461 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4462 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4464 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004465 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467 p_ic = save_p_ic;
4468 break;
4469
4470 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4473 {
4474
4475 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004477 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479 break;
4480
4481 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 if (expand_cmdline(&compl_xp, compl_pattern,
4483 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004485 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004488#ifdef FEAT_COMPL_FUNC
4489 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004490 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004491 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004492 break;
4493#endif
4494
Bram Moolenaar488c6512005-08-11 20:09:58 +00004495 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004496#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004497 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004498 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004499 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004500 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004501#endif
4502 break;
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 default: /* normal ^P/^N and ^X^L */
4505 /*
4506 * If 'infercase' is set, don't use 'smartcase' here
4507 */
4508 save_p_scs = p_scs;
4509 if (ins_buf->b_p_inf)
4510 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004511
Bram Moolenaar361aa502014-01-23 22:45:58 +01004512 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 * end but never from the middle, thus setting nowrapscan in this
4514 * buffers is a good idea, on the other hand, we always set
4515 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4516 save_p_ws = p_ws;
4517 if (ins_buf != curbuf)
4518 p_ws = FALSE;
4519 else if (*e_cpt == '.')
4520 p_ws = TRUE;
4521 for (;;)
4522 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004523 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004525 ++msg_silent; /* Don't want messages for wrapscan. */
4526
Bram Moolenaare4214502015-03-05 18:08:43 +01004527 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4528 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004529 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004530 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004533 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004535 found_new_match = searchit(NULL, ins_buf, pos,
4536 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004538 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004539 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004540 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004542 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 first_match_pos = *pos;
4545 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004546 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004549 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 found_new_match = FAIL;
4551 if (found_new_match == FAIL)
4552 {
4553 if (ins_buf == curbuf)
4554 found_all = TRUE;
4555 break;
4556 }
4557
4558 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 && ini->lnum == pos->lnum
4561 && ini->col == pos->col)
4562 continue;
4563 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004564 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
4568 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4569 continue;
4570 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4571 if (!p_paste)
4572 ptr = skipwhite(ptr);
4573 }
4574 len = (int)STRLEN(ptr);
4575 }
4576 else
4577 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 char_u *tmp_ptr = ptr;
4579
4580 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /* Skip if already inside a word. */
4584 if (vim_iswordp(tmp_ptr))
4585 continue;
4586 /* Find start of next word. */
4587 tmp_ptr = find_word_start(tmp_ptr);
4588 }
4589 /* Find end of this word. */
4590 tmp_ptr = find_word_end(tmp_ptr);
4591 len = (int)(tmp_ptr - ptr);
4592
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 if ((compl_cont_status & CONT_ADDING)
4594 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4597 {
4598 /* Try next line, if any. the new word will be
4599 * "join" as if the normal command "J" was used.
4600 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 * works -- Acevedo */
4603 STRNCPY(IObuff, ptr, len);
4604 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4605 tmp_ptr = ptr = skipwhite(ptr);
4606 /* Find start of next word. */
4607 tmp_ptr = find_word_start(tmp_ptr);
4608 /* Find end of next word. */
4609 tmp_ptr = find_word_end(tmp_ptr);
4610 if (tmp_ptr > ptr)
4611 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004612 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004614 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 IObuff[len++] = ' ';
4616 /* IObuf =~ "\k.* ", thus len >= 2 */
4617 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004618 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 || (vim_strchr(p_cpo, CPO_JOINSP)
4620 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004621 && (IObuff[len - 2] == '?'
4622 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 IObuff[len++] = ' ';
4624 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004625 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (tmp_ptr - ptr >= IOSIZE - len)
4627 tmp_ptr = ptr + IOSIZE - len - 1;
4628 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4629 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004630 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 IObuff[len] = NUL;
4633 ptr = IObuff;
4634 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 continue;
4637 }
4638 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004639 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004640 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004641 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 found_new_match = OK;
4644 break;
4645 }
4646 }
4647 p_scs = save_p_scs;
4648 p_ws = save_p_ws;
4649 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004650
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004652 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004653 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 found_new_match = OK;
4655
4656 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004657 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4658 * match */
4659 if ((ctrl_x_mode != CTRL_X_NORMAL
4660 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004662 {
4663 if (got_int)
4664 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004665 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004666 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004667 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004668
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004669 if ((ctrl_x_mode != CTRL_X_NORMAL
4670 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004671 || compl_interrupted)
4672 break;
4673 compl_started = TRUE;
4674 }
4675 else
4676 {
4677 /* Mark a buffer scanned when it has been scanned completely */
4678 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4679 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004681 compl_started = FALSE;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004686 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 && *e_cpt == NUL) /* Got to end of 'complete' */
4688 found_new_match = FAIL;
4689
4690 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004691 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4692 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 i = ins_compl_make_cyclic();
4694
Bram Moolenaar4475b622017-05-01 20:46:52 +02004695 if (compl_old_match != NULL)
4696 {
4697 /* If several matches were added (FORWARD) or the search failed and has
4698 * just been made cyclic then we have to move compl_curr_match to the
4699 * next or previous entry (if any) -- Acevedo */
4700 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4701 : compl_old_match->cp_prev;
4702 if (compl_curr_match == NULL)
4703 compl_curr_match = compl_old_match;
4704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return i;
4706}
4707
4708/* Delete the old text being completed. */
4709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004710ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004712 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714 /*
4715 * In insert mode: Delete the typed part.
4716 * In replace mode: Put the old characters back, if any.
4717 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004718 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4719 if ((int)curwin->w_cursor.col > col)
4720 {
4721 if (stop_arrow() == FAIL)
4722 return;
4723 backspace_until_column(col);
4724 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004725
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004726 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4727 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004729 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004730 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731}
4732
Bram Moolenaar472e8592016-10-15 17:06:47 +02004733/*
4734 * Insert the new text being completed.
4735 * "in_compl_func" is TRUE when called from complete_check().
4736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004738ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004740 dict_T *dict;
4741
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004742 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004743 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4744 compl_used_match = FALSE;
4745 else
4746 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004747
4748 /* Set completed item. */
4749 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004750 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004751 if (dict != NULL)
4752 {
4753 dict_add_nr_str(dict, "word", 0L,
4754 EMPTY_IF_NULL(compl_shown_match->cp_str));
4755 dict_add_nr_str(dict, "abbr", 0L,
4756 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4757 dict_add_nr_str(dict, "menu", 0L,
4758 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4759 dict_add_nr_str(dict, "kind", 0L,
4760 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4761 dict_add_nr_str(dict, "info", 0L,
4762 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004763 dict_add_nr_str(dict, "user_data", 0L,
4764 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004765 }
4766 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004767 if (!in_compl_func)
4768 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769}
4770
4771/*
4772 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004773 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4774 * get more completions. If it is FALSE, then we just do nothing when there
4775 * are no more completions in a given direction. The latter case is used when
4776 * we are still in the middle of finding completions, to allow browsing
4777 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 * Return the total number of matches, or -1 if still unknown -- webb.
4779 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004780 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4781 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 *
4783 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004784 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4785 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 */
4787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004788ins_compl_next(
4789 int allow_get_expansion,
4790 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004791 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004792 int insert_match, /* Insert the newly selected match */
4793 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004796 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004797 compl_T *found_compl = NULL;
4798 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004799 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004800 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004802 /* When user complete function return -1 for findstart which is next
4803 * time of 'always', compl_shown_match become NULL. */
4804 if (compl_shown_match == NULL)
4805 return -1;
4806
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004807 if (compl_leader != NULL
4808 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004810 /* Set "compl_shown_match" to the actually shown match, it may differ
4811 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004812 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004813 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004814 && compl_shown_match->cp_next != NULL
4815 && compl_shown_match->cp_next != compl_first_match)
4816 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004817
4818 /* If we didn't find it searching forward, and compl_shows_dir is
4819 * backward, find the last match. */
4820 if (compl_shows_dir == BACKWARD
4821 && !ins_compl_equal(compl_shown_match,
4822 compl_leader, (int)STRLEN(compl_leader))
4823 && (compl_shown_match->cp_next == NULL
4824 || compl_shown_match->cp_next == compl_first_match))
4825 {
4826 while (!ins_compl_equal(compl_shown_match,
4827 compl_leader, (int)STRLEN(compl_leader))
4828 && compl_shown_match->cp_prev != NULL
4829 && compl_shown_match->cp_prev != compl_first_match)
4830 compl_shown_match = compl_shown_match->cp_prev;
4831 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004832 }
4833
4834 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004835 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 /* Delete old text to be replaced */
4837 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004838
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004839 /* When finding the longest common text we stick at the original text,
4840 * don't let CTRL-N or CTRL-P move to the first match. */
4841 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4842
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004843 /* When restarting the search don't insert the first match either. */
4844 if (compl_restarting)
4845 {
4846 advance = FALSE;
4847 compl_restarting = FALSE;
4848 }
4849
Bram Moolenaare3226be2005-12-18 22:10:00 +00004850 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4851 * around. */
4852 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004854 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004856 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004857 found_end = (compl_first_match != NULL
4858 && (compl_shown_match->cp_next == compl_first_match
4859 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004860 }
4861 else if (compl_shows_dir == BACKWARD
4862 && compl_shown_match->cp_prev != NULL)
4863 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004864 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004865 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004866 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004869 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004870 if (!allow_get_expansion)
4871 {
4872 if (advance)
4873 {
4874 if (compl_shows_dir == BACKWARD)
4875 compl_pending -= todo + 1;
4876 else
4877 compl_pending += todo + 1;
4878 }
4879 return -1;
4880 }
4881
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004882 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004883 {
4884 if (compl_shows_dir == BACKWARD)
4885 --compl_pending;
4886 else
4887 ++compl_pending;
4888 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004889
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004890 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004891 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004892
4893 /* handle any pending completions */
4894 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004895 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004896 {
4897 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4898 {
4899 compl_shown_match = compl_shown_match->cp_next;
4900 --compl_pending;
4901 }
4902 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4903 {
4904 compl_shown_match = compl_shown_match->cp_prev;
4905 ++compl_pending;
4906 }
4907 else
4908 break;
4909 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004910 found_end = FALSE;
4911 }
4912 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4913 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004914 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004915 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004916 ++todo;
4917 else
4918 /* Remember a matching item. */
4919 found_compl = compl_shown_match;
4920
4921 /* Stop at the end of the list when we found a usable match. */
4922 if (found_end)
4923 {
4924 if (found_compl != NULL)
4925 {
4926 compl_shown_match = found_compl;
4927 break;
4928 }
4929 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004933 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004934 if (compl_no_insert && !started)
4935 {
4936 ins_bytes(compl_orig_text + ins_compl_len());
4937 compl_used_match = FALSE;
4938 }
4939 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004940 {
4941 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004942 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004943 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004944 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004945 }
4946 else
4947 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 if (!allow_get_expansion)
4950 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004951 /* may undisplay the popup menu first */
4952 ins_compl_upd_pum();
4953
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004954 /* redraw to show the user what was inserted */
4955 update_screen(0);
4956
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004957 /* display the updated popup menu */
4958 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004959#ifdef FEAT_GUI
4960 if (gui.in_use)
4961 {
4962 /* Show the cursor after the match, not after the redrawn text. */
4963 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004964 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004965 }
4966#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 /* Delete old text to be replaced, since we're still searching and
4969 * don't want to match ourselves! */
4970 ins_compl_delete();
4971 }
4972
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004973 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004974 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004975 if (compl_no_insert && !started)
4976 compl_enter_selects = TRUE;
4977 else
4978 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /*
4981 * Show the file name for the match (if any)
4982 * Truncate the file name to avoid a wait for return.
4983 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004984 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004986 char *lead = _("match in file");
4987 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4988 char_u *s;
4989 char_u *e;
4990
4991 if (space > 0)
4992 {
4993 /* We need the tail that fits. With double-byte encoding going
4994 * back from the end is very slow, thus go from the start and keep
4995 * the text that fits in "space" between "s" and "e". */
4996 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4997 {
4998 space -= ptr2cells(e);
4999 while (space < 0)
5000 {
5001 space += ptr2cells(s);
5002 MB_PTR_ADV(s);
5003 }
5004 }
5005 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5006 s > compl_shown_match->cp_fname ? "<" : "", s);
5007 msg(IObuff);
5008 redraw_cmdline = FALSE; /* don't overwrite! */
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011
5012 return num_matches;
5013}
5014
5015/*
5016 * Call this while finding completions, to check whether the user has hit a key
5017 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005018 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005020 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005021 * "in_compl_func" is TRUE when called from complete_check(), don't set
5022 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 */
5024 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005025ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
5027 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005028 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005030 /* Don't check when reading keys from a script, :normal or feedkeys().
5031 * That would break the test scripts. But do check for keys when called
5032 * from complete_check(). */
5033 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 return;
5035
5036 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005037 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 return;
5039 count = 0;
5040
Bram Moolenaara260a972006-06-23 19:36:29 +00005041 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5042 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (c != NUL)
5045 {
5046 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5047 {
5048 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005049 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005050 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005051 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005053 else
5054 {
5055 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005056 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005057 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005058 if (c != K_IGNORE)
5059 {
5060 /* Don't interrupt completion when the character wasn't typed,
5061 * e.g., when doing @q to replay keys. */
5062 if (c != Ctrl_R && KeyTyped)
5063 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005064
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005065 vungetc(c);
5066 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005069 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005070 {
5071 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5072
5073 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005074 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005075 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005076}
5077
5078/*
5079 * Decide the direction of Insert mode complete from the key typed.
5080 * Returns BACKWARD or FORWARD.
5081 */
5082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005083ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005084{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005085 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005086 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005087 return BACKWARD;
5088 return FORWARD;
5089}
5090
5091/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005092 * Return TRUE for keys that are used for completion only when the popup menu
5093 * is visible.
5094 */
5095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005096ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005097{
5098 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005099 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5100 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005101}
5102
5103/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005104 * Decide the number of completions to move forward.
5105 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5106 */
5107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005108ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005109{
5110 int h;
5111
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005112 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005113 {
5114 h = pum_get_height();
5115 if (h > 3)
5116 h -= 2; /* keep some context */
5117 return h;
5118 }
5119 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120}
5121
5122/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005123 * Return TRUE if completion with "c" should insert the match, FALSE if only
5124 * to change the currently selected completion.
5125 */
5126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005127ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005128{
5129 switch (c)
5130 {
5131 case K_UP:
5132 case K_DOWN:
5133 case K_PAGEDOWN:
5134 case K_KPAGEDOWN:
5135 case K_S_DOWN:
5136 case K_PAGEUP:
5137 case K_KPAGEUP:
5138 case K_S_UP:
5139 return FALSE;
5140 }
5141 return TRUE;
5142}
5143
5144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 * Do Insert mode completion.
5146 * Called when character "c" was typed, which has a meaning for completion.
5147 * Returns OK if completion was done, FAIL if something failed (out of mem).
5148 */
5149 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005150ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 char_u *line;
5153 int startcol = 0; /* column where searched text starts */
5154 colnr_T curs_col; /* cursor column */
5155 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005156 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005157 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005158 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005159 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
Bram Moolenaare3226be2005-12-18 22:10:00 +00005161 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005162 insert_match = ins_compl_use_match(c);
5163
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 /* First time we hit ^N or ^P (in a row, I mean) */
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 did_ai = FALSE;
5169#ifdef FEAT_SMARTINDENT
5170 did_si = FALSE;
5171 can_si = FALSE;
5172 can_si_back = FALSE;
5173#endif
5174 if (stop_arrow() == FAIL)
5175 return FAIL;
5176
5177 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005179 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005181 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 * "compl_startpos" to the cursor as a pattern to add a new word
5183 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005184 * "compl_startpos" is not in the same line as the cursor then fix it
5185 * (the line has been split because it was longer than 'tw'). if SOL
5186 * is set then skip the previous pattern, a word at the beginning of
5187 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005188 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5189 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 /*
5192 * it is a continued search
5193 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005195 if (ctrl_x_mode == CTRL_X_NORMAL
5196 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5197 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 /* line (probably) wrapped, set compl_startpos to the
5202 * first non_blank in the line, if it is not a wordchar
5203 * include it to get a better pattern, but then we don't
5204 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005205 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 compl_startpos.col = compl_col;
5207 compl_startpos.lnum = curwin->w_cursor.lnum;
5208 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 else
5211 {
5212 /* S_IPOS was set when we inserted a word that was at the
5213 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 * mode but first we need to redefine compl_startpos */
5215 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 compl_cont_status |= CONT_SOL;
5218 compl_startpos.col = (colnr_T)(skipwhite(
5219 line + compl_length
5220 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005225 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005226 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 compl_cont_status &= ~CONT_SOL;
5231 compl_length = (IOSIZE - MIN_SPACE);
5232 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5235 if (compl_length < 1)
5236 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005238 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005249 if (ctrl_x_mode != CTRL_X_NORMAL)
5250 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005251 compl_cont_status = 0;
5252 compl_cont_status |= CONT_N_ADDS;
5253 compl_startpos = curwin->w_cursor;
5254 startcol = (int)curs_col;
5255 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257
5258 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005259 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5263 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 compl_col += ++startcol;
5269 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 compl_pattern = str_foldcase(line + compl_col,
5273 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 compl_pattern = vim_strnsave(line + compl_col,
5276 compl_length);
5277 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 return FAIL;
5279 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 char_u *prefix = (char_u *)"\\<";
5283
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005284 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005286 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005287 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 if (!vim_iswordp(line + compl_col)
5290 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 && (
5292#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#endif
5297 )))
5298 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 STRCPY((char *)compl_pattern, prefix);
5300 (void)quote_meta(compl_pattern + STRLEN(prefix),
5301 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#endif
5309 )
5310 {
5311 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005312 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5313 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_col += curs_col;
5316 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318 else
5319 {
5320#ifdef FEAT_MBYTE
5321 /* Search the point of change class of multibyte character
5322 * or not a word single byte character backward. */
5323 if (has_mbyte)
5324 {
5325 int base_class;
5326 int head_off;
5327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 startcol -= (*mb_head_off)(line, line + startcol);
5329 base_class = mb_get_class(line + startcol);
5330 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 head_off = (*mb_head_off)(line, line + startcol);
5333 if (base_class != mb_get_class(line + startcol
5334 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 }
5339 else
5340#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 compl_col += ++startcol;
5344 compl_length = (int)curs_col - startcol;
5345 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 /* Only match word with at least two chars -- webb
5348 * there's no need to call quote_meta,
5349 * alloc(7) is enough -- Acevedo
5350 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_pattern = alloc(7);
5352 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 STRCPY((char *)compl_pattern, "\\<");
5355 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5356 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358 else
5359 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005361 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 STRCPY((char *)compl_pattern, "\\<");
5365 (void)quote_meta(compl_pattern + 2, line + compl_col,
5366 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368 }
5369 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005370 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005372 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_length = (int)curs_col - (int)compl_col;
5374 if (compl_length < 0) /* cursor in indent: empty pattern */
5375 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 compl_pattern = str_foldcase(line + compl_col, compl_length,
5378 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5381 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 return FAIL;
5383 }
5384 else if (ctrl_x_mode == CTRL_X_FILES)
5385 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005386 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005387 if (startcol > 0)
5388 {
5389 char_u *p = line + startcol;
5390
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005391 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005392 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005393 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005394 if (p == line && vim_isfilec(PTR2CHAR(p)))
5395 startcol = 0;
5396 else
5397 startcol = (int)(p - line) + 1;
5398 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005399
Bram Moolenaar0300e462013-09-08 16:03:45 +02005400 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 compl_length = (int)curs_col - startcol;
5402 compl_pattern = addstar(line + compl_col, compl_length,
5403 EXPAND_FILES);
5404 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 return FAIL;
5406 }
5407 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5408 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 compl_pattern = vim_strnsave(line, curs_col);
5410 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005413 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5415 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005416 /* No completion possible, use an empty pattern to get a
5417 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005418 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005419 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005420 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5421 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005423 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005424 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005425#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005426 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005427 * Call user defined function 'completefunc' with "a:findstart"
5428 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005429 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005430 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005431 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005432 char_u *funcname;
5433 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005434 win_T *curwin_save;
5435 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005436
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005437 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005438 * string */
5439 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5440 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5441 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005442 {
5443 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5444 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005445 /* restore did_ai, so that adding comment leader works */
5446 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005447 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005448 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005449
5450 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005451 args[1] = NULL;
5452 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005453 curwin_save = curwin;
5454 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005455 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005456 if (curwin_save != curwin || curbuf_save != curbuf)
5457 {
5458 EMSG(_(e_complwin));
5459 return FAIL;
5460 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005461 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005462 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005463 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005464 {
5465 EMSG(_(e_compldel));
5466 return FAIL;
5467 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005468
Bram Moolenaar6110a002012-01-26 18:58:38 +01005469 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005470 * cancel the complete without an error.
5471 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005472 if (col == -2)
5473 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005474 if (col == -3)
5475 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005476 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005477 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005478 if (!shortmess(SHM_COMPLETIONMENU))
5479 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005480 return FAIL;
5481 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005482
Bram Moolenaar82139082011-09-14 16:52:09 +02005483 /*
5484 * Reset extended parameters of completion, when start new
5485 * completion.
5486 */
5487 compl_opt_refresh_always = FALSE;
5488
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005489 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005490 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005491 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005492 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005493 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005494
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005495 /* Setup variables for completion. Need to obtain "line" again,
5496 * it may have become invalid. */
5497 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005498 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5500 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005501#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 return FAIL;
5503 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005504 else if (ctrl_x_mode == CTRL_X_SPELL)
5505 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005506#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005507 if (spell_bad_len > 0)
5508 compl_col = curs_col - spell_bad_len;
5509 else
5510 compl_col = spell_word_start(startcol);
5511 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005512 {
5513 compl_length = 0;
5514 compl_col = curs_col;
5515 }
5516 else
5517 {
5518 spell_expand_check_cap(compl_col);
5519 compl_length = (int)curs_col - compl_col;
5520 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005521 /* Need to obtain "line" again, it may have become invalid. */
5522 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005523 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5524 if (compl_pattern == NULL)
5525#endif
5526 return FAIL;
5527 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 else
5529 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005530 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005531 return FAIL;
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005534 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005537 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
5539 /* Insert a new line, keep indentation but ignore 'comments' */
5540#ifdef FEAT_COMMENTS
5541 char_u *old = curbuf->b_p_com;
5542
5543 curbuf->b_p_com = (char_u *)"";
5544#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005545 compl_startpos.lnum = curwin->w_cursor.lnum;
5546 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 ins_eol('\r');
5548#ifdef FEAT_COMMENTS
5549 curbuf->b_p_com = old;
5550#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005551 compl_length = 0;
5552 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554 }
5555 else
5556 {
5557 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005558 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005561 if (compl_cont_status & CONT_LOCAL)
5562 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 else
5564 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5565
Bram Moolenaar62951b12011-09-21 18:23:05 +02005566 /* If any of the original typed text has been changed we need to fix
5567 * the redo buffer. */
5568 ins_compl_fixRedoBufForLeader(NULL);
5569
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005570 /* Always add completion for the original text. */
5571 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005572 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5573 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005574 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
Bram Moolenaar15675582018-02-09 12:29:56 +01005576 vim_clear((void **)&compl_pattern);
5577 vim_clear((void **)&compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 return FAIL;
5579 }
5580
5581 /* showmode might reset the internal line pointers, so it must
5582 * be called before line = ml_get(), or when this address is no
5583 * longer needed. -- Acevedo.
5584 */
5585 edit_submode_extra = (char_u *)_("-- Searching...");
5586 edit_submode_highl = HLF_COUNT;
5587 showmode();
5588 edit_submode_extra = NULL;
5589 out_flush();
5590 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005591 else if (insert_match && stop_arrow() == FAIL)
5592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005594 compl_shown_match = compl_curr_match;
5595 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
5597 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005598 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005600 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005601 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005602 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005604 /* may undisplay the popup menu */
5605 ins_compl_upd_pum();
5606
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005607 if (n > 1) /* all matches have been found */
5608 compl_matches = n;
5609 compl_curr_match = compl_shown_match;
5610 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
Bram Moolenaard68071d2006-05-02 22:08:30 +00005612 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5613 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if (got_int && !global_busy)
5615 {
5616 (void)vgetc();
5617 got_int = FALSE;
5618 }
5619
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005620 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005621 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005623 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5624 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5626 edit_submode_highl = HLF_E;
5627 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5628 * because we couldn't expand anything at first place, but if we used
5629 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5630 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 if ( compl_length > 1
5632 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005633 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5635 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005636 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638
Bram Moolenaar572cb562005-08-05 21:35:02 +00005639 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005640 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005642 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
5644 if (edit_submode_extra == NULL)
5645 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005646 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
5648 edit_submode_extra = (char_u *)_("Back at original");
5649 edit_submode_highl = HLF_W;
5650 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005651 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 edit_submode_extra = (char_u *)_("Word from other line");
5654 edit_submode_highl = HLF_COUNT;
5655 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005656 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 edit_submode_extra = (char_u *)_("The only match");
5659 edit_submode_highl = HLF_COUNT;
5660 }
5661 else
5662 {
5663 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005664 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005666 int number = 0;
5667 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005669 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 /* search backwards for the first valid (!= -1) number.
5672 * This should normally succeed already at the first loop
5673 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005674 for (match = compl_curr_match->cp_prev; match != NULL
5675 && match != compl_first_match;
5676 match = match->cp_prev)
5677 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005679 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681 }
5682 if (match != NULL)
5683 /* go up and assign all numbers which are not assigned
5684 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005685 for (match = match->cp_next;
5686 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005687 match = match->cp_next)
5688 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 else /* BACKWARD */
5691 {
5692 /* search forwards (upwards) for the first valid (!= -1)
5693 * number. This should normally succeed already at the
5694 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005695 for (match = compl_curr_match->cp_next; match != NULL
5696 && match != compl_first_match;
5697 match = match->cp_next)
5698 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005700 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702 }
5703 if (match != NULL)
5704 /* go down and assign all numbers which are not
5705 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005706 for (match = match->cp_prev; match
5707 && match->cp_number == -1;
5708 match = match->cp_prev)
5709 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711 }
5712
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005713 /* The match should always have a sequence number now, this is
5714 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005715 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005717 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5718 * Translations may need more than twice that. */
5719 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005721 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005722 vim_snprintf((char *)match_ref, sizeof(match_ref),
5723 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005724 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005726 vim_snprintf((char *)match_ref, sizeof(match_ref),
5727 _("match %d"),
5728 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 edit_submode_extra = match_ref;
5730 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005731 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 curs_columns(FALSE);
5733 }
5734 }
5735 }
5736
5737 /* Show a message about what (completion) mode we're in. */
5738 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005739 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005741 if (edit_submode_extra != NULL)
5742 {
5743 if (!p_smd)
5744 msg_attr(edit_submode_extra,
5745 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005746 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005747 }
5748 else
5749 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaard68071d2006-05-02 22:08:30 +00005752 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005753 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005754 show_pum(save_w_wrow, save_w_leftcol);
5755
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005756 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005757 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 return OK;
5760}
5761
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005762 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005763show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005764{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005765 /* RedrawingDisabled may be set when invoked through complete(). */
5766 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005767
Bram Moolenaarcb036422017-03-01 12:29:10 +01005768 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005769
Bram Moolenaarcb036422017-03-01 12:29:10 +01005770 /* If the cursor moved or the display scrolled we need to remove the pum
5771 * first. */
5772 setcursor();
5773 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5774 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005775
Bram Moolenaarcb036422017-03-01 12:29:10 +01005776 ins_compl_show_pum();
5777 setcursor();
5778 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005779}
5780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781/*
5782 * Looks in the first "len" chars. of "src" for search-metachars.
5783 * If dest is not NULL the chars. are copied there quoting (with
5784 * a backslash) the metachars, and dest would be NUL terminated.
5785 * Returns the length (needed) of dest
5786 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005787 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005790 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005792 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 switch (*src)
5795 {
5796 case '.':
5797 case '*':
5798 case '[':
5799 if (ctrl_x_mode == CTRL_X_DICTIONARY
5800 || ctrl_x_mode == CTRL_X_THESAURUS)
5801 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005802 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 case '~':
5804 if (!p_magic) /* quote these only if magic is set */
5805 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005806 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 case '\\':
5808 if (ctrl_x_mode == CTRL_X_DICTIONARY
5809 || ctrl_x_mode == CTRL_X_THESAURUS)
5810 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005811 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 case '^': /* currently it's not needed. */
5813 case '$':
5814 m++;
5815 if (dest != NULL)
5816 *dest++ = '\\';
5817 break;
5818 }
5819 if (dest != NULL)
5820 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005821# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 /* Copy remaining bytes of a multibyte character. */
5823 if (has_mbyte)
5824 {
5825 int i, mb_len;
5826
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005827 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 if (mb_len > 0 && len >= mb_len)
5829 for (i = 0; i < mb_len; ++i)
5830 {
5831 --len;
5832 ++src;
5833 if (dest != NULL)
5834 *dest++ = *src;
5835 }
5836 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839 if (dest != NULL)
5840 *dest = NUL;
5841
5842 return m;
5843}
5844#endif /* FEAT_INS_EXPAND */
5845
5846/*
5847 * Next character is interpreted literally.
5848 * A one, two or three digit decimal number is interpreted as its byte value.
5849 * If one or two digits are entered, the next character is given to vungetc().
5850 * For Unicode a character > 255 may be returned.
5851 */
5852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005853get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 int cc;
5856 int nc;
5857 int i;
5858 int hex = FALSE;
5859 int octal = FALSE;
5860#ifdef FEAT_MBYTE
5861 int unicode = 0;
5862#endif
5863
5864 if (got_int)
5865 return Ctrl_C;
5866
5867#ifdef FEAT_GUI
5868 /*
5869 * In GUI there is no point inserting the internal code for a special key.
5870 * It is more useful to insert the string "<KEY>" instead. This would
5871 * probably be useful in a text window too, but it would not be
5872 * vi-compatible (maybe there should be an option for it?) -- webb
5873 */
5874 if (gui.in_use)
5875 ++allow_keys;
5876#endif
5877#ifdef USE_ON_FLY_SCROLL
5878 dont_scroll = TRUE; /* disallow scrolling here */
5879#endif
5880 ++no_mapping; /* don't map the next key hits */
5881 cc = 0;
5882 i = 0;
5883 for (;;)
5884 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005885 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#ifdef FEAT_CMDL_INFO
5887 if (!(State & CMDLINE)
5888# ifdef FEAT_MBYTE
5889 && MB_BYTE2LEN_CHECK(nc) == 1
5890# endif
5891 )
5892 add_to_showcmd(nc);
5893#endif
5894 if (nc == 'x' || nc == 'X')
5895 hex = TRUE;
5896 else if (nc == 'o' || nc == 'O')
5897 octal = TRUE;
5898#ifdef FEAT_MBYTE
5899 else if (nc == 'u' || nc == 'U')
5900 unicode = nc;
5901#endif
5902 else
5903 {
5904 if (hex
5905#ifdef FEAT_MBYTE
5906 || unicode != 0
5907#endif
5908 )
5909 {
5910 if (!vim_isxdigit(nc))
5911 break;
5912 cc = cc * 16 + hex2nr(nc);
5913 }
5914 else if (octal)
5915 {
5916 if (nc < '0' || nc > '7')
5917 break;
5918 cc = cc * 8 + nc - '0';
5919 }
5920 else
5921 {
5922 if (!VIM_ISDIGIT(nc))
5923 break;
5924 cc = cc * 10 + nc - '0';
5925 }
5926
5927 ++i;
5928 }
5929
5930 if (cc > 255
5931#ifdef FEAT_MBYTE
5932 && unicode == 0
5933#endif
5934 )
5935 cc = 255; /* limit range to 0-255 */
5936 nc = 0;
5937
5938 if (hex) /* hex: up to two chars */
5939 {
5940 if (i >= 2)
5941 break;
5942 }
5943#ifdef FEAT_MBYTE
5944 else if (unicode) /* Unicode: up to four or eight chars */
5945 {
5946 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5947 break;
5948 }
5949#endif
5950 else if (i >= 3) /* decimal or octal: up to three chars */
5951 break;
5952 }
5953 if (i == 0) /* no number entered */
5954 {
5955 if (nc == K_ZERO) /* NUL is stored as NL */
5956 {
5957 cc = '\n';
5958 nc = 0;
5959 }
5960 else
5961 {
5962 cc = nc;
5963 nc = 0;
5964 }
5965 }
5966
5967 if (cc == 0) /* NUL is stored as NL */
5968 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005969#ifdef FEAT_MBYTE
5970 if (enc_dbcs && (cc & 0xff) == 0)
5971 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5972 second byte will cause trouble! */
5973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
5975 --no_mapping;
5976#ifdef FEAT_GUI
5977 if (gui.in_use)
5978 --allow_keys;
5979#endif
5980 if (nc)
5981 vungetc(nc);
5982 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5983 return cc;
5984}
5985
5986/*
5987 * Insert character, taking care of special keys and mod_mask
5988 */
5989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005990insert_special(
5991 int c,
5992 int allow_modmask,
5993 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 char_u *p;
5996 int len;
5997
5998 /*
5999 * Special function key, translate into "<Key>". Up to the last '>' is
6000 * inserted with ins_str(), so as not to replace characters in replace
6001 * mode.
6002 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6003 * unless 'allow_modmask' is TRUE.
6004 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006005#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 /* Command-key never produces a normal key */
6007 if (mod_mask & MOD_MASK_CMD)
6008 allow_modmask = TRUE;
6009#endif
6010 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6011 {
6012 p = get_special_key_name(c, mod_mask);
6013 len = (int)STRLEN(p);
6014 c = p[len - 1];
6015 if (len > 2)
6016 {
6017 if (stop_arrow() == FAIL)
6018 return;
6019 p[len - 1] = NUL;
6020 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006021 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 ctrlv = FALSE;
6023 }
6024 }
6025 if (stop_arrow() == OK)
6026 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6027}
6028
6029/*
6030 * Special characters in this context are those that need processing other
6031 * than the simple insertion that can be performed here. This includes ESC
6032 * which terminates the insert, and CR/NL which need special processing to
6033 * open up a new line. This routine tries to optimize insertions performed by
6034 * the "redo", "undo" or "put" commands, so it needs to know when it should
6035 * stop and defer processing to the "normal" mechanism.
6036 * '0' and '^' are special, because they can be followed by CTRL-D.
6037 */
6038#ifdef EBCDIC
6039# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6040#else
6041# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6042#endif
6043
6044#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006045# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006047# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#endif
6049
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006050/*
6051 * "flags": INSCHAR_FORMAT - force formatting
6052 * INSCHAR_CTRLV - char typed just after CTRL-V
6053 * INSCHAR_NO_FEX - don't use 'formatexpr'
6054 *
6055 * NOTE: passes the flags value straight through to internal_format() which,
6056 * beside INSCHAR_FORMAT (above), is also looking for these:
6057 * INSCHAR_DO_COM - format comments
6058 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061insertchar(
6062 int c, /* character to insert or NUL */
6063 int flags, /* INSCHAR_FORMAT, etc. */
6064 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 int textwidth;
6067#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006071 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006073 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 /*
6077 * Try to break the line in two or more pieces when:
6078 * - Always do this if we have been called to do formatting only.
6079 * - Always do this when 'formatoptions' has the 'a' flag and the line
6080 * ends in white space.
6081 * - Otherwise:
6082 * - Don't do this if inserting a blank
6083 * - Don't do this if an existing character is being replaced, unless
6084 * we're in VREPLACE mode.
6085 * - Do this if the cursor is not on the line where insert started
6086 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6087 * before the insert.
6088 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6089 * before 'textwidth'
6090 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006092 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006093 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 && !((State & REPLACE_FLAG)
6095#ifdef FEAT_VREPLACE
6096 && !(State & VREPLACE_FLAG)
6097#endif
6098 && *ml_get_cursor() != NUL)
6099 && (curwin->w_cursor.lnum != Insstart.lnum
6100 || ((!has_format_option(FO_INS_LONG)
6101 || Insstart_textlen <= (colnr_T)textwidth)
6102 && (!fo_ins_blank
6103 || Insstart_blank_vcol <= (colnr_T)textwidth
6104 ))))))
6105 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006106 /* Format with 'formatexpr' when it's set. Use internal formatting
6107 * when 'formatexpr' isn't set or it returns non-zero. */
6108#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006109 int do_internal = TRUE;
6110 colnr_T virtcol = get_nolist_virtcol()
6111 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006112
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006113 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6114 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006115 {
6116 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6117 /* It may be required to save for undo again, e.g. when setline()
6118 * was called. */
6119 ins_need_undo = TRUE;
6120 }
6121 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006123 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (c == NUL) /* only formatting was wanted */
6127 return;
6128
6129#ifdef FEAT_COMMENTS
6130 /* Check whether this character should end a comment. */
6131 if (did_ai && (int)c == end_comment_pending)
6132 {
6133 char_u *line;
6134 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6135 int middle_len, end_len;
6136 int i;
6137
6138 /*
6139 * Need to remove existing (middle) comment leader and insert end
6140 * comment leader. First, check what comment leader we can find.
6141 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006142 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6144 {
6145 /* Skip middle-comment string */
6146 while (*p && p[-1] != ':') /* find end of middle flags */
6147 ++p;
6148 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6149 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006150 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 --middle_len;
6152
6153 /* Find the end-comment string */
6154 while (*p && p[-1] != ':') /* find end of end flags */
6155 ++p;
6156 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6157
6158 /* Skip white space before the cursor */
6159 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006160 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 ;
6162 i++;
6163
6164 /* Skip to before the middle leader */
6165 i -= middle_len;
6166
6167 /* Check some expected things before we go on */
6168 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6169 {
6170 /* Backspace over all the stuff we want to replace */
6171 backspace_until_column(i);
6172
6173 /*
6174 * Insert the end-comment string, except for the last
6175 * character, which will get inserted as normal later.
6176 */
6177 ins_bytes_len(lead_end, end_len - 1);
6178 }
6179 }
6180 }
6181 end_comment_pending = NUL;
6182#endif
6183
6184 did_ai = FALSE;
6185#ifdef FEAT_SMARTINDENT
6186 did_si = FALSE;
6187 can_si = FALSE;
6188 can_si_back = FALSE;
6189#endif
6190
6191 /*
6192 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6193 * This speeds up normal text input considerably.
6194 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6195 * need to re-indent at a ':', or any other character (but not what
6196 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006197 * Don't do this when there an InsertCharPre autocommand is defined,
6198 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
6200#ifdef USE_ON_FLY_SCROLL
6201 dont_scroll = FALSE; /* allow scrolling here */
6202#endif
6203
6204 if ( !ISSPECIAL(c)
6205#ifdef FEAT_MBYTE
6206 && (!has_mbyte || (*mb_char2len)(c) == 1)
6207#endif
6208 && vpeekc() != NUL
6209 && !(State & REPLACE_FLAG)
6210#ifdef FEAT_CINDENT
6211 && !cindent_on()
6212#endif
6213#ifdef FEAT_RIGHTLEFT
6214 && !p_ri
6215#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006216#ifdef FEAT_AUTOCMD
6217 && !has_insertcharpre()
6218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 )
6220 {
6221#define INPUT_BUFLEN 100
6222 char_u buf[INPUT_BUFLEN + 1];
6223 int i;
6224 colnr_T virtcol = 0;
6225
6226 buf[0] = c;
6227 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 virtcol = get_nolist_virtcol();
6230 /*
6231 * Stop the string when:
6232 * - no more chars available
6233 * - finding a special character (command key)
6234 * - buffer is full
6235 * - running into the 'textwidth' boundary
6236 * - need to check for abbreviation: A non-word char after a word-char
6237 */
6238 while ( (c = vpeekc()) != NUL
6239 && !ISSPECIAL(c)
6240#ifdef FEAT_MBYTE
6241 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6242#endif
6243 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006244# ifdef FEAT_FKMAP
6245 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 && (textwidth == 0
6248 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6249 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6250 {
6251#ifdef FEAT_RIGHTLEFT
6252 c = vgetc();
6253 if (p_hkmap && KeyTyped)
6254 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 buf[i++] = c;
6256#else
6257 buf[i++] = vgetc();
6258#endif
6259 }
6260
6261#ifdef FEAT_DIGRAPHS
6262 do_digraph(-1); /* clear digraphs */
6263 do_digraph(buf[i-1]); /* may be the start of a digraph */
6264#endif
6265 buf[i] = NUL;
6266 ins_str(buf);
6267 if (flags & INSCHAR_CTRLV)
6268 {
6269 redo_literal(*buf);
6270 i = 1;
6271 }
6272 else
6273 i = 0;
6274 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006275 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277 else
6278 {
6279#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006280 int cc;
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6283 {
6284 char_u buf[MB_MAXBYTES + 1];
6285
6286 (*mb_char2bytes)(c, buf);
6287 buf[cc] = NUL;
6288 ins_char_bytes(buf, cc);
6289 AppendCharToRedobuff(c);
6290 }
6291 else
6292#endif
6293 {
6294 ins_char(c);
6295 if (flags & INSCHAR_CTRLV)
6296 redo_literal(c);
6297 else
6298 AppendCharToRedobuff(c);
6299 }
6300 }
6301}
6302
6303/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006304 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006305 *
6306 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6307 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308 */
6309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310internal_format(
6311 int textwidth,
6312 int second_indent,
6313 int flags,
6314 int format_only,
6315 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006316{
6317 int cc;
6318 int save_char = NUL;
6319 int haveto_redraw = FALSE;
6320 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6321#ifdef FEAT_MBYTE
6322 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6323#endif
6324 int fo_white_par = has_format_option(FO_WHITE_PAR);
6325 int first_line = TRUE;
6326#ifdef FEAT_COMMENTS
6327 colnr_T leader_len;
6328 int no_leader = FALSE;
6329 int do_comments = (flags & INSCHAR_DO_COM);
6330#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006331#ifdef FEAT_LINEBREAK
6332 int has_lbr = curwin->w_p_lbr;
6333
6334 /* make sure win_lbr_chartabsize() counts correctly */
6335 curwin->w_p_lbr = FALSE;
6336#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006337
6338 /*
6339 * When 'ai' is off we don't want a space under the cursor to be
6340 * deleted. Replace it with an 'x' temporarily.
6341 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006342 if (!curbuf->b_p_ai
6343#ifdef FEAT_VREPLACE
6344 && !(State & VREPLACE_FLAG)
6345#endif
6346 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 {
6348 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006349 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350 {
6351 save_char = cc;
6352 pchar_cursor('x');
6353 }
6354 }
6355
6356 /*
6357 * Repeat breaking lines, until the current line is not too long.
6358 */
6359 while (!got_int)
6360 {
6361 int startcol; /* Cursor column at entry */
6362 int wantcol; /* column at textwidth border */
6363 int foundcol; /* column for start of spaces */
6364 int end_foundcol = 0; /* column for start of word */
6365 colnr_T len;
6366 colnr_T virtcol;
6367#ifdef FEAT_VREPLACE
6368 int orig_col = 0;
6369 char_u *saved_text = NULL;
6370#endif
6371 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006372 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373
Bram Moolenaar97b98102009-11-17 16:41:01 +00006374 virtcol = get_nolist_virtcol()
6375 + char2cells(c != NUL ? c : gchar_cursor());
6376 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006377 break;
6378
6379#ifdef FEAT_COMMENTS
6380 if (no_leader)
6381 do_comments = FALSE;
6382 else if (!(flags & INSCHAR_FORMAT)
6383 && has_format_option(FO_WRAP_COMS))
6384 do_comments = TRUE;
6385
6386 /* Don't break until after the comment leader */
6387 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006388 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 else
6390 leader_len = 0;
6391
6392 /* If the line doesn't start with a comment leader, then don't
6393 * start one in a following broken line. Avoids that a %word
6394 * moved to the start of the next line causes all following lines
6395 * to start with %. */
6396 if (leader_len == 0)
6397 no_leader = TRUE;
6398#endif
6399 if (!(flags & INSCHAR_FORMAT)
6400#ifdef FEAT_COMMENTS
6401 && leader_len == 0
6402#endif
6403 && !has_format_option(FO_WRAP))
6404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006405 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406 if ((startcol = curwin->w_cursor.col) == 0)
6407 break;
6408
6409 /* find column of textwidth border */
6410 coladvance((colnr_T)textwidth);
6411 wantcol = curwin->w_cursor.col;
6412
Bram Moolenaar97b98102009-11-17 16:41:01 +00006413 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414 foundcol = 0;
6415
6416 /*
6417 * Find position to break at.
6418 * Stop at first entered white when 'formatoptions' has 'v'
6419 */
6420 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006421 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006422 || curwin->w_cursor.lnum != Insstart.lnum
6423 || curwin->w_cursor.col >= Insstart.col)
6424 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006425 if (curwin->w_cursor.col == startcol && c != NUL)
6426 cc = c;
6427 else
6428 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429 if (WHITECHAR(cc))
6430 {
6431 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006432 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433
6434 /* find start of sequence of blanks */
6435 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6436 {
6437 dec_cursor();
6438 cc = gchar_cursor();
6439 }
6440 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6441 break; /* only spaces in front of text */
6442#ifdef FEAT_COMMENTS
6443 /* Don't break until after the comment leader */
6444 if (curwin->w_cursor.col < leader_len)
6445 break;
6446#endif
6447 if (has_format_option(FO_ONE_LETTER))
6448 {
6449 /* do not break after one-letter words */
6450 if (curwin->w_cursor.col == 0)
6451 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006452#ifdef FEAT_COMMENTS
6453 /* do not break "#a b" when 'tw' is 2 */
6454 if (curwin->w_cursor.col <= leader_len)
6455 break;
6456#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457 col = curwin->w_cursor.col;
6458 dec_cursor();
6459 cc = gchar_cursor();
6460
6461 if (WHITECHAR(cc))
6462 continue; /* one-letter, continue */
6463 curwin->w_cursor.col = col;
6464 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006465
6466 inc_cursor();
6467
6468 end_foundcol = end_col + 1;
6469 foundcol = curwin->w_cursor.col;
6470 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471 break;
6472 }
6473#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006474 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475 {
6476 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006477 if (curwin->w_cursor.col != startcol)
6478 {
6479#ifdef FEAT_COMMENTS
6480 /* Don't break until after the comment leader */
6481 if (curwin->w_cursor.col < leader_len)
6482 break;
6483#endif
6484 col = curwin->w_cursor.col;
6485 inc_cursor();
6486 /* Don't change end_foundcol if already set. */
6487 if (foundcol != curwin->w_cursor.col)
6488 {
6489 foundcol = curwin->w_cursor.col;
6490 end_foundcol = foundcol;
6491 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6492 break;
6493 }
6494 curwin->w_cursor.col = col;
6495 }
6496
6497 if (curwin->w_cursor.col == 0)
6498 break;
6499
6500 col = curwin->w_cursor.col;
6501
6502 dec_cursor();
6503 cc = gchar_cursor();
6504
6505 if (WHITECHAR(cc))
6506 continue; /* break with space */
6507#ifdef FEAT_COMMENTS
6508 /* Don't break until after the comment leader */
6509 if (curwin->w_cursor.col < leader_len)
6510 break;
6511#endif
6512
6513 curwin->w_cursor.col = col;
6514
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006516 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006517 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6518 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 }
6520#endif
6521 if (curwin->w_cursor.col == 0)
6522 break;
6523 dec_cursor();
6524 }
6525
6526 if (foundcol == 0) /* no spaces, cannot break line */
6527 {
6528 curwin->w_cursor.col = startcol;
6529 break;
6530 }
6531
6532 /* Going to break the line, remove any "$" now. */
6533 undisplay_dollar();
6534
6535 /*
6536 * Offset between cursor position and line break is used by replace
6537 * stack functions. VREPLACE does not use this, and backspaces
6538 * over the text instead.
6539 */
6540#ifdef FEAT_VREPLACE
6541 if (State & VREPLACE_FLAG)
6542 orig_col = startcol; /* Will start backspacing from here */
6543 else
6544#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006545 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006546
6547 /*
6548 * adjust startcol for spaces that will be deleted and
6549 * characters that will remain on top line
6550 */
6551 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006552 while ((cc = gchar_cursor(), WHITECHAR(cc))
6553 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006554 inc_cursor();
6555 startcol -= curwin->w_cursor.col;
6556 if (startcol < 0)
6557 startcol = 0;
6558
6559#ifdef FEAT_VREPLACE
6560 if (State & VREPLACE_FLAG)
6561 {
6562 /*
6563 * In VREPLACE mode, we will backspace over the text to be
6564 * wrapped, so save a copy now to put on the next line.
6565 */
6566 saved_text = vim_strsave(ml_get_cursor());
6567 curwin->w_cursor.col = orig_col;
6568 if (saved_text == NULL)
6569 break; /* Can't do it, out of memory */
6570 saved_text[startcol] = NUL;
6571
6572 /* Backspace over characters that will move to the next line */
6573 if (!fo_white_par)
6574 backspace_until_column(foundcol);
6575 }
6576 else
6577#endif
6578 {
6579 /* put cursor after pos. to break line */
6580 if (!fo_white_par)
6581 curwin->w_cursor.col = foundcol;
6582 }
6583
6584 /*
6585 * Split the line just before the margin.
6586 * Only insert/delete lines, but don't really redraw the window.
6587 */
6588 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6589 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6590#ifdef FEAT_COMMENTS
6591 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006592 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006593#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006594 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6595 if (!(flags & INSCHAR_COM_LIST))
6596 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006597
6598 replace_offset = 0;
6599 if (first_line)
6600 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006601 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006602 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006603 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006604 * This section is for auto-wrap of numeric lists. When not
6605 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6606 * flag will be set and open_line() will handle it (as seen
6607 * above). The code here (and in get_number_indent()) will
6608 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006609 */
6610 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006611 second_indent =
6612 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006613 if (second_indent >= 0)
6614 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006615#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006616 if (State & VREPLACE_FLAG)
6617 change_indent(INDENT_SET, second_indent,
6618 FALSE, NUL, TRUE);
6619 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006620#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006621#ifdef FEAT_COMMENTS
6622 if (leader_len > 0 && second_indent - leader_len > 0)
6623 {
6624 int i;
6625 int padding = second_indent - leader_len;
6626
6627 /* We started at the first_line of a numbered list
6628 * that has a comment. the open_line() function has
6629 * inserted the proper comment leader and positioned
6630 * the cursor at the end of the split line. Now we
6631 * add the additional whitespace needed after the
6632 * comment leader for the numbered list. */
6633 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006634 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006635 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006636 }
6637 else
6638 {
6639#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006640 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006641#ifdef FEAT_COMMENTS
6642 }
6643#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006644 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645 }
6646 first_line = FALSE;
6647 }
6648
6649#ifdef FEAT_VREPLACE
6650 if (State & VREPLACE_FLAG)
6651 {
6652 /*
6653 * In VREPLACE mode we have backspaced over the text to be
6654 * moved, now we re-insert it into the new line.
6655 */
6656 ins_bytes(saved_text);
6657 vim_free(saved_text);
6658 }
6659 else
6660#endif
6661 {
6662 /*
6663 * Check if cursor is not past the NUL off the line, cindent
6664 * may have added or removed indent.
6665 */
6666 curwin->w_cursor.col += startcol;
6667 len = (colnr_T)STRLEN(ml_get_curline());
6668 if (curwin->w_cursor.col > len)
6669 curwin->w_cursor.col = len;
6670 }
6671
6672 haveto_redraw = TRUE;
6673#ifdef FEAT_CINDENT
6674 can_cindent = TRUE;
6675#endif
6676 /* moved the cursor, don't autoindent or cindent now */
6677 did_ai = FALSE;
6678#ifdef FEAT_SMARTINDENT
6679 did_si = FALSE;
6680 can_si = FALSE;
6681 can_si_back = FALSE;
6682#endif
6683 line_breakcheck();
6684 }
6685
6686 if (save_char != NUL) /* put back space after cursor */
6687 pchar_cursor(save_char);
6688
Bram Moolenaar0026d472014-09-09 16:32:39 +02006689#ifdef FEAT_LINEBREAK
6690 curwin->w_p_lbr = has_lbr;
6691#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006692 if (!format_only && haveto_redraw)
6693 {
6694 update_topline();
6695 redraw_curbuf_later(VALID);
6696 }
6697}
6698
6699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 * Called after inserting or deleting text: When 'formatoptions' includes the
6701 * 'a' flag format from the current line until the end of the paragraph.
6702 * Keep the cursor at the same position relative to the text.
6703 * The caller must have saved the cursor line for undo, following ones will be
6704 * saved here.
6705 */
6706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006707auto_format(
6708 int trailblank, /* when TRUE also format with trailing blank */
6709 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 pos_T pos;
6712 colnr_T len;
6713 char_u *old;
6714 char_u *new, *pnew;
6715 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006716 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718 if (!has_format_option(FO_AUTO))
6719 return;
6720
6721 pos = curwin->w_cursor;
6722 old = ml_get_curline();
6723
6724 /* may remove added space */
6725 check_auto_format(FALSE);
6726
6727 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6728 * user might insert normal text next. Also skip formatting when "1" is
6729 * in 'formatoptions' and there is a single character before the cursor.
6730 * Otherwise the line would be broken and when typing another non-white
6731 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006732 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (*old != NUL && !trailblank && wasatend)
6734 {
6735 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006736 cc = gchar_cursor();
6737 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6738 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006740 cc = gchar_cursor();
6741 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
6743 curwin->w_cursor = pos;
6744 return;
6745 }
6746 curwin->w_cursor = pos;
6747 }
6748
6749#ifdef FEAT_COMMENTS
6750 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6751 * comments. */
6752 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006753 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 return;
6755#endif
6756
6757 /*
6758 * May start formatting in a previous line, so that after "x" a word is
6759 * moved to the previous line if it fits there now. Only when this is not
6760 * the start of a paragraph.
6761 */
6762 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6763 {
6764 --curwin->w_cursor.lnum;
6765 if (u_save_cursor() == FAIL)
6766 return;
6767 }
6768
6769 /*
6770 * Do the formatting and restore the cursor position. "saved_cursor" will
6771 * be adjusted for the text formatting.
6772 */
6773 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006774 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 curwin->w_cursor = saved_cursor;
6776 saved_cursor.lnum = 0;
6777
6778 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6779 {
6780 /* "cannot happen" */
6781 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6782 coladvance((colnr_T)MAXCOL);
6783 }
6784 else
6785 check_cursor_col();
6786
6787 /* Insert mode: If the cursor is now after the end of the line while it
6788 * previously wasn't, the line was broken. Because of the rule above we
6789 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6790 * formatted. */
6791 if (!wasatend && has_format_option(FO_WHITE_PAR))
6792 {
6793 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006794 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (curwin->w_cursor.col == len)
6796 {
6797 pnew = vim_strnsave(new, len + 2);
6798 pnew[len] = ' ';
6799 pnew[len + 1] = NUL;
6800 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6801 /* remove the space later */
6802 did_add_space = TRUE;
6803 }
6804 else
6805 /* may remove added space */
6806 check_auto_format(FALSE);
6807 }
6808
6809 check_cursor();
6810}
6811
6812/*
6813 * When an extra space was added to continue a paragraph for auto-formatting,
6814 * delete it now. The space must be under the cursor, just after the insert
6815 * position.
6816 */
6817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006818check_auto_format(
6819 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006822 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
6824 if (did_add_space)
6825 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006826 cc = gchar_cursor();
6827 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 /* Somehow the space was removed already. */
6829 did_add_space = FALSE;
6830 else
6831 {
6832 if (!end_insert)
6833 {
6834 inc_cursor();
6835 c = gchar_cursor();
6836 dec_cursor();
6837 }
6838 if (c != NUL)
6839 {
6840 /* The space is no longer at the end of the line, delete it. */
6841 del_char(FALSE);
6842 did_add_space = FALSE;
6843 }
6844 }
6845 }
6846}
6847
6848/*
6849 * Find out textwidth to be used for formatting:
6850 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006851 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 * if invalid value, use 0.
6853 * Set default to window width (maximum 79) for "gq" operator.
6854 */
6855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856comp_textwidth(
6857 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 int textwidth;
6860
6861 textwidth = curbuf->b_p_tw;
6862 if (textwidth == 0 && curbuf->b_p_wm)
6863 {
6864 /* The width is the window width minus 'wrapmargin' minus all the
6865 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006866 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#ifdef FEAT_CMDWIN
6868 if (cmdwin_type != 0)
6869 textwidth -= 1;
6870#endif
6871#ifdef FEAT_FOLDING
6872 textwidth -= curwin->w_p_fdc;
6873#endif
6874#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006875 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 textwidth -= 1;
6877#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006878 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 textwidth -= 8;
6880 }
6881 if (textwidth < 0)
6882 textwidth = 0;
6883 if (ff && textwidth == 0)
6884 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006885 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (textwidth > 79)
6887 textwidth = 79;
6888 }
6889 return textwidth;
6890}
6891
6892/*
6893 * Put a character in the redo buffer, for when just after a CTRL-V.
6894 */
6895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006896redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 char_u buf[10];
6899
6900 /* Only digits need special treatment. Translate them into a string of
6901 * three digits. */
6902 if (VIM_ISDIGIT(c))
6903 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006904 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 AppendToRedobuff(buf);
6906 }
6907 else
6908 AppendCharToRedobuff(c);
6909}
6910
6911/*
6912 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006913 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 */
6915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006916start_arrow(
6917 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006919 start_arrow_common(end_insert_pos, TRUE);
6920}
6921
6922/*
6923 * Like start_arrow() but with end_change argument.
6924 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6925 */
6926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006927start_arrow_with_change(
6928 pos_T *end_insert_pos, /* can be NULL */
6929 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006930{
6931 start_arrow_common(end_insert_pos, end_change);
6932 if (!end_change)
6933 {
6934 AppendCharToRedobuff(Ctrl_G);
6935 AppendCharToRedobuff('U');
6936 }
6937}
6938
6939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006940start_arrow_common(
6941 pos_T *end_insert_pos, /* can be NULL */
6942 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006943{
6944 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006947 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 arrow_used = TRUE; /* this means we stopped the current insert */
6949 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006950#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006951 check_spell_redraw();
6952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953}
6954
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006955#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006956/*
6957 * If we skipped highlighting word at cursor, do it now.
6958 * It may be skipped again, thus reset spell_redraw_lnum first.
6959 */
6960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006961check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006962{
6963 if (spell_redraw_lnum != 0)
6964 {
6965 linenr_T lnum = spell_redraw_lnum;
6966
6967 spell_redraw_lnum = 0;
6968 redrawWinline(lnum, FALSE);
6969 }
6970}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006971
6972/*
6973 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6974 * spelled word, if there is one.
6975 */
6976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006977spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006978{
6979 pos_T tpos = curwin->w_cursor;
6980
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006981 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006982 if (curwin->w_cursor.col != tpos.col)
6983 start_arrow(&tpos);
6984}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006985#endif
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987/*
6988 * stop_arrow() is called before a change is made in insert mode.
6989 * If an arrow key has been used, start a new insertion.
6990 * Returns FAIL if undo is impossible, shouldn't insert then.
6991 */
6992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006993stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 if (arrow_used)
6996 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006997 Insstart = curwin->w_cursor; /* new insertion starts here */
6998 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6999 /* Don't update the original insert position when moved to the
7000 * right, except when nothing was inserted yet. */
7001 update_Insstart_orig = FALSE;
7002 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (u_save_cursor() == OK)
7005 {
7006 arrow_used = FALSE;
7007 ins_need_undo = FALSE;
7008 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 ai_col = 0;
7011#ifdef FEAT_VREPLACE
7012 if (State & VREPLACE_FLAG)
7013 {
7014 orig_line_count = curbuf->b_ml.ml_line_count;
7015 vr_lines_changed = 1;
7016 }
7017#endif
7018 ResetRedobuff();
7019 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007020 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022 else if (ins_need_undo)
7023 {
7024 if (u_save_cursor() == OK)
7025 ins_need_undo = FALSE;
7026 }
7027
7028#ifdef FEAT_FOLDING
7029 /* Always open fold at the cursor line when inserting something. */
7030 foldOpenCursor();
7031#endif
7032
7033 return (arrow_used || ins_need_undo ? FAIL : OK);
7034}
7035
7036/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007037 * Do a few things to stop inserting.
7038 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7039 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 */
7041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042stop_insert(
7043 pos_T *end_insert_pos,
7044 int esc, /* called by ins_esc() */
7045 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007047 int cc;
7048 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
7050 stop_redo_ins();
7051 replace_flush(); /* abandon replace stack */
7052
7053 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007054 * Save the inserted text for later redo with ^@ and CTRL-A.
7055 * Don't do it when "restart_edit" was set and nothing was inserted,
7056 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007058 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007059 if (did_restart_edit == 0 || (ptr != NULL
7060 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007061 {
7062 vim_free(last_insert);
7063 last_insert = ptr;
7064 last_insert_skip = new_insert_skip;
7065 }
7066 else
7067 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007069 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
7071 /* Auto-format now. It may seem strange to do this when stopping an
7072 * insertion (or moving the cursor), but it's required when appending
7073 * a line and having it end in a space. But only do it when something
7074 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007075 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007077 pos_T tpos = curwin->w_cursor;
7078
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 /* When the cursor is at the end of the line after a space the
7080 * formatting will move it to the following word. Avoid that by
7081 * moving the cursor onto the space. */
7082 cc = 'x';
7083 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7084 {
7085 dec_cursor();
7086 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007087 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007088 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
7090
7091 auto_format(TRUE, FALSE);
7092
Bram Moolenaar1c465442017-03-12 20:10:05 +01007093 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007094 {
7095 if (gchar_cursor() != NUL)
7096 inc_cursor();
7097#ifdef FEAT_VIRTUALEDIT
7098 /* If the cursor is still at the same character, also keep
7099 * the "coladd". */
7100 if (gchar_cursor() == NUL
7101 && curwin->w_cursor.lnum == tpos.lnum
7102 && curwin->w_cursor.col == tpos.col)
7103 curwin->w_cursor.coladd = tpos.coladd;
7104#endif
7105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 }
7107
7108 /* If a space was inserted for auto-formatting, remove it now. */
7109 check_auto_format(TRUE);
7110
7111 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007112 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007113 * Do this when ESC was used or moving the cursor up/down.
7114 * Check for the old position still being valid, just in case the text
7115 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007116 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007117 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7118 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007120 pos_T tpos = curwin->w_cursor;
7121
7122 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007123 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007124 for (;;)
7125 {
7126 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7127 --curwin->w_cursor.col;
7128 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007129 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007130 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007131 if (del_char(TRUE) == FAIL)
7132 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007133 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007134 if (curwin->w_cursor.lnum != tpos.lnum)
7135 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007136 else
7137 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007138 /* reset tpos, could have been invalidated in the loop above */
7139 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007140 tpos.col++;
7141 if (cc != NUL && gchar_pos(&tpos) == NUL)
7142 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 /* <C-S-Right> may have started Visual mode, adjust the position for
7146 * deleted characters. */
7147 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7148 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007149 int len = (int)STRLEN(ml_get_curline());
7150
7151 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007153 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007154#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
7158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 }
7160 }
7161 did_ai = FALSE;
7162#ifdef FEAT_SMARTINDENT
7163 did_si = FALSE;
7164 can_si = FALSE;
7165 can_si_back = FALSE;
7166#endif
7167
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007168 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7169 * now in a different buffer. */
7170 if (end_insert_pos != NULL)
7171 {
7172 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007173 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007174 curbuf->b_op_end = *end_insert_pos;
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176}
7177
7178/*
7179 * Set the last inserted text to a single character.
7180 * Used for the replace command.
7181 */
7182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007183set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 char_u *s;
7186
7187 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (last_insert != NULL)
7190 {
7191 s = last_insert;
7192 /* Use the CTRL-V only when entering a special char */
7193 if (c < ' ' || c == DEL)
7194 *s++ = Ctrl_V;
7195 s = add_char2buf(c, s);
7196 *s++ = ESC;
7197 *s++ = NUL;
7198 last_insert_skip = 0;
7199 }
7200}
7201
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007202#if defined(EXITFREE) || defined(PROTO)
7203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007205{
Bram Moolenaar15675582018-02-09 12:29:56 +01007206 vim_clear((void **)&last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007207# ifdef FEAT_INS_EXPAND
Bram Moolenaar15675582018-02-09 12:29:56 +01007208 vim_clear((void **)&compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007209# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007210}
7211#endif
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213/*
7214 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7215 * and CSI. Handle multi-byte characters.
7216 * Returns a pointer to after the added bytes.
7217 */
7218 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007222 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 int i;
7224 int len;
7225
7226 len = (*mb_char2bytes)(c, temp);
7227 for (i = 0; i < len; ++i)
7228 {
7229 c = temp[i];
7230#endif
7231 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7232 if (c == K_SPECIAL)
7233 {
7234 *s++ = K_SPECIAL;
7235 *s++ = KS_SPECIAL;
7236 *s++ = KE_FILLER;
7237 }
7238#ifdef FEAT_GUI
7239 else if (c == CSI)
7240 {
7241 *s++ = CSI;
7242 *s++ = KS_EXTRA;
7243 *s++ = (int)KE_CSI;
7244 }
7245#endif
7246 else
7247 *s++ = c;
7248#ifdef FEAT_MBYTE
7249 }
7250#endif
7251 return s;
7252}
7253
7254/*
7255 * move cursor to start of line
7256 * if flags & BL_WHITE move to first non-white
7257 * if flags & BL_SOL move to first non-white if startofline is set,
7258 * otherwise keep "curswant" column
7259 * if flags & BL_FIX don't leave the cursor on a NUL.
7260 */
7261 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007262beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263{
7264 if ((flags & BL_SOL) && !p_sol)
7265 coladvance(curwin->w_curswant);
7266 else
7267 {
7268 curwin->w_cursor.col = 0;
7269#ifdef FEAT_VIRTUALEDIT
7270 curwin->w_cursor.coladd = 0;
7271#endif
7272
7273 if (flags & (BL_WHITE | BL_SOL))
7274 {
7275 char_u *ptr;
7276
Bram Moolenaar1c465442017-03-12 20:10:05 +01007277 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7279 ++curwin->w_cursor.col;
7280 }
7281 curwin->w_set_curswant = TRUE;
7282 }
7283}
7284
7285/*
7286 * oneright oneleft cursor_down cursor_up
7287 *
7288 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007289 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 * Return OK when successful, FAIL when we hit a line of file boundary.
7291 */
7292
7293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
7299#ifdef FEAT_VIRTUALEDIT
7300 if (virtual_active())
7301 {
7302 pos_T prevpos = curwin->w_cursor;
7303
7304 /* Adjust for multi-wide char (excluding TAB) */
7305 ptr = ml_get_cursor();
7306 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007307# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007309# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 ))
7313 ? ptr2cells(ptr) : 1));
7314 curwin->w_set_curswant = TRUE;
7315 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7316 return (prevpos.col != curwin->w_cursor.col
7317 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7318 }
7319#endif
7320
7321 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007322 if (*ptr == NUL)
7323 return FAIL; /* already at the very end */
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007326 if (has_mbyte)
7327 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 else
7329#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007330 l = 1;
7331
7332 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7333 * contains "onemore". */
7334 if (ptr[l] == NUL
7335#ifdef FEAT_VIRTUALEDIT
7336 && (ve_flags & VE_ONEMORE) == 0
7337#endif
7338 )
7339 return FAIL;
7340 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
7342 curwin->w_set_curswant = TRUE;
7343 return OK;
7344}
7345
7346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007347oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349#ifdef FEAT_VIRTUALEDIT
7350 if (virtual_active())
7351 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007352# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 int v = getviscol();
7356
7357 if (v == 0)
7358 return FAIL;
7359
7360# ifdef FEAT_LINEBREAK
7361 /* We might get stuck on 'showbreak', skip over it. */
7362 width = 1;
7363 for (;;)
7364 {
7365 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007366 /* getviscol() is slow, skip it when 'showbreak' is empty,
7367 * 'breakindent' is not set and there are no multi-byte
7368 * characters */
7369 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370# ifdef FEAT_MBYTE
7371 && !has_mbyte
7372# endif
7373 ) || getviscol() < v)
7374 break;
7375 ++width;
7376 }
7377# else
7378 coladvance(v - 1);
7379# endif
7380
7381 if (curwin->w_cursor.coladd == 1)
7382 {
7383 char_u *ptr;
7384
7385 /* Adjust for multi-wide char (not a TAB) */
7386 ptr = ml_get_cursor();
7387 if (*ptr != TAB && vim_isprintc(
7388# ifdef FEAT_MBYTE
7389 (*mb_ptr2char)(ptr)
7390# else
7391 *ptr
7392# endif
7393 ) && ptr2cells(ptr) > 1)
7394 curwin->w_cursor.coladd = 0;
7395 }
7396
7397 curwin->w_set_curswant = TRUE;
7398 return OK;
7399 }
7400#endif
7401
7402 if (curwin->w_cursor.col == 0)
7403 return FAIL;
7404
7405 curwin->w_set_curswant = TRUE;
7406 --curwin->w_cursor.col;
7407
7408#ifdef FEAT_MBYTE
7409 /* if the character on the left of the current cursor is a multi-byte
7410 * character, move to its first byte */
7411 if (has_mbyte)
7412 mb_adjust_cursor();
7413#endif
7414 return OK;
7415}
7416
7417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007418cursor_up(
7419 long n,
7420 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 linenr_T lnum;
7423
7424 if (n > 0)
7425 {
7426 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007427 /* This fails if the cursor is already in the first line or the count
7428 * is larger than the line number and '-' is in 'cpoptions' */
7429 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return FAIL;
7431 if (n >= lnum)
7432 lnum = 1;
7433 else
7434#ifdef FEAT_FOLDING
7435 if (hasAnyFolding(curwin))
7436 {
7437 /*
7438 * Count each sequence of folded lines as one logical line.
7439 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007440 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 (void)hasFolding(lnum, &lnum, NULL);
7442
7443 while (n--)
7444 {
7445 /* move up one line */
7446 --lnum;
7447 if (lnum <= 1)
7448 break;
7449 /* If we entered a fold, move to the beginning, unless in
7450 * Insert mode or when 'foldopen' contains "all": it will open
7451 * in a moment. */
7452 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7453 (void)hasFolding(lnum, &lnum, NULL);
7454 }
7455 if (lnum < 1)
7456 lnum = 1;
7457 }
7458 else
7459#endif
7460 lnum -= n;
7461 curwin->w_cursor.lnum = lnum;
7462 }
7463
7464 /* try to advance to the column we want to be at */
7465 coladvance(curwin->w_curswant);
7466
7467 if (upd_topline)
7468 update_topline(); /* make sure curwin->w_topline is valid */
7469
7470 return OK;
7471}
7472
7473/*
7474 * Cursor down a number of logical lines.
7475 */
7476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477cursor_down(
7478 long n,
7479 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 linenr_T lnum;
7482
7483 if (n > 0)
7484 {
7485 lnum = curwin->w_cursor.lnum;
7486#ifdef FEAT_FOLDING
7487 /* Move to last line of fold, will fail if it's the end-of-file. */
7488 (void)hasFolding(lnum, NULL, &lnum);
7489#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007490 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007491 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007492 if (lnum >= curbuf->b_ml.ml_line_count
7493 || (lnum + n > curbuf->b_ml.ml_line_count
7494 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return FAIL;
7496 if (lnum + n >= curbuf->b_ml.ml_line_count)
7497 lnum = curbuf->b_ml.ml_line_count;
7498 else
7499#ifdef FEAT_FOLDING
7500 if (hasAnyFolding(curwin))
7501 {
7502 linenr_T last;
7503
7504 /* count each sequence of folded lines as one logical line */
7505 while (n--)
7506 {
7507 if (hasFolding(lnum, NULL, &last))
7508 lnum = last + 1;
7509 else
7510 ++lnum;
7511 if (lnum >= curbuf->b_ml.ml_line_count)
7512 break;
7513 }
7514 if (lnum > curbuf->b_ml.ml_line_count)
7515 lnum = curbuf->b_ml.ml_line_count;
7516 }
7517 else
7518#endif
7519 lnum += n;
7520 curwin->w_cursor.lnum = lnum;
7521 }
7522
7523 /* try to advance to the column we want to be at */
7524 coladvance(curwin->w_curswant);
7525
7526 if (upd_topline)
7527 update_topline(); /* make sure curwin->w_topline is valid */
7528
7529 return OK;
7530}
7531
7532/*
7533 * Stuff the last inserted text in the read buffer.
7534 * Last_insert actually is a copy of the redo buffer, so we
7535 * first have to remove the command.
7536 */
7537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007538stuff_inserted(
7539 int c, /* Command character to be inserted */
7540 long count, /* Repeat this many times */
7541 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 char_u *esc_ptr;
7544 char_u *ptr;
7545 char_u *last_ptr;
7546 char_u last = NUL;
7547
7548 ptr = get_last_insert();
7549 if (ptr == NULL)
7550 {
7551 EMSG(_(e_noinstext));
7552 return FAIL;
7553 }
7554
7555 /* may want to stuff the command character, to start Insert mode */
7556 if (c != NUL)
7557 stuffcharReadbuff(c);
7558 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7559 *esc_ptr = NUL; /* remove the ESC */
7560
7561 /* when the last char is either "0" or "^" it will be quoted if no ESC
7562 * comes after it OR if it will inserted more than once and "ptr"
7563 * starts with ^D. -- Acevedo
7564 */
7565 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7566 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7567 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7568 {
7569 last = *last_ptr;
7570 *last_ptr = NUL;
7571 }
7572
7573 do
7574 {
7575 stuffReadbuff(ptr);
7576 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7577 if (last)
7578 stuffReadbuff((char_u *)(last == '0'
7579 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7580 : IF_EB("\026^", CTRL_V_STR "^")));
7581 }
7582 while (--count > 0);
7583
7584 if (last)
7585 *last_ptr = last;
7586
7587 if (esc_ptr != NULL)
7588 *esc_ptr = ESC; /* put the ESC back */
7589
7590 /* may want to stuff a trailing ESC, to get out of Insert mode */
7591 if (!no_esc)
7592 stuffcharReadbuff(ESC);
7593
7594 return OK;
7595}
7596
7597 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007598get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
7600 if (last_insert == NULL)
7601 return NULL;
7602 return last_insert + last_insert_skip;
7603}
7604
7605/*
7606 * Get last inserted string, and remove trailing <Esc>.
7607 * Returns pointer to allocated memory (must be freed) or NULL.
7608 */
7609 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007610get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 char_u *s;
7613 int len;
7614
7615 if (last_insert == NULL)
7616 return NULL;
7617 s = vim_strsave(last_insert + last_insert_skip);
7618 if (s != NULL)
7619 {
7620 len = (int)STRLEN(s);
7621 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7622 s[len - 1] = NUL;
7623 }
7624 return s;
7625}
7626
7627/*
7628 * Check the word in front of the cursor for an abbreviation.
7629 * Called when the non-id character "c" has been entered.
7630 * When an abbreviation is recognized it is removed from the text and
7631 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7632 */
7633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007634echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 /* Don't check for abbreviation in paste mode, when disabled and just
7637 * after moving around with cursor keys. */
7638 if (p_paste || no_abbr || arrow_used)
7639 return FALSE;
7640
7641 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7642 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7643}
7644
7645/*
7646 * replace-stack functions
7647 *
7648 * When replacing characters, the replaced characters are remembered for each
7649 * new character. This is used to re-insert the old text when backspacing.
7650 *
7651 * There is a NUL headed list of characters for each character that is
7652 * currently in the file after the insertion point. When BS is used, one NUL
7653 * headed list is put back for the deleted character.
7654 *
7655 * For a newline, there are two NUL headed lists. One contains the characters
7656 * that the NL replaced. The extra one stores the characters after the cursor
7657 * that were deleted (always white space).
7658 *
7659 * Replace_offset is normally 0, in which case replace_push will add a new
7660 * character at the end of the stack. If replace_offset is not 0, that many
7661 * characters will be left on the stack above the newly inserted character.
7662 */
7663
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007664static char_u *replace_stack = NULL;
7665static long replace_stack_nr = 0; /* next entry in replace stack */
7666static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
7668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007669replace_push(
7670 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 char_u *p;
7673
7674 if (replace_stack_nr < replace_offset) /* nothing to do */
7675 return;
7676 if (replace_stack_len <= replace_stack_nr)
7677 {
7678 replace_stack_len += 50;
7679 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7680 if (p == NULL) /* out of memory */
7681 {
7682 replace_stack_len -= 50;
7683 return;
7684 }
7685 if (replace_stack != NULL)
7686 {
7687 mch_memmove(p, replace_stack,
7688 (size_t)(replace_stack_nr * sizeof(char_u)));
7689 vim_free(replace_stack);
7690 }
7691 replace_stack = p;
7692 }
7693 p = replace_stack + replace_stack_nr - replace_offset;
7694 if (replace_offset)
7695 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7696 *p = c;
7697 ++replace_stack_nr;
7698}
7699
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007700#if defined(FEAT_MBYTE) || defined(PROTO)
7701/*
7702 * Push a character onto the replace stack. Handles a multi-byte character in
7703 * reverse byte order, so that the first byte is popped off first.
7704 * Return the number of bytes done (includes composing characters).
7705 */
7706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007707replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007708{
7709 int l = (*mb_ptr2len)(p);
7710 int j;
7711
7712 for (j = l - 1; j >= 0; --j)
7713 replace_push(p[j]);
7714 return l;
7715}
7716#endif
7717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718/*
7719 * Pop one item from the replace stack.
7720 * return -1 if stack empty
7721 * return replaced character or NUL otherwise
7722 */
7723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 if (replace_stack_nr == 0)
7727 return -1;
7728 return (int)replace_stack[--replace_stack_nr];
7729}
7730
7731/*
7732 * Join the top two items on the replace stack. This removes to "off"'th NUL
7733 * encountered.
7734 */
7735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736replace_join(
7737 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 int i;
7740
7741 for (i = replace_stack_nr; --i >= 0; )
7742 if (replace_stack[i] == NUL && off-- <= 0)
7743 {
7744 --replace_stack_nr;
7745 mch_memmove(replace_stack + i, replace_stack + i + 1,
7746 (size_t)(replace_stack_nr - i));
7747 return;
7748 }
7749}
7750
7751/*
7752 * Pop bytes from the replace stack until a NUL is found, and insert them
7753 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7754 */
7755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007756replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758 int cc;
7759 int oldState = State;
7760
7761 State = NORMAL; /* don't want REPLACE here */
7762 while ((cc = replace_pop()) > 0)
7763 {
7764#ifdef FEAT_MBYTE
7765 mb_replace_pop_ins(cc);
7766#else
7767 ins_char(cc);
7768#endif
7769 dec_cursor();
7770 }
7771 State = oldState;
7772}
7773
7774#ifdef FEAT_MBYTE
7775/*
7776 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7777 * indicates a multi-byte char, pop the other bytes too.
7778 */
7779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007780mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007783 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 int i;
7785 int c;
7786
7787 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7788 {
7789 buf[0] = cc;
7790 for (i = 1; i < n; ++i)
7791 buf[i] = replace_pop();
7792 ins_bytes_len(buf, n);
7793 }
7794 else
7795 ins_char(cc);
7796
7797 if (enc_utf8)
7798 /* Handle composing chars. */
7799 for (;;)
7800 {
7801 c = replace_pop();
7802 if (c == -1) /* stack empty */
7803 break;
7804 if ((n = MB_BYTE2LEN(c)) == 1)
7805 {
7806 /* Not a multi-byte char, put it back. */
7807 replace_push(c);
7808 break;
7809 }
7810 else
7811 {
7812 buf[0] = c;
7813 for (i = 1; i < n; ++i)
7814 buf[i] = replace_pop();
7815 if (utf_iscomposing(utf_ptr2char(buf)))
7816 ins_bytes_len(buf, n);
7817 else
7818 {
7819 /* Not a composing char, put it back. */
7820 for (i = n - 1; i >= 0; --i)
7821 replace_push(buf[i]);
7822 break;
7823 }
7824 }
7825 }
7826}
7827#endif
7828
7829/*
7830 * make the replace stack empty
7831 * (called when exiting replace mode)
7832 */
7833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007834replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
Bram Moolenaar15675582018-02-09 12:29:56 +01007836 vim_clear((void **)&replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 replace_stack_len = 0;
7838 replace_stack_nr = 0;
7839}
7840
7841/*
7842 * Handle doing a BS for one character.
7843 * cc < 0: replace stack empty, just move cursor
7844 * cc == 0: character was inserted, delete it
7845 * cc > 0: character was replaced, put cc (first byte of original char) back
7846 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007847 * When "limit_col" is >= 0, don't delete before this column. Matters when
7848 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 */
7850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007851replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 int cc;
7854#ifdef FEAT_VREPLACE
7855 int orig_len = 0;
7856 int ins_len;
7857 int orig_vcols = 0;
7858 colnr_T start_vcol;
7859 char_u *p;
7860 int i;
7861 int vcol;
7862#endif
7863
7864 cc = replace_pop();
7865 if (cc > 0)
7866 {
7867#ifdef FEAT_VREPLACE
7868 if (State & VREPLACE_FLAG)
7869 {
7870 /* Get the number of screen cells used by the character we are
7871 * going to delete. */
7872 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7873 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7874 }
7875#endif
7876#ifdef FEAT_MBYTE
7877 if (has_mbyte)
7878 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007879 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880# ifdef FEAT_VREPLACE
7881 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007882 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883# endif
7884 replace_push(cc);
7885 }
7886 else
7887#endif
7888 {
7889 pchar_cursor(cc);
7890#ifdef FEAT_VREPLACE
7891 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007892 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#endif
7894 }
7895 replace_pop_ins();
7896
7897#ifdef FEAT_VREPLACE
7898 if (State & VREPLACE_FLAG)
7899 {
7900 /* Get the number of screen cells used by the inserted characters */
7901 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007902 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 vcol = start_vcol;
7904 for (i = 0; i < ins_len; ++i)
7905 {
7906 vcol += chartabsize(p + i, vcol);
7907#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007908 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#endif
7910 }
7911 vcol -= start_vcol;
7912
7913 /* Delete spaces that were inserted after the cursor to keep the
7914 * text aligned. */
7915 curwin->w_cursor.col += ins_len;
7916 while (vcol > orig_vcols && gchar_cursor() == ' ')
7917 {
7918 del_char(FALSE);
7919 ++orig_vcols;
7920 }
7921 curwin->w_cursor.col -= ins_len;
7922 }
7923#endif
7924
7925 /* mark the buffer as changed and prepare for displaying */
7926 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7927 }
7928 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007929 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930}
7931
7932#ifdef FEAT_CINDENT
7933/*
7934 * Return TRUE if C-indenting is on.
7935 */
7936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007937cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
7939 return (!p_paste && (curbuf->b_p_cin
7940# ifdef FEAT_EVAL
7941 || *curbuf->b_p_inde != NUL
7942# endif
7943 ));
7944}
7945#endif
7946
7947#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7948/*
7949 * Re-indent the current line, based on the current contents of it and the
7950 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7951 * confused what all the part that handles Control-T is doing that I'm not.
7952 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7953 */
7954
7955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007956fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007958 int amount = get_the_indent();
7959
7960 if (amount >= 0)
7961 {
7962 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7963 if (linewhite(curwin->w_cursor.lnum))
7964 did_ai = TRUE; /* delete the indent if the line stays empty */
7965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
7968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007969fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 if (p_paste)
7972 return;
7973# ifdef FEAT_LISP
7974 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7975 fixthisline(get_lisp_indent);
7976# endif
7977# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7978 else
7979# endif
7980# ifdef FEAT_CINDENT
7981 if (cindent_on())
7982 do_c_expr_indent();
7983# endif
7984}
7985
7986#endif
7987
7988#ifdef FEAT_CINDENT
7989/*
7990 * return TRUE if 'cinkeys' contains the key "keytyped",
7991 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007992 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7994 *
7995 * "keytyped" can have a few special values:
7996 * KEY_OPEN_FORW
7997 * KEY_OPEN_BACK
7998 * KEY_COMPLETE just finished completion.
7999 *
8000 * If line_is_empty is TRUE accept keys with '0' before them.
8001 */
8002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003in_cinkeys(
8004 int keytyped,
8005 int when,
8006 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 char_u *look;
8009 int try_match;
8010 int try_match_word;
8011 char_u *p;
8012 char_u *line;
8013 int icase;
8014 int i;
8015
Bram Moolenaar30848942009-12-24 14:46:12 +00008016 if (keytyped == NUL)
8017 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8018 return FALSE;
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020#ifdef FEAT_EVAL
8021 if (*curbuf->b_p_inde != NUL)
8022 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8023 else
8024#endif
8025 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8026 while (*look)
8027 {
8028 /*
8029 * Find out if we want to try a match with this key, depending on
8030 * 'when' and a '*' or '!' before the key.
8031 */
8032 switch (when)
8033 {
8034 case '*': try_match = (*look == '*'); break;
8035 case '!': try_match = (*look == '!'); break;
8036 default: try_match = (*look != '*'); break;
8037 }
8038 if (*look == '*' || *look == '!')
8039 ++look;
8040
8041 /*
8042 * If there is a '0', only accept a match if the line is empty.
8043 * But may still match when typing last char of a word.
8044 */
8045 if (*look == '0')
8046 {
8047 try_match_word = try_match;
8048 if (!line_is_empty)
8049 try_match = FALSE;
8050 ++look;
8051 }
8052 else
8053 try_match_word = FALSE;
8054
8055 /*
8056 * does it look like a control character?
8057 */
8058 if (*look == '^'
8059#ifdef EBCDIC
8060 && (Ctrl_chr(look[1]) != 0)
8061#else
8062 && look[1] >= '?' && look[1] <= '_'
8063#endif
8064 )
8065 {
8066 if (try_match && keytyped == Ctrl_chr(look[1]))
8067 return TRUE;
8068 look += 2;
8069 }
8070 /*
8071 * 'o' means "o" command, open forward.
8072 * 'O' means "O" command, open backward.
8073 */
8074 else if (*look == 'o')
8075 {
8076 if (try_match && keytyped == KEY_OPEN_FORW)
8077 return TRUE;
8078 ++look;
8079 }
8080 else if (*look == 'O')
8081 {
8082 if (try_match && keytyped == KEY_OPEN_BACK)
8083 return TRUE;
8084 ++look;
8085 }
8086
8087 /*
8088 * 'e' means to check for "else" at start of line and just before the
8089 * cursor.
8090 */
8091 else if (*look == 'e')
8092 {
8093 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8094 {
8095 p = ml_get_curline();
8096 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8097 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8098 return TRUE;
8099 }
8100 ++look;
8101 }
8102
8103 /*
8104 * ':' only causes an indent if it is at the end of a label or case
8105 * statement, or when it was before typing the ':' (to fix
8106 * class::method for C++).
8107 */
8108 else if (*look == ':')
8109 {
8110 if (try_match && keytyped == ':')
8111 {
8112 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008113 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008115 /* Need to get the line again after cin_islabel(). */
8116 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (curwin->w_cursor.col > 2
8118 && p[curwin->w_cursor.col - 1] == ':'
8119 && p[curwin->w_cursor.col - 2] == ':')
8120 {
8121 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008122 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008123 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 p = ml_get_curline();
8125 p[curwin->w_cursor.col - 1] = ':';
8126 if (i)
8127 return TRUE;
8128 }
8129 }
8130 ++look;
8131 }
8132
8133
8134 /*
8135 * Is it a key in <>, maybe?
8136 */
8137 else if (*look == '<')
8138 {
8139 if (try_match)
8140 {
8141 /*
8142 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8143 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8144 * >, *, : and ! keys if they really really want to.
8145 */
8146 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8147 && keytyped == look[1])
8148 return TRUE;
8149
8150 if (keytyped == get_special_key_code(look + 1))
8151 return TRUE;
8152 }
8153 while (*look && *look != '>')
8154 look++;
8155 while (*look == '>')
8156 look++;
8157 }
8158
8159 /*
8160 * Is it a word: "=word"?
8161 */
8162 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8163 {
8164 ++look;
8165 if (*look == '~')
8166 {
8167 icase = TRUE;
8168 ++look;
8169 }
8170 else
8171 icase = FALSE;
8172 p = vim_strchr(look, ',');
8173 if (p == NULL)
8174 p = look + STRLEN(look);
8175 if ((try_match || try_match_word)
8176 && curwin->w_cursor.col >= (colnr_T)(p - look))
8177 {
8178 int match = FALSE;
8179
8180#ifdef FEAT_INS_EXPAND
8181 if (keytyped == KEY_COMPLETE)
8182 {
8183 char_u *s;
8184
8185 /* Just completed a word, check if it starts with "look".
8186 * search back for the start of a word. */
8187 line = ml_get_curline();
8188# ifdef FEAT_MBYTE
8189 if (has_mbyte)
8190 {
8191 char_u *n;
8192
8193 for (s = line + curwin->w_cursor.col; s > line; s = n)
8194 {
8195 n = mb_prevptr(line, s);
8196 if (!vim_iswordp(n))
8197 break;
8198 }
8199 }
8200 else
8201# endif
8202 for (s = line + curwin->w_cursor.col; s > line; --s)
8203 if (!vim_iswordc(s[-1]))
8204 break;
8205 if (s + (p - look) <= line + curwin->w_cursor.col
8206 && (icase
8207 ? MB_STRNICMP(s, look, p - look)
8208 : STRNCMP(s, look, p - look)) == 0)
8209 match = TRUE;
8210 }
8211 else
8212#endif
8213 /* TODO: multi-byte */
8214 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8215 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8216 {
8217 line = ml_get_cursor();
8218 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8219 || !vim_iswordc(line[-(p - look) - 1]))
8220 && (icase
8221 ? MB_STRNICMP(line - (p - look), look, p - look)
8222 : STRNCMP(line - (p - look), look, p - look))
8223 == 0)
8224 match = TRUE;
8225 }
8226 if (match && try_match_word && !try_match)
8227 {
8228 /* "0=word": Check if there are only blanks before the
8229 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008230 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 (int)(curwin->w_cursor.col - (p - look)))
8232 match = FALSE;
8233 }
8234 if (match)
8235 return TRUE;
8236 }
8237 look = p;
8238 }
8239
8240 /*
8241 * ok, it's a boring generic character.
8242 */
8243 else
8244 {
8245 if (try_match && *look == keytyped)
8246 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008247 if (*look != NUL)
8248 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 }
8250
8251 /*
8252 * Skip over ", ".
8253 */
8254 look = skip_to_option_part(look);
8255 }
8256 return FALSE;
8257}
8258#endif /* FEAT_CINDENT */
8259
8260#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8261/*
8262 * Map Hebrew keyboard when in hkmap mode.
8263 */
8264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008265hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8268 {
8269 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8270 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8271 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8272 static char_u map[26] =
8273 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8274 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8275 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8276 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8277 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8278 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8279 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8280 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8281 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8282
8283 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8284 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8285 /* '-1'='sofit' */
8286 else if (c == 'x')
8287 return 'X';
8288 else if (c == 'q')
8289 return '\''; /* {geresh}={'} */
8290 else if (c == 246)
8291 return ' '; /* \"o --> ' ' for a german keyboard */
8292 else if (c == 228)
8293 return ' '; /* \"a --> ' ' -- / -- */
8294 else if (c == 252)
8295 return ' '; /* \"u --> ' ' -- / -- */
8296#ifdef EBCDIC
8297 else if (islower(c))
8298#else
8299 /* NOTE: islower() does not do the right thing for us on Linux so we
8300 * do this the same was as 5.7 and previous, so it works correctly on
8301 * all systems. Specifically, the e.g. Delete and Arrow keys are
8302 * munged and won't work if e.g. searching for Hebrew text.
8303 */
8304 else if (c >= 'a' && c <= 'z')
8305#endif
8306 return (int)(map[CharOrdLow(c)] + p_aleph);
8307 else
8308 return c;
8309 }
8310 else
8311 {
8312 switch (c)
8313 {
8314 case '`': return ';';
8315 case '/': return '.';
8316 case '\'': return ',';
8317 case 'q': return '/';
8318 case 'w': return '\'';
8319
8320 /* Hebrew letters - set offset from 'a' */
8321 case ',': c = '{'; break;
8322 case '.': c = 'v'; break;
8323 case ';': c = 't'; break;
8324 default: {
8325 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8326
8327#ifdef EBCDIC
8328 /* see note about islower() above */
8329 if (!islower(c))
8330#else
8331 if (c < 'a' || c > 'z')
8332#endif
8333 return c;
8334 c = str[CharOrdLow(c)];
8335 break;
8336 }
8337 }
8338
8339 return (int)(CharOrdLow(c) + p_aleph);
8340 }
8341}
8342#endif
8343
8344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008345ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 int need_redraw = FALSE;
8348 int regname;
8349 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008350 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
8352 /*
8353 * If we are going to wait for a character, show a '"'.
8354 */
8355 pc_status = PC_STATUS_UNSET;
8356 if (redrawing() && !char_avail())
8357 {
8358 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008359 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
8361 edit_putchar('"', TRUE);
8362#ifdef FEAT_CMDL_INFO
8363 add_to_showcmd_c(Ctrl_R);
8364#endif
8365 }
8366
8367#ifdef USE_ON_FLY_SCROLL
8368 dont_scroll = TRUE; /* disallow scrolling here */
8369#endif
8370
8371 /*
8372 * Don't map the register name. This also prevents the mode message to be
8373 * deleted when ESC is hit.
8374 */
8375 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008376 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8379 {
8380 /* Get a third key for literal register insertion */
8381 literally = regname;
8382#ifdef FEAT_CMDL_INFO
8383 add_to_showcmd_c(literally);
8384#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008385 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
8388 --no_mapping;
8389
8390#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008391 /* Don't call u_sync() while typing the expression or giving an error
8392 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 ++no_u_sync;
8394 if (regname == '=')
8395 {
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008396# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008398# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008399 /* Sync undo when evaluating the expression calls setline() or
8400 * append(), so that it can be undone separately. */
8401 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 regname = get_expr_register();
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008404# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 /* Restore the Input Method. */
8406 if (im_on)
8407 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008410 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8411 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008412 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 else
8416 {
8417#endif
8418 if (literally == Ctrl_O || literally == Ctrl_P)
8419 {
8420 /* Append the command to the redo buffer. */
8421 AppendCharToRedobuff(Ctrl_R);
8422 AppendCharToRedobuff(literally);
8423 AppendCharToRedobuff(regname);
8424
8425 do_put(regname, BACKWARD, 1L,
8426 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8427 }
8428 else if (insert_reg(regname, literally) == FAIL)
8429 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008430 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 need_redraw = TRUE; /* remove the '"' */
8432 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008433 else if (stop_insert_mode)
8434 /* When the '=' register was used and a function was invoked that
8435 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8436 * insert anything, need to remove the '"' */
8437 need_redraw = TRUE;
8438
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439#ifdef FEAT_EVAL
8440 }
8441 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008442 if (u_sync_once == 1)
8443 ins_need_undo = TRUE;
8444 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#endif
8446#ifdef FEAT_CMDL_INFO
8447 clear_showcmd();
8448#endif
8449
8450 /* If the inserted register is empty, we need to remove the '"' */
8451 if (need_redraw || stuff_empty())
8452 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008453
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008454 /* Disallow starting Visual mode here, would get a weird mode. */
8455 if (!vis_active && VIsual_active)
8456 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
8459/*
8460 * CTRL-G commands in Insert mode.
8461 */
8462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008463ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 int c;
8466
8467#ifdef FEAT_INS_EXPAND
8468 /* Right after CTRL-X the cursor will be after the ruler. */
8469 setcursor();
8470#endif
8471
8472 /*
8473 * Don't map the second key. This also prevents the mode message to be
8474 * deleted when ESC is hit.
8475 */
8476 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008477 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 --no_mapping;
8479 switch (c)
8480 {
8481 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8482 case K_UP:
8483 case Ctrl_K:
8484 case 'k': ins_up(TRUE);
8485 break;
8486
8487 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8488 case K_DOWN:
8489 case Ctrl_J:
8490 case 'j': ins_down(TRUE);
8491 break;
8492
8493 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008494 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008496
8497 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008498 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008499 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008500 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 break;
8502
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008503 /* CTRL-G U: do not break undo with the next char */
8504 case 'U':
8505 /* Allow one left/right cursor movement with the next char,
8506 * without breaking undo. */
8507 dont_sync_undo = MAYBE;
8508 break;
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008511 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
8513}
8514
8515/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008516 * CTRL-^ in Insert mode.
8517 */
8518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008519ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008520{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008521 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008522 {
8523 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8524 if (State & LANGMAP)
8525 {
8526 curbuf->b_p_iminsert = B_IMODE_NONE;
8527 State &= ~LANGMAP;
8528 }
8529 else
8530 {
8531 curbuf->b_p_iminsert = B_IMODE_LMAP;
8532 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008533#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008534 im_set_active(FALSE);
8535#endif
8536 }
8537 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008538#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008539 else
8540 {
8541 /* There are no ":lmap" mappings, toggle IM */
8542 if (im_get_status())
8543 {
8544 curbuf->b_p_iminsert = B_IMODE_NONE;
8545 im_set_active(FALSE);
8546 }
8547 else
8548 {
8549 curbuf->b_p_iminsert = B_IMODE_IM;
8550 State &= ~LANGMAP;
8551 im_set_active(TRUE);
8552 }
8553 }
8554#endif
8555 set_iminsert_global();
8556 showmode();
8557#ifdef FEAT_GUI
8558 /* may show different cursor shape or color */
8559 if (gui.in_use)
8560 gui_update_cursor(TRUE, FALSE);
8561#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008562#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008563 /* Show/unshow value of 'keymap' in status lines. */
8564 status_redraw_curbuf();
8565#endif
8566}
8567
8568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 * Handle ESC in insert mode.
8570 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8571 * insert.
8572 */
8573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008574ins_esc(
8575 long *count,
8576 int cmdchar,
8577 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 int temp;
8580 static int disabled_redraw = FALSE;
8581
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008582#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008583 check_spell_redraw();
8584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585#if defined(FEAT_HANGULIN)
8586# if defined(ESC_CHG_TO_ENG_MODE)
8587 hangul_input_state_set(0);
8588# endif
8589 if (composing_hangul)
8590 {
8591 push_raw_key(composing_hangul_buffer, 2);
8592 composing_hangul = 0;
8593 }
8594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596 temp = curwin->w_cursor.col;
8597 if (disabled_redraw)
8598 {
8599 --RedrawingDisabled;
8600 disabled_redraw = FALSE;
8601 }
8602 if (!arrow_used)
8603 {
8604 /*
8605 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008606 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8607 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 */
8609 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008610 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
8612 /*
8613 * Repeating insert may take a long time. Check for
8614 * interrupt now and then.
8615 */
8616 if (*count > 0)
8617 {
8618 line_breakcheck();
8619 if (got_int)
8620 *count = 0;
8621 }
8622
8623 if (--*count > 0) /* repeat what was typed */
8624 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008625 /* Vi repeats the insert without replacing characters. */
8626 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8627 State &= ~REPLACE_FLAG;
8628
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 (void)start_redo_ins();
8630 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008631 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 ++RedrawingDisabled;
8633 disabled_redraw = TRUE;
8634 return FALSE; /* repeat the insert */
8635 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008636 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 undisplay_dollar();
8638 }
8639
8640 /* When an autoindent was removed, curswant stays after the
8641 * indent */
8642 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8643 curwin->w_set_curswant = TRUE;
8644
8645 /* Remember the last Insert position in the '^ mark. */
8646 if (!cmdmod.keepjumps)
8647 curbuf->b_last_insert = curwin->w_cursor;
8648
8649 /*
8650 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008651 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008653 if (!nomove
8654 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655#ifdef FEAT_VIRTUALEDIT
8656 || curwin->w_cursor.coladd > 0
8657#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008658 )
8659 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008660 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661#ifdef FEAT_RIGHTLEFT
8662 && !revins_on
8663#endif
8664 )
8665 {
8666#ifdef FEAT_VIRTUALEDIT
8667 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8668 {
8669 oneleft();
8670 if (restart_edit != NUL)
8671 ++curwin->w_cursor.coladd;
8672 }
8673 else
8674#endif
8675 {
8676 --curwin->w_cursor.col;
8677#ifdef FEAT_MBYTE
8678 /* Correct cursor for multi-byte character. */
8679 if (has_mbyte)
8680 mb_adjust_cursor();
8681#endif
8682 }
8683 }
8684
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008685#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 /* Disable IM to allow typing English directly for Normal mode commands.
8687 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8688 * well). */
8689 if (!(State & LANGMAP))
8690 im_save_status(&curbuf->b_p_iminsert);
8691 im_set_active(FALSE);
8692#endif
8693
8694 State = NORMAL;
8695 /* need to position cursor again (e.g. when on a TAB ) */
8696 changed_cline_bef_curs();
8697
8698#ifdef FEAT_MOUSE
8699 setmouse();
8700#endif
8701#ifdef CURSOR_SHAPE
8702 ui_cursor_shape(); /* may show different cursor shape */
8703#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008704 if (!p_ek)
8705 /* Re-enable bracketed paste mode. */
8706 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707
8708 /*
8709 * When recording or for CTRL-O, need to display the new mode.
8710 * Otherwise remove the mode message.
8711 */
8712 if (Recording || restart_edit != NUL)
8713 showmode();
8714 else if (p_smd)
8715 MSG("");
8716
8717 return TRUE; /* exit Insert mode */
8718}
8719
8720#ifdef FEAT_RIGHTLEFT
8721/*
8722 * Toggle language: hkmap and revins_on.
8723 * Move to end of reverse inserted text.
8724 */
8725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008726ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 if (revins_on && revins_chars && revins_scol >= 0)
8729 {
8730 while (gchar_cursor() != NUL && revins_chars--)
8731 ++curwin->w_cursor.col;
8732 }
8733 p_ri = !p_ri;
8734 revins_on = (State == INSERT && p_ri);
8735 if (revins_on)
8736 {
8737 revins_scol = curwin->w_cursor.col;
8738 revins_legal++;
8739 revins_chars = 0;
8740 undisplay_dollar();
8741 }
8742 else
8743 revins_scol = -1;
8744#ifdef FEAT_FKMAP
8745 if (p_altkeymap)
8746 {
8747 /*
8748 * to be consistent also for redo command, using '.'
8749 * set arrow_used to true and stop it - causing to redo
8750 * characters entered in one mode (normal/reverse insert).
8751 */
8752 arrow_used = TRUE;
8753 (void)stop_arrow();
8754 p_fkmap = curwin->w_p_rl ^ p_ri;
8755 if (p_fkmap && p_ri)
8756 State = INSERT;
8757 }
8758 else
8759#endif
8760 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8761 showmode();
8762}
8763#endif
8764
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765/*
8766 * If 'keymodel' contains "startsel", may start selection.
8767 * Returns TRUE when a CTRL-O and other keys stuffed.
8768 */
8769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008770ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 if (km_startsel)
8773 switch (c)
8774 {
8775 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 case K_PAGEUP:
8778 case K_KPAGEUP:
8779 case K_PAGEDOWN:
8780 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008781# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 case K_LEFT:
8783 case K_RIGHT:
8784 case K_UP:
8785 case K_DOWN:
8786 case K_END:
8787 case K_HOME:
8788# endif
8789 if (!(mod_mask & MOD_MASK_SHIFT))
8790 break;
8791 /* FALLTHROUGH */
8792 case K_S_LEFT:
8793 case K_S_RIGHT:
8794 case K_S_UP:
8795 case K_S_DOWN:
8796 case K_S_END:
8797 case K_S_HOME:
8798 /* Start selection right away, the cursor can move with
8799 * CTRL-O when beyond the end of the line. */
8800 start_selection();
8801
8802 /* Execute the key in (insert) Select mode. */
8803 stuffcharReadbuff(Ctrl_O);
8804 if (mod_mask)
8805 {
8806 char_u buf[4];
8807
8808 buf[0] = K_SPECIAL;
8809 buf[1] = KS_MODIFIER;
8810 buf[2] = mod_mask;
8811 buf[3] = NUL;
8812 stuffReadbuff(buf);
8813 }
8814 stuffcharReadbuff(c);
8815 return TRUE;
8816 }
8817 return FALSE;
8818}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819
8820/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008821 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008822 */
8823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008824ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008825{
8826#ifdef FEAT_FKMAP
8827 if (p_fkmap && p_ri)
8828 {
8829 beep_flush();
8830 EMSG(farsi_text_3); /* encoded in Farsi */
8831 return;
8832 }
8833#endif
8834
8835#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008836# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008837 set_vim_var_string(VV_INSERTMODE,
8838 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008839# ifdef FEAT_VREPLACE
8840 replaceState == VREPLACE ? "v" :
8841# endif
8842 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008843# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008844 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8845#endif
8846 if (State & REPLACE_FLAG)
8847 State = INSERT | (State & LANGMAP);
8848 else
8849 State = replaceState | (State & LANGMAP);
8850 AppendCharToRedobuff(K_INS);
8851 showmode();
8852#ifdef CURSOR_SHAPE
8853 ui_cursor_shape(); /* may show different cursor shape */
8854#endif
8855}
8856
8857/*
8858 * Pressed CTRL-O in Insert mode.
8859 */
8860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008861ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008862{
8863#ifdef FEAT_VREPLACE
8864 if (State & VREPLACE_FLAG)
8865 restart_edit = 'V';
8866 else
8867#endif
8868 if (State & REPLACE_FLAG)
8869 restart_edit = 'R';
8870 else
8871 restart_edit = 'I';
8872#ifdef FEAT_VIRTUALEDIT
8873 if (virtual_active())
8874 ins_at_eol = FALSE; /* cursor always keeps its column */
8875 else
8876#endif
8877 ins_at_eol = (gchar_cursor() == NUL);
8878}
8879
8880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 * If the cursor is on an indent, ^T/^D insert/delete one
8882 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008883 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 * with vi. But vi only supports ^T and ^D after an
8885 * autoindent, we support it everywhere.
8886 */
8887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008888ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 if (stop_arrow() == FAIL)
8891 return;
8892 AppendCharToRedobuff(c);
8893
8894 /*
8895 * 0^D and ^^D: remove all indent.
8896 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008897 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8898 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 --curwin->w_cursor.col;
8901 (void)del_char(FALSE); /* delete the '^' or '0' */
8902 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8903 if (State & REPLACE_FLAG)
8904 replace_pop_ins();
8905 if (lastc == '^')
8906 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008907 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 }
8909 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008910 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
8912 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8913 did_ai = FALSE;
8914#ifdef FEAT_SMARTINDENT
8915 did_si = FALSE;
8916 can_si = FALSE;
8917 can_si_back = FALSE;
8918#endif
8919#ifdef FEAT_CINDENT
8920 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8921#endif
8922}
8923
8924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008925ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 int temp;
8928
8929 if (stop_arrow() == FAIL)
8930 return;
8931 if (gchar_cursor() == NUL) /* delete newline */
8932 {
8933 temp = curwin->w_cursor.col;
8934 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008935 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008936 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 else
8938 curwin->w_cursor.col = temp;
8939 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008940 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8941 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 did_ai = FALSE;
8943#ifdef FEAT_SMARTINDENT
8944 did_si = FALSE;
8945 can_si = FALSE;
8946 can_si_back = FALSE;
8947#endif
8948 AppendCharToRedobuff(K_DEL);
8949}
8950
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008951static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008952
8953/*
8954 * Delete one character for ins_bs().
8955 */
8956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008957ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008958{
8959 dec_cursor();
8960 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8961 if (State & REPLACE_FLAG)
8962 {
8963 /* Don't delete characters before the insert point when in
8964 * Replace mode */
8965 if (curwin->w_cursor.lnum != Insstart.lnum
8966 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008967 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008968 }
8969 else
8970 (void)del_char(FALSE);
8971}
8972
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973/*
8974 * Handle Backspace, delete-word and delete-line in Insert mode.
8975 * Return TRUE when backspace was actually used.
8976 */
8977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008978ins_bs(
8979 int c,
8980 int mode,
8981 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983 linenr_T lnum;
8984 int cc;
8985 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008986 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 colnr_T mincol;
8988 int did_backspace = FALSE;
8989 int in_indent;
8990 int oldState;
8991#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008992 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993#endif
8994
8995 /*
8996 * can't delete anything in an empty file
8997 * can't backup past first character in buffer
8998 * can't backup past starting point unless 'backspace' > 1
8999 * can backup to a previous line if 'backspace' == 0
9000 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009001 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 || (
9003#ifdef FEAT_RIGHTLEFT
9004 !revins_on &&
9005#endif
9006 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9007 || (!can_bs(BS_START)
9008 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009009 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9010 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9012 && curwin->w_cursor.col <= ai_col)
9013 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9014 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009015 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 return FALSE;
9017 }
9018
9019 if (stop_arrow() == FAIL)
9020 return FALSE;
9021 in_indent = inindent(0);
9022#ifdef FEAT_CINDENT
9023 if (in_indent)
9024 can_cindent = FALSE;
9025#endif
9026#ifdef FEAT_COMMENTS
9027 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9028#endif
9029#ifdef FEAT_RIGHTLEFT
9030 if (revins_on) /* put cursor after last inserted char */
9031 inc_cursor();
9032#endif
9033
9034#ifdef FEAT_VIRTUALEDIT
9035 /* Virtualedit:
9036 * BACKSPACE_CHAR eats a virtual space
9037 * BACKSPACE_WORD eats all coladd
9038 * BACKSPACE_LINE eats all coladd and keeps going
9039 */
9040 if (curwin->w_cursor.coladd > 0)
9041 {
9042 if (mode == BACKSPACE_CHAR)
9043 {
9044 --curwin->w_cursor.coladd;
9045 return TRUE;
9046 }
9047 if (mode == BACKSPACE_WORD)
9048 {
9049 curwin->w_cursor.coladd = 0;
9050 return TRUE;
9051 }
9052 curwin->w_cursor.coladd = 0;
9053 }
9054#endif
9055
9056 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009057 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 */
9059 if (curwin->w_cursor.col == 0)
9060 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009061 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009062 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063#ifdef FEAT_RIGHTLEFT
9064 || revins_on
9065#endif
9066 )
9067 {
9068 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9069 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9070 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009071 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009072 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 }
9074 /*
9075 * In replace mode:
9076 * cc < 0: NL was inserted, delete it
9077 * cc >= 0: NL was replaced, put original characters back
9078 */
9079 cc = -1;
9080 if (State & REPLACE_FLAG)
9081 cc = replace_pop(); /* returns -1 if NL was inserted */
9082 /*
9083 * In replace mode, in the line we started replacing, we only move the
9084 * cursor.
9085 */
9086 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9087 {
9088 dec_cursor();
9089 }
9090 else
9091 {
9092#ifdef FEAT_VREPLACE
9093 if (!(State & VREPLACE_FLAG)
9094 || curwin->w_cursor.lnum > orig_line_count)
9095#endif
9096 {
9097 temp = gchar_cursor(); /* remember current char */
9098 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009099
9100 /* When "aw" is in 'formatoptions' we must delete the space at
9101 * the end of the line, otherwise the line will be broken
9102 * again when auto-formatting. */
9103 if (has_format_option(FO_AUTO)
9104 && has_format_option(FO_WHITE_PAR))
9105 {
9106 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9107 TRUE);
9108 int len;
9109
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009110 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009111 if (len > 0 && ptr[len - 1] == ' ')
9112 ptr[len - 1] = NUL;
9113 }
9114
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009115 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 if (temp == NUL && gchar_cursor() != NUL)
9117 inc_cursor();
9118 }
9119#ifdef FEAT_VREPLACE
9120 else
9121 dec_cursor();
9122#endif
9123
9124 /*
9125 * In REPLACE mode we have to put back the text that was replaced
9126 * by the NL. On the replace stack is first a NUL-terminated
9127 * sequence of characters that were deleted and then the
9128 * characters that NL replaced.
9129 */
9130 if (State & REPLACE_FLAG)
9131 {
9132 /*
9133 * Do the next ins_char() in NORMAL state, to
9134 * prevent ins_char() from replacing characters and
9135 * avoiding showmatch().
9136 */
9137 oldState = State;
9138 State = NORMAL;
9139 /*
9140 * restore characters (blanks) deleted after cursor
9141 */
9142 while (cc > 0)
9143 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009144 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145#ifdef FEAT_MBYTE
9146 mb_replace_pop_ins(cc);
9147#else
9148 ins_char(cc);
9149#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009150 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 cc = replace_pop();
9152 }
9153 /* restore the characters that NL replaced */
9154 replace_pop_ins();
9155 State = oldState;
9156 }
9157 }
9158 did_ai = FALSE;
9159 }
9160 else
9161 {
9162 /*
9163 * Delete character(s) before the cursor.
9164 */
9165#ifdef FEAT_RIGHTLEFT
9166 if (revins_on) /* put cursor on last inserted char */
9167 dec_cursor();
9168#endif
9169 mincol = 0;
9170 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009171 if (mode == BACKSPACE_LINE
9172 && (curbuf->b_p_ai
9173#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009174 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009175#endif
9176 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177#ifdef FEAT_RIGHTLEFT
9178 && !revins_on
9179#endif
9180 )
9181 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009182 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009184 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009186 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 }
9188
9189 /*
9190 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9191 */
9192 if ( mode == BACKSPACE_CHAR
9193 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009194 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009195 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 && (*(ml_get_cursor() - 1) == TAB
9197 || (*(ml_get_cursor() - 1) == ' '
9198 && (!*inserted_space_p
9199 || arrow_used))))))
9200 {
9201 int ts;
9202 colnr_T vcol;
9203 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009204 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205
9206 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009207 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009208 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009210 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 /* Compute the virtual column where we want to be. Since
9212 * 'showbreak' may get in the way, need to get the last column of
9213 * the previous character. */
9214 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009215 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 dec_cursor();
9217 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9218 inc_cursor();
9219 want_vcol = (want_vcol / ts) * ts;
9220
9221 /* delete characters until we are at or before want_vcol */
9222 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009223 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009224 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226 /* insert extra spaces until we are at want_vcol */
9227 while (vcol < want_vcol)
9228 {
9229 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009230 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9231 && curwin->w_cursor.col < Insstart_orig.col)
9232 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234#ifdef FEAT_VREPLACE
9235 if (State & VREPLACE_FLAG)
9236 ins_char(' ');
9237 else
9238#endif
9239 {
9240 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009241 if ((State & REPLACE_FLAG))
9242 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 }
9244 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9245 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009246
9247 /* If we are now back where we started delete one character. Can
9248 * happen when using 'sts' and 'linebreak'. */
9249 if (vcol >= start_vcol)
9250 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 }
9252
9253 /*
9254 * Delete upto starting point, start of line or previous word.
9255 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009258#ifdef FEAT_MBYTE
9259 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009261 if (has_mbyte)
9262 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009264 do
9265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009267 if (!revins_on) /* put cursor on char to be deleted */
9268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009270
9271 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009273 /* look multi-byte character class */
9274 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009276 prev_cclass = cclass;
9277 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009280
9281 /* start of word? */
9282 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9283 {
9284 mode = BACKSPACE_WORD_NOT_SPACE;
9285 temp = vim_iswordc(cc);
9286 }
9287 /* end of word? */
9288 else if (mode == BACKSPACE_WORD_NOT_SPACE
9289 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9290#ifdef FEAT_MBYTE
9291 || prev_cclass != cclass
9292#endif
9293 ))
9294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009296 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009298 inc_cursor();
9299#ifdef FEAT_RIGHTLEFT
9300 else if (State & REPLACE_FLAG)
9301 dec_cursor();
9302#endif
9303 break;
9304 }
9305 if (State & REPLACE_FLAG)
9306 replace_do_bs(-1);
9307 else
9308 {
9309#ifdef FEAT_MBYTE
9310 if (enc_utf8 && p_deco)
9311 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9312#endif
9313 (void)del_char(FALSE);
9314#ifdef FEAT_MBYTE
9315 /*
9316 * If there are combining characters and 'delcombine' is set
9317 * move the cursor back. Don't back up before the base
9318 * character.
9319 */
9320 if (enc_utf8 && p_deco && cpc[0] != NUL)
9321 inc_cursor();
9322#endif
9323#ifdef FEAT_RIGHTLEFT
9324 if (revins_chars)
9325 {
9326 revins_chars--;
9327 revins_legal++;
9328 }
9329 if (revins_on && gchar_cursor() == NUL)
9330 break;
9331#endif
9332 }
9333 /* Just a single backspace?: */
9334 if (mode == BACKSPACE_CHAR)
9335 break;
9336 } while (
9337#ifdef FEAT_RIGHTLEFT
9338 revins_on ||
9339#endif
9340 (curwin->w_cursor.col > mincol
9341 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9342 || curwin->w_cursor.col != Insstart_orig.col)));
9343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 did_backspace = TRUE;
9345 }
9346#ifdef FEAT_SMARTINDENT
9347 did_si = FALSE;
9348 can_si = FALSE;
9349 can_si_back = FALSE;
9350#endif
9351 if (curwin->w_cursor.col <= 1)
9352 did_ai = FALSE;
9353 /*
9354 * It's a little strange to put backspaces into the redo
9355 * buffer, but it makes auto-indent a lot easier to deal
9356 * with.
9357 */
9358 AppendCharToRedobuff(c);
9359
9360 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009361 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9362 && curwin->w_cursor.col < Insstart_orig.col)
9363 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
9365 /* vi behaviour: the cursor moves backward but the character that
9366 * was there remains visible
9367 * Vim behaviour: the cursor moves backward and the character that
9368 * was there is erased from the screen.
9369 * We can emulate the vi behaviour by pretending there is a dollar
9370 * displayed even when there isn't.
9371 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009372 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 dollar_vcol = curwin->w_virtcol;
9374
Bram Moolenaarce3be472008-01-14 19:12:28 +00009375#ifdef FEAT_FOLDING
9376 /* When deleting a char the cursor line must never be in a closed fold.
9377 * E.g., when 'foldmethod' is indent and deleting the first non-white
9378 * char before a Tab. */
9379 if (did_backspace)
9380 foldOpenCursor();
9381#endif
9382
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 return did_backspace;
9384}
9385
9386#ifdef FEAT_MOUSE
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009391 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392
9393# ifdef FEAT_GUI
9394 /* When GUI is active, also move/paste when 'mouse' is empty */
9395 if (!gui.in_use)
9396# endif
9397 if (!mouse_has(MOUSE_INSERT))
9398 return;
9399
9400 undisplay_dollar();
9401 tpos = curwin->w_cursor;
9402 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9403 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009404 win_T *new_curwin = curwin;
9405
9406 if (curwin != old_curwin && win_valid(old_curwin))
9407 {
9408 /* Mouse took us to another window. We need to go back to the
9409 * previous one to stop insert there properly. */
9410 curwin = old_curwin;
9411 curbuf = curwin->w_buffer;
9412 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009413 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009414 if (curwin != new_curwin && win_valid(new_curwin))
9415 {
9416 curwin = new_curwin;
9417 curbuf = curwin->w_buffer;
9418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419# ifdef FEAT_CINDENT
9420 can_cindent = TRUE;
9421# endif
9422 }
9423
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 /* redraw status lines (in case another window became active) */
9425 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
9428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009432 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009433# ifdef FEAT_INS_EXPAND
9434 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435# endif
9436
9437 tpos = curwin->w_cursor;
9438
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009439 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 {
9441 int row, col;
9442
9443 row = mouse_row;
9444 col = mouse_col;
9445
9446 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009447 wp = mouse_find_win(&row, &col);
9448 if (wp == NULL)
9449 return;
9450 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 curbuf = curwin->w_buffer;
9452 }
9453 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 undisplay_dollar();
9455
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009456# ifdef FEAT_INS_EXPAND
9457 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009458 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009459# endif
9460 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009461 if (dir == MSCR_DOWN || dir == MSCR_UP)
9462 {
9463 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9464 scroll_redraw(dir,
9465 (long)(curwin->w_botline - curwin->w_topline));
9466 else
9467 scroll_redraw(dir, 3L);
9468 }
9469#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009470 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009471 {
9472 int val, step = 6;
9473
9474 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009475 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009476 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9477 if (val < 0)
9478 val = 0;
9479 gui_do_horiz_scroll(val, TRUE);
9480 }
9481#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009482# ifdef FEAT_INS_EXPAND
9483 did_scroll = TRUE;
9484# endif
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 curwin->w_redr_status = TRUE;
9488
9489 curwin = old_curwin;
9490 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009492# ifdef FEAT_INS_EXPAND
9493 /* The popup menu may overlay the window, need to redraw it.
9494 * TODO: Would be more efficient to only redraw the windows that are
9495 * overlapped by the popup menu. */
9496 if (pum_visible() && did_scroll)
9497 {
9498 redraw_all_later(NOT_VALID);
9499 ins_compl_show_pum();
9500 }
9501# endif
9502
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009503 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 {
9505 start_arrow(&tpos);
9506# ifdef FEAT_CINDENT
9507 can_cindent = TRUE;
9508# endif
9509 }
9510}
9511#endif
9512
Bram Moolenaarec2da362017-01-21 20:04:22 +01009513/*
9514 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9515 * P_PE literally.
9516 * When "drop" is TRUE then consume the text and drop it.
9517 */
9518 int
9519bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9520{
9521 int c;
9522 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9523 int idx = 0;
9524 char_u *end = find_termcode((char_u *)"PE");
9525 int ret_char = -1;
9526 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009527 int save_paste = p_paste;
9528 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009529
9530 /* If the end code is too long we can't detect it, read everything. */
9531 if (STRLEN(end) >= NUMBUFLEN)
9532 end = NULL;
9533 ++no_mapping;
9534 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009535 p_paste = TRUE;
9536 curbuf->b_p_ai = FALSE;
9537
Bram Moolenaarec2da362017-01-21 20:04:22 +01009538 for (;;)
9539 {
9540 /* When the end is not defined read everything. */
9541 if (end == NULL && vpeekc() == NUL)
9542 break;
9543 c = plain_vgetc();
9544#ifdef FEAT_MBYTE
9545 if (has_mbyte)
9546 idx += (*mb_char2bytes)(c, buf + idx);
9547 else
9548#endif
9549 buf[idx++] = c;
9550 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009551 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009552 {
9553 if (end[idx] == NUL)
9554 break; /* Found the end of paste code. */
9555 continue;
9556 }
9557 if (!drop)
9558 {
9559 switch (mode)
9560 {
9561 case PASTE_CMDLINE:
9562 put_on_cmdline(buf, idx, TRUE);
9563 break;
9564
9565 case PASTE_EX:
9566 if (gap != NULL && ga_grow(gap, idx) == OK)
9567 {
9568 mch_memmove((char *)gap->ga_data + gap->ga_len,
9569 buf, (size_t)idx);
9570 gap->ga_len += idx;
9571 }
9572 break;
9573
9574 case PASTE_INSERT:
9575 if (stop_arrow() == OK)
9576 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009577 c = buf[0];
9578 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9579 ins_eol(c);
9580 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009581 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009582 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009583 AppendToRedobuffLit(buf, idx);
9584 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009585 }
9586 break;
9587
9588 case PASTE_ONE_CHAR:
9589 if (ret_char == -1)
9590 {
9591#ifdef FEAT_MBYTE
9592 if (has_mbyte)
9593 ret_char = (*mb_ptr2char)(buf);
9594 else
9595#endif
9596 ret_char = buf[0];
9597 }
9598 break;
9599 }
9600 }
9601 idx = 0;
9602 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009603
Bram Moolenaarec2da362017-01-21 20:04:22 +01009604 --no_mapping;
9605 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009606 p_paste = save_paste;
9607 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009608
9609 return ret_char;
9610}
9611
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009612#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009614ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009615{
9616 /* We will be leaving the current window, unless closing another tab. */
9617 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9618 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9619 {
9620 undisplay_dollar();
9621 start_arrow(&curwin->w_cursor);
9622# ifdef FEAT_CINDENT
9623 can_cindent = TRUE;
9624# endif
9625 }
9626
9627 if (c == K_TABLINE)
9628 goto_tabpage(current_tab);
9629 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009630 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009631 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009632 redraw_statuslines(); /* will redraw the tabline when needed */
9633 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009634}
9635#endif
9636
9637#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009639ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640{
9641 pos_T tpos;
9642
9643 undisplay_dollar();
9644 tpos = curwin->w_cursor;
9645 if (gui_do_scroll())
9646 {
9647 start_arrow(&tpos);
9648# ifdef FEAT_CINDENT
9649 can_cindent = TRUE;
9650# endif
9651 }
9652}
9653
9654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009655ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 pos_T tpos;
9658
9659 undisplay_dollar();
9660 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009661 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
9663 start_arrow(&tpos);
9664# ifdef FEAT_CINDENT
9665 can_cindent = TRUE;
9666# endif
9667 }
9668}
9669#endif
9670
9671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009672ins_left(
9673 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675 pos_T tpos;
9676
9677#ifdef FEAT_FOLDING
9678 if ((fdo_flags & FDO_HOR) && KeyTyped)
9679 foldOpenCursor();
9680#endif
9681 undisplay_dollar();
9682 tpos = curwin->w_cursor;
9683 if (oneleft() == OK)
9684 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009685#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9686 /* Only call start_arrow() when not busy with preediting, it will
9687 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009688 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009689#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009690 {
9691 start_arrow_with_change(&tpos, end_change);
9692 if (!end_change)
9693 AppendCharToRedobuff(K_LEFT);
9694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695#ifdef FEAT_RIGHTLEFT
9696 /* If exit reversed string, position is fixed */
9697 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9698 revins_legal++;
9699 revins_chars++;
9700#endif
9701 }
9702
9703 /*
9704 * if 'whichwrap' set for cursor in insert mode may go to
9705 * previous line
9706 */
9707 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9708 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009709 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 start_arrow(&tpos);
9711 --(curwin->w_cursor.lnum);
9712 coladvance((colnr_T)MAXCOL);
9713 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9714 }
9715 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009716 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009717 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009721ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723 pos_T tpos;
9724
9725#ifdef FEAT_FOLDING
9726 if ((fdo_flags & FDO_HOR) && KeyTyped)
9727 foldOpenCursor();
9728#endif
9729 undisplay_dollar();
9730 tpos = curwin->w_cursor;
9731 if (c == K_C_HOME)
9732 curwin->w_cursor.lnum = 1;
9733 curwin->w_cursor.col = 0;
9734#ifdef FEAT_VIRTUALEDIT
9735 curwin->w_cursor.coladd = 0;
9736#endif
9737 curwin->w_curswant = 0;
9738 start_arrow(&tpos);
9739}
9740
9741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009742ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
9744 pos_T tpos;
9745
9746#ifdef FEAT_FOLDING
9747 if ((fdo_flags & FDO_HOR) && KeyTyped)
9748 foldOpenCursor();
9749#endif
9750 undisplay_dollar();
9751 tpos = curwin->w_cursor;
9752 if (c == K_C_END)
9753 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9754 coladvance((colnr_T)MAXCOL);
9755 curwin->w_curswant = MAXCOL;
9756
9757 start_arrow(&tpos);
9758}
9759
9760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
9763#ifdef FEAT_FOLDING
9764 if ((fdo_flags & FDO_HOR) && KeyTyped)
9765 foldOpenCursor();
9766#endif
9767 undisplay_dollar();
9768 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9769 {
9770 start_arrow(&curwin->w_cursor);
9771 (void)bck_word(1L, FALSE, FALSE);
9772 curwin->w_set_curswant = TRUE;
9773 }
9774 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009775 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776}
9777
9778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779ins_right(
9780 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782#ifdef FEAT_FOLDING
9783 if ((fdo_flags & FDO_HOR) && KeyTyped)
9784 foldOpenCursor();
9785#endif
9786 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009787 if (gchar_cursor() != NUL
9788#ifdef FEAT_VIRTUALEDIT
9789 || virtual_active()
9790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 )
9792 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009793 start_arrow_with_change(&curwin->w_cursor, end_change);
9794 if (!end_change)
9795 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 curwin->w_set_curswant = TRUE;
9797#ifdef FEAT_VIRTUALEDIT
9798 if (virtual_active())
9799 oneright();
9800 else
9801#endif
9802 {
9803#ifdef FEAT_MBYTE
9804 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009805 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else
9807#endif
9808 ++curwin->w_cursor.col;
9809 }
9810
9811#ifdef FEAT_RIGHTLEFT
9812 revins_legal++;
9813 if (revins_chars)
9814 revins_chars--;
9815#endif
9816 }
9817 /* if 'whichwrap' set for cursor in insert mode, may move the
9818 * cursor to the next line */
9819 else if (vim_strchr(p_ww, ']') != NULL
9820 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9821 {
9822 start_arrow(&curwin->w_cursor);
9823 curwin->w_set_curswant = TRUE;
9824 ++curwin->w_cursor.lnum;
9825 curwin->w_cursor.col = 0;
9826 }
9827 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009828 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009829 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830}
9831
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834{
9835#ifdef FEAT_FOLDING
9836 if ((fdo_flags & FDO_HOR) && KeyTyped)
9837 foldOpenCursor();
9838#endif
9839 undisplay_dollar();
9840 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9841 || gchar_cursor() != NUL)
9842 {
9843 start_arrow(&curwin->w_cursor);
9844 (void)fwd_word(1L, FALSE, 0);
9845 curwin->w_set_curswant = TRUE;
9846 }
9847 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009848 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849}
9850
9851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852ins_up(
9853 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
9855 pos_T tpos;
9856 linenr_T old_topline = curwin->w_topline;
9857#ifdef FEAT_DIFF
9858 int old_topfill = curwin->w_topfill;
9859#endif
9860
9861 undisplay_dollar();
9862 tpos = curwin->w_cursor;
9863 if (cursor_up(1L, TRUE) == OK)
9864 {
9865 if (startcol)
9866 coladvance(getvcol_nolist(&Insstart));
9867 if (old_topline != curwin->w_topline
9868#ifdef FEAT_DIFF
9869 || old_topfill != curwin->w_topfill
9870#endif
9871 )
9872 redraw_later(VALID);
9873 start_arrow(&tpos);
9874#ifdef FEAT_CINDENT
9875 can_cindent = TRUE;
9876#endif
9877 }
9878 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009879 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880}
9881
9882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009883ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884{
9885 pos_T tpos;
9886
9887 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009888
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009889 if (mod_mask & MOD_MASK_CTRL)
9890 {
9891 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009892 if (first_tabpage->tp_next != NULL)
9893 {
9894 start_arrow(&curwin->w_cursor);
9895 goto_tabpage(-1);
9896 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009897 return;
9898 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009899
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 tpos = curwin->w_cursor;
9901 if (onepage(BACKWARD, 1L) == OK)
9902 {
9903 start_arrow(&tpos);
9904#ifdef FEAT_CINDENT
9905 can_cindent = TRUE;
9906#endif
9907 }
9908 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009909 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910}
9911
9912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009913ins_down(
9914 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 pos_T tpos;
9917 linenr_T old_topline = curwin->w_topline;
9918#ifdef FEAT_DIFF
9919 int old_topfill = curwin->w_topfill;
9920#endif
9921
9922 undisplay_dollar();
9923 tpos = curwin->w_cursor;
9924 if (cursor_down(1L, TRUE) == OK)
9925 {
9926 if (startcol)
9927 coladvance(getvcol_nolist(&Insstart));
9928 if (old_topline != curwin->w_topline
9929#ifdef FEAT_DIFF
9930 || old_topfill != curwin->w_topfill
9931#endif
9932 )
9933 redraw_later(VALID);
9934 start_arrow(&tpos);
9935#ifdef FEAT_CINDENT
9936 can_cindent = TRUE;
9937#endif
9938 }
9939 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009940 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946 pos_T tpos;
9947
9948 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009949
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009950 if (mod_mask & MOD_MASK_CTRL)
9951 {
9952 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009953 if (first_tabpage->tp_next != NULL)
9954 {
9955 start_arrow(&curwin->w_cursor);
9956 goto_tabpage(0);
9957 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009958 return;
9959 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009960
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 tpos = curwin->w_cursor;
9962 if (onepage(FORWARD, 1L) == OK)
9963 {
9964 start_arrow(&tpos);
9965#ifdef FEAT_CINDENT
9966 can_cindent = TRUE;
9967#endif
9968 }
9969 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009970 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971}
9972
9973#ifdef FEAT_DND
9974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009975ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
9977 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9978}
9979#endif
9980
9981/*
9982 * Handle TAB in Insert or Replace mode.
9983 * Return TRUE when the TAB needs to be inserted like a normal character.
9984 */
9985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 int ind;
9989 int i;
9990 int temp;
9991
9992 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9993 Insstart_blank_vcol = get_nolist_virtcol();
9994 if (echeck_abbr(TAB + ABBR_OFF))
9995 return FALSE;
9996
9997 ind = inindent(0);
9998#ifdef FEAT_CINDENT
9999 if (ind)
10000 can_cindent = FALSE;
10001#endif
10002
10003 /*
10004 * When nothing special, insert TAB like a normal character
10005 */
10006 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010007 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010008 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 return TRUE;
10010
10011 if (stop_arrow() == FAIL)
10012 return TRUE;
10013
10014 did_ai = FALSE;
10015#ifdef FEAT_SMARTINDENT
10016 did_si = FALSE;
10017 can_si = FALSE;
10018 can_si_back = FALSE;
10019#endif
10020 AppendToRedobuff((char_u *)"\t");
10021
10022 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010023 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010024 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10025 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else /* otherwise use 'tabstop' */
10027 temp = (int)curbuf->b_p_ts;
10028 temp -= get_nolist_virtcol() % temp;
10029
10030 /*
10031 * Insert the first space with ins_char(). It will delete one char in
10032 * replace mode. Insert the rest with ins_str(); it will not delete any
10033 * chars. For VREPLACE mode, we use ins_char() for all characters.
10034 */
10035 ins_char(' ');
10036 while (--temp > 0)
10037 {
10038#ifdef FEAT_VREPLACE
10039 if (State & VREPLACE_FLAG)
10040 ins_char(' ');
10041 else
10042#endif
10043 {
10044 ins_str((char_u *)" ");
10045 if (State & REPLACE_FLAG) /* no char replaced */
10046 replace_push(NUL);
10047 }
10048 }
10049
10050 /*
10051 * When 'expandtab' not set: Replace spaces by TABs where possible.
10052 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010053 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
10055 char_u *ptr;
10056#ifdef FEAT_VREPLACE
10057 char_u *saved_line = NULL; /* init for GCC */
10058 pos_T pos;
10059#endif
10060 pos_T fpos;
10061 pos_T *cursor;
10062 colnr_T want_vcol, vcol;
10063 int change_col = -1;
10064 int save_list = curwin->w_p_list;
10065
10066 /*
10067 * Get the current line. For VREPLACE mode, don't make real changes
10068 * yet, just work on a copy of the line.
10069 */
10070#ifdef FEAT_VREPLACE
10071 if (State & VREPLACE_FLAG)
10072 {
10073 pos = curwin->w_cursor;
10074 cursor = &pos;
10075 saved_line = vim_strsave(ml_get_curline());
10076 if (saved_line == NULL)
10077 return FALSE;
10078 ptr = saved_line + pos.col;
10079 }
10080 else
10081#endif
10082 {
10083 ptr = ml_get_cursor();
10084 cursor = &curwin->w_cursor;
10085 }
10086
10087 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10088 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10089 curwin->w_p_list = FALSE;
10090
10091 /* Find first white before the cursor */
10092 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010093 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 {
10095 --fpos.col;
10096 --ptr;
10097 }
10098
10099 /* In Replace mode, don't change characters before the insert point. */
10100 if ((State & REPLACE_FLAG)
10101 && fpos.lnum == Insstart.lnum
10102 && fpos.col < Insstart.col)
10103 {
10104 ptr += Insstart.col - fpos.col;
10105 fpos.col = Insstart.col;
10106 }
10107
10108 /* compute virtual column numbers of first white and cursor */
10109 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10110 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10111
Bram Moolenaar597a4222014-06-25 14:39:50 +020010112 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10113 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010114 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010116 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 if (vcol + i > want_vcol)
10118 break;
10119 if (*ptr != TAB)
10120 {
10121 *ptr = TAB;
10122 if (change_col < 0)
10123 {
10124 change_col = fpos.col; /* Column of first change */
10125 /* May have to adjust Insstart */
10126 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10127 Insstart.col = fpos.col;
10128 }
10129 }
10130 ++fpos.col;
10131 ++ptr;
10132 vcol += i;
10133 }
10134
10135 if (change_col >= 0)
10136 {
10137 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010138 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139
10140 /* Skip over the spaces we need. */
10141 while (vcol < want_vcol && *ptr == ' ')
10142 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010143 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 ++ptr;
10145 ++repl_off;
10146 }
10147 if (vcol > want_vcol)
10148 {
10149 /* Must have a char with 'showbreak' just before it. */
10150 --ptr;
10151 --repl_off;
10152 }
10153 fpos.col += repl_off;
10154
10155 /* Delete following spaces. */
10156 i = cursor->col - fpos.col;
10157 if (i > 0)
10158 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010159 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 /* correct replace stack. */
10161 if ((State & REPLACE_FLAG)
10162#ifdef FEAT_VREPLACE
10163 && !(State & VREPLACE_FLAG)
10164#endif
10165 )
10166 for (temp = i; --temp >= 0; )
10167 replace_join(repl_off);
10168 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010169#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010170 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010171 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010172 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010173 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10174 (char_u *)"\t", 1);
10175 }
10176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 cursor->col -= i;
10178
10179#ifdef FEAT_VREPLACE
10180 /*
10181 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10182 * backspacing over the changed spacing and then inserting the new
10183 * spacing.
10184 */
10185 if (State & VREPLACE_FLAG)
10186 {
10187 /* Backspace from real cursor to change_col */
10188 backspace_until_column(change_col);
10189
10190 /* Insert each char in saved_line from changed_col to
10191 * ptr-cursor */
10192 ins_bytes_len(saved_line + change_col,
10193 cursor->col - change_col);
10194 }
10195#endif
10196 }
10197
10198#ifdef FEAT_VREPLACE
10199 if (State & VREPLACE_FLAG)
10200 vim_free(saved_line);
10201#endif
10202 curwin->w_p_list = save_list;
10203 }
10204
10205 return FALSE;
10206}
10207
10208/*
10209 * Handle CR or NL in insert mode.
10210 * Return TRUE when out of memory or can't undo.
10211 */
10212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010213ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 int i;
10216
10217 if (echeck_abbr(c + ABBR_OFF))
10218 return FALSE;
10219 if (stop_arrow() == FAIL)
10220 return TRUE;
10221 undisplay_dollar();
10222
10223 /*
10224 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10225 * character under the cursor. Only push a NUL on the replace stack,
10226 * nothing to put back when the NL is deleted.
10227 */
10228 if ((State & REPLACE_FLAG)
10229#ifdef FEAT_VREPLACE
10230 && !(State & VREPLACE_FLAG)
10231#endif
10232 )
10233 replace_push(NUL);
10234
10235 /*
10236 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10237 * replacing the next line, so we push all of the characters left on the
10238 * line onto the replace stack. This is not done here though, it is done
10239 * in open_line().
10240 */
10241
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010242#ifdef FEAT_VIRTUALEDIT
10243 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10244 * CTRL-O). */
10245 if (virtual_active() && curwin->w_cursor.coladd > 0)
10246 coladvance(getviscol());
10247#endif
10248
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249#ifdef FEAT_RIGHTLEFT
10250# ifdef FEAT_FKMAP
10251 if (p_altkeymap && p_fkmap)
10252 fkmap(NL);
10253# endif
10254 /* NL in reverse insert will always start in the end of
10255 * current line. */
10256 if (revins_on)
10257 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10258#endif
10259
10260 AppendToRedobuff(NL_STR);
10261 i = open_line(FORWARD,
10262#ifdef FEAT_COMMENTS
10263 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10264#endif
10265 0, old_indent);
10266 old_indent = 0;
10267#ifdef FEAT_CINDENT
10268 can_cindent = TRUE;
10269#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010270#ifdef FEAT_FOLDING
10271 /* When inserting a line the cursor line must never be in a closed fold. */
10272 foldOpenCursor();
10273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274
10275 return (!i);
10276}
10277
10278#ifdef FEAT_DIGRAPHS
10279/*
10280 * Handle digraph in insert mode.
10281 * Returns character still to be inserted, or NUL when nothing remaining to be
10282 * done.
10283 */
10284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010285ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286{
10287 int c;
10288 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010289 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290
10291 pc_status = PC_STATUS_UNSET;
10292 if (redrawing() && !char_avail())
10293 {
10294 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010295 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296
10297 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010298 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299#ifdef FEAT_CMDL_INFO
10300 add_to_showcmd_c(Ctrl_K);
10301#endif
10302 }
10303
10304#ifdef USE_ON_FLY_SCROLL
10305 dont_scroll = TRUE; /* disallow scrolling here */
10306#endif
10307
10308 /* don't map the digraph chars. This also prevents the
10309 * mode message to be deleted when ESC is hit */
10310 ++no_mapping;
10311 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010312 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 --no_mapping;
10314 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010315 if (did_putchar)
10316 /* when the line fits in 'columns' the '?' is at the start of the next
10317 * line and will not be removed by the redraw */
10318 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (IS_SPECIAL(c) || mod_mask) /* special key */
10321 {
10322#ifdef FEAT_CMDL_INFO
10323 clear_showcmd();
10324#endif
10325 insert_special(c, TRUE, FALSE);
10326 return NUL;
10327 }
10328 if (c != ESC)
10329 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010330 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 if (redrawing() && !char_avail())
10332 {
10333 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010334 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335
10336 if (char2cells(c) == 1)
10337 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010338 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010340 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 }
10342#ifdef FEAT_CMDL_INFO
10343 add_to_showcmd_c(c);
10344#endif
10345 }
10346 ++no_mapping;
10347 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010348 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 --no_mapping;
10350 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010351 if (did_putchar)
10352 /* when the line fits in 'columns' the '?' is at the start of the
10353 * next line and will not be removed by a redraw */
10354 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 if (cc != ESC)
10356 {
10357 AppendToRedobuff((char_u *)CTRL_V_STR);
10358 c = getdigraph(c, cc, TRUE);
10359#ifdef FEAT_CMDL_INFO
10360 clear_showcmd();
10361#endif
10362 return c;
10363 }
10364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365#ifdef FEAT_CMDL_INFO
10366 clear_showcmd();
10367#endif
10368 return NUL;
10369}
10370#endif
10371
10372/*
10373 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10374 * Returns the char to be inserted, or NUL if none found.
10375 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010376 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010377ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 int c;
10380 int temp;
10381 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010382 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383
10384 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10385 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010386 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 return NUL;
10388 }
10389
10390 /* try to advance to the cursor column */
10391 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010392 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 prev_ptr = ptr;
10394 validate_virtcol();
10395 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10396 {
10397 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010398 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 }
10400 if ((colnr_T)temp > curwin->w_virtcol)
10401 ptr = prev_ptr;
10402
10403#ifdef FEAT_MBYTE
10404 c = (*mb_ptr2char)(ptr);
10405#else
10406 c = *ptr;
10407#endif
10408 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010409 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 return c;
10411}
10412
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010413/*
10414 * CTRL-Y or CTRL-E typed in Insert mode.
10415 */
10416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010417ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010418{
10419 int c = tc;
10420
10421#ifdef FEAT_INS_EXPAND
10422 if (ctrl_x_mode == CTRL_X_SCROLL)
10423 {
10424 if (c == Ctrl_Y)
10425 scrolldown_clamp();
10426 else
10427 scrollup_clamp();
10428 redraw_later(VALID);
10429 }
10430 else
10431#endif
10432 {
10433 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10434 if (c != NUL)
10435 {
10436 long tw_save;
10437
10438 /* The character must be taken literally, insert like it
10439 * was typed after a CTRL-V, and pretend 'textwidth'
10440 * wasn't set. Digits, 'o' and 'x' are special after a
10441 * CTRL-V, don't use it for these. */
10442 if (c < 256 && !isalnum(c))
10443 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10444 tw_save = curbuf->b_p_tw;
10445 curbuf->b_p_tw = -1;
10446 insert_special(c, TRUE, FALSE);
10447 curbuf->b_p_tw = tw_save;
10448#ifdef FEAT_RIGHTLEFT
10449 revins_chars++;
10450 revins_legal++;
10451#endif
10452 c = Ctrl_V; /* pretend CTRL-V is last character */
10453 auto_format(FALSE, TRUE);
10454 }
10455 }
10456 return c;
10457}
10458
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459#ifdef FEAT_SMARTINDENT
10460/*
10461 * Try to do some very smart auto-indenting.
10462 * Used when inserting a "normal" character.
10463 */
10464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010465ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466{
10467 pos_T *pos, old_pos;
10468 char_u *ptr;
10469 int i;
10470 int temp;
10471
10472 /*
10473 * do some very smart indenting when entering '{' or '}'
10474 */
10475 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10476 {
10477 /*
10478 * for '}' set indent equal to indent of line containing matching '{'
10479 */
10480 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10481 {
10482 old_pos = curwin->w_cursor;
10483 /*
10484 * If the matching '{' has a ')' immediately before it (ignoring
10485 * white-space), then line up with the start of the line
10486 * containing the matching '(' if there is one. This handles the
10487 * case where an "if (..\n..) {" statement continues over multiple
10488 * lines -- webb
10489 */
10490 ptr = ml_get(pos->lnum);
10491 i = pos->col;
10492 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010493 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 ;
10495 curwin->w_cursor.lnum = pos->lnum;
10496 curwin->w_cursor.col = i;
10497 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10498 curwin->w_cursor = *pos;
10499 i = get_indent();
10500 curwin->w_cursor = old_pos;
10501#ifdef FEAT_VREPLACE
10502 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010503 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 else
10505#endif
10506 (void)set_indent(i, SIN_CHANGED);
10507 }
10508 else if (curwin->w_cursor.col > 0)
10509 {
10510 /*
10511 * when inserting '{' after "O" reduce indent, but not
10512 * more than indent of previous line
10513 */
10514 temp = TRUE;
10515 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10516 {
10517 old_pos = curwin->w_cursor;
10518 i = get_indent();
10519 while (curwin->w_cursor.lnum > 1)
10520 {
10521 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10522
10523 /* ignore empty lines and lines starting with '#'. */
10524 if (*ptr != '#' && *ptr != NUL)
10525 break;
10526 }
10527 if (get_indent() >= i)
10528 temp = FALSE;
10529 curwin->w_cursor = old_pos;
10530 }
10531 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010532 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
10534 }
10535
10536 /*
10537 * set indent of '#' always to 0
10538 */
10539 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10540 {
10541 /* remember current indent for next line */
10542 old_indent = get_indent();
10543 (void)set_indent(0, SIN_CHANGED);
10544 }
10545
10546 /* Adjust ai_col, the char at this position can be deleted. */
10547 if (ai_col > curwin->w_cursor.col)
10548 ai_col = curwin->w_cursor.col;
10549}
10550#endif
10551
10552/*
10553 * Get the value that w_virtcol would have when 'list' is off.
10554 * Unless 'cpo' contains the 'L' flag.
10555 */
10556 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010557get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
10559 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10560 return getvcol_nolist(&curwin->w_cursor);
10561 validate_virtcol();
10562 return curwin->w_virtcol;
10563}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010564
10565#ifdef FEAT_AUTOCMD
10566/*
10567 * Handle the InsertCharPre autocommand.
10568 * "c" is the character that was typed.
10569 * Return a pointer to allocated memory with the replacement string.
10570 * Return NULL to continue inserting "c".
10571 */
10572 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010573do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010574{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010575 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010576 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010577
10578 /* Return quickly when there is nothing to do. */
10579 if (!has_insertcharpre())
10580 return NULL;
10581
Bram Moolenaar704984a2012-06-01 14:57:51 +020010582#ifdef FEAT_MBYTE
10583 if (has_mbyte)
10584 buf[(*mb_char2bytes)(c, buf)] = NUL;
10585 else
10586#endif
10587 {
10588 buf[0] = c;
10589 buf[1] = NUL;
10590 }
10591
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010592 /* Lock the text to avoid weird things from happening. */
10593 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010594 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010595
Bram Moolenaar704984a2012-06-01 14:57:51 +020010596 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010597 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010598 {
10599 /* Get the value of v:char. It may be empty or more than one
10600 * character. Only use it when changed, otherwise continue with the
10601 * original character to avoid breaking autoindent. */
10602 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10603 res = vim_strsave(get_vim_var_str(VV_CHAR));
10604 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010605
10606 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10607 --textlock;
10608
10609 return res;
10610}
10611#endif