blob: 863f1cb68d310262ad3779ac8b5febb5ff3adbc4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010020# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
23# define CTRL_X_NOT_DEFINED_YET 1
24# define CTRL_X_SCROLL 2
25# define CTRL_X_WHOLE_LINE 3
26# define CTRL_X_FILES 4
27# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
28# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
30# define CTRL_X_FINISHED 8
31# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
32# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
33# define CTRL_X_CMDLINE 11
34# define CTRL_X_FUNCTION 12
35# define CTRL_X_OMNI 13
36# define CTRL_X_SPELL 14
37# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010040# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
41# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010043/* Message for CTRL-X mode, index is ctrl_x_mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static char *ctrl_x_msgs[] =
45{
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010046 N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000047 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010048 NULL, /* CTRL_X_SCROLL: depends on state */
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010054 NULL, /* CTRL_X_FINISHED */
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000057 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000059 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000060 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000061 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010062 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000063};
64
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000065static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010066# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010067static char e_complwin[] = N_("E839: Completion function changed window");
68static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
72 * Structure used to store one match for insert completion.
73 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000074typedef struct compl_S compl_T;
75struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 compl_T *cp_next;
78 compl_T *cp_prev;
79 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000080 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000081 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000082 char_u *cp_fname; /* file containing the match, allocated when
83 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000084 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
85 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086};
87
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010088# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
89# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093 * "compl_first_match" points to the start of the list.
94 * "compl_curr_match" points to the currently selected entry.
95 * "compl_shown_match" is different from compl_curr_match during
96 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000098static compl_T *compl_first_match = NULL;
99static compl_T *compl_curr_match = NULL;
100static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200101static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000103/* After using a cursor key <Enter> selects a match in the popup menu,
104 * otherwise it inserts a line break. */
105static int compl_enter_selects = FALSE;
106
Bram Moolenaara6557602006-02-04 22:43:20 +0000107/* When "compl_leader" is not NULL only matches that start with this string
108 * are used. */
109static char_u *compl_leader = NULL;
110
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000111static int compl_get_longest = FALSE; /* put longest common string
112 in compl_leader */
113
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200114static int compl_no_insert = FALSE; /* FALSE: select & insert
115 TRUE: noinsert */
116static int compl_no_select = FALSE; /* FALSE: select & insert
117 TRUE: noselect */
118
Bram Moolenaara6557602006-02-04 22:43:20 +0000119static int compl_used_match; /* Selected one of the matches. When
120 FALSE the match was edited or using
121 the longest common string. */
122
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000123static int compl_was_interrupted = FALSE; /* didn't finish finding
124 completions. */
125
126static int compl_restarting = FALSE; /* don't insert match */
127
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128/* When the first completion is done "compl_started" is set. When it's
129 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000131
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100132/* Which Ctrl-X mode are we in? */
133static int ctrl_x_mode = CTRL_X_NORMAL;
134
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000135/* Set when doing something for completion that may call edit() recursively,
136 * which is not allowed. */
137static int compl_busy = FALSE;
138
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static int compl_matches = 0;
140static char_u *compl_pattern = NULL;
141static int compl_direction = FORWARD;
142static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000143static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000144static pos_T compl_startpos;
145static colnr_T compl_col = 0; /* column where the text starts
146 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000147static char_u *compl_orig_text = NULL; /* text as it was before
148 * completion started */
149static int compl_cont_mode = 0;
150static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar82139082011-09-14 16:52:09 +0200152static int compl_opt_refresh_always = FALSE;
153
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static void ins_ctrl_x(void);
155static int has_compl_option(int dict_opt);
156static int ins_compl_accept_char(int c);
157static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
158static int ins_compl_equal(compl_T *match, char_u *str, int len);
159static void ins_compl_longest_match(compl_T *match);
160static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
161static int ins_compl_make_cyclic(void);
162static void ins_compl_upd_pum(void);
163static void ins_compl_del_pum(void);
164static int pum_wanted(void);
165static int pum_enough_matches(void);
166static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
167static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
168static char_u *find_line_end(char_u *ptr);
169static void ins_compl_free(void);
170static void ins_compl_clear(void);
171static int ins_compl_bs(void);
172static int ins_compl_need_restart(void);
173static void ins_compl_new_leader(void);
174static void ins_compl_addleader(int c);
175static int ins_compl_len(void);
176static void ins_compl_restart(void);
177static void ins_compl_set_original_text(char_u *str);
178static void ins_compl_addfrommatch(void);
179static int ins_compl_prep(int c);
180static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
181static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100182# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_add_list(list_T *list);
184static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100185# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static int ins_compl_get_exp(pos_T *ini);
187static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200188static void ins_compl_insert(int in_compl_func);
189static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_key2dir(int c);
191static int ins_compl_pum_key(int c);
192static int ins_compl_key2count(int c);
193static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100194static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100195static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100196static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* FEAT_INS_EXPAND */
198
199#define BACKSPACE_CHAR 1
200#define BACKSPACE_WORD 2
201#define BACKSPACE_WORD_NOT_SPACE 3
202#define BACKSPACE_LINE 4
203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_redraw(int ready);
205static void ins_ctrl_v(void);
206static void undisplay_dollar(void);
207static void insert_special(int, int, int);
208static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
209static void check_auto_format(int);
210static void redo_literal(int c);
211static void start_arrow(pos_T *end_insert_pos);
212static void start_arrow_with_change(pos_T *end_insert_pos, int change);
213static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000214#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void check_spell_redraw(void);
216static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000217static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
220static int echeck_abbr(int);
221static int replace_pop(void);
222static void replace_join(int off);
223static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void replace_flush(void);
228static void replace_do_bs(int limit_col);
229static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100233static void ins_reg(void);
234static void ins_ctrl_g(void);
235static void ins_ctrl_hat(void);
236static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static int ins_start_select(int c);
241static void ins_insert(int replaceState);
242static void ins_ctrl_o(void);
243static void ins_shift(int c, int lastc);
244static void ins_del(void);
245static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_mouse(int c);
248static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000250#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000252#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ins_left(int end_change);
254static void ins_home(int c);
255static void ins_end(int c);
256static void ins_s_left(void);
257static void ins_right(int end_change);
258static void ins_s_right(void);
259static void ins_up(int startcol);
260static void ins_pageup(void);
261static void ins_down(int startcol);
262static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static int ins_tab(void);
267static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100273static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100276#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280static colnr_T Insstart_textlen; /* length of line when insert started */
281static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100282static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284static char_u *last_insert = NULL; /* the text of the previous insert,
285 K_SPECIAL and CSI are escaped */
286static int last_insert_skip; /* nr of chars in front of previous insert */
287static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000288static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
290#ifdef FEAT_CINDENT
291static int can_cindent; /* may do cindenting on this line */
292#endif
293
294static int old_indent = 0; /* for ^^D command in insert mode */
295
296#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000297static int revins_on; /* reverse insert mode on */
298static int revins_chars; /* how much to skip after edit */
299static int revins_legal; /* was the last char 'legal'? */
300static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static int ins_need_undo; /* call u_save() before inserting a
304 char. Set when edit() is called.
305 after that arrow_used is used. */
306
307static int did_add_space = FALSE; /* auto_format() added an extra space
308 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200309static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
310 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312/*
313 * edit(): Start inserting text.
314 *
315 * "cmdchar" can be:
316 * 'i' normal insert command
317 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100318 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 * 'R' replace command
320 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
321 * but still only one <CR> is inserted. The <Esc> is not used for redo.
322 * 'g' "gI" command.
323 * 'V' "gR" command for Virtual Replace mode.
324 * 'v' "gr" command for single character Virtual Replace mode.
325 *
326 * This function is not called recursively. For CTRL-O commands, it returns
327 * and lets the caller handle the Normal-mode command.
328 *
329 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
330 */
331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100332edit(
333 int cmdchar,
334 int startln, /* if set, insert at start of line */
335 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 int c = 0;
338 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100339 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000340 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 int i;
343 int did_backspace = TRUE; /* previous char was backspace */
344#ifdef FEAT_CINDENT
345 int line_is_white = FALSE; /* line is empty before insert */
346#endif
347 linenr_T old_topline = 0; /* topline before insertion */
348#ifdef FEAT_DIFF
349 int old_topfill = -1;
350#endif
351 int inserted_space = FALSE; /* just inserted a space */
352 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000353 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000355 /* Remember whether editing was restarted after CTRL-O. */
356 did_restart_edit = restart_edit;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* sleep before redrawing, needed for "CTRL-O :" that results in an
359 * error message */
360 check_for_delay(TRUE);
361
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100362 /* set Insstart_orig to Insstart */
363 update_Insstart_orig = TRUE;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365#ifdef HAVE_SANDBOX
366 /* Don't allow inserting in the sandbox. */
367 if (sandbox != 0)
368 {
369 EMSG(_(e_sandbox));
370 return FALSE;
371 }
372#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000373 /* Don't allow changes in the buffer while editing the cmdline. The
374 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000375 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000376 {
377 EMSG(_(e_secure));
378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
381#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000382 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000383 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000384 {
385 EMSG(_(e_secure));
386 return FALSE;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 ins_compl_clear(); /* clear stuff for CTRL-X mode */
389#endif
390
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391#ifdef FEAT_AUTOCMD
392 /*
393 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
394 */
395 if (cmdchar != 'r' && cmdchar != 'v')
396 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100397 pos_T save_cursor = curwin->w_cursor;
398
Bram Moolenaar1e015462005-09-25 22:16:38 +0000399# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000400 if (cmdchar == 'R')
401 ptr = (char_u *)"r";
402 else if (cmdchar == 'V')
403 ptr = (char_u *)"v";
404 else
405 ptr = (char_u *)"i";
406 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200407 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000408# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000409 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100410
Bram Moolenaar097c9922013-05-19 21:15:15 +0200411 /* Make sure the cursor didn't move. Do call check_cursor_col() in
412 * case the text was modified. Since Insert mode was not started yet
413 * a call to check_cursor_col() may move the cursor, especially with
414 * the "A" command, thus set State to avoid that. Also check that the
415 * line number is still valid (lines may have been deleted).
416 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100417 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaar097c9922013-05-19 21:15:15 +0200418# ifdef FEAT_EVAL
419 && *get_vim_var_str(VV_CHAR) == NUL
420# endif
421 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100422 {
423 int save_state = State;
424
425 curwin->w_cursor = save_cursor;
426 State = INSERT;
427 check_cursor_col();
428 State = save_state;
429 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000430 }
431#endif
432
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200433#ifdef FEAT_CONCEAL
434 /* Check if the cursor line needs redrawing before changing State. If
435 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200436 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#endif
438
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#ifdef FEAT_MOUSE
440 /*
441 * When doing a paste with the middle mouse button, Insstart is set to
442 * where the paste started.
443 */
444 if (where_paste_started.lnum != 0)
445 Insstart = where_paste_started;
446 else
447#endif
448 {
449 Insstart = curwin->w_cursor;
450 if (startln)
451 Insstart.col = 0;
452 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000453 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 Insstart_blank_vcol = MAXCOL;
455 if (!did_ai)
456 ai_col = 0;
457
458 if (cmdchar != NUL && restart_edit == 0)
459 {
460 ResetRedobuff();
461 AppendNumberToRedobuff(count);
462#ifdef FEAT_VREPLACE
463 if (cmdchar == 'V' || cmdchar == 'v')
464 {
465 /* "gR" or "gr" command */
466 AppendCharToRedobuff('g');
467 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
468 }
469 else
470#endif
471 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100472 if (cmdchar == K_PS)
473 AppendCharToRedobuff('a');
474 else
475 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (cmdchar == 'g') /* "gI" command */
477 AppendCharToRedobuff('I');
478 else if (cmdchar == 'r') /* "r<CR>" command */
479 count = 1; /* insert only one <CR> */
480 }
481 }
482
483 if (cmdchar == 'R')
484 {
485#ifdef FEAT_FKMAP
486 if (p_fkmap && p_ri)
487 {
488 beep_flush();
489 EMSG(farsi_text_3); /* encoded in Farsi */
490 State = INSERT;
491 }
492 else
493#endif
494 State = REPLACE;
495 }
496#ifdef FEAT_VREPLACE
497 else if (cmdchar == 'V' || cmdchar == 'v')
498 {
499 State = VREPLACE;
500 replaceState = VREPLACE;
501 orig_line_count = curbuf->b_ml.ml_line_count;
502 vr_lines_changed = 1;
503 }
504#endif
505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100524#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
712 /*
713 * If we inserted a character at the last position of the last line in
714 * the window, scroll the window one line up. This avoids an extra
715 * redraw.
716 * This is detected when the cursor column is smaller after inserting
717 * something.
718 * Don't do this when the topline changed already, it has
719 * already been adjusted (by insertchar() calling open_line())).
720 */
721 if (curbuf->b_mod_set
722 && curwin->w_p_wrap
723 && !did_backspace
724 && curwin->w_topline == old_topline
725#ifdef FEAT_DIFF
726 && curwin->w_topfill == old_topfill
727#endif
728 )
729 {
730 mincol = curwin->w_wcol;
731 validate_cursor_col();
732
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000733 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 && curwin->w_wrow == W_WINROW(curwin)
735 + curwin->w_height - 1 - p_so
736 && (curwin->w_cursor.lnum != curwin->w_topline
737#ifdef FEAT_DIFF
738 || curwin->w_topfill > 0
739#endif
740 ))
741 {
742#ifdef FEAT_DIFF
743 if (curwin->w_topfill > 0)
744 --curwin->w_topfill;
745 else
746#endif
747#ifdef FEAT_FOLDING
748 if (hasFolding(curwin->w_topline, NULL, &old_topline))
749 set_topline(curwin, old_topline + 1);
750 else
751#endif
752 set_topline(curwin, curwin->w_topline + 1);
753 }
754 }
755
756 /* May need to adjust w_topline to show the cursor. */
757 update_topline();
758
759 did_backspace = FALSE;
760
761 validate_cursor(); /* may set must_redraw */
762
763 /*
764 * Redraw the display when no characters are waiting.
765 * Also shows mode, ruler and positions cursor.
766 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000767 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769#ifdef FEAT_SCROLLBIND
770 if (curwin->w_p_scb)
771 do_check_scrollbind(TRUE);
772#endif
773
Bram Moolenaar860cae12010-06-05 23:22:07 +0200774#ifdef FEAT_CURSORBIND
775 if (curwin->w_p_crb)
776 do_check_cursorbind();
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 update_curswant();
779 old_topline = curwin->w_topline;
780#ifdef FEAT_DIFF
781 old_topfill = curwin->w_topfill;
782#endif
783
784#ifdef USE_ON_FLY_SCROLL
785 dont_scroll = FALSE; /* allow scrolling here */
786#endif
787
788 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100789 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100791 if (c != K_CURSORHOLD)
792 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200793
794 /* After using CTRL-G U the next cursor key will not break undo. */
795 if (dont_sync_undo == MAYBE)
796 dont_sync_undo = TRUE;
797 else
798 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100799 if (cmdchar == K_PS)
800 /* Got here from normal mode when bracketed paste started. */
801 c = K_PS;
802 else
803 do
804 {
805 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100806 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000808#ifdef FEAT_AUTOCMD
809 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
810 did_cursorhold = TRUE;
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_RIGHTLEFT
814 if (p_hkmap && KeyTyped)
815 c = hkmap(c); /* Hebrew mode mapping */
816#endif
817#ifdef FEAT_FKMAP
818 if (p_fkmap && KeyTyped)
819 c = fkmap(c); /* Farsi mode mapping */
820#endif
821
822#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000823 /*
824 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000825 * and the cursor is still in the completed word. Only when there is
826 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000828 if (compl_started
829 && pum_wanted()
830 && curwin->w_cursor.col >= compl_col
831 && (compl_shown_match == NULL
832 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000833 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* BS: Delete one character from "compl_leader". */
835 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000836 && curwin->w_cursor.col > compl_col
837 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 continue;
839
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /* When no match was selected or it was edited. */
841 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000843 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000844 * "compl_leader". Except when at the original match and
845 * there is nothing to add, CTRL-L works like CTRL-P then. */
846 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100847 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000848 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000849 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000850 {
851 ins_compl_addfrommatch();
852 continue;
853 }
854
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000855 /* A non-white character that fits in with the current
856 * completion: Add to "compl_leader". */
857 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000858 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100859#ifdef FEAT_AUTOCMD
860 /* Trigger InsertCharPre. */
861 char_u *str = do_insert_char_pre(c);
862 char_u *p;
863
864 if (str != NULL)
865 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100866 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100867 ins_compl_addleader(PTR2CHAR(p));
868 vim_free(str);
869 }
870 else
871#endif
872 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000873 continue;
874 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000876 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000877 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200878 if ((c == Ctrl_Y || (compl_enter_selects
879 && (c == CAR || c == K_KENTER || c == NL)))
880 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000881 {
882 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200883 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000885 }
886 }
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
889 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000890 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000891 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000892 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894
Bram Moolenaar488c6512005-08-11 20:09:58 +0000895 /* CTRL-\ CTRL-N goes to Normal mode,
896 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
897 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (c == Ctrl_BSL)
899 {
900 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000901 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ++no_mapping;
903 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000904 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 --no_mapping;
906 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000907 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 vungetc(c);
911 c = Ctrl_BSL;
912 }
913 else if (c == Ctrl_G && p_im)
914 continue;
915 else
916 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000917 if (c == Ctrl_O)
918 {
919 ins_ctrl_o();
920 ins_at_eol = FALSE; /* cursor keeps its column */
921 nomove = TRUE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 count = 0;
924 goto doESCkey;
925 }
926 }
927
928#ifdef FEAT_DIGRAPHS
929 c = do_digraph(c);
930#endif
931
932#ifdef FEAT_INS_EXPAND
933 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
934 goto docomplete;
935#endif
936 if (c == Ctrl_V || c == Ctrl_Q)
937 {
938 ins_ctrl_v();
939 c = Ctrl_V; /* pretend CTRL-V is last typed character */
940 continue;
941 }
942
943#ifdef FEAT_CINDENT
944 if (cindent_on()
945# ifdef FEAT_INS_EXPAND
946 && ctrl_x_mode == 0
947# endif
948 )
949 {
950 /* A key name preceded by a bang means this key is not to be
951 * inserted. Skip ahead to the re-indenting below.
952 * A key name preceded by a star means that indenting has to be
953 * done before inserting the key. */
954 line_is_white = inindent(0);
955 if (in_cinkeys(c, '!', line_is_white))
956 goto force_cindent;
957 if (can_cindent && in_cinkeys(c, '*', line_is_white)
958 && stop_arrow() == OK)
959 do_c_expr_indent();
960 }
961#endif
962
963#ifdef FEAT_RIGHTLEFT
964 if (curwin->w_p_rl)
965 switch (c)
966 {
967 case K_LEFT: c = K_RIGHT; break;
968 case K_S_LEFT: c = K_S_RIGHT; break;
969 case K_C_LEFT: c = K_C_RIGHT; break;
970 case K_RIGHT: c = K_LEFT; break;
971 case K_S_RIGHT: c = K_S_LEFT; break;
972 case K_C_RIGHT: c = K_C_LEFT; break;
973 }
974#endif
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /*
977 * If 'keymodel' contains "startsel", may start selection. If it
978 * does, a CTRL-O and c will be stuffed, we need to get these
979 * characters.
980 */
981 if (ins_start_select(c))
982 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * The big switch to handle a character in insert mode.
986 */
987 switch (c)
988 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (echeck_abbr(ESC + ABBR_OFF))
991 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200992 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_CMDWIN
996 if (c == Ctrl_C && cmdwin_type != 0)
997 {
998 /* Close the cmdline window. */
999 cmdwin_result = K_IGNORE;
1000 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001001 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 goto doESCkey;
1003 }
1004#endif
1005
1006#ifdef UNIX
1007do_intr:
1008#endif
1009 /* when 'insertmode' set, and not halfway a mapping, don't leave
1010 * Insert mode */
1011 if (goto_im())
1012 {
1013 if (got_int)
1014 {
1015 (void)vgetc(); /* flush all buffers */
1016 got_int = FALSE;
1017 }
1018 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001019 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021 }
1022doESCkey:
1023 /*
1024 * This is the ONLY return from edit()!
1025 */
1026 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1027 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001028 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 o_lnum = curwin->w_cursor.lnum;
1030
Bram Moolenaar488c6512005-08-11 20:09:58 +00001031 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032 {
1033#ifdef FEAT_AUTOCMD
1034 if (cmdchar != 'r' && cmdchar != 'v')
1035 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1036 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001037 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 continue;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case Ctrl_Z: /* suspend when 'insertmode' set */
1044 if (!p_im)
1045 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001046 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001047#ifdef CURSOR_SHAPE
1048 ui_cursor_shape(); /* may need to update cursor shape */
1049#endif
1050 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051
1052 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001053#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001054 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 goto docomplete;
1056#endif
1057 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1058 break;
1059 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001060
1061#ifdef FEAT_VIRTUALEDIT
1062 /* don't move the cursor left when 'virtualedit' has "onemore". */
1063 if (ve_flags & VE_ONEMORE)
1064 {
1065 ins_at_eol = FALSE;
1066 nomove = TRUE;
1067 }
1068#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 count = 0;
1070 goto doESCkey;
1071
Bram Moolenaar572cb562005-08-05 21:35:02 +00001072 case K_INS: /* toggle insert/replace mode */
1073 case K_KINS:
1074 ins_insert(replaceState);
1075 break;
1076
1077 case K_SELECT: /* end of Select mode mapping - ignore */
1078 break;
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_HELP: /* Help key works like <ESC> <Help> */
1081 case K_F1:
1082 case K_XF1:
1083 stuffcharReadbuff(K_HELP);
1084 if (p_im)
1085 need_start_insertmode = TRUE;
1086 goto doESCkey;
1087
1088#ifdef FEAT_NETBEANS_INTG
1089 case K_F21: /* NetBeans command */
1090 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001091 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 --no_mapping;
1093 netbeans_keycommand(i);
1094 break;
1095#endif
1096
1097 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case NUL:
1099 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 /* For ^@ the trailing ESC will end the insert, unless there is an
1101 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1103 && c != Ctrl_A && !p_im)
1104 goto doESCkey; /* quit insert mode */
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_reg();
1110 auto_format(FALSE, TRUE);
1111 inserted_space = FALSE;
1112 break;
1113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 ins_ctrl_g();
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_HAT: /* switch input mode and/or langmap */
1119 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
1122#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!p_ari)
1125 goto normalchar;
1126 ins_ctrl_();
1127 break;
1128#endif
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1132 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1133 goto docomplete;
1134#endif
1135 /* FALLTHROUGH */
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138# ifdef FEAT_INS_EXPAND
1139 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 if (has_compl_option(FALSE))
1142 goto docomplete;
1143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145# endif
1146 ins_shift(c, lastc);
1147 auto_format(FALSE, TRUE);
1148 inserted_space = FALSE;
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_KDEL:
1153 ins_del();
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case Ctrl_H:
1159 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1160 auto_format(FALSE, TRUE);
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1165 auto_format(FALSE, TRUE);
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001169# ifdef FEAT_COMPL_FUNC
1170 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001172 goto docomplete;
1173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
1179#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_LEFTMOUSE_NM:
1182 case K_LEFTDRAG:
1183 case K_LEFTRELEASE:
1184 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001185 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_MIDDLEMOUSE:
1187 case K_MIDDLEDRAG:
1188 case K_MIDDLERELEASE:
1189 case K_RIGHTMOUSE:
1190 case K_RIGHTDRAG:
1191 case K_RIGHTRELEASE:
1192 case K_X1MOUSE:
1193 case K_X1DRAG:
1194 case K_X1RELEASE:
1195 case K_X2MOUSE:
1196 case K_X2DRAG:
1197 case K_X2RELEASE:
1198 ins_mouse(c);
1199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001202 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001206 ins_mousescroll(MSCR_UP);
1207 break;
1208
1209 case K_MOUSELEFT: /* Scroll wheel left */
1210 ins_mousescroll(MSCR_LEFT);
1211 break;
1212
1213 case K_MOUSERIGHT: /* Scroll wheel right */
1214 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 break;
1216#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001217 case K_PS:
1218 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1219 if (cmdchar == K_PS)
1220 /* invoked from normal mode, bail out */
1221 goto doESCkey;
1222 break;
1223 case K_PE:
1224 /* Got K_PE without K_PS, ignore. */
1225 break;
1226
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001227#ifdef FEAT_GUI_TABLINE
1228 case K_TABLINE:
1229 case K_TABMENU:
1230 ins_tabline(c);
1231 break;
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar754b5602006-02-09 23:53:20 +00001237#ifdef FEAT_AUTOCMD
1238 case K_CURSORHOLD: /* Didn't type something for a while. */
1239 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1240 did_cursorhold = TRUE;
1241 break;
1242#endif
1243
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244#ifdef FEAT_GUI_W32
1245 /* On Win32 ignore <M-F4>, we get it when closing the window was
1246 * cancelled. */
1247 case K_F4:
1248 if (mod_mask != MOD_MASK_ALT)
1249 goto normalchar;
1250 break;
1251#endif
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#ifdef FEAT_GUI
1254 case K_VER_SCROLLBAR:
1255 ins_scroll();
1256 break;
1257
1258 case K_HOR_SCROLLBAR:
1259 ins_horscroll();
1260 break;
1261#endif
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_HOME:
1266 case K_C_HOME:
1267 ins_home(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 case K_S_END:
1273 case K_C_END:
1274 ins_end(c);
1275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001278 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1279 ins_s_left();
1280 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001281 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 case K_C_LEFT:
1286 ins_s_left();
1287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001290 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1291 ins_s_right();
1292 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001293 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 case K_C_RIGHT:
1298 ins_s_right();
1299 break;
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001302#ifdef FEAT_INS_EXPAND
1303 if (pum_visible())
1304 goto docomplete;
1305#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001306 if (mod_mask & MOD_MASK_SHIFT)
1307 ins_pageup();
1308 else
1309 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_PAGEUP:
1314 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001315#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001316 if (pum_visible())
1317 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 ins_pageup();
1320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001323#ifdef FEAT_INS_EXPAND
1324 if (pum_visible())
1325 goto docomplete;
1326#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001327 if (mod_mask & MOD_MASK_SHIFT)
1328 ins_pagedown();
1329 else
1330 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 break;
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case K_PAGEDOWN:
1335 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001336#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001337 if (pum_visible())
1338 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ins_pagedown();
1341 break;
1342
1343#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 ins_drop();
1346 break;
1347#endif
1348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 c = TAB;
1351 /* FALLTHROUGH */
1352
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001353 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1355 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1356 goto docomplete;
1357#endif
1358 inserted_space = FALSE;
1359 if (ins_tab())
1360 goto normalchar; /* insert TAB as a normal char */
1361 auto_format(FALSE, TRUE);
1362 break;
1363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 c = CAR;
1366 /* FALLTHROUGH */
1367 case CAR:
1368 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001369#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* In a quickfix window a <CR> jumps to the error under the
1371 * cursor. */
1372 if (bt_quickfix(curbuf) && c == CAR)
1373 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 if (curwin->w_llist_ref == NULL) /* quickfix window */
1375 do_cmdline_cmd((char_u *)".cc");
1376 else /* location list window */
1377 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 break;
1379 }
1380#endif
1381#ifdef FEAT_CMDWIN
1382 if (cmdwin_type != 0)
1383 {
1384 /* Execute the command in the cmdline window. */
1385 cmdwin_result = CAR;
1386 goto doESCkey;
1387 }
1388#endif
1389 if (ins_eol(c) && !p_im)
1390 goto doESCkey; /* out of memory */
1391 auto_format(FALSE, FALSE);
1392 inserted_space = FALSE;
1393 break;
1394
Bram Moolenaar860cae12010-06-05 23:22:07 +02001395#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001396 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397# ifdef FEAT_INS_EXPAND
1398 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1399 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001400 if (has_compl_option(TRUE))
1401 goto docomplete;
1402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404# endif
1405# ifdef FEAT_DIGRAPHS
1406 c = ins_digraph();
1407 if (c == NUL)
1408 break;
1409# endif
1410 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001414 case Ctrl_X: /* Enter CTRL-X mode */
1415 ins_ctrl_x();
1416 break;
1417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (ctrl_x_mode != CTRL_X_TAGS)
1420 goto normalchar;
1421 goto docomplete;
1422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (ctrl_x_mode != CTRL_X_FILES)
1425 goto normalchar;
1426 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001427
1428 case 's': /* Spelling completion after ^X */
1429 case Ctrl_S:
1430 if (ctrl_x_mode != CTRL_X_SPELL)
1431 goto normalchar;
1432 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#endif
1434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef FEAT_INS_EXPAND
1437 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1438#endif
1439 {
1440 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1441 if (p_im)
1442 {
1443 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1444 break;
1445 goto doESCkey;
1446 }
1447 goto normalchar;
1448 }
1449#ifdef FEAT_INS_EXPAND
1450 /* FALLTHROUGH */
1451
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 case Ctrl_N:
1454 /* if 'complete' is empty then plain ^P is no longer special,
1455 * but it is under other ^X modes */
1456 if (*curbuf->b_p_cpt == NUL
1457 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 goto normalchar;
1460
1461docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001462 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001463#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001464 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001465#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001466 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001467 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001468#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001469 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001470#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001471 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 break;
1473#endif /* FEAT_INS_EXPAND */
1474
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001475 case Ctrl_Y: /* copy from previous line or scroll down */
1476 case Ctrl_E: /* copy from next line or scroll up */
1477 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 break;
1479
1480 default:
1481#ifdef UNIX
1482 if (c == intr_char) /* special interrupt char */
1483 goto do_intr;
1484#endif
1485
Bram Moolenaare659c952011-05-19 17:25:41 +02001486normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001488 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001490#ifdef FEAT_AUTOCMD
1491 if (!p_paste)
1492 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001493 /* Trigger InsertCharPre. */
1494 char_u *str = do_insert_char_pre(c);
1495 char_u *p;
1496
1497 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001498 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001500 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001501 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001502 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001503 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001504 c = PTR2CHAR(p);
1505 if (c == CAR || c == K_KENTER || c == NL)
1506 ins_eol(c);
1507 else
1508 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001509 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001510 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001511 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001512 vim_free(str);
1513 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001514 }
1515
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001516 /* If the new value is already inserted or an empty string
1517 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001518 if (c == NUL)
1519 break;
1520 }
1521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522#ifdef FEAT_SMARTINDENT
1523 /* Try to perform smart-indenting. */
1524 ins_try_si(c);
1525#endif
1526
1527 if (c == ' ')
1528 {
1529 inserted_space = TRUE;
1530#ifdef FEAT_CINDENT
1531 if (inindent(0))
1532 can_cindent = FALSE;
1533#endif
1534 if (Insstart_blank_vcol == MAXCOL
1535 && curwin->w_cursor.lnum == Insstart.lnum)
1536 Insstart_blank_vcol = get_nolist_virtcol();
1537 }
1538
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001539 /* Insert a normal character and check for abbreviations on a
1540 * special character. Let CTRL-] expand abbreviations without
1541 * inserting it. */
1542 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#ifdef FEAT_MBYTE
1544 /* Add ABBR_OFF for characters above 0x100, this is
1545 * what check_abbr() expects. */
1546 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1547#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001548 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
1550 insert_special(c, FALSE, FALSE);
1551#ifdef FEAT_RIGHTLEFT
1552 revins_legal++;
1553 revins_chars++;
1554#endif
1555 }
1556
1557 auto_format(FALSE, TRUE);
1558
1559#ifdef FEAT_FOLDING
1560 /* When inserting a character the cursor line must never be in a
1561 * closed fold. */
1562 foldOpenCursor();
1563#endif
1564 break;
1565 } /* end of switch (c) */
1566
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001567#ifdef FEAT_AUTOCMD
1568 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001569 if (c != K_CURSORHOLD
1570# ifdef FEAT_COMPL_FUNC
1571 /* but not in CTRL-X mode, a script can't restore the state */
1572 && ctrl_x_mode == 0
1573# endif
1574 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001575 did_cursorhold = FALSE;
1576#endif
1577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 /* If the cursor was moved we didn't just insert a space */
1579 if (arrow_used)
1580 inserted_space = FALSE;
1581
1582#ifdef FEAT_CINDENT
1583 if (can_cindent && cindent_on()
1584# ifdef FEAT_INS_EXPAND
1585 && ctrl_x_mode == 0
1586# endif
1587 )
1588 {
1589force_cindent:
1590 /*
1591 * Indent now if a key was typed that is in 'cinkeys'.
1592 */
1593 if (in_cinkeys(c, ' ', line_is_white))
1594 {
1595 if (stop_arrow() == OK)
1596 /* re-indent the current line */
1597 do_c_expr_indent();
1598 }
1599 }
1600#endif /* FEAT_CINDENT */
1601
1602 } /* for (;;) */
1603 /* NOTREACHED */
1604}
1605
1606/*
1607 * Redraw for Insert mode.
1608 * This is postponed until getting the next character to make '$' in the 'cpo'
1609 * option work correctly.
1610 * Only redraw when there are no characters available. This speeds up
1611 * inserting sequences of characters (e.g., for CTRL-R).
1612 */
1613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614ins_redraw(
1615 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617#ifdef FEAT_CONCEAL
1618 linenr_T conceal_old_cursor_line = 0;
1619 linenr_T conceal_new_cursor_line = 0;
1620 int conceal_update_lines = FALSE;
1621#endif
1622
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001623 if (char_avail())
1624 return;
1625
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001626#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1628 * visible, the command might delete it. */
1629 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001630# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001631 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001632# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001633# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001634 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001635# endif
1636# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001637 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001638# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001639 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001640# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001641 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001642# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001643# ifdef FEAT_INS_EXPAND
1644 && !pum_visible()
1645# endif
1646 )
1647 {
1648# ifdef FEAT_SYN_HL
1649 /* Need to update the screen first, to make sure syntax
1650 * highlighting is correct after making a change (e.g., inserting
1651 * a "(". The autocommand may also require a redraw, so it's done
1652 * again below, unfortunately. */
1653 if (syntax_present(curwin) && must_redraw)
1654 update_screen(0);
1655# endif
1656# ifdef FEAT_AUTOCMD
1657 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001658 {
1659 /* Make sure curswant is correct, an autocommand may call
1660 * getcurpos(). */
1661 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001662 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001663 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664# endif
1665# ifdef FEAT_CONCEAL
1666 if (curwin->w_p_cole > 0)
1667 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001668# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 conceal_new_cursor_line = curwin->w_cursor.lnum;
1672 conceal_update_lines = TRUE;
1673 }
1674# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001675# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001676 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001677# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001678 }
1679#endif
1680
1681#ifdef FEAT_AUTOCMD
1682 /* Trigger TextChangedI if b_changedtick differs. */
1683 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001684 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001685# ifdef FEAT_INS_EXPAND
1686 && !pum_visible()
1687# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001688 )
1689 {
1690 if (last_changedtick_buf == curbuf)
1691 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1692 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001693 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001695#endif
1696
1697 if (must_redraw)
1698 update_screen(0);
1699 else if (clear_cmdline || redraw_cmdline)
1700 showmode(); /* clear cmdline and show mode */
1701# if defined(FEAT_CONCEAL)
1702 if ((conceal_update_lines
1703 && (conceal_old_cursor_line != conceal_new_cursor_line
1704 || conceal_cursor_line(curwin)))
1705 || need_cursor_line_redraw)
1706 {
1707 if (conceal_old_cursor_line != conceal_new_cursor_line)
1708 update_single_line(curwin, conceal_old_cursor_line);
1709 update_single_line(curwin, conceal_new_cursor_line == 0
1710 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1711 curwin->w_valid &= ~VALID_CROW;
1712 }
1713# endif
1714 showruler(FALSE);
1715 setcursor();
1716 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717}
1718
1719/*
1720 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1721 */
1722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001723ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001726 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001729 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001734 did_putchar = TRUE;
1735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1737
1738#ifdef FEAT_CMDL_INFO
1739 add_to_showcmd_c(Ctrl_V);
1740#endif
1741
1742 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001743 if (did_putchar)
1744 /* when the line fits in 'columns' the '^' is at the start of the next
1745 * line and will not removed by the redraw */
1746 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#ifdef FEAT_CMDL_INFO
1748 clear_showcmd();
1749#endif
1750 insert_special(c, FALSE, TRUE);
1751#ifdef FEAT_RIGHTLEFT
1752 revins_chars++;
1753 revins_legal++;
1754#endif
1755}
1756
1757/*
1758 * Put a character directly onto the screen. It's not stored in a buffer.
1759 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1760 */
1761static int pc_status;
1762#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1763#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1764#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1765#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767static int pc_attr;
1768static int pc_row;
1769static int pc_col;
1770
1771 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 int attr;
1775
1776 if (ScreenLines != NULL)
1777 {
1778 update_topline(); /* just in case w_topline isn't valid */
1779 validate_cursor();
1780 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001781 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 else
1783 attr = 0;
1784 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001785 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1787 pc_status = PC_STATUS_UNSET;
1788#endif
1789#ifdef FEAT_RIGHTLEFT
1790 if (curwin->w_p_rl)
1791 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001792 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793# ifdef FEAT_MBYTE
1794 if (has_mbyte)
1795 {
1796 int fix_col = mb_fix_col(pc_col, pc_row);
1797
1798 if (fix_col != pc_col)
1799 {
1800 screen_putchar(' ', pc_row, fix_col, attr);
1801 --curwin->w_wcol;
1802 pc_status = PC_STATUS_RIGHT;
1803 }
1804 }
1805# endif
1806 }
1807 else
1808#endif
1809 {
1810 pc_col += curwin->w_wcol;
1811#ifdef FEAT_MBYTE
1812 if (mb_lefthalve(pc_row, pc_col))
1813 pc_status = PC_STATUS_LEFT;
1814#endif
1815 }
1816
1817 /* save the character to be able to put it back */
1818#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1819 if (pc_status == PC_STATUS_UNSET)
1820#endif
1821 {
1822 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1823 pc_status = PC_STATUS_SET;
1824 }
1825 screen_putchar(c, pc_row, pc_col, attr);
1826 }
1827}
1828
1829/*
1830 * Undo the previous edit_putchar().
1831 */
1832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1836 {
1837#if defined(FEAT_MBYTE)
1838 if (pc_status == PC_STATUS_RIGHT)
1839 ++curwin->w_wcol;
1840 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1841 redrawWinline(curwin->w_cursor.lnum, FALSE);
1842 else
1843#endif
1844 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1845 }
1846}
1847
1848/*
1849 * Called when p_dollar is set: display a '$' at the end of the changed text
1850 * Only works when cursor is in the line that changes.
1851 */
1852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854{
1855 colnr_T save_col;
1856
1857 if (!redrawing())
1858 return;
1859
1860 cursor_off();
1861 save_col = curwin->w_cursor.col;
1862 curwin->w_cursor.col = col;
1863#ifdef FEAT_MBYTE
1864 if (has_mbyte)
1865 {
1866 char_u *p;
1867
1868 /* If on the last byte of a multi-byte move to the first byte. */
1869 p = ml_get_curline();
1870 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1871 }
1872#endif
1873 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001874 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 {
1876 edit_putchar('$', FALSE);
1877 dollar_vcol = curwin->w_virtcol;
1878 }
1879 curwin->w_cursor.col = save_col;
1880}
1881
1882/*
1883 * Call this function before moving the cursor from the normal insert position
1884 * in insert mode.
1885 */
1886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001889 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001891 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 redrawWinline(curwin->w_cursor.lnum, FALSE);
1893 }
1894}
1895
1896/*
1897 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1898 * Keep the cursor on the same character.
1899 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1900 * type == INDENT_DEC decrease indent (for CTRL-D)
1901 * type == INDENT_SET set indent to "amount"
1902 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1903 */
1904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905change_indent(
1906 int type,
1907 int amount,
1908 int round,
1909 int replaced, /* replaced character, put on replace stack */
1910 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 int vcol;
1913 int last_vcol;
1914 int insstart_less; /* reduction for Insstart.col */
1915 int new_cursor_col;
1916 int i;
1917 char_u *ptr;
1918 int save_p_list;
1919 int start_col;
1920 colnr_T vc;
1921#ifdef FEAT_VREPLACE
1922 colnr_T orig_col = 0; /* init for GCC */
1923 char_u *new_line, *orig_line = NULL; /* init for GCC */
1924
1925 /* VREPLACE mode needs to know what the line was like before changing */
1926 if (State & VREPLACE_FLAG)
1927 {
1928 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1929 orig_col = curwin->w_cursor.col;
1930 }
1931#endif
1932
1933 /* for the following tricks we don't want list mode */
1934 save_p_list = curwin->w_p_list;
1935 curwin->w_p_list = FALSE;
1936 vc = getvcol_nolist(&curwin->w_cursor);
1937 vcol = vc;
1938
1939 /*
1940 * For Replace mode we need to fix the replace stack later, which is only
1941 * possible when the cursor is in the indent. Remember the number of
1942 * characters before the cursor if it's possible.
1943 */
1944 start_col = curwin->w_cursor.col;
1945
1946 /* determine offset from first non-blank */
1947 new_cursor_col = curwin->w_cursor.col;
1948 beginline(BL_WHITE);
1949 new_cursor_col -= curwin->w_cursor.col;
1950
1951 insstart_less = curwin->w_cursor.col;
1952
1953 /*
1954 * If the cursor is in the indent, compute how many screen columns the
1955 * cursor is to the left of the first non-blank.
1956 */
1957 if (new_cursor_col < 0)
1958 vcol = get_indent() - vcol;
1959
1960 if (new_cursor_col > 0) /* can't fix replace stack */
1961 start_col = -1;
1962
1963 /*
1964 * Set the new indent. The cursor will be put on the first non-blank.
1965 */
1966 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001967 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
1969 {
1970#ifdef FEAT_VREPLACE
1971 int save_State = State;
1972
1973 /* Avoid being called recursively. */
1974 if (State & VREPLACE_FLAG)
1975 State = INSERT;
1976#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001977 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef FEAT_VREPLACE
1979 State = save_State;
1980#endif
1981 }
1982 insstart_less -= curwin->w_cursor.col;
1983
1984 /*
1985 * Try to put cursor on same character.
1986 * If the cursor is at or after the first non-blank in the line,
1987 * compute the cursor column relative to the column of the first
1988 * non-blank character.
1989 * If we are not in insert mode, leave the cursor on the first non-blank.
1990 * If the cursor is before the first non-blank, position it relative
1991 * to the first non-blank, counted in screen columns.
1992 */
1993 if (new_cursor_col >= 0)
1994 {
1995 /*
1996 * When changing the indent while the cursor is touching it, reset
1997 * Insstart_col to 0.
1998 */
1999 if (new_cursor_col == 0)
2000 insstart_less = MAXCOL;
2001 new_cursor_col += curwin->w_cursor.col;
2002 }
2003 else if (!(State & INSERT))
2004 new_cursor_col = curwin->w_cursor.col;
2005 else
2006 {
2007 /*
2008 * Compute the screen column where the cursor should be.
2009 */
2010 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002011 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 /*
2014 * Advance the cursor until we reach the right screen column.
2015 */
2016 vcol = last_vcol = 0;
2017 new_cursor_col = -1;
2018 ptr = ml_get_curline();
2019 while (vcol <= (int)curwin->w_virtcol)
2020 {
2021 last_vcol = vcol;
2022#ifdef FEAT_MBYTE
2023 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002024 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 else
2026#endif
2027 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002028 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030 vcol = last_vcol;
2031
2032 /*
2033 * May need to insert spaces to be able to position the cursor on
2034 * the right screen column.
2035 */
2036 if (vcol != (int)curwin->w_virtcol)
2037 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002038 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002040 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (ptr != NULL)
2042 {
2043 new_cursor_col += i;
2044 ptr[i] = NUL;
2045 while (--i >= 0)
2046 ptr[i] = ' ';
2047 ins_str(ptr);
2048 vim_free(ptr);
2049 }
2050 }
2051
2052 /*
2053 * When changing the indent while the cursor is in it, reset
2054 * Insstart_col to 0.
2055 */
2056 insstart_less = MAXCOL;
2057 }
2058
2059 curwin->w_p_list = save_p_list;
2060
2061 if (new_cursor_col <= 0)
2062 curwin->w_cursor.col = 0;
2063 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002064 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 curwin->w_set_curswant = TRUE;
2066 changed_cline_bef_curs();
2067
2068 /*
2069 * May have to adjust the start of the insert.
2070 */
2071 if (State & INSERT)
2072 {
2073 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2074 {
2075 if ((int)Insstart.col <= insstart_less)
2076 Insstart.col = 0;
2077 else
2078 Insstart.col -= insstart_less;
2079 }
2080 if ((int)ai_col <= insstart_less)
2081 ai_col = 0;
2082 else
2083 ai_col -= insstart_less;
2084 }
2085
2086 /*
2087 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2088 * If the number of characters before the cursor decreased, need to pop a
2089 * few characters from the replace stack.
2090 * If the number of characters before the cursor increased, need to push a
2091 * few NULs onto the replace stack.
2092 */
2093 if (REPLACE_NORMAL(State) && start_col >= 0)
2094 {
2095 while (start_col > (int)curwin->w_cursor.col)
2096 {
2097 replace_join(0); /* remove a NUL from the replace stack */
2098 --start_col;
2099 }
2100 while (start_col < (int)curwin->w_cursor.col || replaced)
2101 {
2102 replace_push(NUL);
2103 if (replaced)
2104 {
2105 replace_push(replaced);
2106 replaced = NUL;
2107 }
2108 ++start_col;
2109 }
2110 }
2111
2112#ifdef FEAT_VREPLACE
2113 /*
2114 * For VREPLACE mode, we also have to fix the replace stack. In this case
2115 * it is always possible because we backspace over the whole line and then
2116 * put it back again the way we wanted it.
2117 */
2118 if (State & VREPLACE_FLAG)
2119 {
2120 /* If orig_line didn't allocate, just return. At least we did the job,
2121 * even if you can't backspace. */
2122 if (orig_line == NULL)
2123 return;
2124
2125 /* Save new line */
2126 new_line = vim_strsave(ml_get_curline());
2127 if (new_line == NULL)
2128 return;
2129
2130 /* We only put back the new line up to the cursor */
2131 new_line[curwin->w_cursor.col] = NUL;
2132
2133 /* Put back original line */
2134 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2135 curwin->w_cursor.col = orig_col;
2136
2137 /* Backspace from cursor to start of line */
2138 backspace_until_column(0);
2139
2140 /* Insert new stuff into line again */
2141 ins_bytes(new_line);
2142
2143 vim_free(new_line);
2144 }
2145#endif
2146}
2147
2148/*
2149 * Truncate the space at the end of a line. This is to be used only in an
2150 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2151 * modes.
2152 */
2153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002154truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
2156 int i;
2157
2158 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002159 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 if (State & REPLACE_FLAG)
2162 replace_join(0); /* remove a NUL from the replace stack */
2163 }
2164 line[i + 1] = NUL;
2165}
2166
2167#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2168 || defined(FEAT_COMMENTS) || defined(PROTO)
2169/*
2170 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2171 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002172 * Will attempt not to go before "col" even when there is a composing
2173 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 */
2175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 while ((int)curwin->w_cursor.col > col)
2179 {
2180 curwin->w_cursor.col--;
2181 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002182 replace_do_bs(col);
2183 else if (!del_char_after_col(col))
2184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186}
2187#endif
2188
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002189/*
2190 * Like del_char(), but make sure not to go before column "limit_col".
2191 * Only matters when there are composing characters.
2192 * Return TRUE when something was deleted.
2193 */
2194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002196{
2197#ifdef FEAT_MBYTE
2198 if (enc_utf8 && limit_col >= 0)
2199 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002200 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002201
2202 /* Make sure the cursor is at the start of a character, but
2203 * skip forward again when going too far back because of a
2204 * composing character. */
2205 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002206 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002207 {
2208 int l = utf_ptr2len(ml_get_cursor());
2209
2210 if (l == 0) /* end of line */
2211 break;
2212 curwin->w_cursor.col += l;
2213 }
2214 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2215 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002216 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002217 }
2218 else
2219#endif
2220 (void)del_char(FALSE);
2221 return TRUE;
2222}
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2225/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002226 * CTRL-X pressed in Insert mode.
2227 */
2228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002229ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002230{
2231 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2232 * CTRL-V works like CTRL-N */
2233 if (ctrl_x_mode != CTRL_X_CMDLINE)
2234 {
2235 /* if the next ^X<> won't ADD nothing, then reset
2236 * compl_cont_status */
2237 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002238 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002239 else
2240 compl_cont_status = 0;
2241 /* We're not sure which CTRL-X mode it will be yet */
2242 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2243 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2244 edit_submode_pre = NULL;
2245 showmode();
2246 }
2247}
2248
2249/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002250 * Whether other than default completion has been selected.
2251 */
2252 int
2253ctrl_x_mode_not_default(void)
2254{
2255 return ctrl_x_mode != CTRL_X_NORMAL;
2256}
2257
2258/*
2259 * Whether CTRL-X was typed without a following character.
2260 */
2261 int
2262ctrl_x_mode_not_defined_yet(void)
2263{
2264 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2265}
2266
2267/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002268 * Return TRUE if the 'dict' or 'tsr' option can be used.
2269 */
2270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002272{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002273 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002274# ifdef FEAT_SPELL
2275 && !curwin->w_p_spell
2276# endif
2277 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002278 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2279 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002280 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002281 edit_submode = NULL;
2282 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2283 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002284 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002285 if (emsg_silent == 0)
2286 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002287 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 setcursor();
2289 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002290#ifdef FEAT_EVAL
2291 if (!get_vim_var_nr(VV_TESTING))
2292#endif
2293 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002294 }
2295 return FALSE;
2296 }
2297 return TRUE;
2298}
2299
2300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2302 * This depends on the current mode.
2303 */
2304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002305vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 /* Always allow ^R - let it's results then be checked */
2308 if (c == Ctrl_R)
2309 return TRUE;
2310
Bram Moolenaare3226be2005-12-18 22:10:00 +00002311 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002313 return TRUE;
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 switch (ctrl_x_mode)
2316 {
2317 case 0: /* Not in any CTRL-X mode */
2318 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2319 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002320 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2322 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2323 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002324 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002325 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 case CTRL_X_SCROLL:
2327 return (c == Ctrl_Y || c == Ctrl_E);
2328 case CTRL_X_WHOLE_LINE:
2329 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2330 case CTRL_X_FILES:
2331 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2332 case CTRL_X_DICTIONARY:
2333 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2334 case CTRL_X_THESAURUS:
2335 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2336 case CTRL_X_TAGS:
2337 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2338#ifdef FEAT_FIND_ID
2339 case CTRL_X_PATH_PATTERNS:
2340 return (c == Ctrl_P || c == Ctrl_N);
2341 case CTRL_X_PATH_DEFINES:
2342 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2343#endif
2344 case CTRL_X_CMDLINE:
2345 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2346 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002347#ifdef FEAT_COMPL_FUNC
2348 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002349 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002350 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002351 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002352#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002353 case CTRL_X_SPELL:
2354 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002355 case CTRL_X_EVAL:
2356 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002358 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return FALSE;
2360}
2361
2362/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002363 * Return TRUE when character "c" is part of the item currently being
2364 * completed. Used to decide whether to abandon complete mode when the menu
2365 * is visible.
2366 */
2367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002368ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002369{
2370 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2371 /* When expanding an identifier only accept identifier chars. */
2372 return vim_isIDc(c);
2373
2374 switch (ctrl_x_mode)
2375 {
2376 case CTRL_X_FILES:
2377 /* When expanding file name only accept file name chars. But not
2378 * path separators, so that "proto/<Tab>" expands files in
2379 * "proto", not "proto/" as a whole */
2380 return vim_isfilec(c) && !vim_ispathsep(c);
2381
2382 case CTRL_X_CMDLINE:
2383 case CTRL_X_OMNI:
2384 /* Command line and Omni completion can work with just about any
2385 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002386 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002387
2388 case CTRL_X_WHOLE_LINE:
2389 /* For while line completion a space can be part of the line. */
2390 return vim_isprintc(c);
2391 }
2392 return vim_iswordc(c);
2393}
2394
2395/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002396 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002398 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * the rest of the word to be in -- webb
2400 */
2401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002402ins_compl_add_infercase(
2403 char_u *str,
2404 int len,
2405 int icase,
2406 char_u *fname,
2407 int dir,
2408 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 char_u *p;
2411 int i, c;
2412 int actual_len; /* Take multi-byte characters */
2413 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002414 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002415 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 int has_lower = FALSE;
2417 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002419 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002421 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaara2993e12007-08-12 14:38:46 +00002423 /* Find actual length of completion. */
2424#ifdef FEAT_MBYTE
2425 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002427 p = str;
2428 actual_len = 0;
2429 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002431 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002432 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002435 else
2436#endif
2437 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaara2993e12007-08-12 14:38:46 +00002439 /* Find actual length of original text. */
2440#ifdef FEAT_MBYTE
2441 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002443 p = compl_orig_text;
2444 actual_compl_length = 0;
2445 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002447 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002448 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002451 else
2452#endif
2453 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002455 /* "actual_len" may be smaller than "actual_compl_length" when using
2456 * thesaurus, only use the minimum when comparing. */
2457 min_len = actual_len < actual_compl_length
2458 ? actual_len : actual_compl_length;
2459
Bram Moolenaara2993e12007-08-12 14:38:46 +00002460 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002461 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002462 if (wca != NULL)
2463 {
2464 p = str;
2465 for (i = 0; i < actual_len; ++i)
2466#ifdef FEAT_MBYTE
2467 if (has_mbyte)
2468 wca[i] = mb_ptr2char_adv(&p);
2469 else
2470#endif
2471 wca[i] = *(p++);
2472
2473 /* Rule 1: Were any chars converted to lower? */
2474 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002475 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002476 {
2477#ifdef FEAT_MBYTE
2478 if (has_mbyte)
2479 c = mb_ptr2char_adv(&p);
2480 else
2481#endif
2482 c = *(p++);
2483 if (MB_ISLOWER(c))
2484 {
2485 has_lower = TRUE;
2486 if (MB_ISUPPER(wca[i]))
2487 {
2488 /* Rule 1 is satisfied. */
2489 for (i = actual_compl_length; i < actual_len; ++i)
2490 wca[i] = MB_TOLOWER(wca[i]);
2491 break;
2492 }
2493 }
2494 }
2495
2496 /*
2497 * Rule 2: No lower case, 2nd consecutive letter converted to
2498 * upper case.
2499 */
2500 if (!has_lower)
2501 {
2502 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002503 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002504 {
2505#ifdef FEAT_MBYTE
2506 if (has_mbyte)
2507 c = mb_ptr2char_adv(&p);
2508 else
2509#endif
2510 c = *(p++);
2511 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2512 {
2513 /* Rule 2 is satisfied. */
2514 for (i = actual_compl_length; i < actual_len; ++i)
2515 wca[i] = MB_TOUPPER(wca[i]);
2516 break;
2517 }
2518 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2519 }
2520 }
2521
2522 /* Copy the original case of the part we typed. */
2523 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002524 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002525 {
2526#ifdef FEAT_MBYTE
2527 if (has_mbyte)
2528 c = mb_ptr2char_adv(&p);
2529 else
2530#endif
2531 c = *(p++);
2532 if (MB_ISLOWER(c))
2533 wca[i] = MB_TOLOWER(wca[i]);
2534 else if (MB_ISUPPER(c))
2535 wca[i] = MB_TOUPPER(wca[i]);
2536 }
2537
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002538 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002539 * Generate encoding specific output from wide character array.
2540 * Multi-byte characters can occupy up to five bytes more than
2541 * ASCII characters, and we also need one byte for NUL, so stay
2542 * six bytes away from the edge of IObuff.
2543 */
2544 p = IObuff;
2545 i = 0;
2546 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2547#ifdef FEAT_MBYTE
2548 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002549 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002550 else
2551#endif
2552 *(p++) = wca[i++];
2553 *p = NUL;
2554
2555 vim_free(wca);
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002558 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2559 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002561 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
2564/*
2565 * Add a match to the list of matches.
2566 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002567 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002568 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002571ins_compl_add(
2572 char_u *str,
2573 int len,
2574 int icase,
2575 char_u *fname,
2576 char_u **cptext, /* extra text for popup menu or NULL */
2577 int cdir,
2578 int flags,
2579 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002582 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 ui_breakcheck();
2585 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (len < 0)
2588 len = (int)STRLEN(str);
2589
2590 /*
2591 * If the same match is already present, don't add it.
2592 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002595 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 do
2597 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002598 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002599 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 && match->cp_str[len] == NUL)
2601 return NOTDONE;
2602 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002603 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002606 /* Remove any popup menu before changing the list of matches. */
2607 ins_compl_del_pum();
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 /*
2610 * Allocate a new match structure.
2611 * Copy the values to the new match structure.
2612 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002613 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002615 return FAIL;
2616 match->cp_number = -1;
2617 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002618 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002619 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002624 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2629 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002630 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002631 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002632 && compl_curr_match->cp_fname != NULL
2633 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002635 else if (fname != NULL)
2636 {
2637 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002638 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002641 match->cp_fname = NULL;
2642 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002643
2644 if (cptext != NULL)
2645 {
2646 int i;
2647
2648 for (i = 0; i < CPT_COUNT; ++i)
2649 if (cptext[i] != NULL && *cptext[i] != NUL)
2650 match->cp_text[i] = vim_strsave(cptext[i]);
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /*
2654 * Link the new match structure in the list of matches.
2655 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002656 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002657 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else if (dir == FORWARD)
2659 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002660 match->cp_next = compl_curr_match->cp_next;
2661 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 else /* BACKWARD */
2664 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002665 match->cp_next = compl_curr_match;
2666 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002668 if (match->cp_next)
2669 match->cp_next->cp_prev = match;
2670 if (match->cp_prev)
2671 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002673 compl_first_match = match;
2674 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002676 /*
2677 * Find the longest common string if still doing that.
2678 */
2679 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2680 ins_compl_longest_match(match);
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return OK;
2683}
2684
2685/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002686 * Return TRUE if "str[len]" matches with match->cp_str, considering
2687 * match->cp_icase.
2688 */
2689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002690ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002691{
2692 if (match->cp_icase)
2693 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2694 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2695}
2696
2697/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002698 * Reduce the longest common string for match "match".
2699 */
2700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002701ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002702{
2703 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002704 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002705 int had_match;
2706
2707 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002708 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002709 /* First match, use it as a whole. */
2710 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002711 if (compl_leader != NULL)
2712 {
2713 had_match = (curwin->w_cursor.col > compl_col);
2714 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002715 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002716 ins_redraw(FALSE);
2717
2718 /* When the match isn't there (to avoid matching itself) remove it
2719 * again after redrawing. */
2720 if (!had_match)
2721 ins_compl_delete();
2722 compl_used_match = FALSE;
2723 }
2724 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002725 else
2726 {
2727 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002728 p = compl_leader;
2729 s = match->cp_str;
2730 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002731 {
2732#ifdef FEAT_MBYTE
2733 if (has_mbyte)
2734 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002735 c1 = mb_ptr2char(p);
2736 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002737 }
2738 else
2739#endif
2740 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002741 c1 = *p;
2742 c2 = *s;
2743 }
2744 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2745 : (c1 != c2))
2746 break;
2747#ifdef FEAT_MBYTE
2748 if (has_mbyte)
2749 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002750 MB_PTR_ADV(p);
2751 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002752 }
2753 else
2754#endif
2755 {
2756 ++p;
2757 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002758 }
2759 }
2760
2761 if (*p != NUL)
2762 {
2763 /* Leader was shortened, need to change the inserted text. */
2764 *p = NUL;
2765 had_match = (curwin->w_cursor.col > compl_col);
2766 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002767 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002768 ins_redraw(FALSE);
2769
2770 /* When the match isn't there (to avoid matching itself) remove it
2771 * again after redrawing. */
2772 if (!had_match)
2773 ins_compl_delete();
2774 }
2775
2776 compl_used_match = FALSE;
2777 }
2778}
2779
2780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 * Add an array of matches to the list of matches.
2782 * Frees matches[].
2783 */
2784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002785ins_compl_add_matches(
2786 int num_matches,
2787 char_u **matches,
2788 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 int i;
2791 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002792 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar572cb562005-08-05 21:35:02 +00002794 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002796 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002798 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 FreeWild(num_matches, matches);
2800}
2801
2802/* Make the completion list cyclic.
2803 * Return the number of matches (excluding the original).
2804 */
2805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002806ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002808 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 int count = 0;
2810
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002811 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
2813 /*
2814 * Find the end of the list.
2815 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002816 match = compl_first_match;
2817 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002818 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002820 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ++count;
2822 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002823 match->cp_next = compl_first_match;
2824 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826 return count;
2827}
2828
Bram Moolenaara94bc432006-03-10 21:42:59 +00002829/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002830 * Set variables that store noselect and noinsert behavior from the
2831 * 'completeopt' value.
2832 */
2833 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002834completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002835{
2836 compl_no_insert = FALSE;
2837 compl_no_select = FALSE;
2838 if (strstr((char *)p_cot, "noselect") != NULL)
2839 compl_no_select = TRUE;
2840 if (strstr((char *)p_cot, "noinsert") != NULL)
2841 compl_no_insert = TRUE;
2842}
2843
2844/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002845 * Start completion for the complete() function.
2846 * "startcol" is where the matched text starts (1 is first column).
2847 * "list" is the list of matches.
2848 */
2849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002850set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002851{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002852 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002853 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002854
Bram Moolenaara94bc432006-03-10 21:42:59 +00002855 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002856 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002857 ins_compl_prep(' ');
2858 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002859 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002860
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002861 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002862 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002863 startcol = curwin->w_cursor.col;
2864 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002865 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002866 /* compl_pattern doesn't need to be set */
2867 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2868 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002869 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002870 return;
2871
Bram Moolenaare4214502015-03-05 18:08:43 +01002872 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002873
2874 ins_compl_add_list(list);
2875 compl_matches = ins_compl_make_cyclic();
2876 compl_started = TRUE;
2877 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002878 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002879
2880 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002881 if (compl_no_insert || compl_no_select)
2882 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002883 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002884 if (compl_no_select)
2885 /* Down/Up has no real effect. */
2886 ins_complete(K_UP, FALSE);
2887 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002888 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002889 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002890 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002891
2892 /* Lazily show the popup menu, unless we got interrupted. */
2893 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002894 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002895 out_flush();
2896}
2897
2898
Bram Moolenaar9372a112005-12-06 19:59:18 +00002899/* "compl_match_array" points the currently displayed list of entries in the
2900 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002902static int compl_match_arraysize;
2903
2904/*
2905 * Update the screen and when there is any scrolling remove the popup menu.
2906 */
2907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002908ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002909{
2910 int h;
2911
2912 if (compl_match_array != NULL)
2913 {
2914 h = curwin->w_cline_height;
2915 update_screen(0);
2916 if (h != curwin->w_cline_height)
2917 ins_compl_del_pum();
2918 }
2919}
2920
2921/*
2922 * Remove any popup menu.
2923 */
2924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002926{
2927 if (compl_match_array != NULL)
2928 {
2929 pum_undisplay();
Bram Moolenaar15675582018-02-09 12:29:56 +01002930 vim_clear((void **)&compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002931 }
2932}
2933
2934/*
2935 * Return TRUE if the popup menu should be displayed.
2936 */
2937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002939{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002940 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002941 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002942 return FALSE;
2943
2944 /* The display looks bad on a B&W display. */
2945 if (t_colors < 8
2946#ifdef FEAT_GUI
2947 && !gui.in_use
2948#endif
2949 )
2950 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002951 return TRUE;
2952}
2953
2954/*
2955 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002956 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002957 */
2958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002960{
2961 compl_T *compl;
2962 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002963
2964 /* Don't display the popup menu if there are no matches or there is only
2965 * one (ignoring the original text). */
2966 compl = compl_first_match;
2967 i = 0;
2968 do
2969 {
2970 if (compl == NULL
2971 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2972 break;
2973 compl = compl->cp_next;
2974 } while (compl != compl_first_match);
2975
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002976 if (strstr((char *)p_cot, "menuone") != NULL)
2977 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002978 return (i >= 2);
2979}
2980
2981/*
2982 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002983 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002984 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002986ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002987{
2988 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002989 compl_T *shown_compl = NULL;
2990 int did_find_shown_match = FALSE;
2991 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 int i;
2993 int cur = -1;
2994 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002995 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002997 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002998 return;
2999
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003000#if defined(FEAT_EVAL)
3001 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3002 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3003#endif
3004
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003005 /* Update the screen before drawing the popup menu over it. */
3006 update_screen(0);
3007
3008 if (compl_match_array == NULL)
3009 {
3010 /* Need to build the popup menu list. */
3011 compl_match_arraysize = 0;
3012 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003013 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003014 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003015 do
3016 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003017 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3018 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003019 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003020 ++compl_match_arraysize;
3021 compl = compl->cp_next;
3022 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003023 if (compl_match_arraysize == 0)
3024 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003025 compl_match_array = (pumitem_T *)alloc_clear(
3026 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003027 * compl_match_arraysize));
3028 if (compl_match_array != NULL)
3029 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003030 /* If the current match is the original text don't find the first
3031 * match after it, don't highlight anything. */
3032 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3033 shown_match_ok = TRUE;
3034
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035 i = 0;
3036 compl = compl_first_match;
3037 do
3038 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003039 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3040 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003041 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003042 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003043 if (!shown_match_ok)
3044 {
3045 if (compl == compl_shown_match || did_find_shown_match)
3046 {
3047 /* This item is the shown match or this is the
3048 * first displayed item after the shown match. */
3049 compl_shown_match = compl;
3050 did_find_shown_match = TRUE;
3051 shown_match_ok = TRUE;
3052 }
3053 else
3054 /* Remember this displayed match for when the
3055 * shown match is just below it. */
3056 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003057 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003058 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003059
3060 if (compl->cp_text[CPT_ABBR] != NULL)
3061 compl_match_array[i].pum_text =
3062 compl->cp_text[CPT_ABBR];
3063 else
3064 compl_match_array[i].pum_text = compl->cp_str;
3065 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3066 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3067 if (compl->cp_text[CPT_MENU] != NULL)
3068 compl_match_array[i++].pum_extra =
3069 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003070 else
3071 compl_match_array[i++].pum_extra = compl->cp_fname;
3072 }
3073
3074 if (compl == compl_shown_match)
3075 {
3076 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003077
3078 /* When the original text is the shown match don't set
3079 * compl_shown_match. */
3080 if (compl->cp_flags & ORIGINAL_TEXT)
3081 shown_match_ok = TRUE;
3082
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003083 if (!shown_match_ok && shown_compl != NULL)
3084 {
3085 /* The shown match isn't displayed, set it to the
3086 * previously displayed match. */
3087 compl_shown_match = shown_compl;
3088 shown_match_ok = TRUE;
3089 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090 }
3091 compl = compl->cp_next;
3092 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003093
3094 if (!shown_match_ok) /* no displayed match at all */
3095 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096 }
3097 }
3098 else
3099 {
3100 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003101 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003102 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3103 || compl_match_array[i].pum_text
3104 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003105 {
3106 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003107 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003108 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 }
3110
3111 if (compl_match_array != NULL)
3112 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003113 /* In Replace mode when a $ is displayed at the end of the line only
3114 * part of the screen would be updated. We do need to redraw here. */
3115 dollar_vcol = -1;
3116
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003117 /* Compute the screen column of the start of the completed text.
3118 * Use the cursor to get all wrapping and other settings right. */
3119 col = curwin->w_cursor.col;
3120 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003121 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003122 curwin->w_cursor.col = col;
3123 }
3124}
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#define DICT_FIRST (1) /* use just first element in "dict" */
3127#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130 * Add any identifiers that match the given pattern in the list of dictionary
3131 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 */
3133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003134ins_compl_dictionaries(
3135 char_u *dict_start,
3136 char_u *pat,
3137 int flags, /* DICT_FIRST and/or DICT_EXACT */
3138 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003140 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 char_u *ptr;
3142 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 char_u **files;
3145 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003147 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
Bram Moolenaar0b238792006-03-02 22:49:12 +00003149 if (*dict == NUL)
3150 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003151#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003152 /* When 'dictionary' is empty and spell checking is enabled use
3153 * "spell". */
3154 if (!thesaurus && curwin->w_p_spell)
3155 dict = (char_u *)"spell";
3156 else
3157#endif
3158 return;
3159 }
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003162 if (buf == NULL)
3163 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003164 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /* If 'infercase' is set, don't use 'smartcase' here */
3167 save_p_scs = p_scs;
3168 if (curbuf->b_p_inf)
3169 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003170
3171 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3172 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003173 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003174 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003175 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003176 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003177 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003178
3179 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003180 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003181 len = STRLEN(pat_esc) + 10;
3182 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003183 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003184 {
3185 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003186 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003187 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003188 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003189 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3190 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003191 vim_free(ptr);
3192 }
3193 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003196 if (regmatch.regprog == NULL)
3197 goto theend;
3198 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3201 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003202 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
3204 /* copy one dictionary file name into buf */
3205 if (flags == DICT_EXACT)
3206 {
3207 count = 1;
3208 files = &dict;
3209 }
3210 else
3211 {
3212 /* Expand wildcards in the dictionary name, but do not allow
3213 * backticks (for security, the 'dict' option may have been set in
3214 * a modeline). */
3215 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003216# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003217 if (!thesaurus && STRCMP(buf, "spell") == 0)
3218 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003219 else
3220# endif
3221 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 || expand_wildcards(1, &buf, &count, &files,
3223 EW_FILE|EW_SILENT) != OK)
3224 count = 0;
3225 }
3226
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003227# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003228 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003230 /* Complete from active spelling. Skip "\<" in the pattern, we
3231 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003232 if (pat[0] == '\\' && pat[1] == '<')
3233 ptr = pat + 2;
3234 else
3235 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003236 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003238 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003239# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003240 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003241 {
3242 ins_compl_files(count, files, thesaurus, flags,
3243 &regmatch, buf, &dir);
3244 if (flags != DICT_EXACT)
3245 FreeWild(count, files);
3246 }
3247 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 break;
3249 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003250
3251theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003253 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 vim_free(buf);
3255}
3256
Bram Moolenaar0b238792006-03-02 22:49:12 +00003257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003258ins_compl_files(
3259 int count,
3260 char_u **files,
3261 int thesaurus,
3262 int flags,
3263 regmatch_T *regmatch,
3264 char_u *buf,
3265 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003266{
3267 char_u *ptr;
3268 int i;
3269 FILE *fp;
3270 int add_r;
3271
3272 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3273 {
3274 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3275 if (flags != DICT_EXACT)
3276 {
3277 vim_snprintf((char *)IObuff, IOSIZE,
3278 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003279 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003280 }
3281
3282 if (fp != NULL)
3283 {
3284 /*
3285 * Read dictionary file line by line.
3286 * Check each line for a match.
3287 */
3288 while (!got_int && !compl_interrupted
3289 && !vim_fgets(buf, LSIZE, fp))
3290 {
3291 ptr = buf;
3292 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3293 {
3294 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003295 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003296 ptr = find_line_end(ptr);
3297 else
3298 ptr = find_word_end(ptr);
3299 add_r = ins_compl_add_infercase(regmatch->startp[0],
3300 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003301 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003302 if (thesaurus)
3303 {
3304 char_u *wstart;
3305
3306 /*
3307 * Add the other matches on the line
3308 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003309 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003310 while (!got_int)
3311 {
3312 /* Find start of the next word. Skip white
3313 * space and punctuation. */
3314 ptr = find_word_start(ptr);
3315 if (*ptr == NUL || *ptr == NL)
3316 break;
3317 wstart = ptr;
3318
Bram Moolenaara2993e12007-08-12 14:38:46 +00003319 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003320#ifdef FEAT_MBYTE
3321 if (has_mbyte)
3322 /* Japanese words may have characters in
3323 * different classes, only separate words
3324 * with single-byte non-word characters. */
3325 while (*ptr != NUL)
3326 {
3327 int l = (*mb_ptr2len)(ptr);
3328
3329 if (l < 2 && !vim_iswordc(*ptr))
3330 break;
3331 ptr += l;
3332 }
3333 else
3334#endif
3335 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003336
3337 /* Add the word. Skip the regexp match. */
3338 if (wstart != regmatch->startp[0])
3339 add_r = ins_compl_add_infercase(wstart,
3340 (int)(ptr - wstart),
3341 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003342 }
3343 }
3344 if (add_r == OK)
3345 /* if dir was BACKWARD then honor it just once */
3346 *dir = FORWARD;
3347 else if (add_r == FAIL)
3348 break;
3349 /* avoid expensive call to vim_regexec() when at end
3350 * of line */
3351 if (*ptr == '\n' || got_int)
3352 break;
3353 }
3354 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003355 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003356 }
3357 fclose(fp);
3358 }
3359 }
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Find the start of the next word.
3364 * Returns a pointer to the first char of the word. Also stops at a NUL.
3365 */
3366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369#ifdef FEAT_MBYTE
3370 if (has_mbyte)
3371 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003372 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
3374#endif
3375 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3376 ++ptr;
3377 return ptr;
3378}
3379
3380/*
3381 * Find the end of the word. Assumes it starts inside a word.
3382 * Returns a pointer to just after the word.
3383 */
3384 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003385find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
3387#ifdef FEAT_MBYTE
3388 int start_class;
3389
3390 if (has_mbyte)
3391 {
3392 start_class = mb_get_class(ptr);
3393 if (start_class > 1)
3394 while (*ptr != NUL)
3395 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003396 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (mb_get_class(ptr) != start_class)
3398 break;
3399 }
3400 }
3401 else
3402#endif
3403 while (vim_iswordc(*ptr))
3404 ++ptr;
3405 return ptr;
3406}
3407
3408/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003409 * Find the end of the line, omitting CR and NL at the end.
3410 * Returns a pointer to just after the line.
3411 */
3412 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003413find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003414{
3415 char_u *s;
3416
3417 s = ptr + STRLEN(ptr);
3418 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3419 --s;
3420 return s;
3421}
3422
3423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Free the list of completions
3425 */
3426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003429 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003430 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar15675582018-02-09 12:29:56 +01003432 vim_clear((void **)&compl_pattern);
3433 vim_clear((void **)&compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003435 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003437
3438 ins_compl_del_pum();
3439 pum_clear();
3440
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003441 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 do
3443 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003444 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003445 compl_curr_match = compl_curr_match->cp_next;
3446 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003448 if (match->cp_flags & FREE_FNAME)
3449 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003450 for (i = 0; i < CPT_COUNT; ++i)
3451 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003453 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3454 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003455 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003456 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457}
3458
3459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003462 compl_cont_status = 0;
3463 compl_started = FALSE;
3464 compl_matches = 0;
Bram Moolenaar15675582018-02-09 12:29:56 +01003465 vim_clear((void **)&compl_pattern);
3466 vim_clear((void **)&compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 edit_submode_extra = NULL;
Bram Moolenaar15675582018-02-09 12:29:56 +01003468 vim_clear((void **)&compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003469 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003470 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003471 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472}
3473
3474/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003475 * Return TRUE when Insert completion is active.
3476 */
3477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003478ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003479{
3480 return compl_started;
3481}
3482
3483/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003484 * Delete one character before the cursor and show the subset of the matches
3485 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003486 * Returns the character to be used, NUL if the work is done and another char
3487 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003488 */
3489 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003490ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003491{
3492 char_u *line;
3493 char_u *p;
3494
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003495 line = ml_get_curline();
3496 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003497 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003498
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003499 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003500 * allow the word to be deleted, we won't match everything.
3501 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003502 if ((int)(p - line) - (int)compl_col < 0
3503 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003504 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3505 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3506 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003507 return K_BS;
3508
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003509 /* Deleted more than what was used to find matches or didn't finish
3510 * finding all matches: need to look for matches all over again. */
3511 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003512 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003513 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003514
Bram Moolenaara6557602006-02-04 22:43:20 +00003515 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003516 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003517 if (compl_leader != NULL)
3518 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003519 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003520 if (compl_shown_match != NULL)
3521 /* Make sure current match is not a hidden item. */
3522 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003523 return NUL;
3524 }
3525 return K_BS;
3526}
Bram Moolenaara6557602006-02-04 22:43:20 +00003527
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003528/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003529 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3530 * be called.
3531 */
3532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003533ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003534{
3535 /* Return TRUE if we didn't complete finding matches or when the
3536 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3537 return compl_was_interrupted
3538 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3539 && compl_opt_refresh_always);
3540}
3541
3542/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003543 * Called after changing "compl_leader".
3544 * Show the popup menu with a different set of matches.
3545 * May also search for matches again if the previous search was interrupted.
3546 */
3547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003549{
3550 ins_compl_del_pum();
3551 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003552 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003553 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003554
3555 if (compl_started)
3556 ins_compl_set_original_text(compl_leader);
3557 else
3558 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003559#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003560 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003561#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003562 /*
3563 * Matches were cleared, need to search for them now. First display
3564 * the changed text before the cursor. Set "compl_restarting" to
3565 * avoid that the first match is inserted.
3566 */
3567 update_screen(0);
3568#ifdef FEAT_GUI
3569 if (gui.in_use)
3570 {
3571 /* Show the cursor after the match, not after the redrawn text. */
3572 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003573 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003574 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003575#endif
3576 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003577 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003578 compl_cont_status = 0;
3579 compl_restarting = FALSE;
3580 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003581
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003582 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003583
3584 /* Show the popup menu with a different set of matches. */
3585 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003586
3587 /* Don't let Enter select the original text when there is no popup menu. */
3588 if (compl_match_array == NULL)
3589 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003590}
3591
3592/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003593 * Return the length of the completion, from the completion start column to
3594 * the cursor column. Making sure it never goes below zero.
3595 */
3596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003598{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003599 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003600
3601 if (off < 0)
3602 return 0;
3603 return off;
3604}
3605
3606/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003607 * Append one character to the match leader. May reduce the number of
3608 * matches.
3609 */
3610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003611ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003612{
3613#ifdef FEAT_MBYTE
3614 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003615#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003616
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003617 if (stop_arrow() == FAIL)
3618 return;
3619#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003620 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3621 {
3622 char_u buf[MB_MAXBYTES + 1];
3623
3624 (*mb_char2bytes)(c, buf);
3625 buf[cc] = NUL;
3626 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003627 if (compl_opt_refresh_always)
3628 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003629 }
3630 else
3631#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003632 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003633 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003634 if (compl_opt_refresh_always)
3635 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003636 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003637
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003638 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003639 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003640 ins_compl_restart();
3641
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003642 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003643 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003644 * break redo. */
3645 if (!compl_opt_refresh_always)
3646 {
3647 vim_free(compl_leader);
3648 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003649 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003650 if (compl_leader != NULL)
3651 ins_compl_new_leader();
3652 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003653}
3654
3655/*
3656 * Setup for finding completions again without leaving CTRL-X mode. Used when
3657 * BS or a key was typed while still searching for matches.
3658 */
3659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003660ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003661{
3662 ins_compl_free();
3663 compl_started = FALSE;
3664 compl_matches = 0;
3665 compl_cont_status = 0;
3666 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003667}
3668
3669/*
3670 * Set the first match, the original text.
3671 */
3672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003673ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003674{
3675 char_u *p;
3676
3677 /* Replace the original text entry. */
3678 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3679 {
3680 p = vim_strsave(str);
3681 if (p != NULL)
3682 {
3683 vim_free(compl_first_match->cp_str);
3684 compl_first_match->cp_str = p;
3685 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003686 }
3687}
3688
3689/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003690 * Append one character to the match leader. May reduce the number of
3691 * matches.
3692 */
3693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003694ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003695{
3696 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003697 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003698 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003699 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003700
3701 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003702 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003703 {
3704 /* When still at the original match use the first entry that matches
3705 * the leader. */
3706 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3707 {
3708 p = NULL;
3709 for (cp = compl_shown_match->cp_next; cp != NULL
3710 && cp != compl_first_match; cp = cp->cp_next)
3711 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003712 if (compl_leader == NULL
3713 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003714 (int)STRLEN(compl_leader)))
3715 {
3716 p = cp->cp_str;
3717 break;
3718 }
3719 }
3720 if (p == NULL || (int)STRLEN(p) <= len)
3721 return;
3722 }
3723 else
3724 return;
3725 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003726 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003727 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003728 ins_compl_addleader(c);
3729}
3730
3731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003733 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003734 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003741 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 /* Forget any previous 'special' messages if this is actually
3744 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3745 */
3746 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3747 edit_submode_extra = NULL;
3748
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003749 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003750 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3751 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003752 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003754 /* Set "compl_get_longest" when finding the first matches. */
3755 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003756 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003757 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003758 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003759 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003760
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003761 }
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3764 {
3765 /*
3766 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3767 * it will be yet. Now we decide.
3768 */
3769 switch (c)
3770 {
3771 case Ctrl_E:
3772 case Ctrl_Y:
3773 ctrl_x_mode = CTRL_X_SCROLL;
3774 if (!(State & REPLACE_FLAG))
3775 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3776 else
3777 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3778 edit_submode_pre = NULL;
3779 showmode();
3780 break;
3781 case Ctrl_L:
3782 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3783 break;
3784 case Ctrl_F:
3785 ctrl_x_mode = CTRL_X_FILES;
3786 break;
3787 case Ctrl_K:
3788 ctrl_x_mode = CTRL_X_DICTIONARY;
3789 break;
3790 case Ctrl_R:
3791 /* Simply allow ^R to happen without affecting ^X mode */
3792 break;
3793 case Ctrl_T:
3794 ctrl_x_mode = CTRL_X_THESAURUS;
3795 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003796#ifdef FEAT_COMPL_FUNC
3797 case Ctrl_U:
3798 ctrl_x_mode = CTRL_X_FUNCTION;
3799 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003800 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003801 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003802 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003803#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003804 case 's':
3805 case Ctrl_S:
3806 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003807#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003808 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003809 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003810 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003811#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 case Ctrl_RSB:
3814 ctrl_x_mode = CTRL_X_TAGS;
3815 break;
3816#ifdef FEAT_FIND_ID
3817 case Ctrl_I:
3818 case K_S_TAB:
3819 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3820 break;
3821 case Ctrl_D:
3822 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3823 break;
3824#endif
3825 case Ctrl_V:
3826 case Ctrl_Q:
3827 ctrl_x_mode = CTRL_X_CMDLINE;
3828 break;
3829 case Ctrl_P:
3830 case Ctrl_N:
3831 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3832 * just started ^X mode, or there were enough ^X's to cancel
3833 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3834 * do normal expansion when interrupting a different mode (say
3835 * ^X^F^X^P or ^P^X^X^P, see below)
3836 * nothing changes if interrupting mode 0, (eg, the flag
3837 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003838 if (!(compl_cont_status & CONT_INTRPT))
3839 compl_cont_status |= CONT_LOCAL;
3840 else if (compl_cont_mode != 0)
3841 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 /* FALLTHROUGH */
3843 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 /* If we have typed at least 2 ^X's... for modes != 0, we set
3845 * compl_cont_status = 0 (eg, as if we had just started ^X
3846 * mode).
3847 * For mode 0, we set "compl_cont_mode" to an impossible
3848 * value, in both cases ^X^X can be used to restart the same
3849 * mode (avoiding ADDING mode).
3850 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3851 * 'complete' and local ^P expansions respectively.
3852 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3853 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (c == Ctrl_X)
3855 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 if (compl_cont_mode != 0)
3857 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003861 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 edit_submode = NULL;
3863 showmode();
3864 break;
3865 }
3866 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003867 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 /* We're already in CTRL-X mode, do we stay in it? */
3870 if (!vim_is_ctrl_x_key(c))
3871 {
3872 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003873 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 else
3875 ctrl_x_mode = CTRL_X_FINISHED;
3876 edit_submode = NULL;
3877 }
3878 showmode();
3879 }
3880
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 /* Show error message from attempted keyword completion (probably
3884 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003887 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3888 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 || ctrl_x_mode == CTRL_X_FINISHED)
3890 {
3891 /* Get here when we have finished typing a sequence of ^N and
3892 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003894 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003897 * If any of the original typed text has been changed, eg when
3898 * ignorecase is set, we must add back-spaces to the redo
3899 * buffer. We add as few as necessary to delete just the part
3900 * of the original text that has changed.
3901 * When using the longest match, edited the match or used
3902 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003904 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3905 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003906 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003907 ptr = NULL;
3908 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910
3911#ifdef FEAT_CINDENT
3912 want_cindent = (can_cindent && cindent_on());
3913#endif
3914 /*
3915 * When completing whole lines: fix indent for 'cindent'.
3916 * Otherwise, break line if it's too long.
3917 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920#ifdef FEAT_CINDENT
3921 /* re-indent the current line */
3922 if (want_cindent)
3923 {
3924 do_c_expr_indent();
3925 want_cindent = FALSE; /* don't do it again */
3926 }
3927#endif
3928 }
3929 else
3930 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003931 int prev_col = curwin->w_cursor.col;
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003934 if (prev_col > 0)
3935 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003936 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003937 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003939 if (prev_col > 0
3940 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3941 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003944 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003945 * the selection without inserting anything. When
3946 * compl_enter_selects is set the Enter key does the same. */
3947 if ((c == Ctrl_Y || (compl_enter_selects
3948 && (c == CAR || c == K_KENTER || c == NL)))
3949 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003950 retval = TRUE;
3951
Bram Moolenaar47247282016-08-02 22:36:02 +02003952 /* CTRL-E means completion is Ended, go back to the typed text.
3953 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003954 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003955 {
3956 ins_compl_delete();
3957 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003958 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003959 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003960 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003961 retval = TRUE;
3962 }
3963
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003964 auto_format(FALSE, TRUE);
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003967 compl_started = FALSE;
3968 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003969 if (!shortmess(SHM_COMPLETIONMENU))
3970 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003971 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003972 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (edit_submode != NULL)
3974 {
3975 edit_submode = NULL;
3976 showmode();
3977 }
3978
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003979#ifdef FEAT_CMDWIN
3980 if (c == Ctrl_C && cmdwin_type != 0)
3981 /* Avoid the popup menu remains displayed when leaving the
3982 * command line window. */
3983 update_screen(0);
3984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#ifdef FEAT_CINDENT
3986 /*
3987 * Indent now if a key was typed that is in 'cinkeys'.
3988 */
3989 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3990 do_c_expr_indent();
3991#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003992#ifdef FEAT_AUTOCMD
3993 /* Trigger the CompleteDone event to give scripts a chance to act
3994 * upon the completion. */
3995 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003999#ifdef FEAT_AUTOCMD
4000 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4001 /* Trigger the CompleteDone event to give scripts a chance to act
4002 * upon the (possibly failed) completion. */
4003 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
4004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
4006 /* reset continue_* if we left expansion-mode, if we stay they'll be
4007 * (re)set properly in ins_complete() */
4008 if (!vim_is_ctrl_x_key(c))
4009 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010 compl_cont_status = 0;
4011 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004013
4014 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015}
4016
4017/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004018 * Fix the redo buffer for the completion leader replacing some of the typed
4019 * text. This inserts backspaces and appends the changed text.
4020 * "ptr" is the known leader text or NUL.
4021 */
4022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004023ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004024{
4025 int len;
4026 char_u *p;
4027 char_u *ptr = ptr_arg;
4028
4029 if (ptr == NULL)
4030 {
4031 if (compl_leader != NULL)
4032 ptr = compl_leader;
4033 else
4034 return; /* nothing to do */
4035 }
4036 if (compl_orig_text != NULL)
4037 {
4038 p = compl_orig_text;
4039 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4040 ;
4041#ifdef FEAT_MBYTE
4042 if (len > 0)
4043 len -= (*mb_head_off)(p, p + len);
4044#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004045 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004046 AppendCharToRedobuff(K_BS);
4047 }
4048 else
4049 len = 0;
4050 if (ptr != NULL)
4051 AppendToRedobuffLit(ptr + len, -1);
4052}
4053
4054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4056 * (depending on flag) starting from buf and looking for a non-scanned
4057 * buffer (other than curbuf). curbuf is special, if it is called with
4058 * buf=curbuf then it has to be the first call for a given flag/expansion.
4059 *
4060 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4061 */
4062 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004063ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 if (flag == 'w') /* just windows */
4068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (buf == curbuf) /* first call for this flag/expansion */
4070 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004071 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 && wp->w_buffer->b_scanned)
4073 ;
4074 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 else
4077 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4078 * (unlisted buffers)
4079 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004080 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 && ((flag == 'U'
4082 ? buf->b_p_bl
4083 : (!buf->b_p_bl
4084 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004085 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 ;
4087 return buf;
4088}
4089
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004090#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004091/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004092 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004093 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004094 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004096expand_by_function(
4097 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4098 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004099{
Bram Moolenaar82139082011-09-14 16:52:09 +02004100 list_T *matchlist = NULL;
4101 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004102 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004103 char_u *funcname;
4104 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004105 win_T *curwin_save;
4106 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004107 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004108
Bram Moolenaare344bea2005-09-01 20:46:49 +00004109 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4110 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004111 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004112
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004113 /* Call 'completefunc' to obtain the list of matches. */
4114 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004115 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004116
Bram Moolenaare344bea2005-09-01 20:46:49 +00004117 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004118 curwin_save = curwin;
4119 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004120
4121 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004122 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004123 {
4124 switch (rettv.v_type)
4125 {
4126 case VAR_LIST:
4127 matchlist = rettv.vval.v_list;
4128 break;
4129 case VAR_DICT:
4130 matchdict = rettv.vval.v_dict;
4131 break;
4132 default:
4133 /* TODO: Give error message? */
4134 clear_tv(&rettv);
4135 break;
4136 }
4137 }
4138
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004139 if (curwin_save != curwin || curbuf_save != curbuf)
4140 {
4141 EMSG(_(e_complwin));
4142 goto theend;
4143 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004144 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004145 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004146 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004147 {
4148 EMSG(_(e_compldel));
4149 goto theend;
4150 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004151
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004152 if (matchlist != NULL)
4153 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004154 else if (matchdict != NULL)
4155 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004156
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004157theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004158 if (matchdict != NULL)
4159 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004160 if (matchlist != NULL)
4161 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004162}
4163#endif /* FEAT_COMPL_FUNC */
4164
Bram Moolenaar39f05632006-03-19 22:15:26 +00004165#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004166/*
4167 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004168 */
4169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004171{
4172 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004173 int dir = compl_direction;
4174
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004175 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004176 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004177 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004178 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4179 /* if dir was BACKWARD then honor it just once */
4180 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004181 else if (did_emsg)
4182 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004183 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004184}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004185
4186/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004187 * Add completions from a dict.
4188 */
4189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004190ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004191{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004192 dictitem_T *di_refresh;
4193 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004194
4195 /* Check for optional "refresh" item. */
4196 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004197 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4198 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004199 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004200 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004201
4202 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4203 compl_opt_refresh_always = TRUE;
4204 }
4205
4206 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004207 di_words = dict_find(dict, (char_u *)"words", 5);
4208 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4209 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004210}
4211
4212/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004213 * Add a match to the list of matches from a typeval_T.
4214 * If the given string is already in the list of completions, then return
4215 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4216 * maybe because alloc() returns NULL, then FAIL is returned.
4217 */
4218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004219ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004220{
4221 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004222 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004223 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004224 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004225 char_u *(cptext[CPT_COUNT]);
4226
4227 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4228 {
4229 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4230 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4231 (char_u *)"abbr", FALSE);
4232 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4233 (char_u *)"menu", FALSE);
4234 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4235 (char_u *)"kind", FALSE);
4236 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4237 (char_u *)"info", FALSE);
4238 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4239 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004240 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004241 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004242 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4243 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004244 }
4245 else
4246 {
4247 word = get_tv_string_chk(tv);
4248 vim_memset(cptext, 0, sizeof(cptext));
4249 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004250 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004251 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004252 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004253}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004254#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256/*
4257 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004258 * The search starts at position "ini" in curbuf and in the direction
4259 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004260 * When "compl_started" is FALSE start at that position, otherwise continue
4261 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 * This may return before finding all the matches.
4263 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 */
4265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004266ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
4268 static pos_T first_match_pos;
4269 static pos_T last_match_pos;
4270 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 static int found_all = FALSE; /* Found all matches of a
4272 certain type. */
4273 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar572cb562005-08-05 21:35:02 +00004275 pos_T *pos;
4276 char_u **matches;
4277 int save_p_scs;
4278 int save_p_ws;
4279 int save_p_ic;
4280 int i;
4281 int num_matches;
4282 int len;
4283 int found_new_match;
4284 int type = ctrl_x_mode;
4285 char_u *ptr;
4286 char_u *dict = NULL;
4287 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004288 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004292 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 ins_buf->b_scanned = 0;
4294 found_all = FALSE;
4295 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004297 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 last_match_pos = first_match_pos = *ini;
4299 }
4300
Bram Moolenaar4475b622017-05-01 20:46:52 +02004301 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004302 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4304 for (;;)
4305 {
4306 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004307 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004309 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * or if found_all says this entry is done. For ^X^L only use the
4311 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004312 if ((ctrl_x_mode == CTRL_X_NORMAL
4313 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 found_all = FALSE;
4317 while (*e_cpt == ',' || *e_cpt == ' ')
4318 e_cpt++;
4319 if (*e_cpt == '.' && !curbuf->b_scanned)
4320 {
4321 ins_buf = curbuf;
4322 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004323 /* Move the cursor back one character so that ^N can match the
4324 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004325 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004326 {
4327 /* Move the cursor to after the last character in the
4328 * buffer, so that word at start of buffer is found
4329 * correctly. */
4330 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4331 first_match_pos.col =
4332 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 last_match_pos = first_match_pos;
4335 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004336
4337 /* Remember the first match so that the loop stops when we
4338 * wrap and come back there a second time. */
4339 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4342 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4343 {
4344 /* Scan a buffer, but not the current one. */
4345 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4346 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 first_match_pos.col = last_match_pos.col = 0;
4349 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4350 last_match_pos.lnum = 0;
4351 type = 0;
4352 }
4353 else /* unloaded buffer, scan like dictionary */
4354 {
4355 found_all = TRUE;
4356 if (ins_buf->b_fname == NULL)
4357 continue;
4358 type = CTRL_X_DICTIONARY;
4359 dict = ins_buf->b_fname;
4360 dict_f = DICT_EXACT;
4361 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004362 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 ins_buf->b_fname == NULL
4364 ? buf_spname(ins_buf)
4365 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004366 ? ins_buf->b_fname
4367 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004368 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 else if (*e_cpt == NUL)
4371 break;
4372 else
4373 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004374 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 type = -1;
4376 else if (*e_cpt == 'k' || *e_cpt == 's')
4377 {
4378 if (*e_cpt == 'k')
4379 type = CTRL_X_DICTIONARY;
4380 else
4381 type = CTRL_X_THESAURUS;
4382 if (*++e_cpt != ',' && *e_cpt != NUL)
4383 {
4384 dict = e_cpt;
4385 dict_f = DICT_FIRST;
4386 }
4387 }
4388#ifdef FEAT_FIND_ID
4389 else if (*e_cpt == 'i')
4390 type = CTRL_X_PATH_PATTERNS;
4391 else if (*e_cpt == 'd')
4392 type = CTRL_X_PATH_DEFINES;
4393#endif
4394 else if (*e_cpt == ']' || *e_cpt == 't')
4395 {
4396 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004397 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004398 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 else
4401 type = -1;
4402
4403 /* in any case e_cpt is advanced to the next entry */
4404 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4405
4406 found_all = TRUE;
4407 if (type == -1)
4408 continue;
4409 }
4410 }
4411
Bram Moolenaar4475b622017-05-01 20:46:52 +02004412 /* If complete() was called then compl_pattern has been reset. The
4413 * following won't work then, bail out. */
4414 if (compl_pattern == NULL)
4415 break;
4416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 switch (type)
4418 {
4419 case -1:
4420 break;
4421#ifdef FEAT_FIND_ID
4422 case CTRL_X_PATH_PATTERNS:
4423 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004424 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4429 (linenr_T)1, (linenr_T)MAXLNUM);
4430 break;
4431#endif
4432
4433 case CTRL_X_DICTIONARY:
4434 case CTRL_X_THESAURUS:
4435 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004436 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 : (type == CTRL_X_THESAURUS
4438 ? (*curbuf->b_p_tsr == NUL
4439 ? p_tsr
4440 : curbuf->b_p_tsr)
4441 : (*curbuf->b_p_dict == NUL
4442 ? p_dict
4443 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004444 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004445 dict != NULL ? dict_f
4446 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 dict = NULL;
4448 break;
4449
4450 case CTRL_X_TAGS:
4451 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4452 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004455 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 * of matches is found when compl_pattern is empty */
4457 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004458 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4459 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4461 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004462 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 p_ic = save_p_ic;
4465 break;
4466
4467 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4470 {
4471
4472 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004473 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004474 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 break;
4477
4478 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 if (expand_cmdline(&compl_xp, compl_pattern,
4480 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004482 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 break;
4484
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004485#ifdef FEAT_COMPL_FUNC
4486 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004487 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004488 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004489 break;
4490#endif
4491
Bram Moolenaar488c6512005-08-11 20:09:58 +00004492 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004493#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004494 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004495 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004496 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004497 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004498#endif
4499 break;
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 default: /* normal ^P/^N and ^X^L */
4502 /*
4503 * If 'infercase' is set, don't use 'smartcase' here
4504 */
4505 save_p_scs = p_scs;
4506 if (ins_buf->b_p_inf)
4507 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508
Bram Moolenaar361aa502014-01-23 22:45:58 +01004509 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 * end but never from the middle, thus setting nowrapscan in this
4511 * buffers is a good idea, on the other hand, we always set
4512 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4513 save_p_ws = p_ws;
4514 if (ins_buf != curbuf)
4515 p_ws = FALSE;
4516 else if (*e_cpt == '.')
4517 p_ws = TRUE;
4518 for (;;)
4519 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004520 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004522 ++msg_silent; /* Don't want messages for wrapscan. */
4523
Bram Moolenaare4214502015-03-05 18:08:43 +01004524 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4525 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004526 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004527 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004530 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004532 found_new_match = searchit(NULL, ins_buf, pos,
4533 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004535 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004536 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004537 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004539 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 first_match_pos = *pos;
4542 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004543 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004546 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 found_new_match = FAIL;
4548 if (found_new_match == FAIL)
4549 {
4550 if (ins_buf == curbuf)
4551 found_all = TRUE;
4552 break;
4553 }
4554
4555 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 && ini->lnum == pos->lnum
4558 && ini->col == pos->col)
4559 continue;
4560 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004561 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4566 continue;
4567 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4568 if (!p_paste)
4569 ptr = skipwhite(ptr);
4570 }
4571 len = (int)STRLEN(ptr);
4572 }
4573 else
4574 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575 char_u *tmp_ptr = ptr;
4576
4577 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 /* Skip if already inside a word. */
4581 if (vim_iswordp(tmp_ptr))
4582 continue;
4583 /* Find start of next word. */
4584 tmp_ptr = find_word_start(tmp_ptr);
4585 }
4586 /* Find end of this word. */
4587 tmp_ptr = find_word_end(tmp_ptr);
4588 len = (int)(tmp_ptr - ptr);
4589
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 if ((compl_cont_status & CONT_ADDING)
4591 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
4593 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4594 {
4595 /* Try next line, if any. the new word will be
4596 * "join" as if the normal command "J" was used.
4597 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 * works -- Acevedo */
4600 STRNCPY(IObuff, ptr, len);
4601 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4602 tmp_ptr = ptr = skipwhite(ptr);
4603 /* Find start of next word. */
4604 tmp_ptr = find_word_start(tmp_ptr);
4605 /* Find end of next word. */
4606 tmp_ptr = find_word_end(tmp_ptr);
4607 if (tmp_ptr > ptr)
4608 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004609 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004611 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 IObuff[len++] = ' ';
4613 /* IObuf =~ "\k.* ", thus len >= 2 */
4614 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004615 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 || (vim_strchr(p_cpo, CPO_JOINSP)
4617 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004618 && (IObuff[len - 2] == '?'
4619 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 IObuff[len++] = ' ';
4621 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004622 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (tmp_ptr - ptr >= IOSIZE - len)
4624 tmp_ptr = ptr + IOSIZE - len - 1;
4625 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4626 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004627 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 IObuff[len] = NUL;
4630 ptr = IObuff;
4631 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 continue;
4634 }
4635 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004636 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004637 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004638 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 found_new_match = OK;
4641 break;
4642 }
4643 }
4644 p_scs = save_p_scs;
4645 p_ws = save_p_ws;
4646 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004647
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004649 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004650 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 found_new_match = OK;
4652
4653 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004654 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4655 * match */
4656 if ((ctrl_x_mode != CTRL_X_NORMAL
4657 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004659 {
4660 if (got_int)
4661 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004662 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004663 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004664 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004665
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004666 if ((ctrl_x_mode != CTRL_X_NORMAL
4667 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004668 || compl_interrupted)
4669 break;
4670 compl_started = TRUE;
4671 }
4672 else
4673 {
4674 /* Mark a buffer scanned when it has been scanned completely */
4675 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4676 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004678 compl_started = FALSE;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004683 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 && *e_cpt == NUL) /* Got to end of 'complete' */
4685 found_new_match = FAIL;
4686
4687 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004688 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4689 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 i = ins_compl_make_cyclic();
4691
Bram Moolenaar4475b622017-05-01 20:46:52 +02004692 if (compl_old_match != NULL)
4693 {
4694 /* If several matches were added (FORWARD) or the search failed and has
4695 * just been made cyclic then we have to move compl_curr_match to the
4696 * next or previous entry (if any) -- Acevedo */
4697 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4698 : compl_old_match->cp_prev;
4699 if (compl_curr_match == NULL)
4700 compl_curr_match = compl_old_match;
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return i;
4703}
4704
4705/* Delete the old text being completed. */
4706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004709 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 /*
4712 * In insert mode: Delete the typed part.
4713 * In replace mode: Put the old characters back, if any.
4714 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004715 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4716 if ((int)curwin->w_cursor.col > col)
4717 {
4718 if (stop_arrow() == FAIL)
4719 return;
4720 backspace_until_column(col);
4721 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004722
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004723 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4724 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004726 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004727 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728}
4729
Bram Moolenaar472e8592016-10-15 17:06:47 +02004730/*
4731 * Insert the new text being completed.
4732 * "in_compl_func" is TRUE when called from complete_check().
4733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004735ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004737 dict_T *dict;
4738
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004739 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004740 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4741 compl_used_match = FALSE;
4742 else
4743 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004744
4745 /* Set completed item. */
4746 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004747 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004748 if (dict != NULL)
4749 {
4750 dict_add_nr_str(dict, "word", 0L,
4751 EMPTY_IF_NULL(compl_shown_match->cp_str));
4752 dict_add_nr_str(dict, "abbr", 0L,
4753 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4754 dict_add_nr_str(dict, "menu", 0L,
4755 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4756 dict_add_nr_str(dict, "kind", 0L,
4757 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4758 dict_add_nr_str(dict, "info", 0L,
4759 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4760 }
4761 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004762 if (!in_compl_func)
4763 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764}
4765
4766/*
4767 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004768 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4769 * get more completions. If it is FALSE, then we just do nothing when there
4770 * are no more completions in a given direction. The latter case is used when
4771 * we are still in the middle of finding completions, to allow browsing
4772 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * Return the total number of matches, or -1 if still unknown -- webb.
4774 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4776 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 *
4778 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004779 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4780 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 */
4782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004783ins_compl_next(
4784 int allow_get_expansion,
4785 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004786 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004787 int insert_match, /* Insert the newly selected match */
4788 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004791 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004792 compl_T *found_compl = NULL;
4793 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004794 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004795 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004797 /* When user complete function return -1 for findstart which is next
4798 * time of 'always', compl_shown_match become NULL. */
4799 if (compl_shown_match == NULL)
4800 return -1;
4801
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004802 if (compl_leader != NULL
4803 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004805 /* Set "compl_shown_match" to the actually shown match, it may differ
4806 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004807 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004808 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004809 && compl_shown_match->cp_next != NULL
4810 && compl_shown_match->cp_next != compl_first_match)
4811 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004812
4813 /* If we didn't find it searching forward, and compl_shows_dir is
4814 * backward, find the last match. */
4815 if (compl_shows_dir == BACKWARD
4816 && !ins_compl_equal(compl_shown_match,
4817 compl_leader, (int)STRLEN(compl_leader))
4818 && (compl_shown_match->cp_next == NULL
4819 || compl_shown_match->cp_next == compl_first_match))
4820 {
4821 while (!ins_compl_equal(compl_shown_match,
4822 compl_leader, (int)STRLEN(compl_leader))
4823 && compl_shown_match->cp_prev != NULL
4824 && compl_shown_match->cp_prev != compl_first_match)
4825 compl_shown_match = compl_shown_match->cp_prev;
4826 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004827 }
4828
4829 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004830 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 /* Delete old text to be replaced */
4832 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004833
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004834 /* When finding the longest common text we stick at the original text,
4835 * don't let CTRL-N or CTRL-P move to the first match. */
4836 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4837
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004838 /* When restarting the search don't insert the first match either. */
4839 if (compl_restarting)
4840 {
4841 advance = FALSE;
4842 compl_restarting = FALSE;
4843 }
4844
Bram Moolenaare3226be2005-12-18 22:10:00 +00004845 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4846 * around. */
4847 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004849 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004851 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004852 found_end = (compl_first_match != NULL
4853 && (compl_shown_match->cp_next == compl_first_match
4854 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004855 }
4856 else if (compl_shows_dir == BACKWARD
4857 && compl_shown_match->cp_prev != NULL)
4858 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004859 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004860 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004861 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004864 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004865 if (!allow_get_expansion)
4866 {
4867 if (advance)
4868 {
4869 if (compl_shows_dir == BACKWARD)
4870 compl_pending -= todo + 1;
4871 else
4872 compl_pending += todo + 1;
4873 }
4874 return -1;
4875 }
4876
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004877 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004878 {
4879 if (compl_shows_dir == BACKWARD)
4880 --compl_pending;
4881 else
4882 ++compl_pending;
4883 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004884
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004885 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004886 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004887
4888 /* handle any pending completions */
4889 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004890 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004891 {
4892 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4893 {
4894 compl_shown_match = compl_shown_match->cp_next;
4895 --compl_pending;
4896 }
4897 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4898 {
4899 compl_shown_match = compl_shown_match->cp_prev;
4900 ++compl_pending;
4901 }
4902 else
4903 break;
4904 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004905 found_end = FALSE;
4906 }
4907 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4908 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004909 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004910 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004911 ++todo;
4912 else
4913 /* Remember a matching item. */
4914 found_compl = compl_shown_match;
4915
4916 /* Stop at the end of the list when we found a usable match. */
4917 if (found_end)
4918 {
4919 if (found_compl != NULL)
4920 {
4921 compl_shown_match = found_compl;
4922 break;
4923 }
4924 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004928 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004929 if (compl_no_insert && !started)
4930 {
4931 ins_bytes(compl_orig_text + ins_compl_len());
4932 compl_used_match = FALSE;
4933 }
4934 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004935 {
4936 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004937 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004938 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004939 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004940 }
4941 else
4942 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 if (!allow_get_expansion)
4945 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004946 /* may undisplay the popup menu first */
4947 ins_compl_upd_pum();
4948
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004949 /* redraw to show the user what was inserted */
4950 update_screen(0);
4951
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004952 /* display the updated popup menu */
4953 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004954#ifdef FEAT_GUI
4955 if (gui.in_use)
4956 {
4957 /* Show the cursor after the match, not after the redrawn text. */
4958 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004959 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00004960 }
4961#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 /* Delete old text to be replaced, since we're still searching and
4964 * don't want to match ourselves! */
4965 ins_compl_delete();
4966 }
4967
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004968 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004969 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004970 if (compl_no_insert && !started)
4971 compl_enter_selects = TRUE;
4972 else
4973 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 /*
4976 * Show the file name for the match (if any)
4977 * Truncate the file name to avoid a wait for return.
4978 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004979 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02004981 char *lead = _("match in file");
4982 int space = sc_col - vim_strsize((char_u *)lead) - 2;
4983 char_u *s;
4984 char_u *e;
4985
4986 if (space > 0)
4987 {
4988 /* We need the tail that fits. With double-byte encoding going
4989 * back from the end is very slow, thus go from the start and keep
4990 * the text that fits in "space" between "s" and "e". */
4991 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
4992 {
4993 space -= ptr2cells(e);
4994 while (space < 0)
4995 {
4996 space += ptr2cells(s);
4997 MB_PTR_ADV(s);
4998 }
4999 }
5000 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5001 s > compl_shown_match->cp_fname ? "<" : "", s);
5002 msg(IObuff);
5003 redraw_cmdline = FALSE; /* don't overwrite! */
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006
5007 return num_matches;
5008}
5009
5010/*
5011 * Call this while finding completions, to check whether the user has hit a key
5012 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005013 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005015 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005016 * "in_compl_func" is TRUE when called from complete_check(), don't set
5017 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 */
5019 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005020ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
5022 static int count = 0;
5023
5024 int c;
5025
5026 /* Don't check when reading keys from a script. That would break the test
5027 * scripts */
5028 if (using_script())
5029 return;
5030
5031 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005032 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 return;
5034 count = 0;
5035
Bram Moolenaara260a972006-06-23 19:36:29 +00005036 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5037 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (c != NUL)
5040 {
5041 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5042 {
5043 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005044 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005045 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005046 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005048 else
5049 {
5050 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005051 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005052 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005053 if (c != K_IGNORE)
5054 {
5055 /* Don't interrupt completion when the character wasn't typed,
5056 * e.g., when doing @q to replay keys. */
5057 if (c != Ctrl_R && KeyTyped)
5058 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005059
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005060 vungetc(c);
5061 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005064 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005065 {
5066 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5067
5068 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005069 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005070 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005071}
5072
5073/*
5074 * Decide the direction of Insert mode complete from the key typed.
5075 * Returns BACKWARD or FORWARD.
5076 */
5077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005078ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005079{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005080 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005081 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005082 return BACKWARD;
5083 return FORWARD;
5084}
5085
5086/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005087 * Return TRUE for keys that are used for completion only when the popup menu
5088 * is visible.
5089 */
5090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005091ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005092{
5093 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005094 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5095 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005096}
5097
5098/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005099 * Decide the number of completions to move forward.
5100 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5101 */
5102 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005103ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005104{
5105 int h;
5106
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005107 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005108 {
5109 h = pum_get_height();
5110 if (h > 3)
5111 h -= 2; /* keep some context */
5112 return h;
5113 }
5114 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115}
5116
5117/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005118 * Return TRUE if completion with "c" should insert the match, FALSE if only
5119 * to change the currently selected completion.
5120 */
5121 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005122ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005123{
5124 switch (c)
5125 {
5126 case K_UP:
5127 case K_DOWN:
5128 case K_PAGEDOWN:
5129 case K_KPAGEDOWN:
5130 case K_S_DOWN:
5131 case K_PAGEUP:
5132 case K_KPAGEUP:
5133 case K_S_UP:
5134 return FALSE;
5135 }
5136 return TRUE;
5137}
5138
5139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 * Do Insert mode completion.
5141 * Called when character "c" was typed, which has a meaning for completion.
5142 * Returns OK if completion was done, FAIL if something failed (out of mem).
5143 */
5144 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005145ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 char_u *line;
5148 int startcol = 0; /* column where searched text starts */
5149 colnr_T curs_col; /* cursor column */
5150 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005151 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005152 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005153 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005154 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
Bram Moolenaare3226be2005-12-18 22:10:00 +00005156 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005157 insert_match = ins_compl_use_match(c);
5158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 /* First time we hit ^N or ^P (in a row, I mean) */
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 did_ai = FALSE;
5164#ifdef FEAT_SMARTINDENT
5165 did_si = FALSE;
5166 can_si = FALSE;
5167 can_si_back = FALSE;
5168#endif
5169 if (stop_arrow() == FAIL)
5170 return FAIL;
5171
5172 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005174 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005176 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 * "compl_startpos" to the cursor as a pattern to add a new word
5178 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005179 * "compl_startpos" is not in the same line as the cursor then fix it
5180 * (the line has been split because it was longer than 'tw'). if SOL
5181 * is set then skip the previous pattern, a word at the beginning of
5182 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005183 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5184 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 /*
5187 * it is a continued search
5188 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005190 if (ctrl_x_mode == CTRL_X_NORMAL
5191 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5192 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 /* line (probably) wrapped, set compl_startpos to the
5197 * first non_blank in the line, if it is not a wordchar
5198 * include it to get a better pattern, but then we don't
5199 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005200 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 compl_startpos.col = compl_col;
5202 compl_startpos.lnum = curwin->w_cursor.lnum;
5203 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205 else
5206 {
5207 /* S_IPOS was set when we inserted a word that was at the
5208 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 * mode but first we need to redefine compl_startpos */
5210 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 compl_cont_status |= CONT_SOL;
5213 compl_startpos.col = (colnr_T)(skipwhite(
5214 line + compl_length
5215 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005220 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005221 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 compl_cont_status &= ~CONT_SOL;
5226 compl_length = (IOSIZE - MIN_SPACE);
5227 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5230 if (compl_length < 1)
5231 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005233 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005244 if (ctrl_x_mode != CTRL_X_NORMAL)
5245 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 compl_cont_status = 0;
5247 compl_cont_status |= CONT_N_ADDS;
5248 compl_startpos = curwin->w_cursor;
5249 startcol = (int)curs_col;
5250 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252
5253 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005254 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5258 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005259 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005263 compl_col += ++startcol;
5264 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 compl_pattern = str_foldcase(line + compl_col,
5268 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 compl_pattern = vim_strnsave(line + compl_col,
5271 compl_length);
5272 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 return FAIL;
5274 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 char_u *prefix = (char_u *)"\\<";
5278
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005279 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005281 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 if (!vim_iswordp(line + compl_col)
5285 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 && (
5287#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#endif
5292 )))
5293 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 STRCPY((char *)compl_pattern, prefix);
5295 (void)quote_meta(compl_pattern + STRLEN(prefix),
5296 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#endif
5304 )
5305 {
5306 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5308 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_col += curs_col;
5311 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313 else
5314 {
5315#ifdef FEAT_MBYTE
5316 /* Search the point of change class of multibyte character
5317 * or not a word single byte character backward. */
5318 if (has_mbyte)
5319 {
5320 int base_class;
5321 int head_off;
5322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 startcol -= (*mb_head_off)(line, line + startcol);
5324 base_class = mb_get_class(line + startcol);
5325 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 head_off = (*mb_head_off)(line, line + startcol);
5328 if (base_class != mb_get_class(line + startcol
5329 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333 }
5334 else
5335#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_col += ++startcol;
5339 compl_length = (int)curs_col - startcol;
5340 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 /* Only match word with at least two chars -- webb
5343 * there's no need to call quote_meta,
5344 * alloc(7) is enough -- Acevedo
5345 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_pattern = alloc(7);
5347 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 STRCPY((char *)compl_pattern, "\\<");
5350 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5351 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353 else
5354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005356 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 STRCPY((char *)compl_pattern, "\\<");
5360 (void)quote_meta(compl_pattern + 2, line + compl_col,
5361 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 }
5364 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005365 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005367 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_length = (int)curs_col - (int)compl_col;
5369 if (compl_length < 0) /* cursor in indent: empty pattern */
5370 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_pattern = str_foldcase(line + compl_col, compl_length,
5373 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5376 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 return FAIL;
5378 }
5379 else if (ctrl_x_mode == CTRL_X_FILES)
5380 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005381 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005382 if (startcol > 0)
5383 {
5384 char_u *p = line + startcol;
5385
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005386 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005387 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005388 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005389 if (p == line && vim_isfilec(PTR2CHAR(p)))
5390 startcol = 0;
5391 else
5392 startcol = (int)(p - line) + 1;
5393 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005394
Bram Moolenaar0300e462013-09-08 16:03:45 +02005395 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 compl_length = (int)curs_col - startcol;
5397 compl_pattern = addstar(line + compl_col, compl_length,
5398 EXPAND_FILES);
5399 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 return FAIL;
5401 }
5402 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5403 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 compl_pattern = vim_strnsave(line, curs_col);
5405 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005408 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5410 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005411 /* No completion possible, use an empty pattern to get a
5412 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005413 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005414 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005415 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5416 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005418 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005419 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005420#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005421 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005422 * Call user defined function 'completefunc' with "a:findstart"
5423 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005424 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005425 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005426 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005427 char_u *funcname;
5428 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005429 win_T *curwin_save;
5430 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005431
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005432 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005433 * string */
5434 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5435 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5436 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005437 {
5438 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5439 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005440 /* restore did_ai, so that adding comment leader works */
5441 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005442 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005443 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005444
5445 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005446 args[1] = NULL;
5447 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005448 curwin_save = curwin;
5449 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005450 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005451 if (curwin_save != curwin || curbuf_save != curbuf)
5452 {
5453 EMSG(_(e_complwin));
5454 return FAIL;
5455 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005456 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005457 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005458 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005459 {
5460 EMSG(_(e_compldel));
5461 return FAIL;
5462 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005463
Bram Moolenaar6110a002012-01-26 18:58:38 +01005464 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005465 * cancel the complete without an error.
5466 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005467 if (col == -2)
5468 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005469 if (col == -3)
5470 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005471 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005472 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005473 if (!shortmess(SHM_COMPLETIONMENU))
5474 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005475 return FAIL;
5476 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005477
Bram Moolenaar82139082011-09-14 16:52:09 +02005478 /*
5479 * Reset extended parameters of completion, when start new
5480 * completion.
5481 */
5482 compl_opt_refresh_always = FALSE;
5483
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005484 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005485 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005486 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005487 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005488 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005489
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005490 /* Setup variables for completion. Need to obtain "line" again,
5491 * it may have become invalid. */
5492 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005493 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005494 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5495 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005496#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005497 return FAIL;
5498 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005499 else if (ctrl_x_mode == CTRL_X_SPELL)
5500 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005501#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005502 if (spell_bad_len > 0)
5503 compl_col = curs_col - spell_bad_len;
5504 else
5505 compl_col = spell_word_start(startcol);
5506 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005507 {
5508 compl_length = 0;
5509 compl_col = curs_col;
5510 }
5511 else
5512 {
5513 spell_expand_check_cap(compl_col);
5514 compl_length = (int)curs_col - compl_col;
5515 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005516 /* Need to obtain "line" again, it may have become invalid. */
5517 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005518 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5519 if (compl_pattern == NULL)
5520#endif
5521 return FAIL;
5522 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005523 else
5524 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005525 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 return FAIL;
5527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005532 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 /* Insert a new line, keep indentation but ignore 'comments' */
5535#ifdef FEAT_COMMENTS
5536 char_u *old = curbuf->b_p_com;
5537
5538 curbuf->b_p_com = (char_u *)"";
5539#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 compl_startpos.lnum = curwin->w_cursor.lnum;
5541 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 ins_eol('\r');
5543#ifdef FEAT_COMMENTS
5544 curbuf->b_p_com = old;
5545#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005546 compl_length = 0;
5547 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549 }
5550 else
5551 {
5552 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005553 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005556 if (compl_cont_status & CONT_LOCAL)
5557 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 else
5559 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5560
Bram Moolenaar62951b12011-09-21 18:23:05 +02005561 /* If any of the original typed text has been changed we need to fix
5562 * the redo buffer. */
5563 ins_compl_fixRedoBufForLeader(NULL);
5564
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005565 /* Always add completion for the original text. */
5566 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005567 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5568 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005569 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaar15675582018-02-09 12:29:56 +01005571 vim_clear((void **)&compl_pattern);
5572 vim_clear((void **)&compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return FAIL;
5574 }
5575
5576 /* showmode might reset the internal line pointers, so it must
5577 * be called before line = ml_get(), or when this address is no
5578 * longer needed. -- Acevedo.
5579 */
5580 edit_submode_extra = (char_u *)_("-- Searching...");
5581 edit_submode_highl = HLF_COUNT;
5582 showmode();
5583 edit_submode_extra = NULL;
5584 out_flush();
5585 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005586 else if (insert_match && stop_arrow() == FAIL)
5587 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005589 compl_shown_match = compl_curr_match;
5590 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
5592 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005593 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005595 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005596 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005597 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005599 /* may undisplay the popup menu */
5600 ins_compl_upd_pum();
5601
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005602 if (n > 1) /* all matches have been found */
5603 compl_matches = n;
5604 compl_curr_match = compl_shown_match;
5605 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaard68071d2006-05-02 22:08:30 +00005607 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5608 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (got_int && !global_busy)
5610 {
5611 (void)vgetc();
5612 got_int = FALSE;
5613 }
5614
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005615 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005616 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005618 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5619 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5621 edit_submode_highl = HLF_E;
5622 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5623 * because we couldn't expand anything at first place, but if we used
5624 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5625 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005626 if ( compl_length > 1
5627 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005628 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5630 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633
Bram Moolenaar572cb562005-08-05 21:35:02 +00005634 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005635 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005637 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
5639 if (edit_submode_extra == NULL)
5640 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005641 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 edit_submode_extra = (char_u *)_("Back at original");
5644 edit_submode_highl = HLF_W;
5645 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005646 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
5648 edit_submode_extra = (char_u *)_("Word from other line");
5649 edit_submode_highl = HLF_COUNT;
5650 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005651 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 edit_submode_extra = (char_u *)_("The only match");
5654 edit_submode_highl = HLF_COUNT;
5655 }
5656 else
5657 {
5658 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005659 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005661 int number = 0;
5662 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005664 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 /* search backwards for the first valid (!= -1) number.
5667 * This should normally succeed already at the first loop
5668 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005669 for (match = compl_curr_match->cp_prev; match != NULL
5670 && match != compl_first_match;
5671 match = match->cp_prev)
5672 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005674 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 break;
5676 }
5677 if (match != NULL)
5678 /* go up and assign all numbers which are not assigned
5679 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005680 for (match = match->cp_next;
5681 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005682 match = match->cp_next)
5683 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else /* BACKWARD */
5686 {
5687 /* search forwards (upwards) for the first valid (!= -1)
5688 * number. This should normally succeed already at the
5689 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005690 for (match = compl_curr_match->cp_next; match != NULL
5691 && match != compl_first_match;
5692 match = match->cp_next)
5693 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005695 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 break;
5697 }
5698 if (match != NULL)
5699 /* go down and assign all numbers which are not
5700 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005701 for (match = match->cp_prev; match
5702 && match->cp_number == -1;
5703 match = match->cp_prev)
5704 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 }
5707
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005708 /* The match should always have a sequence number now, this is
5709 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005710 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005712 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5713 * Translations may need more than twice that. */
5714 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005716 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005717 vim_snprintf((char *)match_ref, sizeof(match_ref),
5718 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005719 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005721 vim_snprintf((char *)match_ref, sizeof(match_ref),
5722 _("match %d"),
5723 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 edit_submode_extra = match_ref;
5725 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005726 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 curs_columns(FALSE);
5728 }
5729 }
5730 }
5731
5732 /* Show a message about what (completion) mode we're in. */
5733 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005734 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005736 if (edit_submode_extra != NULL)
5737 {
5738 if (!p_smd)
5739 msg_attr(edit_submode_extra,
5740 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005741 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005742 }
5743 else
5744 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaard68071d2006-05-02 22:08:30 +00005747 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005748 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005749 show_pum(save_w_wrow, save_w_leftcol);
5750
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005751 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005752 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 return OK;
5755}
5756
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005757 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005758show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005759{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005760 /* RedrawingDisabled may be set when invoked through complete(). */
5761 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005762
Bram Moolenaarcb036422017-03-01 12:29:10 +01005763 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005764
Bram Moolenaarcb036422017-03-01 12:29:10 +01005765 /* If the cursor moved or the display scrolled we need to remove the pum
5766 * first. */
5767 setcursor();
5768 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5769 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005770
Bram Moolenaarcb036422017-03-01 12:29:10 +01005771 ins_compl_show_pum();
5772 setcursor();
5773 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005774}
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776/*
5777 * Looks in the first "len" chars. of "src" for search-metachars.
5778 * If dest is not NULL the chars. are copied there quoting (with
5779 * a backslash) the metachars, and dest would be NUL terminated.
5780 * Returns the length (needed) of dest
5781 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005782 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005783quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005785 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005787 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
5789 switch (*src)
5790 {
5791 case '.':
5792 case '*':
5793 case '[':
5794 if (ctrl_x_mode == CTRL_X_DICTIONARY
5795 || ctrl_x_mode == CTRL_X_THESAURUS)
5796 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005797 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 case '~':
5799 if (!p_magic) /* quote these only if magic is set */
5800 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005801 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 case '\\':
5803 if (ctrl_x_mode == CTRL_X_DICTIONARY
5804 || ctrl_x_mode == CTRL_X_THESAURUS)
5805 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005806 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 case '^': /* currently it's not needed. */
5808 case '$':
5809 m++;
5810 if (dest != NULL)
5811 *dest++ = '\\';
5812 break;
5813 }
5814 if (dest != NULL)
5815 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005816# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 /* Copy remaining bytes of a multibyte character. */
5818 if (has_mbyte)
5819 {
5820 int i, mb_len;
5821
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005822 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 if (mb_len > 0 && len >= mb_len)
5824 for (i = 0; i < mb_len; ++i)
5825 {
5826 --len;
5827 ++src;
5828 if (dest != NULL)
5829 *dest++ = *src;
5830 }
5831 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
5834 if (dest != NULL)
5835 *dest = NUL;
5836
5837 return m;
5838}
5839#endif /* FEAT_INS_EXPAND */
5840
5841/*
5842 * Next character is interpreted literally.
5843 * A one, two or three digit decimal number is interpreted as its byte value.
5844 * If one or two digits are entered, the next character is given to vungetc().
5845 * For Unicode a character > 255 may be returned.
5846 */
5847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 int cc;
5851 int nc;
5852 int i;
5853 int hex = FALSE;
5854 int octal = FALSE;
5855#ifdef FEAT_MBYTE
5856 int unicode = 0;
5857#endif
5858
5859 if (got_int)
5860 return Ctrl_C;
5861
5862#ifdef FEAT_GUI
5863 /*
5864 * In GUI there is no point inserting the internal code for a special key.
5865 * It is more useful to insert the string "<KEY>" instead. This would
5866 * probably be useful in a text window too, but it would not be
5867 * vi-compatible (maybe there should be an option for it?) -- webb
5868 */
5869 if (gui.in_use)
5870 ++allow_keys;
5871#endif
5872#ifdef USE_ON_FLY_SCROLL
5873 dont_scroll = TRUE; /* disallow scrolling here */
5874#endif
5875 ++no_mapping; /* don't map the next key hits */
5876 cc = 0;
5877 i = 0;
5878 for (;;)
5879 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005880 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#ifdef FEAT_CMDL_INFO
5882 if (!(State & CMDLINE)
5883# ifdef FEAT_MBYTE
5884 && MB_BYTE2LEN_CHECK(nc) == 1
5885# endif
5886 )
5887 add_to_showcmd(nc);
5888#endif
5889 if (nc == 'x' || nc == 'X')
5890 hex = TRUE;
5891 else if (nc == 'o' || nc == 'O')
5892 octal = TRUE;
5893#ifdef FEAT_MBYTE
5894 else if (nc == 'u' || nc == 'U')
5895 unicode = nc;
5896#endif
5897 else
5898 {
5899 if (hex
5900#ifdef FEAT_MBYTE
5901 || unicode != 0
5902#endif
5903 )
5904 {
5905 if (!vim_isxdigit(nc))
5906 break;
5907 cc = cc * 16 + hex2nr(nc);
5908 }
5909 else if (octal)
5910 {
5911 if (nc < '0' || nc > '7')
5912 break;
5913 cc = cc * 8 + nc - '0';
5914 }
5915 else
5916 {
5917 if (!VIM_ISDIGIT(nc))
5918 break;
5919 cc = cc * 10 + nc - '0';
5920 }
5921
5922 ++i;
5923 }
5924
5925 if (cc > 255
5926#ifdef FEAT_MBYTE
5927 && unicode == 0
5928#endif
5929 )
5930 cc = 255; /* limit range to 0-255 */
5931 nc = 0;
5932
5933 if (hex) /* hex: up to two chars */
5934 {
5935 if (i >= 2)
5936 break;
5937 }
5938#ifdef FEAT_MBYTE
5939 else if (unicode) /* Unicode: up to four or eight chars */
5940 {
5941 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5942 break;
5943 }
5944#endif
5945 else if (i >= 3) /* decimal or octal: up to three chars */
5946 break;
5947 }
5948 if (i == 0) /* no number entered */
5949 {
5950 if (nc == K_ZERO) /* NUL is stored as NL */
5951 {
5952 cc = '\n';
5953 nc = 0;
5954 }
5955 else
5956 {
5957 cc = nc;
5958 nc = 0;
5959 }
5960 }
5961
5962 if (cc == 0) /* NUL is stored as NL */
5963 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005964#ifdef FEAT_MBYTE
5965 if (enc_dbcs && (cc & 0xff) == 0)
5966 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5967 second byte will cause trouble! */
5968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 --no_mapping;
5971#ifdef FEAT_GUI
5972 if (gui.in_use)
5973 --allow_keys;
5974#endif
5975 if (nc)
5976 vungetc(nc);
5977 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5978 return cc;
5979}
5980
5981/*
5982 * Insert character, taking care of special keys and mod_mask
5983 */
5984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005985insert_special(
5986 int c,
5987 int allow_modmask,
5988 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 char_u *p;
5991 int len;
5992
5993 /*
5994 * Special function key, translate into "<Key>". Up to the last '>' is
5995 * inserted with ins_str(), so as not to replace characters in replace
5996 * mode.
5997 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5998 * unless 'allow_modmask' is TRUE.
5999 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006000#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 /* Command-key never produces a normal key */
6002 if (mod_mask & MOD_MASK_CMD)
6003 allow_modmask = TRUE;
6004#endif
6005 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6006 {
6007 p = get_special_key_name(c, mod_mask);
6008 len = (int)STRLEN(p);
6009 c = p[len - 1];
6010 if (len > 2)
6011 {
6012 if (stop_arrow() == FAIL)
6013 return;
6014 p[len - 1] = NUL;
6015 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006016 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 ctrlv = FALSE;
6018 }
6019 }
6020 if (stop_arrow() == OK)
6021 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6022}
6023
6024/*
6025 * Special characters in this context are those that need processing other
6026 * than the simple insertion that can be performed here. This includes ESC
6027 * which terminates the insert, and CR/NL which need special processing to
6028 * open up a new line. This routine tries to optimize insertions performed by
6029 * the "redo", "undo" or "put" commands, so it needs to know when it should
6030 * stop and defer processing to the "normal" mechanism.
6031 * '0' and '^' are special, because they can be followed by CTRL-D.
6032 */
6033#ifdef EBCDIC
6034# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6035#else
6036# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6037#endif
6038
6039#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006040# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006042# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#endif
6044
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006045/*
6046 * "flags": INSCHAR_FORMAT - force formatting
6047 * INSCHAR_CTRLV - char typed just after CTRL-V
6048 * INSCHAR_NO_FEX - don't use 'formatexpr'
6049 *
6050 * NOTE: passes the flags value straight through to internal_format() which,
6051 * beside INSCHAR_FORMAT (above), is also looking for these:
6052 * INSCHAR_DO_COM - format comments
6053 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056insertchar(
6057 int c, /* character to insert or NUL */
6058 int flags, /* INSCHAR_FORMAT, etc. */
6059 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 int textwidth;
6062#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006066 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006068 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 /*
6072 * Try to break the line in two or more pieces when:
6073 * - Always do this if we have been called to do formatting only.
6074 * - Always do this when 'formatoptions' has the 'a' flag and the line
6075 * ends in white space.
6076 * - Otherwise:
6077 * - Don't do this if inserting a blank
6078 * - Don't do this if an existing character is being replaced, unless
6079 * we're in VREPLACE mode.
6080 * - Do this if the cursor is not on the line where insert started
6081 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6082 * before the insert.
6083 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6084 * before 'textwidth'
6085 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006087 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006088 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 && !((State & REPLACE_FLAG)
6090#ifdef FEAT_VREPLACE
6091 && !(State & VREPLACE_FLAG)
6092#endif
6093 && *ml_get_cursor() != NUL)
6094 && (curwin->w_cursor.lnum != Insstart.lnum
6095 || ((!has_format_option(FO_INS_LONG)
6096 || Insstart_textlen <= (colnr_T)textwidth)
6097 && (!fo_ins_blank
6098 || Insstart_blank_vcol <= (colnr_T)textwidth
6099 ))))))
6100 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006101 /* Format with 'formatexpr' when it's set. Use internal formatting
6102 * when 'formatexpr' isn't set or it returns non-zero. */
6103#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006104 int do_internal = TRUE;
6105 colnr_T virtcol = get_nolist_virtcol()
6106 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006107
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006108 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6109 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006110 {
6111 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6112 /* It may be required to save for undo again, e.g. when setline()
6113 * was called. */
6114 ins_need_undo = TRUE;
6115 }
6116 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006118 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 if (c == NUL) /* only formatting was wanted */
6122 return;
6123
6124#ifdef FEAT_COMMENTS
6125 /* Check whether this character should end a comment. */
6126 if (did_ai && (int)c == end_comment_pending)
6127 {
6128 char_u *line;
6129 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6130 int middle_len, end_len;
6131 int i;
6132
6133 /*
6134 * Need to remove existing (middle) comment leader and insert end
6135 * comment leader. First, check what comment leader we can find.
6136 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006137 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6139 {
6140 /* Skip middle-comment string */
6141 while (*p && p[-1] != ':') /* find end of middle flags */
6142 ++p;
6143 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6144 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006145 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 --middle_len;
6147
6148 /* Find the end-comment string */
6149 while (*p && p[-1] != ':') /* find end of end flags */
6150 ++p;
6151 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6152
6153 /* Skip white space before the cursor */
6154 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006155 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 ;
6157 i++;
6158
6159 /* Skip to before the middle leader */
6160 i -= middle_len;
6161
6162 /* Check some expected things before we go on */
6163 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6164 {
6165 /* Backspace over all the stuff we want to replace */
6166 backspace_until_column(i);
6167
6168 /*
6169 * Insert the end-comment string, except for the last
6170 * character, which will get inserted as normal later.
6171 */
6172 ins_bytes_len(lead_end, end_len - 1);
6173 }
6174 }
6175 }
6176 end_comment_pending = NUL;
6177#endif
6178
6179 did_ai = FALSE;
6180#ifdef FEAT_SMARTINDENT
6181 did_si = FALSE;
6182 can_si = FALSE;
6183 can_si_back = FALSE;
6184#endif
6185
6186 /*
6187 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6188 * This speeds up normal text input considerably.
6189 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6190 * need to re-indent at a ':', or any other character (but not what
6191 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006192 * Don't do this when there an InsertCharPre autocommand is defined,
6193 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 */
6195#ifdef USE_ON_FLY_SCROLL
6196 dont_scroll = FALSE; /* allow scrolling here */
6197#endif
6198
6199 if ( !ISSPECIAL(c)
6200#ifdef FEAT_MBYTE
6201 && (!has_mbyte || (*mb_char2len)(c) == 1)
6202#endif
6203 && vpeekc() != NUL
6204 && !(State & REPLACE_FLAG)
6205#ifdef FEAT_CINDENT
6206 && !cindent_on()
6207#endif
6208#ifdef FEAT_RIGHTLEFT
6209 && !p_ri
6210#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006211#ifdef FEAT_AUTOCMD
6212 && !has_insertcharpre()
6213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 )
6215 {
6216#define INPUT_BUFLEN 100
6217 char_u buf[INPUT_BUFLEN + 1];
6218 int i;
6219 colnr_T virtcol = 0;
6220
6221 buf[0] = c;
6222 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 virtcol = get_nolist_virtcol();
6225 /*
6226 * Stop the string when:
6227 * - no more chars available
6228 * - finding a special character (command key)
6229 * - buffer is full
6230 * - running into the 'textwidth' boundary
6231 * - need to check for abbreviation: A non-word char after a word-char
6232 */
6233 while ( (c = vpeekc()) != NUL
6234 && !ISSPECIAL(c)
6235#ifdef FEAT_MBYTE
6236 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6237#endif
6238 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006239# ifdef FEAT_FKMAP
6240 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 && (textwidth == 0
6243 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6244 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6245 {
6246#ifdef FEAT_RIGHTLEFT
6247 c = vgetc();
6248 if (p_hkmap && KeyTyped)
6249 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 buf[i++] = c;
6251#else
6252 buf[i++] = vgetc();
6253#endif
6254 }
6255
6256#ifdef FEAT_DIGRAPHS
6257 do_digraph(-1); /* clear digraphs */
6258 do_digraph(buf[i-1]); /* may be the start of a digraph */
6259#endif
6260 buf[i] = NUL;
6261 ins_str(buf);
6262 if (flags & INSCHAR_CTRLV)
6263 {
6264 redo_literal(*buf);
6265 i = 1;
6266 }
6267 else
6268 i = 0;
6269 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006270 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
6272 else
6273 {
6274#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006275 int cc;
6276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6278 {
6279 char_u buf[MB_MAXBYTES + 1];
6280
6281 (*mb_char2bytes)(c, buf);
6282 buf[cc] = NUL;
6283 ins_char_bytes(buf, cc);
6284 AppendCharToRedobuff(c);
6285 }
6286 else
6287#endif
6288 {
6289 ins_char(c);
6290 if (flags & INSCHAR_CTRLV)
6291 redo_literal(c);
6292 else
6293 AppendCharToRedobuff(c);
6294 }
6295 }
6296}
6297
6298/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006299 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006300 *
6301 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6302 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006303 */
6304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006305internal_format(
6306 int textwidth,
6307 int second_indent,
6308 int flags,
6309 int format_only,
6310 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006311{
6312 int cc;
6313 int save_char = NUL;
6314 int haveto_redraw = FALSE;
6315 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6316#ifdef FEAT_MBYTE
6317 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6318#endif
6319 int fo_white_par = has_format_option(FO_WHITE_PAR);
6320 int first_line = TRUE;
6321#ifdef FEAT_COMMENTS
6322 colnr_T leader_len;
6323 int no_leader = FALSE;
6324 int do_comments = (flags & INSCHAR_DO_COM);
6325#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006326#ifdef FEAT_LINEBREAK
6327 int has_lbr = curwin->w_p_lbr;
6328
6329 /* make sure win_lbr_chartabsize() counts correctly */
6330 curwin->w_p_lbr = FALSE;
6331#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332
6333 /*
6334 * When 'ai' is off we don't want a space under the cursor to be
6335 * deleted. Replace it with an 'x' temporarily.
6336 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006337 if (!curbuf->b_p_ai
6338#ifdef FEAT_VREPLACE
6339 && !(State & VREPLACE_FLAG)
6340#endif
6341 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006342 {
6343 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006344 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345 {
6346 save_char = cc;
6347 pchar_cursor('x');
6348 }
6349 }
6350
6351 /*
6352 * Repeat breaking lines, until the current line is not too long.
6353 */
6354 while (!got_int)
6355 {
6356 int startcol; /* Cursor column at entry */
6357 int wantcol; /* column at textwidth border */
6358 int foundcol; /* column for start of spaces */
6359 int end_foundcol = 0; /* column for start of word */
6360 colnr_T len;
6361 colnr_T virtcol;
6362#ifdef FEAT_VREPLACE
6363 int orig_col = 0;
6364 char_u *saved_text = NULL;
6365#endif
6366 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006367 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368
Bram Moolenaar97b98102009-11-17 16:41:01 +00006369 virtcol = get_nolist_virtcol()
6370 + char2cells(c != NUL ? c : gchar_cursor());
6371 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006372 break;
6373
6374#ifdef FEAT_COMMENTS
6375 if (no_leader)
6376 do_comments = FALSE;
6377 else if (!(flags & INSCHAR_FORMAT)
6378 && has_format_option(FO_WRAP_COMS))
6379 do_comments = TRUE;
6380
6381 /* Don't break until after the comment leader */
6382 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006383 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006384 else
6385 leader_len = 0;
6386
6387 /* If the line doesn't start with a comment leader, then don't
6388 * start one in a following broken line. Avoids that a %word
6389 * moved to the start of the next line causes all following lines
6390 * to start with %. */
6391 if (leader_len == 0)
6392 no_leader = TRUE;
6393#endif
6394 if (!(flags & INSCHAR_FORMAT)
6395#ifdef FEAT_COMMENTS
6396 && leader_len == 0
6397#endif
6398 && !has_format_option(FO_WRAP))
6399
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 if ((startcol = curwin->w_cursor.col) == 0)
6402 break;
6403
6404 /* find column of textwidth border */
6405 coladvance((colnr_T)textwidth);
6406 wantcol = curwin->w_cursor.col;
6407
Bram Moolenaar97b98102009-11-17 16:41:01 +00006408 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409 foundcol = 0;
6410
6411 /*
6412 * Find position to break at.
6413 * Stop at first entered white when 'formatoptions' has 'v'
6414 */
6415 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006416 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006417 || curwin->w_cursor.lnum != Insstart.lnum
6418 || curwin->w_cursor.col >= Insstart.col)
6419 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006420 if (curwin->w_cursor.col == startcol && c != NUL)
6421 cc = c;
6422 else
6423 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006424 if (WHITECHAR(cc))
6425 {
6426 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006427 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428
6429 /* find start of sequence of blanks */
6430 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6431 {
6432 dec_cursor();
6433 cc = gchar_cursor();
6434 }
6435 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6436 break; /* only spaces in front of text */
6437#ifdef FEAT_COMMENTS
6438 /* Don't break until after the comment leader */
6439 if (curwin->w_cursor.col < leader_len)
6440 break;
6441#endif
6442 if (has_format_option(FO_ONE_LETTER))
6443 {
6444 /* do not break after one-letter words */
6445 if (curwin->w_cursor.col == 0)
6446 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006447#ifdef FEAT_COMMENTS
6448 /* do not break "#a b" when 'tw' is 2 */
6449 if (curwin->w_cursor.col <= leader_len)
6450 break;
6451#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 col = curwin->w_cursor.col;
6453 dec_cursor();
6454 cc = gchar_cursor();
6455
6456 if (WHITECHAR(cc))
6457 continue; /* one-letter, continue */
6458 curwin->w_cursor.col = col;
6459 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006460
6461 inc_cursor();
6462
6463 end_foundcol = end_col + 1;
6464 foundcol = curwin->w_cursor.col;
6465 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466 break;
6467 }
6468#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006469 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470 {
6471 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006472 if (curwin->w_cursor.col != startcol)
6473 {
6474#ifdef FEAT_COMMENTS
6475 /* Don't break until after the comment leader */
6476 if (curwin->w_cursor.col < leader_len)
6477 break;
6478#endif
6479 col = curwin->w_cursor.col;
6480 inc_cursor();
6481 /* Don't change end_foundcol if already set. */
6482 if (foundcol != curwin->w_cursor.col)
6483 {
6484 foundcol = curwin->w_cursor.col;
6485 end_foundcol = foundcol;
6486 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6487 break;
6488 }
6489 curwin->w_cursor.col = col;
6490 }
6491
6492 if (curwin->w_cursor.col == 0)
6493 break;
6494
6495 col = curwin->w_cursor.col;
6496
6497 dec_cursor();
6498 cc = gchar_cursor();
6499
6500 if (WHITECHAR(cc))
6501 continue; /* break with space */
6502#ifdef FEAT_COMMENTS
6503 /* Don't break until after the comment leader */
6504 if (curwin->w_cursor.col < leader_len)
6505 break;
6506#endif
6507
6508 curwin->w_cursor.col = col;
6509
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006512 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6513 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514 }
6515#endif
6516 if (curwin->w_cursor.col == 0)
6517 break;
6518 dec_cursor();
6519 }
6520
6521 if (foundcol == 0) /* no spaces, cannot break line */
6522 {
6523 curwin->w_cursor.col = startcol;
6524 break;
6525 }
6526
6527 /* Going to break the line, remove any "$" now. */
6528 undisplay_dollar();
6529
6530 /*
6531 * Offset between cursor position and line break is used by replace
6532 * stack functions. VREPLACE does not use this, and backspaces
6533 * over the text instead.
6534 */
6535#ifdef FEAT_VREPLACE
6536 if (State & VREPLACE_FLAG)
6537 orig_col = startcol; /* Will start backspacing from here */
6538 else
6539#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006540 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006541
6542 /*
6543 * adjust startcol for spaces that will be deleted and
6544 * characters that will remain on top line
6545 */
6546 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006547 while ((cc = gchar_cursor(), WHITECHAR(cc))
6548 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006549 inc_cursor();
6550 startcol -= curwin->w_cursor.col;
6551 if (startcol < 0)
6552 startcol = 0;
6553
6554#ifdef FEAT_VREPLACE
6555 if (State & VREPLACE_FLAG)
6556 {
6557 /*
6558 * In VREPLACE mode, we will backspace over the text to be
6559 * wrapped, so save a copy now to put on the next line.
6560 */
6561 saved_text = vim_strsave(ml_get_cursor());
6562 curwin->w_cursor.col = orig_col;
6563 if (saved_text == NULL)
6564 break; /* Can't do it, out of memory */
6565 saved_text[startcol] = NUL;
6566
6567 /* Backspace over characters that will move to the next line */
6568 if (!fo_white_par)
6569 backspace_until_column(foundcol);
6570 }
6571 else
6572#endif
6573 {
6574 /* put cursor after pos. to break line */
6575 if (!fo_white_par)
6576 curwin->w_cursor.col = foundcol;
6577 }
6578
6579 /*
6580 * Split the line just before the margin.
6581 * Only insert/delete lines, but don't really redraw the window.
6582 */
6583 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6584 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6585#ifdef FEAT_COMMENTS
6586 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006587 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006588#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006589 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6590 if (!(flags & INSCHAR_COM_LIST))
6591 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592
6593 replace_offset = 0;
6594 if (first_line)
6595 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006596 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006597 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006598 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006599 * This section is for auto-wrap of numeric lists. When not
6600 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6601 * flag will be set and open_line() will handle it (as seen
6602 * above). The code here (and in get_number_indent()) will
6603 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006604 */
6605 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006606 second_indent =
6607 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006608 if (second_indent >= 0)
6609 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006610#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006611 if (State & VREPLACE_FLAG)
6612 change_indent(INDENT_SET, second_indent,
6613 FALSE, NUL, TRUE);
6614 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006615#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006616#ifdef FEAT_COMMENTS
6617 if (leader_len > 0 && second_indent - leader_len > 0)
6618 {
6619 int i;
6620 int padding = second_indent - leader_len;
6621
6622 /* We started at the first_line of a numbered list
6623 * that has a comment. the open_line() function has
6624 * inserted the proper comment leader and positioned
6625 * the cursor at the end of the split line. Now we
6626 * add the additional whitespace needed after the
6627 * comment leader for the numbered list. */
6628 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006629 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006630 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006631 }
6632 else
6633 {
6634#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006635 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006636#ifdef FEAT_COMMENTS
6637 }
6638#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006639 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006640 }
6641 first_line = FALSE;
6642 }
6643
6644#ifdef FEAT_VREPLACE
6645 if (State & VREPLACE_FLAG)
6646 {
6647 /*
6648 * In VREPLACE mode we have backspaced over the text to be
6649 * moved, now we re-insert it into the new line.
6650 */
6651 ins_bytes(saved_text);
6652 vim_free(saved_text);
6653 }
6654 else
6655#endif
6656 {
6657 /*
6658 * Check if cursor is not past the NUL off the line, cindent
6659 * may have added or removed indent.
6660 */
6661 curwin->w_cursor.col += startcol;
6662 len = (colnr_T)STRLEN(ml_get_curline());
6663 if (curwin->w_cursor.col > len)
6664 curwin->w_cursor.col = len;
6665 }
6666
6667 haveto_redraw = TRUE;
6668#ifdef FEAT_CINDENT
6669 can_cindent = TRUE;
6670#endif
6671 /* moved the cursor, don't autoindent or cindent now */
6672 did_ai = FALSE;
6673#ifdef FEAT_SMARTINDENT
6674 did_si = FALSE;
6675 can_si = FALSE;
6676 can_si_back = FALSE;
6677#endif
6678 line_breakcheck();
6679 }
6680
6681 if (save_char != NUL) /* put back space after cursor */
6682 pchar_cursor(save_char);
6683
Bram Moolenaar0026d472014-09-09 16:32:39 +02006684#ifdef FEAT_LINEBREAK
6685 curwin->w_p_lbr = has_lbr;
6686#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687 if (!format_only && haveto_redraw)
6688 {
6689 update_topline();
6690 redraw_curbuf_later(VALID);
6691 }
6692}
6693
6694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 * Called after inserting or deleting text: When 'formatoptions' includes the
6696 * 'a' flag format from the current line until the end of the paragraph.
6697 * Keep the cursor at the same position relative to the text.
6698 * The caller must have saved the cursor line for undo, following ones will be
6699 * saved here.
6700 */
6701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702auto_format(
6703 int trailblank, /* when TRUE also format with trailing blank */
6704 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 pos_T pos;
6707 colnr_T len;
6708 char_u *old;
6709 char_u *new, *pnew;
6710 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006711 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713 if (!has_format_option(FO_AUTO))
6714 return;
6715
6716 pos = curwin->w_cursor;
6717 old = ml_get_curline();
6718
6719 /* may remove added space */
6720 check_auto_format(FALSE);
6721
6722 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6723 * user might insert normal text next. Also skip formatting when "1" is
6724 * in 'formatoptions' and there is a single character before the cursor.
6725 * Otherwise the line would be broken and when typing another non-white
6726 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006727 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 if (*old != NUL && !trailblank && wasatend)
6729 {
6730 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006731 cc = gchar_cursor();
6732 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6733 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006735 cc = gchar_cursor();
6736 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
6738 curwin->w_cursor = pos;
6739 return;
6740 }
6741 curwin->w_cursor = pos;
6742 }
6743
6744#ifdef FEAT_COMMENTS
6745 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6746 * comments. */
6747 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006748 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 return;
6750#endif
6751
6752 /*
6753 * May start formatting in a previous line, so that after "x" a word is
6754 * moved to the previous line if it fits there now. Only when this is not
6755 * the start of a paragraph.
6756 */
6757 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6758 {
6759 --curwin->w_cursor.lnum;
6760 if (u_save_cursor() == FAIL)
6761 return;
6762 }
6763
6764 /*
6765 * Do the formatting and restore the cursor position. "saved_cursor" will
6766 * be adjusted for the text formatting.
6767 */
6768 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006769 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 curwin->w_cursor = saved_cursor;
6771 saved_cursor.lnum = 0;
6772
6773 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6774 {
6775 /* "cannot happen" */
6776 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6777 coladvance((colnr_T)MAXCOL);
6778 }
6779 else
6780 check_cursor_col();
6781
6782 /* Insert mode: If the cursor is now after the end of the line while it
6783 * previously wasn't, the line was broken. Because of the rule above we
6784 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6785 * formatted. */
6786 if (!wasatend && has_format_option(FO_WHITE_PAR))
6787 {
6788 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006789 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (curwin->w_cursor.col == len)
6791 {
6792 pnew = vim_strnsave(new, len + 2);
6793 pnew[len] = ' ';
6794 pnew[len + 1] = NUL;
6795 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6796 /* remove the space later */
6797 did_add_space = TRUE;
6798 }
6799 else
6800 /* may remove added space */
6801 check_auto_format(FALSE);
6802 }
6803
6804 check_cursor();
6805}
6806
6807/*
6808 * When an extra space was added to continue a paragraph for auto-formatting,
6809 * delete it now. The space must be under the cursor, just after the insert
6810 * position.
6811 */
6812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813check_auto_format(
6814 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006817 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 if (did_add_space)
6820 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006821 cc = gchar_cursor();
6822 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 /* Somehow the space was removed already. */
6824 did_add_space = FALSE;
6825 else
6826 {
6827 if (!end_insert)
6828 {
6829 inc_cursor();
6830 c = gchar_cursor();
6831 dec_cursor();
6832 }
6833 if (c != NUL)
6834 {
6835 /* The space is no longer at the end of the line, delete it. */
6836 del_char(FALSE);
6837 did_add_space = FALSE;
6838 }
6839 }
6840 }
6841}
6842
6843/*
6844 * Find out textwidth to be used for formatting:
6845 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006846 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 * if invalid value, use 0.
6848 * Set default to window width (maximum 79) for "gq" operator.
6849 */
6850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851comp_textwidth(
6852 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 int textwidth;
6855
6856 textwidth = curbuf->b_p_tw;
6857 if (textwidth == 0 && curbuf->b_p_wm)
6858 {
6859 /* The width is the window width minus 'wrapmargin' minus all the
6860 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006861 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862#ifdef FEAT_CMDWIN
6863 if (cmdwin_type != 0)
6864 textwidth -= 1;
6865#endif
6866#ifdef FEAT_FOLDING
6867 textwidth -= curwin->w_p_fdc;
6868#endif
6869#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006870 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 textwidth -= 1;
6872#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006873 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 textwidth -= 8;
6875 }
6876 if (textwidth < 0)
6877 textwidth = 0;
6878 if (ff && textwidth == 0)
6879 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006880 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 if (textwidth > 79)
6882 textwidth = 79;
6883 }
6884 return textwidth;
6885}
6886
6887/*
6888 * Put a character in the redo buffer, for when just after a CTRL-V.
6889 */
6890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006891redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 char_u buf[10];
6894
6895 /* Only digits need special treatment. Translate them into a string of
6896 * three digits. */
6897 if (VIM_ISDIGIT(c))
6898 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006899 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 AppendToRedobuff(buf);
6901 }
6902 else
6903 AppendCharToRedobuff(c);
6904}
6905
6906/*
6907 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006908 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 */
6910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006911start_arrow(
6912 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006914 start_arrow_common(end_insert_pos, TRUE);
6915}
6916
6917/*
6918 * Like start_arrow() but with end_change argument.
6919 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6920 */
6921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006922start_arrow_with_change(
6923 pos_T *end_insert_pos, /* can be NULL */
6924 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006925{
6926 start_arrow_common(end_insert_pos, end_change);
6927 if (!end_change)
6928 {
6929 AppendCharToRedobuff(Ctrl_G);
6930 AppendCharToRedobuff('U');
6931 }
6932}
6933
6934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006935start_arrow_common(
6936 pos_T *end_insert_pos, /* can be NULL */
6937 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006938{
6939 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006942 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 arrow_used = TRUE; /* this means we stopped the current insert */
6944 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006945#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006946 check_spell_redraw();
6947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948}
6949
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006950#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006951/*
6952 * If we skipped highlighting word at cursor, do it now.
6953 * It may be skipped again, thus reset spell_redraw_lnum first.
6954 */
6955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006957{
6958 if (spell_redraw_lnum != 0)
6959 {
6960 linenr_T lnum = spell_redraw_lnum;
6961
6962 spell_redraw_lnum = 0;
6963 redrawWinline(lnum, FALSE);
6964 }
6965}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006966
6967/*
6968 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6969 * spelled word, if there is one.
6970 */
6971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006973{
6974 pos_T tpos = curwin->w_cursor;
6975
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006976 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006977 if (curwin->w_cursor.col != tpos.col)
6978 start_arrow(&tpos);
6979}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006980#endif
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982/*
6983 * stop_arrow() is called before a change is made in insert mode.
6984 * If an arrow key has been used, start a new insertion.
6985 * Returns FAIL if undo is impossible, shouldn't insert then.
6986 */
6987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006988stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
6990 if (arrow_used)
6991 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006992 Insstart = curwin->w_cursor; /* new insertion starts here */
6993 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6994 /* Don't update the original insert position when moved to the
6995 * right, except when nothing was inserted yet. */
6996 update_Insstart_orig = FALSE;
6997 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (u_save_cursor() == OK)
7000 {
7001 arrow_used = FALSE;
7002 ins_need_undo = FALSE;
7003 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 ai_col = 0;
7006#ifdef FEAT_VREPLACE
7007 if (State & VREPLACE_FLAG)
7008 {
7009 orig_line_count = curbuf->b_ml.ml_line_count;
7010 vr_lines_changed = 1;
7011 }
7012#endif
7013 ResetRedobuff();
7014 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007015 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017 else if (ins_need_undo)
7018 {
7019 if (u_save_cursor() == OK)
7020 ins_need_undo = FALSE;
7021 }
7022
7023#ifdef FEAT_FOLDING
7024 /* Always open fold at the cursor line when inserting something. */
7025 foldOpenCursor();
7026#endif
7027
7028 return (arrow_used || ins_need_undo ? FAIL : OK);
7029}
7030
7031/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007032 * Do a few things to stop inserting.
7033 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7034 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 */
7036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007037stop_insert(
7038 pos_T *end_insert_pos,
7039 int esc, /* called by ins_esc() */
7040 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007042 int cc;
7043 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045 stop_redo_ins();
7046 replace_flush(); /* abandon replace stack */
7047
7048 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007049 * Save the inserted text for later redo with ^@ and CTRL-A.
7050 * Don't do it when "restart_edit" was set and nothing was inserted,
7051 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007053 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007054 if (did_restart_edit == 0 || (ptr != NULL
7055 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007056 {
7057 vim_free(last_insert);
7058 last_insert = ptr;
7059 last_insert_skip = new_insert_skip;
7060 }
7061 else
7062 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007064 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
7066 /* Auto-format now. It may seem strange to do this when stopping an
7067 * insertion (or moving the cursor), but it's required when appending
7068 * a line and having it end in a space. But only do it when something
7069 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007070 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007072 pos_T tpos = curwin->w_cursor;
7073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 /* When the cursor is at the end of the line after a space the
7075 * formatting will move it to the following word. Avoid that by
7076 * moving the cursor onto the space. */
7077 cc = 'x';
7078 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7079 {
7080 dec_cursor();
7081 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007082 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007083 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085
7086 auto_format(TRUE, FALSE);
7087
Bram Moolenaar1c465442017-03-12 20:10:05 +01007088 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007089 {
7090 if (gchar_cursor() != NUL)
7091 inc_cursor();
7092#ifdef FEAT_VIRTUALEDIT
7093 /* If the cursor is still at the same character, also keep
7094 * the "coladd". */
7095 if (gchar_cursor() == NUL
7096 && curwin->w_cursor.lnum == tpos.lnum
7097 && curwin->w_cursor.col == tpos.col)
7098 curwin->w_cursor.coladd = tpos.coladd;
7099#endif
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102
7103 /* If a space was inserted for auto-formatting, remove it now. */
7104 check_auto_format(TRUE);
7105
7106 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007107 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007108 * Do this when ESC was used or moving the cursor up/down.
7109 * Check for the old position still being valid, just in case the text
7110 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007111 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007112 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7113 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007115 pos_T tpos = curwin->w_cursor;
7116
7117 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007118 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007119 for (;;)
7120 {
7121 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7122 --curwin->w_cursor.col;
7123 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007124 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007125 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007126 if (del_char(TRUE) == FAIL)
7127 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007128 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007129 if (curwin->w_cursor.lnum != tpos.lnum)
7130 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007131 else
7132 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007133 /* reset tpos, could have been invalidated in the loop above */
7134 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007135 tpos.col++;
7136 if (cc != NUL && gchar_pos(&tpos) == NUL)
7137 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 /* <C-S-Right> may have started Visual mode, adjust the position for
7141 * deleted characters. */
7142 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7143 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007144 int len = (int)STRLEN(ml_get_curline());
7145
7146 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007148 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007149#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
7155 }
7156 did_ai = FALSE;
7157#ifdef FEAT_SMARTINDENT
7158 did_si = FALSE;
7159 can_si = FALSE;
7160 can_si_back = FALSE;
7161#endif
7162
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007163 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7164 * now in a different buffer. */
7165 if (end_insert_pos != NULL)
7166 {
7167 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007168 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 curbuf->b_op_end = *end_insert_pos;
7170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171}
7172
7173/*
7174 * Set the last inserted text to a single character.
7175 * Used for the replace command.
7176 */
7177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 char_u *s;
7181
7182 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (last_insert != NULL)
7185 {
7186 s = last_insert;
7187 /* Use the CTRL-V only when entering a special char */
7188 if (c < ' ' || c == DEL)
7189 *s++ = Ctrl_V;
7190 s = add_char2buf(c, s);
7191 *s++ = ESC;
7192 *s++ = NUL;
7193 last_insert_skip = 0;
7194 }
7195}
7196
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007197#if defined(EXITFREE) || defined(PROTO)
7198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007200{
Bram Moolenaar15675582018-02-09 12:29:56 +01007201 vim_clear((void **)&last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007202# ifdef FEAT_INS_EXPAND
Bram Moolenaar15675582018-02-09 12:29:56 +01007203 vim_clear((void **)&compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007204# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007205}
7206#endif
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208/*
7209 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7210 * and CSI. Handle multi-byte characters.
7211 * Returns a pointer to after the added bytes.
7212 */
7213 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007217 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 int i;
7219 int len;
7220
7221 len = (*mb_char2bytes)(c, temp);
7222 for (i = 0; i < len; ++i)
7223 {
7224 c = temp[i];
7225#endif
7226 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7227 if (c == K_SPECIAL)
7228 {
7229 *s++ = K_SPECIAL;
7230 *s++ = KS_SPECIAL;
7231 *s++ = KE_FILLER;
7232 }
7233#ifdef FEAT_GUI
7234 else if (c == CSI)
7235 {
7236 *s++ = CSI;
7237 *s++ = KS_EXTRA;
7238 *s++ = (int)KE_CSI;
7239 }
7240#endif
7241 else
7242 *s++ = c;
7243#ifdef FEAT_MBYTE
7244 }
7245#endif
7246 return s;
7247}
7248
7249/*
7250 * move cursor to start of line
7251 * if flags & BL_WHITE move to first non-white
7252 * if flags & BL_SOL move to first non-white if startofline is set,
7253 * otherwise keep "curswant" column
7254 * if flags & BL_FIX don't leave the cursor on a NUL.
7255 */
7256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007257beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 if ((flags & BL_SOL) && !p_sol)
7260 coladvance(curwin->w_curswant);
7261 else
7262 {
7263 curwin->w_cursor.col = 0;
7264#ifdef FEAT_VIRTUALEDIT
7265 curwin->w_cursor.coladd = 0;
7266#endif
7267
7268 if (flags & (BL_WHITE | BL_SOL))
7269 {
7270 char_u *ptr;
7271
Bram Moolenaar1c465442017-03-12 20:10:05 +01007272 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7274 ++curwin->w_cursor.col;
7275 }
7276 curwin->w_set_curswant = TRUE;
7277 }
7278}
7279
7280/*
7281 * oneright oneleft cursor_down cursor_up
7282 *
7283 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007284 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 * Return OK when successful, FAIL when we hit a line of file boundary.
7286 */
7287
7288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007289oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294#ifdef FEAT_VIRTUALEDIT
7295 if (virtual_active())
7296 {
7297 pos_T prevpos = curwin->w_cursor;
7298
7299 /* Adjust for multi-wide char (excluding TAB) */
7300 ptr = ml_get_cursor();
7301 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007302# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007304# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 ))
7308 ? ptr2cells(ptr) : 1));
7309 curwin->w_set_curswant = TRUE;
7310 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7311 return (prevpos.col != curwin->w_cursor.col
7312 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7313 }
7314#endif
7315
7316 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007317 if (*ptr == NUL)
7318 return FAIL; /* already at the very end */
7319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007321 if (has_mbyte)
7322 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 else
7324#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007325 l = 1;
7326
7327 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7328 * contains "onemore". */
7329 if (ptr[l] == NUL
7330#ifdef FEAT_VIRTUALEDIT
7331 && (ve_flags & VE_ONEMORE) == 0
7332#endif
7333 )
7334 return FAIL;
7335 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337 curwin->w_set_curswant = TRUE;
7338 return OK;
7339}
7340
7341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344#ifdef FEAT_VIRTUALEDIT
7345 if (virtual_active())
7346 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007347# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 int v = getviscol();
7351
7352 if (v == 0)
7353 return FAIL;
7354
7355# ifdef FEAT_LINEBREAK
7356 /* We might get stuck on 'showbreak', skip over it. */
7357 width = 1;
7358 for (;;)
7359 {
7360 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007361 /* getviscol() is slow, skip it when 'showbreak' is empty,
7362 * 'breakindent' is not set and there are no multi-byte
7363 * characters */
7364 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365# ifdef FEAT_MBYTE
7366 && !has_mbyte
7367# endif
7368 ) || getviscol() < v)
7369 break;
7370 ++width;
7371 }
7372# else
7373 coladvance(v - 1);
7374# endif
7375
7376 if (curwin->w_cursor.coladd == 1)
7377 {
7378 char_u *ptr;
7379
7380 /* Adjust for multi-wide char (not a TAB) */
7381 ptr = ml_get_cursor();
7382 if (*ptr != TAB && vim_isprintc(
7383# ifdef FEAT_MBYTE
7384 (*mb_ptr2char)(ptr)
7385# else
7386 *ptr
7387# endif
7388 ) && ptr2cells(ptr) > 1)
7389 curwin->w_cursor.coladd = 0;
7390 }
7391
7392 curwin->w_set_curswant = TRUE;
7393 return OK;
7394 }
7395#endif
7396
7397 if (curwin->w_cursor.col == 0)
7398 return FAIL;
7399
7400 curwin->w_set_curswant = TRUE;
7401 --curwin->w_cursor.col;
7402
7403#ifdef FEAT_MBYTE
7404 /* if the character on the left of the current cursor is a multi-byte
7405 * character, move to its first byte */
7406 if (has_mbyte)
7407 mb_adjust_cursor();
7408#endif
7409 return OK;
7410}
7411
7412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413cursor_up(
7414 long n,
7415 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
7417 linenr_T lnum;
7418
7419 if (n > 0)
7420 {
7421 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007422 /* This fails if the cursor is already in the first line or the count
7423 * is larger than the line number and '-' is in 'cpoptions' */
7424 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 return FAIL;
7426 if (n >= lnum)
7427 lnum = 1;
7428 else
7429#ifdef FEAT_FOLDING
7430 if (hasAnyFolding(curwin))
7431 {
7432 /*
7433 * Count each sequence of folded lines as one logical line.
7434 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007435 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 (void)hasFolding(lnum, &lnum, NULL);
7437
7438 while (n--)
7439 {
7440 /* move up one line */
7441 --lnum;
7442 if (lnum <= 1)
7443 break;
7444 /* If we entered a fold, move to the beginning, unless in
7445 * Insert mode or when 'foldopen' contains "all": it will open
7446 * in a moment. */
7447 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7448 (void)hasFolding(lnum, &lnum, NULL);
7449 }
7450 if (lnum < 1)
7451 lnum = 1;
7452 }
7453 else
7454#endif
7455 lnum -= n;
7456 curwin->w_cursor.lnum = lnum;
7457 }
7458
7459 /* try to advance to the column we want to be at */
7460 coladvance(curwin->w_curswant);
7461
7462 if (upd_topline)
7463 update_topline(); /* make sure curwin->w_topline is valid */
7464
7465 return OK;
7466}
7467
7468/*
7469 * Cursor down a number of logical lines.
7470 */
7471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007472cursor_down(
7473 long n,
7474 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
7476 linenr_T lnum;
7477
7478 if (n > 0)
7479 {
7480 lnum = curwin->w_cursor.lnum;
7481#ifdef FEAT_FOLDING
7482 /* Move to last line of fold, will fail if it's the end-of-file. */
7483 (void)hasFolding(lnum, NULL, &lnum);
7484#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007485 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007486 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007487 if (lnum >= curbuf->b_ml.ml_line_count
7488 || (lnum + n > curbuf->b_ml.ml_line_count
7489 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 return FAIL;
7491 if (lnum + n >= curbuf->b_ml.ml_line_count)
7492 lnum = curbuf->b_ml.ml_line_count;
7493 else
7494#ifdef FEAT_FOLDING
7495 if (hasAnyFolding(curwin))
7496 {
7497 linenr_T last;
7498
7499 /* count each sequence of folded lines as one logical line */
7500 while (n--)
7501 {
7502 if (hasFolding(lnum, NULL, &last))
7503 lnum = last + 1;
7504 else
7505 ++lnum;
7506 if (lnum >= curbuf->b_ml.ml_line_count)
7507 break;
7508 }
7509 if (lnum > curbuf->b_ml.ml_line_count)
7510 lnum = curbuf->b_ml.ml_line_count;
7511 }
7512 else
7513#endif
7514 lnum += n;
7515 curwin->w_cursor.lnum = lnum;
7516 }
7517
7518 /* try to advance to the column we want to be at */
7519 coladvance(curwin->w_curswant);
7520
7521 if (upd_topline)
7522 update_topline(); /* make sure curwin->w_topline is valid */
7523
7524 return OK;
7525}
7526
7527/*
7528 * Stuff the last inserted text in the read buffer.
7529 * Last_insert actually is a copy of the redo buffer, so we
7530 * first have to remove the command.
7531 */
7532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007533stuff_inserted(
7534 int c, /* Command character to be inserted */
7535 long count, /* Repeat this many times */
7536 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 char_u *esc_ptr;
7539 char_u *ptr;
7540 char_u *last_ptr;
7541 char_u last = NUL;
7542
7543 ptr = get_last_insert();
7544 if (ptr == NULL)
7545 {
7546 EMSG(_(e_noinstext));
7547 return FAIL;
7548 }
7549
7550 /* may want to stuff the command character, to start Insert mode */
7551 if (c != NUL)
7552 stuffcharReadbuff(c);
7553 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7554 *esc_ptr = NUL; /* remove the ESC */
7555
7556 /* when the last char is either "0" or "^" it will be quoted if no ESC
7557 * comes after it OR if it will inserted more than once and "ptr"
7558 * starts with ^D. -- Acevedo
7559 */
7560 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7561 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7562 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7563 {
7564 last = *last_ptr;
7565 *last_ptr = NUL;
7566 }
7567
7568 do
7569 {
7570 stuffReadbuff(ptr);
7571 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7572 if (last)
7573 stuffReadbuff((char_u *)(last == '0'
7574 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7575 : IF_EB("\026^", CTRL_V_STR "^")));
7576 }
7577 while (--count > 0);
7578
7579 if (last)
7580 *last_ptr = last;
7581
7582 if (esc_ptr != NULL)
7583 *esc_ptr = ESC; /* put the ESC back */
7584
7585 /* may want to stuff a trailing ESC, to get out of Insert mode */
7586 if (!no_esc)
7587 stuffcharReadbuff(ESC);
7588
7589 return OK;
7590}
7591
7592 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
7595 if (last_insert == NULL)
7596 return NULL;
7597 return last_insert + last_insert_skip;
7598}
7599
7600/*
7601 * Get last inserted string, and remove trailing <Esc>.
7602 * Returns pointer to allocated memory (must be freed) or NULL.
7603 */
7604 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007605get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 char_u *s;
7608 int len;
7609
7610 if (last_insert == NULL)
7611 return NULL;
7612 s = vim_strsave(last_insert + last_insert_skip);
7613 if (s != NULL)
7614 {
7615 len = (int)STRLEN(s);
7616 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7617 s[len - 1] = NUL;
7618 }
7619 return s;
7620}
7621
7622/*
7623 * Check the word in front of the cursor for an abbreviation.
7624 * Called when the non-id character "c" has been entered.
7625 * When an abbreviation is recognized it is removed from the text and
7626 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7627 */
7628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 /* Don't check for abbreviation in paste mode, when disabled and just
7632 * after moving around with cursor keys. */
7633 if (p_paste || no_abbr || arrow_used)
7634 return FALSE;
7635
7636 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7637 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7638}
7639
7640/*
7641 * replace-stack functions
7642 *
7643 * When replacing characters, the replaced characters are remembered for each
7644 * new character. This is used to re-insert the old text when backspacing.
7645 *
7646 * There is a NUL headed list of characters for each character that is
7647 * currently in the file after the insertion point. When BS is used, one NUL
7648 * headed list is put back for the deleted character.
7649 *
7650 * For a newline, there are two NUL headed lists. One contains the characters
7651 * that the NL replaced. The extra one stores the characters after the cursor
7652 * that were deleted (always white space).
7653 *
7654 * Replace_offset is normally 0, in which case replace_push will add a new
7655 * character at the end of the stack. If replace_offset is not 0, that many
7656 * characters will be left on the stack above the newly inserted character.
7657 */
7658
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007659static char_u *replace_stack = NULL;
7660static long replace_stack_nr = 0; /* next entry in replace stack */
7661static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
7663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007664replace_push(
7665 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 char_u *p;
7668
7669 if (replace_stack_nr < replace_offset) /* nothing to do */
7670 return;
7671 if (replace_stack_len <= replace_stack_nr)
7672 {
7673 replace_stack_len += 50;
7674 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7675 if (p == NULL) /* out of memory */
7676 {
7677 replace_stack_len -= 50;
7678 return;
7679 }
7680 if (replace_stack != NULL)
7681 {
7682 mch_memmove(p, replace_stack,
7683 (size_t)(replace_stack_nr * sizeof(char_u)));
7684 vim_free(replace_stack);
7685 }
7686 replace_stack = p;
7687 }
7688 p = replace_stack + replace_stack_nr - replace_offset;
7689 if (replace_offset)
7690 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7691 *p = c;
7692 ++replace_stack_nr;
7693}
7694
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007695#if defined(FEAT_MBYTE) || defined(PROTO)
7696/*
7697 * Push a character onto the replace stack. Handles a multi-byte character in
7698 * reverse byte order, so that the first byte is popped off first.
7699 * Return the number of bytes done (includes composing characters).
7700 */
7701 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007703{
7704 int l = (*mb_ptr2len)(p);
7705 int j;
7706
7707 for (j = l - 1; j >= 0; --j)
7708 replace_push(p[j]);
7709 return l;
7710}
7711#endif
7712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713/*
7714 * Pop one item from the replace stack.
7715 * return -1 if stack empty
7716 * return replaced character or NUL otherwise
7717 */
7718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007719replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 if (replace_stack_nr == 0)
7722 return -1;
7723 return (int)replace_stack[--replace_stack_nr];
7724}
7725
7726/*
7727 * Join the top two items on the replace stack. This removes to "off"'th NUL
7728 * encountered.
7729 */
7730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731replace_join(
7732 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 int i;
7735
7736 for (i = replace_stack_nr; --i >= 0; )
7737 if (replace_stack[i] == NUL && off-- <= 0)
7738 {
7739 --replace_stack_nr;
7740 mch_memmove(replace_stack + i, replace_stack + i + 1,
7741 (size_t)(replace_stack_nr - i));
7742 return;
7743 }
7744}
7745
7746/*
7747 * Pop bytes from the replace stack until a NUL is found, and insert them
7748 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7749 */
7750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007751replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 int cc;
7754 int oldState = State;
7755
7756 State = NORMAL; /* don't want REPLACE here */
7757 while ((cc = replace_pop()) > 0)
7758 {
7759#ifdef FEAT_MBYTE
7760 mb_replace_pop_ins(cc);
7761#else
7762 ins_char(cc);
7763#endif
7764 dec_cursor();
7765 }
7766 State = oldState;
7767}
7768
7769#ifdef FEAT_MBYTE
7770/*
7771 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7772 * indicates a multi-byte char, pop the other bytes too.
7773 */
7774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007775mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007778 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 int i;
7780 int c;
7781
7782 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7783 {
7784 buf[0] = cc;
7785 for (i = 1; i < n; ++i)
7786 buf[i] = replace_pop();
7787 ins_bytes_len(buf, n);
7788 }
7789 else
7790 ins_char(cc);
7791
7792 if (enc_utf8)
7793 /* Handle composing chars. */
7794 for (;;)
7795 {
7796 c = replace_pop();
7797 if (c == -1) /* stack empty */
7798 break;
7799 if ((n = MB_BYTE2LEN(c)) == 1)
7800 {
7801 /* Not a multi-byte char, put it back. */
7802 replace_push(c);
7803 break;
7804 }
7805 else
7806 {
7807 buf[0] = c;
7808 for (i = 1; i < n; ++i)
7809 buf[i] = replace_pop();
7810 if (utf_iscomposing(utf_ptr2char(buf)))
7811 ins_bytes_len(buf, n);
7812 else
7813 {
7814 /* Not a composing char, put it back. */
7815 for (i = n - 1; i >= 0; --i)
7816 replace_push(buf[i]);
7817 break;
7818 }
7819 }
7820 }
7821}
7822#endif
7823
7824/*
7825 * make the replace stack empty
7826 * (called when exiting replace mode)
7827 */
7828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
Bram Moolenaar15675582018-02-09 12:29:56 +01007831 vim_clear((void **)&replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 replace_stack_len = 0;
7833 replace_stack_nr = 0;
7834}
7835
7836/*
7837 * Handle doing a BS for one character.
7838 * cc < 0: replace stack empty, just move cursor
7839 * cc == 0: character was inserted, delete it
7840 * cc > 0: character was replaced, put cc (first byte of original char) back
7841 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007842 * When "limit_col" is >= 0, don't delete before this column. Matters when
7843 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 */
7845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007846replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 int cc;
7849#ifdef FEAT_VREPLACE
7850 int orig_len = 0;
7851 int ins_len;
7852 int orig_vcols = 0;
7853 colnr_T start_vcol;
7854 char_u *p;
7855 int i;
7856 int vcol;
7857#endif
7858
7859 cc = replace_pop();
7860 if (cc > 0)
7861 {
7862#ifdef FEAT_VREPLACE
7863 if (State & VREPLACE_FLAG)
7864 {
7865 /* Get the number of screen cells used by the character we are
7866 * going to delete. */
7867 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7868 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7869 }
7870#endif
7871#ifdef FEAT_MBYTE
7872 if (has_mbyte)
7873 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007874 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875# ifdef FEAT_VREPLACE
7876 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007877 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878# endif
7879 replace_push(cc);
7880 }
7881 else
7882#endif
7883 {
7884 pchar_cursor(cc);
7885#ifdef FEAT_VREPLACE
7886 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007887 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#endif
7889 }
7890 replace_pop_ins();
7891
7892#ifdef FEAT_VREPLACE
7893 if (State & VREPLACE_FLAG)
7894 {
7895 /* Get the number of screen cells used by the inserted characters */
7896 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007897 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 vcol = start_vcol;
7899 for (i = 0; i < ins_len; ++i)
7900 {
7901 vcol += chartabsize(p + i, vcol);
7902#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007903 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#endif
7905 }
7906 vcol -= start_vcol;
7907
7908 /* Delete spaces that were inserted after the cursor to keep the
7909 * text aligned. */
7910 curwin->w_cursor.col += ins_len;
7911 while (vcol > orig_vcols && gchar_cursor() == ' ')
7912 {
7913 del_char(FALSE);
7914 ++orig_vcols;
7915 }
7916 curwin->w_cursor.col -= ins_len;
7917 }
7918#endif
7919
7920 /* mark the buffer as changed and prepare for displaying */
7921 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7922 }
7923 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007924 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
7926
7927#ifdef FEAT_CINDENT
7928/*
7929 * Return TRUE if C-indenting is on.
7930 */
7931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007932cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 return (!p_paste && (curbuf->b_p_cin
7935# ifdef FEAT_EVAL
7936 || *curbuf->b_p_inde != NUL
7937# endif
7938 ));
7939}
7940#endif
7941
7942#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7943/*
7944 * Re-indent the current line, based on the current contents of it and the
7945 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7946 * confused what all the part that handles Control-T is doing that I'm not.
7947 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7948 */
7949
7950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007951fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007953 int amount = get_the_indent();
7954
7955 if (amount >= 0)
7956 {
7957 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7958 if (linewhite(curwin->w_cursor.lnum))
7959 did_ai = TRUE; /* delete the indent if the line stays empty */
7960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961}
7962
7963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007964fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 if (p_paste)
7967 return;
7968# ifdef FEAT_LISP
7969 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7970 fixthisline(get_lisp_indent);
7971# endif
7972# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7973 else
7974# endif
7975# ifdef FEAT_CINDENT
7976 if (cindent_on())
7977 do_c_expr_indent();
7978# endif
7979}
7980
7981#endif
7982
7983#ifdef FEAT_CINDENT
7984/*
7985 * return TRUE if 'cinkeys' contains the key "keytyped",
7986 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007987 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7989 *
7990 * "keytyped" can have a few special values:
7991 * KEY_OPEN_FORW
7992 * KEY_OPEN_BACK
7993 * KEY_COMPLETE just finished completion.
7994 *
7995 * If line_is_empty is TRUE accept keys with '0' before them.
7996 */
7997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007998in_cinkeys(
7999 int keytyped,
8000 int when,
8001 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003 char_u *look;
8004 int try_match;
8005 int try_match_word;
8006 char_u *p;
8007 char_u *line;
8008 int icase;
8009 int i;
8010
Bram Moolenaar30848942009-12-24 14:46:12 +00008011 if (keytyped == NUL)
8012 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8013 return FALSE;
8014
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015#ifdef FEAT_EVAL
8016 if (*curbuf->b_p_inde != NUL)
8017 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8018 else
8019#endif
8020 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8021 while (*look)
8022 {
8023 /*
8024 * Find out if we want to try a match with this key, depending on
8025 * 'when' and a '*' or '!' before the key.
8026 */
8027 switch (when)
8028 {
8029 case '*': try_match = (*look == '*'); break;
8030 case '!': try_match = (*look == '!'); break;
8031 default: try_match = (*look != '*'); break;
8032 }
8033 if (*look == '*' || *look == '!')
8034 ++look;
8035
8036 /*
8037 * If there is a '0', only accept a match if the line is empty.
8038 * But may still match when typing last char of a word.
8039 */
8040 if (*look == '0')
8041 {
8042 try_match_word = try_match;
8043 if (!line_is_empty)
8044 try_match = FALSE;
8045 ++look;
8046 }
8047 else
8048 try_match_word = FALSE;
8049
8050 /*
8051 * does it look like a control character?
8052 */
8053 if (*look == '^'
8054#ifdef EBCDIC
8055 && (Ctrl_chr(look[1]) != 0)
8056#else
8057 && look[1] >= '?' && look[1] <= '_'
8058#endif
8059 )
8060 {
8061 if (try_match && keytyped == Ctrl_chr(look[1]))
8062 return TRUE;
8063 look += 2;
8064 }
8065 /*
8066 * 'o' means "o" command, open forward.
8067 * 'O' means "O" command, open backward.
8068 */
8069 else if (*look == 'o')
8070 {
8071 if (try_match && keytyped == KEY_OPEN_FORW)
8072 return TRUE;
8073 ++look;
8074 }
8075 else if (*look == 'O')
8076 {
8077 if (try_match && keytyped == KEY_OPEN_BACK)
8078 return TRUE;
8079 ++look;
8080 }
8081
8082 /*
8083 * 'e' means to check for "else" at start of line and just before the
8084 * cursor.
8085 */
8086 else if (*look == 'e')
8087 {
8088 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8089 {
8090 p = ml_get_curline();
8091 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8092 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8093 return TRUE;
8094 }
8095 ++look;
8096 }
8097
8098 /*
8099 * ':' only causes an indent if it is at the end of a label or case
8100 * statement, or when it was before typing the ':' (to fix
8101 * class::method for C++).
8102 */
8103 else if (*look == ':')
8104 {
8105 if (try_match && keytyped == ':')
8106 {
8107 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008108 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008110 /* Need to get the line again after cin_islabel(). */
8111 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (curwin->w_cursor.col > 2
8113 && p[curwin->w_cursor.col - 1] == ':'
8114 && p[curwin->w_cursor.col - 2] == ':')
8115 {
8116 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008117 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008118 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 p = ml_get_curline();
8120 p[curwin->w_cursor.col - 1] = ':';
8121 if (i)
8122 return TRUE;
8123 }
8124 }
8125 ++look;
8126 }
8127
8128
8129 /*
8130 * Is it a key in <>, maybe?
8131 */
8132 else if (*look == '<')
8133 {
8134 if (try_match)
8135 {
8136 /*
8137 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8138 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8139 * >, *, : and ! keys if they really really want to.
8140 */
8141 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8142 && keytyped == look[1])
8143 return TRUE;
8144
8145 if (keytyped == get_special_key_code(look + 1))
8146 return TRUE;
8147 }
8148 while (*look && *look != '>')
8149 look++;
8150 while (*look == '>')
8151 look++;
8152 }
8153
8154 /*
8155 * Is it a word: "=word"?
8156 */
8157 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8158 {
8159 ++look;
8160 if (*look == '~')
8161 {
8162 icase = TRUE;
8163 ++look;
8164 }
8165 else
8166 icase = FALSE;
8167 p = vim_strchr(look, ',');
8168 if (p == NULL)
8169 p = look + STRLEN(look);
8170 if ((try_match || try_match_word)
8171 && curwin->w_cursor.col >= (colnr_T)(p - look))
8172 {
8173 int match = FALSE;
8174
8175#ifdef FEAT_INS_EXPAND
8176 if (keytyped == KEY_COMPLETE)
8177 {
8178 char_u *s;
8179
8180 /* Just completed a word, check if it starts with "look".
8181 * search back for the start of a word. */
8182 line = ml_get_curline();
8183# ifdef FEAT_MBYTE
8184 if (has_mbyte)
8185 {
8186 char_u *n;
8187
8188 for (s = line + curwin->w_cursor.col; s > line; s = n)
8189 {
8190 n = mb_prevptr(line, s);
8191 if (!vim_iswordp(n))
8192 break;
8193 }
8194 }
8195 else
8196# endif
8197 for (s = line + curwin->w_cursor.col; s > line; --s)
8198 if (!vim_iswordc(s[-1]))
8199 break;
8200 if (s + (p - look) <= line + curwin->w_cursor.col
8201 && (icase
8202 ? MB_STRNICMP(s, look, p - look)
8203 : STRNCMP(s, look, p - look)) == 0)
8204 match = TRUE;
8205 }
8206 else
8207#endif
8208 /* TODO: multi-byte */
8209 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8210 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8211 {
8212 line = ml_get_cursor();
8213 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8214 || !vim_iswordc(line[-(p - look) - 1]))
8215 && (icase
8216 ? MB_STRNICMP(line - (p - look), look, p - look)
8217 : STRNCMP(line - (p - look), look, p - look))
8218 == 0)
8219 match = TRUE;
8220 }
8221 if (match && try_match_word && !try_match)
8222 {
8223 /* "0=word": Check if there are only blanks before the
8224 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008225 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 (int)(curwin->w_cursor.col - (p - look)))
8227 match = FALSE;
8228 }
8229 if (match)
8230 return TRUE;
8231 }
8232 look = p;
8233 }
8234
8235 /*
8236 * ok, it's a boring generic character.
8237 */
8238 else
8239 {
8240 if (try_match && *look == keytyped)
8241 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008242 if (*look != NUL)
8243 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 }
8245
8246 /*
8247 * Skip over ", ".
8248 */
8249 look = skip_to_option_part(look);
8250 }
8251 return FALSE;
8252}
8253#endif /* FEAT_CINDENT */
8254
8255#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8256/*
8257 * Map Hebrew keyboard when in hkmap mode.
8258 */
8259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008260hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8263 {
8264 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8265 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8266 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8267 static char_u map[26] =
8268 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8269 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8270 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8271 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8272 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8273 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8274 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8275 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8276 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8277
8278 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8279 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8280 /* '-1'='sofit' */
8281 else if (c == 'x')
8282 return 'X';
8283 else if (c == 'q')
8284 return '\''; /* {geresh}={'} */
8285 else if (c == 246)
8286 return ' '; /* \"o --> ' ' for a german keyboard */
8287 else if (c == 228)
8288 return ' '; /* \"a --> ' ' -- / -- */
8289 else if (c == 252)
8290 return ' '; /* \"u --> ' ' -- / -- */
8291#ifdef EBCDIC
8292 else if (islower(c))
8293#else
8294 /* NOTE: islower() does not do the right thing for us on Linux so we
8295 * do this the same was as 5.7 and previous, so it works correctly on
8296 * all systems. Specifically, the e.g. Delete and Arrow keys are
8297 * munged and won't work if e.g. searching for Hebrew text.
8298 */
8299 else if (c >= 'a' && c <= 'z')
8300#endif
8301 return (int)(map[CharOrdLow(c)] + p_aleph);
8302 else
8303 return c;
8304 }
8305 else
8306 {
8307 switch (c)
8308 {
8309 case '`': return ';';
8310 case '/': return '.';
8311 case '\'': return ',';
8312 case 'q': return '/';
8313 case 'w': return '\'';
8314
8315 /* Hebrew letters - set offset from 'a' */
8316 case ',': c = '{'; break;
8317 case '.': c = 'v'; break;
8318 case ';': c = 't'; break;
8319 default: {
8320 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8321
8322#ifdef EBCDIC
8323 /* see note about islower() above */
8324 if (!islower(c))
8325#else
8326 if (c < 'a' || c > 'z')
8327#endif
8328 return c;
8329 c = str[CharOrdLow(c)];
8330 break;
8331 }
8332 }
8333
8334 return (int)(CharOrdLow(c) + p_aleph);
8335 }
8336}
8337#endif
8338
8339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008340ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341{
8342 int need_redraw = FALSE;
8343 int regname;
8344 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008345 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
8347 /*
8348 * If we are going to wait for a character, show a '"'.
8349 */
8350 pc_status = PC_STATUS_UNSET;
8351 if (redrawing() && !char_avail())
8352 {
8353 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008354 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356 edit_putchar('"', TRUE);
8357#ifdef FEAT_CMDL_INFO
8358 add_to_showcmd_c(Ctrl_R);
8359#endif
8360 }
8361
8362#ifdef USE_ON_FLY_SCROLL
8363 dont_scroll = TRUE; /* disallow scrolling here */
8364#endif
8365
8366 /*
8367 * Don't map the register name. This also prevents the mode message to be
8368 * deleted when ESC is hit.
8369 */
8370 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008371 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8374 {
8375 /* Get a third key for literal register insertion */
8376 literally = regname;
8377#ifdef FEAT_CMDL_INFO
8378 add_to_showcmd_c(literally);
8379#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008380 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 --no_mapping;
8384
8385#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008386 /* Don't call u_sync() while typing the expression or giving an error
8387 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 ++no_u_sync;
8389 if (regname == '=')
8390 {
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008391# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008393# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008394 /* Sync undo when evaluating the expression calls setline() or
8395 * append(), so that it can be undone separately. */
8396 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008397
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 regname = get_expr_register();
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008399# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 /* Restore the Input Method. */
8401 if (im_on)
8402 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008405 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8406 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008407 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 else
8411 {
8412#endif
8413 if (literally == Ctrl_O || literally == Ctrl_P)
8414 {
8415 /* Append the command to the redo buffer. */
8416 AppendCharToRedobuff(Ctrl_R);
8417 AppendCharToRedobuff(literally);
8418 AppendCharToRedobuff(regname);
8419
8420 do_put(regname, BACKWARD, 1L,
8421 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8422 }
8423 else if (insert_reg(regname, literally) == FAIL)
8424 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008425 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 need_redraw = TRUE; /* remove the '"' */
8427 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008428 else if (stop_insert_mode)
8429 /* When the '=' register was used and a function was invoked that
8430 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8431 * insert anything, need to remove the '"' */
8432 need_redraw = TRUE;
8433
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434#ifdef FEAT_EVAL
8435 }
8436 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008437 if (u_sync_once == 1)
8438 ins_need_undo = TRUE;
8439 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#endif
8441#ifdef FEAT_CMDL_INFO
8442 clear_showcmd();
8443#endif
8444
8445 /* If the inserted register is empty, we need to remove the '"' */
8446 if (need_redraw || stuff_empty())
8447 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008448
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008449 /* Disallow starting Visual mode here, would get a weird mode. */
8450 if (!vis_active && VIsual_active)
8451 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452}
8453
8454/*
8455 * CTRL-G commands in Insert mode.
8456 */
8457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008458ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
8460 int c;
8461
8462#ifdef FEAT_INS_EXPAND
8463 /* Right after CTRL-X the cursor will be after the ruler. */
8464 setcursor();
8465#endif
8466
8467 /*
8468 * Don't map the second key. This also prevents the mode message to be
8469 * deleted when ESC is hit.
8470 */
8471 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008472 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 --no_mapping;
8474 switch (c)
8475 {
8476 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8477 case K_UP:
8478 case Ctrl_K:
8479 case 'k': ins_up(TRUE);
8480 break;
8481
8482 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8483 case K_DOWN:
8484 case Ctrl_J:
8485 case 'j': ins_down(TRUE);
8486 break;
8487
8488 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008489 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008491
8492 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008493 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008494 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008495 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 break;
8497
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008498 /* CTRL-G U: do not break undo with the next char */
8499 case 'U':
8500 /* Allow one left/right cursor movement with the next char,
8501 * without breaking undo. */
8502 dont_sync_undo = MAYBE;
8503 break;
8504
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008506 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508}
8509
8510/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008511 * CTRL-^ in Insert mode.
8512 */
8513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008514ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008515{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008516 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008517 {
8518 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8519 if (State & LANGMAP)
8520 {
8521 curbuf->b_p_iminsert = B_IMODE_NONE;
8522 State &= ~LANGMAP;
8523 }
8524 else
8525 {
8526 curbuf->b_p_iminsert = B_IMODE_LMAP;
8527 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008528#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008529 im_set_active(FALSE);
8530#endif
8531 }
8532 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008533#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008534 else
8535 {
8536 /* There are no ":lmap" mappings, toggle IM */
8537 if (im_get_status())
8538 {
8539 curbuf->b_p_iminsert = B_IMODE_NONE;
8540 im_set_active(FALSE);
8541 }
8542 else
8543 {
8544 curbuf->b_p_iminsert = B_IMODE_IM;
8545 State &= ~LANGMAP;
8546 im_set_active(TRUE);
8547 }
8548 }
8549#endif
8550 set_iminsert_global();
8551 showmode();
8552#ifdef FEAT_GUI
8553 /* may show different cursor shape or color */
8554 if (gui.in_use)
8555 gui_update_cursor(TRUE, FALSE);
8556#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008557#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008558 /* Show/unshow value of 'keymap' in status lines. */
8559 status_redraw_curbuf();
8560#endif
8561}
8562
8563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 * Handle ESC in insert mode.
8565 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8566 * insert.
8567 */
8568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008569ins_esc(
8570 long *count,
8571 int cmdchar,
8572 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 int temp;
8575 static int disabled_redraw = FALSE;
8576
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008577#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008578 check_spell_redraw();
8579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#if defined(FEAT_HANGULIN)
8581# if defined(ESC_CHG_TO_ENG_MODE)
8582 hangul_input_state_set(0);
8583# endif
8584 if (composing_hangul)
8585 {
8586 push_raw_key(composing_hangul_buffer, 2);
8587 composing_hangul = 0;
8588 }
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
8591 temp = curwin->w_cursor.col;
8592 if (disabled_redraw)
8593 {
8594 --RedrawingDisabled;
8595 disabled_redraw = FALSE;
8596 }
8597 if (!arrow_used)
8598 {
8599 /*
8600 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008601 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8602 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 */
8604 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008605 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
8607 /*
8608 * Repeating insert may take a long time. Check for
8609 * interrupt now and then.
8610 */
8611 if (*count > 0)
8612 {
8613 line_breakcheck();
8614 if (got_int)
8615 *count = 0;
8616 }
8617
8618 if (--*count > 0) /* repeat what was typed */
8619 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008620 /* Vi repeats the insert without replacing characters. */
8621 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8622 State &= ~REPLACE_FLAG;
8623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 (void)start_redo_ins();
8625 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008626 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 ++RedrawingDisabled;
8628 disabled_redraw = TRUE;
8629 return FALSE; /* repeat the insert */
8630 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008631 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 undisplay_dollar();
8633 }
8634
8635 /* When an autoindent was removed, curswant stays after the
8636 * indent */
8637 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8638 curwin->w_set_curswant = TRUE;
8639
8640 /* Remember the last Insert position in the '^ mark. */
8641 if (!cmdmod.keepjumps)
8642 curbuf->b_last_insert = curwin->w_cursor;
8643
8644 /*
8645 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008646 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008648 if (!nomove
8649 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#ifdef FEAT_VIRTUALEDIT
8651 || curwin->w_cursor.coladd > 0
8652#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008653 )
8654 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008655 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656#ifdef FEAT_RIGHTLEFT
8657 && !revins_on
8658#endif
8659 )
8660 {
8661#ifdef FEAT_VIRTUALEDIT
8662 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8663 {
8664 oneleft();
8665 if (restart_edit != NUL)
8666 ++curwin->w_cursor.coladd;
8667 }
8668 else
8669#endif
8670 {
8671 --curwin->w_cursor.col;
8672#ifdef FEAT_MBYTE
8673 /* Correct cursor for multi-byte character. */
8674 if (has_mbyte)
8675 mb_adjust_cursor();
8676#endif
8677 }
8678 }
8679
Bram Moolenaar819edbe2017-11-25 17:14:33 +01008680#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 /* Disable IM to allow typing English directly for Normal mode commands.
8682 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8683 * well). */
8684 if (!(State & LANGMAP))
8685 im_save_status(&curbuf->b_p_iminsert);
8686 im_set_active(FALSE);
8687#endif
8688
8689 State = NORMAL;
8690 /* need to position cursor again (e.g. when on a TAB ) */
8691 changed_cline_bef_curs();
8692
8693#ifdef FEAT_MOUSE
8694 setmouse();
8695#endif
8696#ifdef CURSOR_SHAPE
8697 ui_cursor_shape(); /* may show different cursor shape */
8698#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008699 if (!p_ek)
8700 /* Re-enable bracketed paste mode. */
8701 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703 /*
8704 * When recording or for CTRL-O, need to display the new mode.
8705 * Otherwise remove the mode message.
8706 */
8707 if (Recording || restart_edit != NUL)
8708 showmode();
8709 else if (p_smd)
8710 MSG("");
8711
8712 return TRUE; /* exit Insert mode */
8713}
8714
8715#ifdef FEAT_RIGHTLEFT
8716/*
8717 * Toggle language: hkmap and revins_on.
8718 * Move to end of reverse inserted text.
8719 */
8720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008721ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723 if (revins_on && revins_chars && revins_scol >= 0)
8724 {
8725 while (gchar_cursor() != NUL && revins_chars--)
8726 ++curwin->w_cursor.col;
8727 }
8728 p_ri = !p_ri;
8729 revins_on = (State == INSERT && p_ri);
8730 if (revins_on)
8731 {
8732 revins_scol = curwin->w_cursor.col;
8733 revins_legal++;
8734 revins_chars = 0;
8735 undisplay_dollar();
8736 }
8737 else
8738 revins_scol = -1;
8739#ifdef FEAT_FKMAP
8740 if (p_altkeymap)
8741 {
8742 /*
8743 * to be consistent also for redo command, using '.'
8744 * set arrow_used to true and stop it - causing to redo
8745 * characters entered in one mode (normal/reverse insert).
8746 */
8747 arrow_used = TRUE;
8748 (void)stop_arrow();
8749 p_fkmap = curwin->w_p_rl ^ p_ri;
8750 if (p_fkmap && p_ri)
8751 State = INSERT;
8752 }
8753 else
8754#endif
8755 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8756 showmode();
8757}
8758#endif
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760/*
8761 * If 'keymodel' contains "startsel", may start selection.
8762 * Returns TRUE when a CTRL-O and other keys stuffed.
8763 */
8764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008765ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 if (km_startsel)
8768 switch (c)
8769 {
8770 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 case K_PAGEUP:
8773 case K_KPAGEUP:
8774 case K_PAGEDOWN:
8775 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008776# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 case K_LEFT:
8778 case K_RIGHT:
8779 case K_UP:
8780 case K_DOWN:
8781 case K_END:
8782 case K_HOME:
8783# endif
8784 if (!(mod_mask & MOD_MASK_SHIFT))
8785 break;
8786 /* FALLTHROUGH */
8787 case K_S_LEFT:
8788 case K_S_RIGHT:
8789 case K_S_UP:
8790 case K_S_DOWN:
8791 case K_S_END:
8792 case K_S_HOME:
8793 /* Start selection right away, the cursor can move with
8794 * CTRL-O when beyond the end of the line. */
8795 start_selection();
8796
8797 /* Execute the key in (insert) Select mode. */
8798 stuffcharReadbuff(Ctrl_O);
8799 if (mod_mask)
8800 {
8801 char_u buf[4];
8802
8803 buf[0] = K_SPECIAL;
8804 buf[1] = KS_MODIFIER;
8805 buf[2] = mod_mask;
8806 buf[3] = NUL;
8807 stuffReadbuff(buf);
8808 }
8809 stuffcharReadbuff(c);
8810 return TRUE;
8811 }
8812 return FALSE;
8813}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
8815/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008816 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008817 */
8818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008820{
8821#ifdef FEAT_FKMAP
8822 if (p_fkmap && p_ri)
8823 {
8824 beep_flush();
8825 EMSG(farsi_text_3); /* encoded in Farsi */
8826 return;
8827 }
8828#endif
8829
8830#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008831# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008832 set_vim_var_string(VV_INSERTMODE,
8833 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008834# ifdef FEAT_VREPLACE
8835 replaceState == VREPLACE ? "v" :
8836# endif
8837 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008838# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008839 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8840#endif
8841 if (State & REPLACE_FLAG)
8842 State = INSERT | (State & LANGMAP);
8843 else
8844 State = replaceState | (State & LANGMAP);
8845 AppendCharToRedobuff(K_INS);
8846 showmode();
8847#ifdef CURSOR_SHAPE
8848 ui_cursor_shape(); /* may show different cursor shape */
8849#endif
8850}
8851
8852/*
8853 * Pressed CTRL-O in Insert mode.
8854 */
8855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008857{
8858#ifdef FEAT_VREPLACE
8859 if (State & VREPLACE_FLAG)
8860 restart_edit = 'V';
8861 else
8862#endif
8863 if (State & REPLACE_FLAG)
8864 restart_edit = 'R';
8865 else
8866 restart_edit = 'I';
8867#ifdef FEAT_VIRTUALEDIT
8868 if (virtual_active())
8869 ins_at_eol = FALSE; /* cursor always keeps its column */
8870 else
8871#endif
8872 ins_at_eol = (gchar_cursor() == NUL);
8873}
8874
8875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 * If the cursor is on an indent, ^T/^D insert/delete one
8877 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008878 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 * with vi. But vi only supports ^T and ^D after an
8880 * autoindent, we support it everywhere.
8881 */
8882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008883ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885 if (stop_arrow() == FAIL)
8886 return;
8887 AppendCharToRedobuff(c);
8888
8889 /*
8890 * 0^D and ^^D: remove all indent.
8891 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008892 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8893 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
8895 --curwin->w_cursor.col;
8896 (void)del_char(FALSE); /* delete the '^' or '0' */
8897 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8898 if (State & REPLACE_FLAG)
8899 replace_pop_ins();
8900 if (lastc == '^')
8901 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008902 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
8904 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008905 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
8907 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8908 did_ai = FALSE;
8909#ifdef FEAT_SMARTINDENT
8910 did_si = FALSE;
8911 can_si = FALSE;
8912 can_si_back = FALSE;
8913#endif
8914#ifdef FEAT_CINDENT
8915 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8916#endif
8917}
8918
8919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008920ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 int temp;
8923
8924 if (stop_arrow() == FAIL)
8925 return;
8926 if (gchar_cursor() == NUL) /* delete newline */
8927 {
8928 temp = curwin->w_cursor.col;
8929 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008930 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008931 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 else
8933 curwin->w_cursor.col = temp;
8934 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008935 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8936 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 did_ai = FALSE;
8938#ifdef FEAT_SMARTINDENT
8939 did_si = FALSE;
8940 can_si = FALSE;
8941 can_si_back = FALSE;
8942#endif
8943 AppendCharToRedobuff(K_DEL);
8944}
8945
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008946static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008947
8948/*
8949 * Delete one character for ins_bs().
8950 */
8951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008952ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008953{
8954 dec_cursor();
8955 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8956 if (State & REPLACE_FLAG)
8957 {
8958 /* Don't delete characters before the insert point when in
8959 * Replace mode */
8960 if (curwin->w_cursor.lnum != Insstart.lnum
8961 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008962 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008963 }
8964 else
8965 (void)del_char(FALSE);
8966}
8967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968/*
8969 * Handle Backspace, delete-word and delete-line in Insert mode.
8970 * Return TRUE when backspace was actually used.
8971 */
8972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973ins_bs(
8974 int c,
8975 int mode,
8976 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 linenr_T lnum;
8979 int cc;
8980 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008981 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 colnr_T mincol;
8983 int did_backspace = FALSE;
8984 int in_indent;
8985 int oldState;
8986#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008987 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#endif
8989
8990 /*
8991 * can't delete anything in an empty file
8992 * can't backup past first character in buffer
8993 * can't backup past starting point unless 'backspace' > 1
8994 * can backup to a previous line if 'backspace' == 0
8995 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008996 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 || (
8998#ifdef FEAT_RIGHTLEFT
8999 !revins_on &&
9000#endif
9001 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9002 || (!can_bs(BS_START)
9003 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009004 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9005 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9007 && curwin->w_cursor.col <= ai_col)
9008 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9009 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009010 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 return FALSE;
9012 }
9013
9014 if (stop_arrow() == FAIL)
9015 return FALSE;
9016 in_indent = inindent(0);
9017#ifdef FEAT_CINDENT
9018 if (in_indent)
9019 can_cindent = FALSE;
9020#endif
9021#ifdef FEAT_COMMENTS
9022 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9023#endif
9024#ifdef FEAT_RIGHTLEFT
9025 if (revins_on) /* put cursor after last inserted char */
9026 inc_cursor();
9027#endif
9028
9029#ifdef FEAT_VIRTUALEDIT
9030 /* Virtualedit:
9031 * BACKSPACE_CHAR eats a virtual space
9032 * BACKSPACE_WORD eats all coladd
9033 * BACKSPACE_LINE eats all coladd and keeps going
9034 */
9035 if (curwin->w_cursor.coladd > 0)
9036 {
9037 if (mode == BACKSPACE_CHAR)
9038 {
9039 --curwin->w_cursor.coladd;
9040 return TRUE;
9041 }
9042 if (mode == BACKSPACE_WORD)
9043 {
9044 curwin->w_cursor.coladd = 0;
9045 return TRUE;
9046 }
9047 curwin->w_cursor.coladd = 0;
9048 }
9049#endif
9050
9051 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009052 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 */
9054 if (curwin->w_cursor.col == 0)
9055 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009056 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009057 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058#ifdef FEAT_RIGHTLEFT
9059 || revins_on
9060#endif
9061 )
9062 {
9063 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9064 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9065 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009066 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009067 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 }
9069 /*
9070 * In replace mode:
9071 * cc < 0: NL was inserted, delete it
9072 * cc >= 0: NL was replaced, put original characters back
9073 */
9074 cc = -1;
9075 if (State & REPLACE_FLAG)
9076 cc = replace_pop(); /* returns -1 if NL was inserted */
9077 /*
9078 * In replace mode, in the line we started replacing, we only move the
9079 * cursor.
9080 */
9081 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9082 {
9083 dec_cursor();
9084 }
9085 else
9086 {
9087#ifdef FEAT_VREPLACE
9088 if (!(State & VREPLACE_FLAG)
9089 || curwin->w_cursor.lnum > orig_line_count)
9090#endif
9091 {
9092 temp = gchar_cursor(); /* remember current char */
9093 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009094
9095 /* When "aw" is in 'formatoptions' we must delete the space at
9096 * the end of the line, otherwise the line will be broken
9097 * again when auto-formatting. */
9098 if (has_format_option(FO_AUTO)
9099 && has_format_option(FO_WHITE_PAR))
9100 {
9101 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9102 TRUE);
9103 int len;
9104
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009105 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009106 if (len > 0 && ptr[len - 1] == ' ')
9107 ptr[len - 1] = NUL;
9108 }
9109
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009110 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 if (temp == NUL && gchar_cursor() != NUL)
9112 inc_cursor();
9113 }
9114#ifdef FEAT_VREPLACE
9115 else
9116 dec_cursor();
9117#endif
9118
9119 /*
9120 * In REPLACE mode we have to put back the text that was replaced
9121 * by the NL. On the replace stack is first a NUL-terminated
9122 * sequence of characters that were deleted and then the
9123 * characters that NL replaced.
9124 */
9125 if (State & REPLACE_FLAG)
9126 {
9127 /*
9128 * Do the next ins_char() in NORMAL state, to
9129 * prevent ins_char() from replacing characters and
9130 * avoiding showmatch().
9131 */
9132 oldState = State;
9133 State = NORMAL;
9134 /*
9135 * restore characters (blanks) deleted after cursor
9136 */
9137 while (cc > 0)
9138 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009139 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#ifdef FEAT_MBYTE
9141 mb_replace_pop_ins(cc);
9142#else
9143 ins_char(cc);
9144#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009145 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 cc = replace_pop();
9147 }
9148 /* restore the characters that NL replaced */
9149 replace_pop_ins();
9150 State = oldState;
9151 }
9152 }
9153 did_ai = FALSE;
9154 }
9155 else
9156 {
9157 /*
9158 * Delete character(s) before the cursor.
9159 */
9160#ifdef FEAT_RIGHTLEFT
9161 if (revins_on) /* put cursor on last inserted char */
9162 dec_cursor();
9163#endif
9164 mincol = 0;
9165 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009166 if (mode == BACKSPACE_LINE
9167 && (curbuf->b_p_ai
9168#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009169 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009170#endif
9171 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#ifdef FEAT_RIGHTLEFT
9173 && !revins_on
9174#endif
9175 )
9176 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009177 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009179 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009181 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 }
9183
9184 /*
9185 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9186 */
9187 if ( mode == BACKSPACE_CHAR
9188 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009189 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009190 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 && (*(ml_get_cursor() - 1) == TAB
9192 || (*(ml_get_cursor() - 1) == ' '
9193 && (!*inserted_space_p
9194 || arrow_used))))))
9195 {
9196 int ts;
9197 colnr_T vcol;
9198 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009199 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
9201 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009202 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009203 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009205 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 /* Compute the virtual column where we want to be. Since
9207 * 'showbreak' may get in the way, need to get the last column of
9208 * the previous character. */
9209 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009210 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 dec_cursor();
9212 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9213 inc_cursor();
9214 want_vcol = (want_vcol / ts) * ts;
9215
9216 /* delete characters until we are at or before want_vcol */
9217 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009218 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009219 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220
9221 /* insert extra spaces until we are at want_vcol */
9222 while (vcol < want_vcol)
9223 {
9224 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009225 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9226 && curwin->w_cursor.col < Insstart_orig.col)
9227 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
9229#ifdef FEAT_VREPLACE
9230 if (State & VREPLACE_FLAG)
9231 ins_char(' ');
9232 else
9233#endif
9234 {
9235 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009236 if ((State & REPLACE_FLAG))
9237 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 }
9239 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9240 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009241
9242 /* If we are now back where we started delete one character. Can
9243 * happen when using 'sts' and 'linebreak'. */
9244 if (vcol >= start_vcol)
9245 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
9247
9248 /*
9249 * Delete upto starting point, start of line or previous word.
9250 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009253#ifdef FEAT_MBYTE
9254 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009256 if (has_mbyte)
9257 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009259 do
9260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009262 if (!revins_on) /* put cursor on char to be deleted */
9263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009265
9266 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009268 /* look multi-byte character class */
9269 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009271 prev_cclass = cclass;
9272 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009275
9276 /* start of word? */
9277 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9278 {
9279 mode = BACKSPACE_WORD_NOT_SPACE;
9280 temp = vim_iswordc(cc);
9281 }
9282 /* end of word? */
9283 else if (mode == BACKSPACE_WORD_NOT_SPACE
9284 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9285#ifdef FEAT_MBYTE
9286 || prev_cclass != cclass
9287#endif
9288 ))
9289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009291 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009293 inc_cursor();
9294#ifdef FEAT_RIGHTLEFT
9295 else if (State & REPLACE_FLAG)
9296 dec_cursor();
9297#endif
9298 break;
9299 }
9300 if (State & REPLACE_FLAG)
9301 replace_do_bs(-1);
9302 else
9303 {
9304#ifdef FEAT_MBYTE
9305 if (enc_utf8 && p_deco)
9306 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9307#endif
9308 (void)del_char(FALSE);
9309#ifdef FEAT_MBYTE
9310 /*
9311 * If there are combining characters and 'delcombine' is set
9312 * move the cursor back. Don't back up before the base
9313 * character.
9314 */
9315 if (enc_utf8 && p_deco && cpc[0] != NUL)
9316 inc_cursor();
9317#endif
9318#ifdef FEAT_RIGHTLEFT
9319 if (revins_chars)
9320 {
9321 revins_chars--;
9322 revins_legal++;
9323 }
9324 if (revins_on && gchar_cursor() == NUL)
9325 break;
9326#endif
9327 }
9328 /* Just a single backspace?: */
9329 if (mode == BACKSPACE_CHAR)
9330 break;
9331 } while (
9332#ifdef FEAT_RIGHTLEFT
9333 revins_on ||
9334#endif
9335 (curwin->w_cursor.col > mincol
9336 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9337 || curwin->w_cursor.col != Insstart_orig.col)));
9338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 did_backspace = TRUE;
9340 }
9341#ifdef FEAT_SMARTINDENT
9342 did_si = FALSE;
9343 can_si = FALSE;
9344 can_si_back = FALSE;
9345#endif
9346 if (curwin->w_cursor.col <= 1)
9347 did_ai = FALSE;
9348 /*
9349 * It's a little strange to put backspaces into the redo
9350 * buffer, but it makes auto-indent a lot easier to deal
9351 * with.
9352 */
9353 AppendCharToRedobuff(c);
9354
9355 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009356 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9357 && curwin->w_cursor.col < Insstart_orig.col)
9358 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
9360 /* vi behaviour: the cursor moves backward but the character that
9361 * was there remains visible
9362 * Vim behaviour: the cursor moves backward and the character that
9363 * was there is erased from the screen.
9364 * We can emulate the vi behaviour by pretending there is a dollar
9365 * displayed even when there isn't.
9366 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009367 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 dollar_vcol = curwin->w_virtcol;
9369
Bram Moolenaarce3be472008-01-14 19:12:28 +00009370#ifdef FEAT_FOLDING
9371 /* When deleting a char the cursor line must never be in a closed fold.
9372 * E.g., when 'foldmethod' is indent and deleting the first non-white
9373 * char before a Tab. */
9374 if (did_backspace)
9375 foldOpenCursor();
9376#endif
9377
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 return did_backspace;
9379}
9380
9381#ifdef FEAT_MOUSE
9382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009383ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009386 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
9388# ifdef FEAT_GUI
9389 /* When GUI is active, also move/paste when 'mouse' is empty */
9390 if (!gui.in_use)
9391# endif
9392 if (!mouse_has(MOUSE_INSERT))
9393 return;
9394
9395 undisplay_dollar();
9396 tpos = curwin->w_cursor;
9397 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9398 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009399 win_T *new_curwin = curwin;
9400
9401 if (curwin != old_curwin && win_valid(old_curwin))
9402 {
9403 /* Mouse took us to another window. We need to go back to the
9404 * previous one to stop insert there properly. */
9405 curwin = old_curwin;
9406 curbuf = curwin->w_buffer;
9407 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009408 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009409 if (curwin != new_curwin && win_valid(new_curwin))
9410 {
9411 curwin = new_curwin;
9412 curbuf = curwin->w_buffer;
9413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414# ifdef FEAT_CINDENT
9415 can_cindent = TRUE;
9416# endif
9417 }
9418
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 /* redraw status lines (in case another window became active) */
9420 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
9422
9423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009424ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009427 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009428# ifdef FEAT_INS_EXPAND
9429 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430# endif
9431
9432 tpos = curwin->w_cursor;
9433
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009434 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 {
9436 int row, col;
9437
9438 row = mouse_row;
9439 col = mouse_col;
9440
9441 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009442 wp = mouse_find_win(&row, &col);
9443 if (wp == NULL)
9444 return;
9445 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 curbuf = curwin->w_buffer;
9447 }
9448 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 undisplay_dollar();
9450
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009451# ifdef FEAT_INS_EXPAND
9452 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009453 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009454# endif
9455 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009456 if (dir == MSCR_DOWN || dir == MSCR_UP)
9457 {
9458 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9459 scroll_redraw(dir,
9460 (long)(curwin->w_botline - curwin->w_topline));
9461 else
9462 scroll_redraw(dir, 3L);
9463 }
9464#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009465 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009466 {
9467 int val, step = 6;
9468
9469 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009470 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009471 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9472 if (val < 0)
9473 val = 0;
9474 gui_do_horiz_scroll(val, TRUE);
9475 }
9476#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009477# ifdef FEAT_INS_EXPAND
9478 did_scroll = TRUE;
9479# endif
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 curwin->w_redr_status = TRUE;
9483
9484 curwin = old_curwin;
9485 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009487# ifdef FEAT_INS_EXPAND
9488 /* The popup menu may overlay the window, need to redraw it.
9489 * TODO: Would be more efficient to only redraw the windows that are
9490 * overlapped by the popup menu. */
9491 if (pum_visible() && did_scroll)
9492 {
9493 redraw_all_later(NOT_VALID);
9494 ins_compl_show_pum();
9495 }
9496# endif
9497
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009498 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 {
9500 start_arrow(&tpos);
9501# ifdef FEAT_CINDENT
9502 can_cindent = TRUE;
9503# endif
9504 }
9505}
9506#endif
9507
Bram Moolenaarec2da362017-01-21 20:04:22 +01009508/*
9509 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9510 * P_PE literally.
9511 * When "drop" is TRUE then consume the text and drop it.
9512 */
9513 int
9514bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9515{
9516 int c;
9517 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9518 int idx = 0;
9519 char_u *end = find_termcode((char_u *)"PE");
9520 int ret_char = -1;
9521 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009522 int save_paste = p_paste;
9523 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009524
9525 /* If the end code is too long we can't detect it, read everything. */
9526 if (STRLEN(end) >= NUMBUFLEN)
9527 end = NULL;
9528 ++no_mapping;
9529 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009530 p_paste = TRUE;
9531 curbuf->b_p_ai = FALSE;
9532
Bram Moolenaarec2da362017-01-21 20:04:22 +01009533 for (;;)
9534 {
9535 /* When the end is not defined read everything. */
9536 if (end == NULL && vpeekc() == NUL)
9537 break;
9538 c = plain_vgetc();
9539#ifdef FEAT_MBYTE
9540 if (has_mbyte)
9541 idx += (*mb_char2bytes)(c, buf + idx);
9542 else
9543#endif
9544 buf[idx++] = c;
9545 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009546 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009547 {
9548 if (end[idx] == NUL)
9549 break; /* Found the end of paste code. */
9550 continue;
9551 }
9552 if (!drop)
9553 {
9554 switch (mode)
9555 {
9556 case PASTE_CMDLINE:
9557 put_on_cmdline(buf, idx, TRUE);
9558 break;
9559
9560 case PASTE_EX:
9561 if (gap != NULL && ga_grow(gap, idx) == OK)
9562 {
9563 mch_memmove((char *)gap->ga_data + gap->ga_len,
9564 buf, (size_t)idx);
9565 gap->ga_len += idx;
9566 }
9567 break;
9568
9569 case PASTE_INSERT:
9570 if (stop_arrow() == OK)
9571 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009572 c = buf[0];
9573 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9574 ins_eol(c);
9575 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009576 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009577 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009578 AppendToRedobuffLit(buf, idx);
9579 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009580 }
9581 break;
9582
9583 case PASTE_ONE_CHAR:
9584 if (ret_char == -1)
9585 {
9586#ifdef FEAT_MBYTE
9587 if (has_mbyte)
9588 ret_char = (*mb_ptr2char)(buf);
9589 else
9590#endif
9591 ret_char = buf[0];
9592 }
9593 break;
9594 }
9595 }
9596 idx = 0;
9597 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009598
Bram Moolenaarec2da362017-01-21 20:04:22 +01009599 --no_mapping;
9600 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009601 p_paste = save_paste;
9602 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009603
9604 return ret_char;
9605}
9606
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009607#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009610{
9611 /* We will be leaving the current window, unless closing another tab. */
9612 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9613 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9614 {
9615 undisplay_dollar();
9616 start_arrow(&curwin->w_cursor);
9617# ifdef FEAT_CINDENT
9618 can_cindent = TRUE;
9619# endif
9620 }
9621
9622 if (c == K_TABLINE)
9623 goto_tabpage(current_tab);
9624 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009625 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009626 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009627 redraw_statuslines(); /* will redraw the tabline when needed */
9628 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009629}
9630#endif
9631
9632#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636 pos_T tpos;
9637
9638 undisplay_dollar();
9639 tpos = curwin->w_cursor;
9640 if (gui_do_scroll())
9641 {
9642 start_arrow(&tpos);
9643# ifdef FEAT_CINDENT
9644 can_cindent = TRUE;
9645# endif
9646 }
9647}
9648
9649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009650ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 pos_T tpos;
9653
9654 undisplay_dollar();
9655 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009656 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
9658 start_arrow(&tpos);
9659# ifdef FEAT_CINDENT
9660 can_cindent = TRUE;
9661# endif
9662 }
9663}
9664#endif
9665
9666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667ins_left(
9668 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670 pos_T tpos;
9671
9672#ifdef FEAT_FOLDING
9673 if ((fdo_flags & FDO_HOR) && KeyTyped)
9674 foldOpenCursor();
9675#endif
9676 undisplay_dollar();
9677 tpos = curwin->w_cursor;
9678 if (oneleft() == OK)
9679 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009680#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9681 /* Only call start_arrow() when not busy with preediting, it will
9682 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009683 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009684#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009685 {
9686 start_arrow_with_change(&tpos, end_change);
9687 if (!end_change)
9688 AppendCharToRedobuff(K_LEFT);
9689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690#ifdef FEAT_RIGHTLEFT
9691 /* If exit reversed string, position is fixed */
9692 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9693 revins_legal++;
9694 revins_chars++;
9695#endif
9696 }
9697
9698 /*
9699 * if 'whichwrap' set for cursor in insert mode may go to
9700 * previous line
9701 */
9702 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9703 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009704 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 start_arrow(&tpos);
9706 --(curwin->w_cursor.lnum);
9707 coladvance((colnr_T)MAXCOL);
9708 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9709 }
9710 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009711 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009712 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713}
9714
9715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009716ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 pos_T tpos;
9719
9720#ifdef FEAT_FOLDING
9721 if ((fdo_flags & FDO_HOR) && KeyTyped)
9722 foldOpenCursor();
9723#endif
9724 undisplay_dollar();
9725 tpos = curwin->w_cursor;
9726 if (c == K_C_HOME)
9727 curwin->w_cursor.lnum = 1;
9728 curwin->w_cursor.col = 0;
9729#ifdef FEAT_VIRTUALEDIT
9730 curwin->w_cursor.coladd = 0;
9731#endif
9732 curwin->w_curswant = 0;
9733 start_arrow(&tpos);
9734}
9735
9736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009737ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
9739 pos_T tpos;
9740
9741#ifdef FEAT_FOLDING
9742 if ((fdo_flags & FDO_HOR) && KeyTyped)
9743 foldOpenCursor();
9744#endif
9745 undisplay_dollar();
9746 tpos = curwin->w_cursor;
9747 if (c == K_C_END)
9748 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9749 coladvance((colnr_T)MAXCOL);
9750 curwin->w_curswant = MAXCOL;
9751
9752 start_arrow(&tpos);
9753}
9754
9755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009756ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
9758#ifdef FEAT_FOLDING
9759 if ((fdo_flags & FDO_HOR) && KeyTyped)
9760 foldOpenCursor();
9761#endif
9762 undisplay_dollar();
9763 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9764 {
9765 start_arrow(&curwin->w_cursor);
9766 (void)bck_word(1L, FALSE, FALSE);
9767 curwin->w_set_curswant = TRUE;
9768 }
9769 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009770 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009774ins_right(
9775 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
9777#ifdef FEAT_FOLDING
9778 if ((fdo_flags & FDO_HOR) && KeyTyped)
9779 foldOpenCursor();
9780#endif
9781 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009782 if (gchar_cursor() != NUL
9783#ifdef FEAT_VIRTUALEDIT
9784 || virtual_active()
9785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 )
9787 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009788 start_arrow_with_change(&curwin->w_cursor, end_change);
9789 if (!end_change)
9790 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 curwin->w_set_curswant = TRUE;
9792#ifdef FEAT_VIRTUALEDIT
9793 if (virtual_active())
9794 oneright();
9795 else
9796#endif
9797 {
9798#ifdef FEAT_MBYTE
9799 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009800 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 else
9802#endif
9803 ++curwin->w_cursor.col;
9804 }
9805
9806#ifdef FEAT_RIGHTLEFT
9807 revins_legal++;
9808 if (revins_chars)
9809 revins_chars--;
9810#endif
9811 }
9812 /* if 'whichwrap' set for cursor in insert mode, may move the
9813 * cursor to the next line */
9814 else if (vim_strchr(p_ww, ']') != NULL
9815 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9816 {
9817 start_arrow(&curwin->w_cursor);
9818 curwin->w_set_curswant = TRUE;
9819 ++curwin->w_cursor.lnum;
9820 curwin->w_cursor.col = 0;
9821 }
9822 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009823 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009824 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825}
9826
9827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009828ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830#ifdef FEAT_FOLDING
9831 if ((fdo_flags & FDO_HOR) && KeyTyped)
9832 foldOpenCursor();
9833#endif
9834 undisplay_dollar();
9835 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9836 || gchar_cursor() != NUL)
9837 {
9838 start_arrow(&curwin->w_cursor);
9839 (void)fwd_word(1L, FALSE, 0);
9840 curwin->w_set_curswant = TRUE;
9841 }
9842 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009843 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844}
9845
9846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009847ins_up(
9848 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
9850 pos_T tpos;
9851 linenr_T old_topline = curwin->w_topline;
9852#ifdef FEAT_DIFF
9853 int old_topfill = curwin->w_topfill;
9854#endif
9855
9856 undisplay_dollar();
9857 tpos = curwin->w_cursor;
9858 if (cursor_up(1L, TRUE) == OK)
9859 {
9860 if (startcol)
9861 coladvance(getvcol_nolist(&Insstart));
9862 if (old_topline != curwin->w_topline
9863#ifdef FEAT_DIFF
9864 || old_topfill != curwin->w_topfill
9865#endif
9866 )
9867 redraw_later(VALID);
9868 start_arrow(&tpos);
9869#ifdef FEAT_CINDENT
9870 can_cindent = TRUE;
9871#endif
9872 }
9873 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009874 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875}
9876
9877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009878ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 pos_T tpos;
9881
9882 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009883
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009884 if (mod_mask & MOD_MASK_CTRL)
9885 {
9886 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009887 if (first_tabpage->tp_next != NULL)
9888 {
9889 start_arrow(&curwin->w_cursor);
9890 goto_tabpage(-1);
9891 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009892 return;
9893 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 tpos = curwin->w_cursor;
9896 if (onepage(BACKWARD, 1L) == OK)
9897 {
9898 start_arrow(&tpos);
9899#ifdef FEAT_CINDENT
9900 can_cindent = TRUE;
9901#endif
9902 }
9903 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009904 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
9907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908ins_down(
9909 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 pos_T tpos;
9912 linenr_T old_topline = curwin->w_topline;
9913#ifdef FEAT_DIFF
9914 int old_topfill = curwin->w_topfill;
9915#endif
9916
9917 undisplay_dollar();
9918 tpos = curwin->w_cursor;
9919 if (cursor_down(1L, TRUE) == OK)
9920 {
9921 if (startcol)
9922 coladvance(getvcol_nolist(&Insstart));
9923 if (old_topline != curwin->w_topline
9924#ifdef FEAT_DIFF
9925 || old_topfill != curwin->w_topfill
9926#endif
9927 )
9928 redraw_later(VALID);
9929 start_arrow(&tpos);
9930#ifdef FEAT_CINDENT
9931 can_cindent = TRUE;
9932#endif
9933 }
9934 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009935 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009939ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 pos_T tpos;
9942
9943 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009944
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009945 if (mod_mask & MOD_MASK_CTRL)
9946 {
9947 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009948 if (first_tabpage->tp_next != NULL)
9949 {
9950 start_arrow(&curwin->w_cursor);
9951 goto_tabpage(0);
9952 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009953 return;
9954 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009955
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 tpos = curwin->w_cursor;
9957 if (onepage(FORWARD, 1L) == OK)
9958 {
9959 start_arrow(&tpos);
9960#ifdef FEAT_CINDENT
9961 can_cindent = TRUE;
9962#endif
9963 }
9964 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009965 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966}
9967
9968#ifdef FEAT_DND
9969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009970ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971{
9972 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9973}
9974#endif
9975
9976/*
9977 * Handle TAB in Insert or Replace mode.
9978 * Return TRUE when the TAB needs to be inserted like a normal character.
9979 */
9980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009981ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
9983 int ind;
9984 int i;
9985 int temp;
9986
9987 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9988 Insstart_blank_vcol = get_nolist_virtcol();
9989 if (echeck_abbr(TAB + ABBR_OFF))
9990 return FALSE;
9991
9992 ind = inindent(0);
9993#ifdef FEAT_CINDENT
9994 if (ind)
9995 can_cindent = FALSE;
9996#endif
9997
9998 /*
9999 * When nothing special, insert TAB like a normal character
10000 */
10001 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010002 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010003 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 return TRUE;
10005
10006 if (stop_arrow() == FAIL)
10007 return TRUE;
10008
10009 did_ai = FALSE;
10010#ifdef FEAT_SMARTINDENT
10011 did_si = FALSE;
10012 can_si = FALSE;
10013 can_si_back = FALSE;
10014#endif
10015 AppendToRedobuff((char_u *)"\t");
10016
10017 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010018 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010019 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10020 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 else /* otherwise use 'tabstop' */
10022 temp = (int)curbuf->b_p_ts;
10023 temp -= get_nolist_virtcol() % temp;
10024
10025 /*
10026 * Insert the first space with ins_char(). It will delete one char in
10027 * replace mode. Insert the rest with ins_str(); it will not delete any
10028 * chars. For VREPLACE mode, we use ins_char() for all characters.
10029 */
10030 ins_char(' ');
10031 while (--temp > 0)
10032 {
10033#ifdef FEAT_VREPLACE
10034 if (State & VREPLACE_FLAG)
10035 ins_char(' ');
10036 else
10037#endif
10038 {
10039 ins_str((char_u *)" ");
10040 if (State & REPLACE_FLAG) /* no char replaced */
10041 replace_push(NUL);
10042 }
10043 }
10044
10045 /*
10046 * When 'expandtab' not set: Replace spaces by TABs where possible.
10047 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010048 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 {
10050 char_u *ptr;
10051#ifdef FEAT_VREPLACE
10052 char_u *saved_line = NULL; /* init for GCC */
10053 pos_T pos;
10054#endif
10055 pos_T fpos;
10056 pos_T *cursor;
10057 colnr_T want_vcol, vcol;
10058 int change_col = -1;
10059 int save_list = curwin->w_p_list;
10060
10061 /*
10062 * Get the current line. For VREPLACE mode, don't make real changes
10063 * yet, just work on a copy of the line.
10064 */
10065#ifdef FEAT_VREPLACE
10066 if (State & VREPLACE_FLAG)
10067 {
10068 pos = curwin->w_cursor;
10069 cursor = &pos;
10070 saved_line = vim_strsave(ml_get_curline());
10071 if (saved_line == NULL)
10072 return FALSE;
10073 ptr = saved_line + pos.col;
10074 }
10075 else
10076#endif
10077 {
10078 ptr = ml_get_cursor();
10079 cursor = &curwin->w_cursor;
10080 }
10081
10082 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10083 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10084 curwin->w_p_list = FALSE;
10085
10086 /* Find first white before the cursor */
10087 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010088 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
10090 --fpos.col;
10091 --ptr;
10092 }
10093
10094 /* In Replace mode, don't change characters before the insert point. */
10095 if ((State & REPLACE_FLAG)
10096 && fpos.lnum == Insstart.lnum
10097 && fpos.col < Insstart.col)
10098 {
10099 ptr += Insstart.col - fpos.col;
10100 fpos.col = Insstart.col;
10101 }
10102
10103 /* compute virtual column numbers of first white and cursor */
10104 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10105 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10106
Bram Moolenaar597a4222014-06-25 14:39:50 +020010107 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10108 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010109 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010111 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 if (vcol + i > want_vcol)
10113 break;
10114 if (*ptr != TAB)
10115 {
10116 *ptr = TAB;
10117 if (change_col < 0)
10118 {
10119 change_col = fpos.col; /* Column of first change */
10120 /* May have to adjust Insstart */
10121 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10122 Insstart.col = fpos.col;
10123 }
10124 }
10125 ++fpos.col;
10126 ++ptr;
10127 vcol += i;
10128 }
10129
10130 if (change_col >= 0)
10131 {
10132 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010133 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134
10135 /* Skip over the spaces we need. */
10136 while (vcol < want_vcol && *ptr == ' ')
10137 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010138 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 ++ptr;
10140 ++repl_off;
10141 }
10142 if (vcol > want_vcol)
10143 {
10144 /* Must have a char with 'showbreak' just before it. */
10145 --ptr;
10146 --repl_off;
10147 }
10148 fpos.col += repl_off;
10149
10150 /* Delete following spaces. */
10151 i = cursor->col - fpos.col;
10152 if (i > 0)
10153 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010154 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 /* correct replace stack. */
10156 if ((State & REPLACE_FLAG)
10157#ifdef FEAT_VREPLACE
10158 && !(State & VREPLACE_FLAG)
10159#endif
10160 )
10161 for (temp = i; --temp >= 0; )
10162 replace_join(repl_off);
10163 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010164#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010165 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010166 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010167 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010168 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10169 (char_u *)"\t", 1);
10170 }
10171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 cursor->col -= i;
10173
10174#ifdef FEAT_VREPLACE
10175 /*
10176 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10177 * backspacing over the changed spacing and then inserting the new
10178 * spacing.
10179 */
10180 if (State & VREPLACE_FLAG)
10181 {
10182 /* Backspace from real cursor to change_col */
10183 backspace_until_column(change_col);
10184
10185 /* Insert each char in saved_line from changed_col to
10186 * ptr-cursor */
10187 ins_bytes_len(saved_line + change_col,
10188 cursor->col - change_col);
10189 }
10190#endif
10191 }
10192
10193#ifdef FEAT_VREPLACE
10194 if (State & VREPLACE_FLAG)
10195 vim_free(saved_line);
10196#endif
10197 curwin->w_p_list = save_list;
10198 }
10199
10200 return FALSE;
10201}
10202
10203/*
10204 * Handle CR or NL in insert mode.
10205 * Return TRUE when out of memory or can't undo.
10206 */
10207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010208ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 int i;
10211
10212 if (echeck_abbr(c + ABBR_OFF))
10213 return FALSE;
10214 if (stop_arrow() == FAIL)
10215 return TRUE;
10216 undisplay_dollar();
10217
10218 /*
10219 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10220 * character under the cursor. Only push a NUL on the replace stack,
10221 * nothing to put back when the NL is deleted.
10222 */
10223 if ((State & REPLACE_FLAG)
10224#ifdef FEAT_VREPLACE
10225 && !(State & VREPLACE_FLAG)
10226#endif
10227 )
10228 replace_push(NUL);
10229
10230 /*
10231 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10232 * replacing the next line, so we push all of the characters left on the
10233 * line onto the replace stack. This is not done here though, it is done
10234 * in open_line().
10235 */
10236
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010237#ifdef FEAT_VIRTUALEDIT
10238 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10239 * CTRL-O). */
10240 if (virtual_active() && curwin->w_cursor.coladd > 0)
10241 coladvance(getviscol());
10242#endif
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244#ifdef FEAT_RIGHTLEFT
10245# ifdef FEAT_FKMAP
10246 if (p_altkeymap && p_fkmap)
10247 fkmap(NL);
10248# endif
10249 /* NL in reverse insert will always start in the end of
10250 * current line. */
10251 if (revins_on)
10252 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10253#endif
10254
10255 AppendToRedobuff(NL_STR);
10256 i = open_line(FORWARD,
10257#ifdef FEAT_COMMENTS
10258 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10259#endif
10260 0, old_indent);
10261 old_indent = 0;
10262#ifdef FEAT_CINDENT
10263 can_cindent = TRUE;
10264#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010265#ifdef FEAT_FOLDING
10266 /* When inserting a line the cursor line must never be in a closed fold. */
10267 foldOpenCursor();
10268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269
10270 return (!i);
10271}
10272
10273#ifdef FEAT_DIGRAPHS
10274/*
10275 * Handle digraph in insert mode.
10276 * Returns character still to be inserted, or NUL when nothing remaining to be
10277 * done.
10278 */
10279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010280ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 int c;
10283 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010284 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285
10286 pc_status = PC_STATUS_UNSET;
10287 if (redrawing() && !char_avail())
10288 {
10289 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010290 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
10292 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010293 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294#ifdef FEAT_CMDL_INFO
10295 add_to_showcmd_c(Ctrl_K);
10296#endif
10297 }
10298
10299#ifdef USE_ON_FLY_SCROLL
10300 dont_scroll = TRUE; /* disallow scrolling here */
10301#endif
10302
10303 /* don't map the digraph chars. This also prevents the
10304 * mode message to be deleted when ESC is hit */
10305 ++no_mapping;
10306 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010307 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 --no_mapping;
10309 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010310 if (did_putchar)
10311 /* when the line fits in 'columns' the '?' is at the start of the next
10312 * line and will not be removed by the redraw */
10313 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010314
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (IS_SPECIAL(c) || mod_mask) /* special key */
10316 {
10317#ifdef FEAT_CMDL_INFO
10318 clear_showcmd();
10319#endif
10320 insert_special(c, TRUE, FALSE);
10321 return NUL;
10322 }
10323 if (c != ESC)
10324 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010325 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (redrawing() && !char_avail())
10327 {
10328 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010329 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330
10331 if (char2cells(c) == 1)
10332 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010333 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010335 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
10337#ifdef FEAT_CMDL_INFO
10338 add_to_showcmd_c(c);
10339#endif
10340 }
10341 ++no_mapping;
10342 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010343 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 --no_mapping;
10345 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010346 if (did_putchar)
10347 /* when the line fits in 'columns' the '?' is at the start of the
10348 * next line and will not be removed by a redraw */
10349 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 if (cc != ESC)
10351 {
10352 AppendToRedobuff((char_u *)CTRL_V_STR);
10353 c = getdigraph(c, cc, TRUE);
10354#ifdef FEAT_CMDL_INFO
10355 clear_showcmd();
10356#endif
10357 return c;
10358 }
10359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360#ifdef FEAT_CMDL_INFO
10361 clear_showcmd();
10362#endif
10363 return NUL;
10364}
10365#endif
10366
10367/*
10368 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10369 * Returns the char to be inserted, or NUL if none found.
10370 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010372ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373{
10374 int c;
10375 int temp;
10376 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010377 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378
10379 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10380 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010381 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 return NUL;
10383 }
10384
10385 /* try to advance to the cursor column */
10386 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010387 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 prev_ptr = ptr;
10389 validate_virtcol();
10390 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10391 {
10392 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010393 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395 if ((colnr_T)temp > curwin->w_virtcol)
10396 ptr = prev_ptr;
10397
10398#ifdef FEAT_MBYTE
10399 c = (*mb_ptr2char)(ptr);
10400#else
10401 c = *ptr;
10402#endif
10403 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010404 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 return c;
10406}
10407
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010408/*
10409 * CTRL-Y or CTRL-E typed in Insert mode.
10410 */
10411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010412ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010413{
10414 int c = tc;
10415
10416#ifdef FEAT_INS_EXPAND
10417 if (ctrl_x_mode == CTRL_X_SCROLL)
10418 {
10419 if (c == Ctrl_Y)
10420 scrolldown_clamp();
10421 else
10422 scrollup_clamp();
10423 redraw_later(VALID);
10424 }
10425 else
10426#endif
10427 {
10428 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10429 if (c != NUL)
10430 {
10431 long tw_save;
10432
10433 /* The character must be taken literally, insert like it
10434 * was typed after a CTRL-V, and pretend 'textwidth'
10435 * wasn't set. Digits, 'o' and 'x' are special after a
10436 * CTRL-V, don't use it for these. */
10437 if (c < 256 && !isalnum(c))
10438 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10439 tw_save = curbuf->b_p_tw;
10440 curbuf->b_p_tw = -1;
10441 insert_special(c, TRUE, FALSE);
10442 curbuf->b_p_tw = tw_save;
10443#ifdef FEAT_RIGHTLEFT
10444 revins_chars++;
10445 revins_legal++;
10446#endif
10447 c = Ctrl_V; /* pretend CTRL-V is last character */
10448 auto_format(FALSE, TRUE);
10449 }
10450 }
10451 return c;
10452}
10453
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454#ifdef FEAT_SMARTINDENT
10455/*
10456 * Try to do some very smart auto-indenting.
10457 * Used when inserting a "normal" character.
10458 */
10459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010460ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 pos_T *pos, old_pos;
10463 char_u *ptr;
10464 int i;
10465 int temp;
10466
10467 /*
10468 * do some very smart indenting when entering '{' or '}'
10469 */
10470 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10471 {
10472 /*
10473 * for '}' set indent equal to indent of line containing matching '{'
10474 */
10475 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10476 {
10477 old_pos = curwin->w_cursor;
10478 /*
10479 * If the matching '{' has a ')' immediately before it (ignoring
10480 * white-space), then line up with the start of the line
10481 * containing the matching '(' if there is one. This handles the
10482 * case where an "if (..\n..) {" statement continues over multiple
10483 * lines -- webb
10484 */
10485 ptr = ml_get(pos->lnum);
10486 i = pos->col;
10487 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010488 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 ;
10490 curwin->w_cursor.lnum = pos->lnum;
10491 curwin->w_cursor.col = i;
10492 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10493 curwin->w_cursor = *pos;
10494 i = get_indent();
10495 curwin->w_cursor = old_pos;
10496#ifdef FEAT_VREPLACE
10497 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010498 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 else
10500#endif
10501 (void)set_indent(i, SIN_CHANGED);
10502 }
10503 else if (curwin->w_cursor.col > 0)
10504 {
10505 /*
10506 * when inserting '{' after "O" reduce indent, but not
10507 * more than indent of previous line
10508 */
10509 temp = TRUE;
10510 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10511 {
10512 old_pos = curwin->w_cursor;
10513 i = get_indent();
10514 while (curwin->w_cursor.lnum > 1)
10515 {
10516 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10517
10518 /* ignore empty lines and lines starting with '#'. */
10519 if (*ptr != '#' && *ptr != NUL)
10520 break;
10521 }
10522 if (get_indent() >= i)
10523 temp = FALSE;
10524 curwin->w_cursor = old_pos;
10525 }
10526 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010527 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 }
10529 }
10530
10531 /*
10532 * set indent of '#' always to 0
10533 */
10534 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10535 {
10536 /* remember current indent for next line */
10537 old_indent = get_indent();
10538 (void)set_indent(0, SIN_CHANGED);
10539 }
10540
10541 /* Adjust ai_col, the char at this position can be deleted. */
10542 if (ai_col > curwin->w_cursor.col)
10543 ai_col = curwin->w_cursor.col;
10544}
10545#endif
10546
10547/*
10548 * Get the value that w_virtcol would have when 'list' is off.
10549 * Unless 'cpo' contains the 'L' flag.
10550 */
10551 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010552get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10555 return getvcol_nolist(&curwin->w_cursor);
10556 validate_virtcol();
10557 return curwin->w_virtcol;
10558}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010559
10560#ifdef FEAT_AUTOCMD
10561/*
10562 * Handle the InsertCharPre autocommand.
10563 * "c" is the character that was typed.
10564 * Return a pointer to allocated memory with the replacement string.
10565 * Return NULL to continue inserting "c".
10566 */
10567 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010568do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010569{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010570 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010571 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010572
10573 /* Return quickly when there is nothing to do. */
10574 if (!has_insertcharpre())
10575 return NULL;
10576
Bram Moolenaar704984a2012-06-01 14:57:51 +020010577#ifdef FEAT_MBYTE
10578 if (has_mbyte)
10579 buf[(*mb_char2bytes)(c, buf)] = NUL;
10580 else
10581#endif
10582 {
10583 buf[0] = c;
10584 buf[1] = NUL;
10585 }
10586
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010587 /* Lock the text to avoid weird things from happening. */
10588 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010589 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010590
Bram Moolenaar704984a2012-06-01 14:57:51 +020010591 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010592 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010593 {
10594 /* Get the value of v:char. It may be empty or more than one
10595 * character. Only use it when changed, otherwise continue with the
10596 * original character to avoid breaking autoindent. */
10597 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10598 res = vim_strsave(get_vim_var_str(VV_CHAR));
10599 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010600
10601 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10602 --textlock;
10603
10604 return res;
10605}
10606#endif