blob: cdde11e408ab52f7150dc732523e85fb73b87d33 [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 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200182static void ins_compl_insert(int in_compl_func);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100189static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100312 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 * 'R' replace command
314 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
315 * but still only one <CR> is inserted. The <Esc> is not used for redo.
316 * 'g' "gI" command.
317 * 'V' "gR" command for Virtual Replace mode.
318 * 'v' "gr" command for single character Virtual Replace mode.
319 *
320 * This function is not called recursively. For CTRL-O commands, it returns
321 * and lets the caller handle the Normal-mode command.
322 *
323 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
324 */
325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326edit(
327 int cmdchar,
328 int startln, /* if set, insert at start of line */
329 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int c = 0;
332 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100333 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000334 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 int i;
337 int did_backspace = TRUE; /* previous char was backspace */
338#ifdef FEAT_CINDENT
339 int line_is_white = FALSE; /* line is empty before insert */
340#endif
341 linenr_T old_topline = 0; /* topline before insertion */
342#ifdef FEAT_DIFF
343 int old_topfill = -1;
344#endif
345 int inserted_space = FALSE; /* just inserted a space */
346 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000347 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385#ifdef FEAT_AUTOCMD
386 /*
387 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
388 */
389 if (cmdchar != 'r' && cmdchar != 'v')
390 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100391 pos_T save_cursor = curwin->w_cursor;
392
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000394 if (cmdchar == 'R')
395 ptr = (char_u *)"r";
396 else if (cmdchar == 'V')
397 ptr = (char_u *)"v";
398 else
399 ptr = (char_u *)"i";
400 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000403 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404
Bram Moolenaar097c9922013-05-19 21:15:15 +0200405 /* Make sure the cursor didn't move. Do call check_cursor_col() in
406 * case the text was modified. Since Insert mode was not started yet
407 * a call to check_cursor_col() may move the cursor, especially with
408 * the "A" command, thus set State to avoid that. Also check that the
409 * line number is still valid (lines may have been deleted).
410 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100411 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412# ifdef FEAT_EVAL
413 && *get_vim_var_str(VV_CHAR) == NUL
414# endif
415 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100416 {
417 int save_state = State;
418
419 curwin->w_cursor = save_cursor;
420 State = INSERT;
421 check_cursor_col();
422 State = save_state;
423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424 }
425#endif
426
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#ifdef FEAT_CONCEAL
428 /* Check if the cursor line needs redrawing before changing State. If
429 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200430 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#endif
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_MOUSE
434 /*
435 * When doing a paste with the middle mouse button, Insstart is set to
436 * where the paste started.
437 */
438 if (where_paste_started.lnum != 0)
439 Insstart = where_paste_started;
440 else
441#endif
442 {
443 Insstart = curwin->w_cursor;
444 if (startln)
445 Insstart.col = 0;
446 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000447 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 Insstart_blank_vcol = MAXCOL;
449 if (!did_ai)
450 ai_col = 0;
451
452 if (cmdchar != NUL && restart_edit == 0)
453 {
454 ResetRedobuff();
455 AppendNumberToRedobuff(count);
456#ifdef FEAT_VREPLACE
457 if (cmdchar == 'V' || cmdchar == 'v')
458 {
459 /* "gR" or "gr" command */
460 AppendCharToRedobuff('g');
461 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
462 }
463 else
464#endif
465 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100466 if (cmdchar == K_PS)
467 AppendCharToRedobuff('a');
468 else
469 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (cmdchar == 'g') /* "gI" command */
471 AppendCharToRedobuff('I');
472 else if (cmdchar == 'r') /* "r<CR>" command */
473 count = 1; /* insert only one <CR> */
474 }
475 }
476
477 if (cmdchar == 'R')
478 {
479#ifdef FEAT_FKMAP
480 if (p_fkmap && p_ri)
481 {
482 beep_flush();
483 EMSG(farsi_text_3); /* encoded in Farsi */
484 State = INSERT;
485 }
486 else
487#endif
488 State = REPLACE;
489 }
490#ifdef FEAT_VREPLACE
491 else if (cmdchar == 'V' || cmdchar == 'v')
492 {
493 State = VREPLACE;
494 replaceState = VREPLACE;
495 orig_line_count = curbuf->b_ml.ml_line_count;
496 vr_lines_changed = 1;
497 }
498#endif
499 else
500 State = INSERT;
501
502 stop_insert_mode = FALSE;
503
504 /*
505 * Need to recompute the cursor position, it might move when the cursor is
506 * on a TAB or special character.
507 */
508 curs_columns(TRUE);
509
510 /*
511 * Enable langmap or IME, indicated by 'iminsert'.
512 * Note that IME may enabled/disabled without us noticing here, thus the
513 * 'iminsert' value may not reflect what is actually used. It is updated
514 * when hitting <Esc>.
515 */
516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
517 State |= LANGMAP;
518#ifdef USE_IM_CONTROL
519 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_MOUSE
523 setmouse();
524#endif
525#ifdef FEAT_CMDL_INFO
526 clear_showcmd();
527#endif
528#ifdef FEAT_RIGHTLEFT
529 /* there is no reverse replace mode */
530 revins_on = (State == INSERT && p_ri);
531 if (revins_on)
532 undisplay_dollar();
533 revins_chars = 0;
534 revins_legal = 0;
535 revins_scol = -1;
536#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100537 if (!p_ek)
538 /* Disable bracketed paste mode, we won't recognize the escape
539 * sequences. */
540 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100544 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
545 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
547 if (restart_edit != 0 && stuff_empty())
548 {
549#ifdef FEAT_MOUSE
550 /*
551 * After a paste we consider text typed to be part of the insert for
552 * the pasted text. You can backspace over the pasted text too.
553 */
554 if (where_paste_started.lnum)
555 arrow_used = FALSE;
556 else
557#endif
558 arrow_used = TRUE;
559 restart_edit = 0;
560
561 /*
562 * If the cursor was after the end-of-line before the CTRL-O and it is
563 * now at the end-of-line, put it after the end-of-line (this is not
564 * correct in very rare cases).
565 * Also do this if curswant is greater than the current virtual
566 * column. Eg after "^O$" or "^O80|".
567 */
568 validate_virtcol();
569 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000570 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || curwin->w_curswant > curwin->w_virtcol)
572 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
573 {
574 if (ptr[1] == NUL)
575 ++curwin->w_cursor.col;
576#ifdef FEAT_MBYTE
577 else if (has_mbyte)
578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000579 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (ptr[i] == NUL)
581 curwin->w_cursor.col += i;
582 }
583#endif
584 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 else
588 arrow_used = FALSE;
589
590 /* we are in insert mode now, don't need to start it anymore */
591 need_start_insertmode = FALSE;
592
593 /* Need to save the line for undo before inserting the first char. */
594 ins_need_undo = TRUE;
595
596#ifdef FEAT_MOUSE
597 where_paste_started.lnum = 0;
598#endif
599#ifdef FEAT_CINDENT
600 can_cindent = TRUE;
601#endif
602#ifdef FEAT_FOLDING
603 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
604 * restarting. */
605 if (!p_im && did_restart_edit == 0)
606 foldOpenCursor();
607#endif
608
609 /*
610 * If 'showmode' is set, show the current (insert/replace/..) mode.
611 * A warning message for changing a readonly file is given here, before
612 * actually changing anything. It's put after the mode, if any.
613 */
614 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000615 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 i = showmode();
617
618 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000619 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef CURSOR_SHAPE
622 ui_cursor_shape(); /* may show different cursor shape */
623#endif
624#ifdef FEAT_DIGRAPHS
625 do_digraph(-1); /* clear digraphs */
626#endif
627
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000628 /*
629 * Get the current length of the redo buffer, those characters have to be
630 * skipped if we want to get to the inserted characters.
631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ptr = get_inserted();
633 if (ptr == NULL)
634 new_insert_skip = 0;
635 else
636 {
637 new_insert_skip = (int)STRLEN(ptr);
638 vim_free(ptr);
639 }
640
641 old_indent = 0;
642
643 /*
644 * Main loop in Insert mode: repeat until Insert mode is left.
645 */
646 for (;;)
647 {
648#ifdef FEAT_RIGHTLEFT
649 if (!revins_legal)
650 revins_scol = -1; /* reset on illegal motions */
651 else
652 revins_legal = 0;
653#endif
654 if (arrow_used) /* don't repeat insert when arrow key used */
655 count = 0;
656
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100657 if (update_Insstart_orig)
658 Insstart_orig = Insstart;
659
Bram Moolenaar00672e12016-06-26 18:38:13 +0200660 if (stop_insert_mode
661#ifdef FEAT_INS_EXPAND
662 && !pum_visible()
663#endif
664 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666 /* ":stopinsert" used or 'insertmode' reset */
667 count = 0;
668 goto doESCkey;
669 }
670
671 /* set curwin->w_curswant for next K_DOWN or K_UP */
672 if (!arrow_used)
673 curwin->w_set_curswant = TRUE;
674
675 /* If there is no typeahead may check for timestamps (e.g., for when a
676 * menu invoked a shell command). */
677 if (stuff_empty())
678 {
679 did_check_timestamps = FALSE;
680 if (need_check_timestamps)
681 check_timestamps(FALSE);
682 }
683
684 /*
685 * When emsg() was called msg_scroll will have been set.
686 */
687 msg_scroll = FALSE;
688
689#ifdef FEAT_GUI
690 /* When 'mousefocus' is set a mouse movement may have taken us to
691 * another window. "need_mouse_correct" may then be set because of an
692 * autocommand. */
693 if (need_mouse_correct)
694 gui_mouse_correct();
695#endif
696
697#ifdef FEAT_FOLDING
698 /* Open fold at the cursor line, according to 'foldopen'. */
699 if (fdo_flags & FDO_INSERT)
700 foldOpenCursor();
701 /* Close folds where the cursor isn't, according to 'foldclose' */
702 if (!char_avail())
703 foldCheckClose();
704#endif
705
706 /*
707 * If we inserted a character at the last position of the last line in
708 * the window, scroll the window one line up. This avoids an extra
709 * redraw.
710 * This is detected when the cursor column is smaller after inserting
711 * something.
712 * Don't do this when the topline changed already, it has
713 * already been adjusted (by insertchar() calling open_line())).
714 */
715 if (curbuf->b_mod_set
716 && curwin->w_p_wrap
717 && !did_backspace
718 && curwin->w_topline == old_topline
719#ifdef FEAT_DIFF
720 && curwin->w_topfill == old_topfill
721#endif
722 )
723 {
724 mincol = curwin->w_wcol;
725 validate_cursor_col();
726
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000727 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 && curwin->w_wrow == W_WINROW(curwin)
729 + curwin->w_height - 1 - p_so
730 && (curwin->w_cursor.lnum != curwin->w_topline
731#ifdef FEAT_DIFF
732 || curwin->w_topfill > 0
733#endif
734 ))
735 {
736#ifdef FEAT_DIFF
737 if (curwin->w_topfill > 0)
738 --curwin->w_topfill;
739 else
740#endif
741#ifdef FEAT_FOLDING
742 if (hasFolding(curwin->w_topline, NULL, &old_topline))
743 set_topline(curwin, old_topline + 1);
744 else
745#endif
746 set_topline(curwin, curwin->w_topline + 1);
747 }
748 }
749
750 /* May need to adjust w_topline to show the cursor. */
751 update_topline();
752
753 did_backspace = FALSE;
754
755 validate_cursor(); /* may set must_redraw */
756
757 /*
758 * Redraw the display when no characters are waiting.
759 * Also shows mode, ruler and positions cursor.
760 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000761 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763#ifdef FEAT_SCROLLBIND
764 if (curwin->w_p_scb)
765 do_check_scrollbind(TRUE);
766#endif
767
Bram Moolenaar860cae12010-06-05 23:22:07 +0200768#ifdef FEAT_CURSORBIND
769 if (curwin->w_p_crb)
770 do_check_cursorbind();
771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 update_curswant();
773 old_topline = curwin->w_topline;
774#ifdef FEAT_DIFF
775 old_topfill = curwin->w_topfill;
776#endif
777
778#ifdef USE_ON_FLY_SCROLL
779 dont_scroll = FALSE; /* allow scrolling here */
780#endif
781
782 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000783 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100785 if (c != K_CURSORHOLD)
786 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200787
788 /* After using CTRL-G U the next cursor key will not break undo. */
789 if (dont_sync_undo == MAYBE)
790 dont_sync_undo = TRUE;
791 else
792 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100793 if (cmdchar == K_PS)
794 /* Got here from normal mode when bracketed paste started. */
795 c = K_PS;
796 else
797 do
798 {
799 c = safe_vgetc();
800 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000802#ifdef FEAT_AUTOCMD
803 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
804 did_cursorhold = TRUE;
805#endif
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#ifdef FEAT_RIGHTLEFT
808 if (p_hkmap && KeyTyped)
809 c = hkmap(c); /* Hebrew mode mapping */
810#endif
811#ifdef FEAT_FKMAP
812 if (p_fkmap && KeyTyped)
813 c = fkmap(c); /* Farsi mode mapping */
814#endif
815
816#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 /*
818 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000819 * and the cursor is still in the completed word. Only when there is
820 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000822 if (compl_started
823 && pum_wanted()
824 && curwin->w_cursor.col >= compl_col
825 && (compl_shown_match == NULL
826 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000827 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /* BS: Delete one character from "compl_leader". */
829 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000830 && curwin->w_cursor.col > compl_col
831 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000832 continue;
833
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000834 /* When no match was selected or it was edited. */
835 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000838 * "compl_leader". Except when at the original match and
839 * there is nothing to add, CTRL-L works like CTRL-P then. */
840 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100841 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000842 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000843 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 {
845 ins_compl_addfrommatch();
846 continue;
847 }
848
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000849 /* A non-white character that fits in with the current
850 * completion: Add to "compl_leader". */
851 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000852 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100853#ifdef FEAT_AUTOCMD
854 /* Trigger InsertCharPre. */
855 char_u *str = do_insert_char_pre(c);
856 char_u *p;
857
858 if (str != NULL)
859 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100860 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100861 ins_compl_addleader(PTR2CHAR(p));
862 vim_free(str);
863 }
864 else
865#endif
866 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 continue;
868 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000869
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000870 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000871 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200872 if ((c == Ctrl_Y || (compl_enter_selects
873 && (c == CAR || c == K_KENTER || c == NL)))
874 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000875 {
876 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200877 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000878 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000879 }
880 }
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
883 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000884 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000885 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888
Bram Moolenaar488c6512005-08-11 20:09:58 +0000889 /* CTRL-\ CTRL-N goes to Normal mode,
890 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
891 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (c == Ctrl_BSL)
893 {
894 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000895 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ++no_mapping;
897 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000898 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 --no_mapping;
900 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000901 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000903 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 vungetc(c);
905 c = Ctrl_BSL;
906 }
907 else if (c == Ctrl_G && p_im)
908 continue;
909 else
910 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000911 if (c == Ctrl_O)
912 {
913 ins_ctrl_o();
914 ins_at_eol = FALSE; /* cursor keeps its column */
915 nomove = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 count = 0;
918 goto doESCkey;
919 }
920 }
921
922#ifdef FEAT_DIGRAPHS
923 c = do_digraph(c);
924#endif
925
926#ifdef FEAT_INS_EXPAND
927 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
928 goto docomplete;
929#endif
930 if (c == Ctrl_V || c == Ctrl_Q)
931 {
932 ins_ctrl_v();
933 c = Ctrl_V; /* pretend CTRL-V is last typed character */
934 continue;
935 }
936
937#ifdef FEAT_CINDENT
938 if (cindent_on()
939# ifdef FEAT_INS_EXPAND
940 && ctrl_x_mode == 0
941# endif
942 )
943 {
944 /* A key name preceded by a bang means this key is not to be
945 * inserted. Skip ahead to the re-indenting below.
946 * A key name preceded by a star means that indenting has to be
947 * done before inserting the key. */
948 line_is_white = inindent(0);
949 if (in_cinkeys(c, '!', line_is_white))
950 goto force_cindent;
951 if (can_cindent && in_cinkeys(c, '*', line_is_white)
952 && stop_arrow() == OK)
953 do_c_expr_indent();
954 }
955#endif
956
957#ifdef FEAT_RIGHTLEFT
958 if (curwin->w_p_rl)
959 switch (c)
960 {
961 case K_LEFT: c = K_RIGHT; break;
962 case K_S_LEFT: c = K_S_RIGHT; break;
963 case K_C_LEFT: c = K_C_RIGHT; break;
964 case K_RIGHT: c = K_LEFT; break;
965 case K_S_RIGHT: c = K_S_LEFT; break;
966 case K_C_RIGHT: c = K_C_LEFT; break;
967 }
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 /*
971 * If 'keymodel' contains "startsel", may start selection. If it
972 * does, a CTRL-O and c will be stuffed, we need to get these
973 * characters.
974 */
975 if (ins_start_select(c))
976 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * The big switch to handle a character in insert mode.
980 */
981 switch (c)
982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (echeck_abbr(ESC + ABBR_OFF))
985 break;
986 /*FALLTHROUGH*/
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_CMDWIN
990 if (c == Ctrl_C && cmdwin_type != 0)
991 {
992 /* Close the cmdline window. */
993 cmdwin_result = K_IGNORE;
994 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000995 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 goto doESCkey;
997 }
998#endif
999
1000#ifdef UNIX
1001do_intr:
1002#endif
1003 /* when 'insertmode' set, and not halfway a mapping, don't leave
1004 * Insert mode */
1005 if (goto_im())
1006 {
1007 if (got_int)
1008 {
1009 (void)vgetc(); /* flush all buffers */
1010 got_int = FALSE;
1011 }
1012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001013 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015 }
1016doESCkey:
1017 /*
1018 * This is the ONLY return from edit()!
1019 */
1020 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1021 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001022 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 o_lnum = curwin->w_cursor.lnum;
1024
Bram Moolenaar488c6512005-08-11 20:09:58 +00001025 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001026 {
1027#ifdef FEAT_AUTOCMD
1028 if (cmdchar != 'r' && cmdchar != 'v')
1029 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1030 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001031 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 continue;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_Z: /* suspend when 'insertmode' set */
1038 if (!p_im)
1039 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001040 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001041#ifdef CURSOR_SHAPE
1042 ui_cursor_shape(); /* may need to update cursor shape */
1043#endif
1044 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045
1046 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001047#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001048 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 goto docomplete;
1050#endif
1051 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1052 break;
1053 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001054
1055#ifdef FEAT_VIRTUALEDIT
1056 /* don't move the cursor left when 'virtualedit' has "onemore". */
1057 if (ve_flags & VE_ONEMORE)
1058 {
1059 ins_at_eol = FALSE;
1060 nomove = TRUE;
1061 }
1062#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 count = 0;
1064 goto doESCkey;
1065
Bram Moolenaar572cb562005-08-05 21:35:02 +00001066 case K_INS: /* toggle insert/replace mode */
1067 case K_KINS:
1068 ins_insert(replaceState);
1069 break;
1070
1071 case K_SELECT: /* end of Select mode mapping - ignore */
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_HELP: /* Help key works like <ESC> <Help> */
1075 case K_F1:
1076 case K_XF1:
1077 stuffcharReadbuff(K_HELP);
1078 if (p_im)
1079 need_start_insertmode = TRUE;
1080 goto doESCkey;
1081
1082#ifdef FEAT_NETBEANS_INTG
1083 case K_F21: /* NetBeans command */
1084 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001085 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 --no_mapping;
1087 netbeans_keycommand(i);
1088 break;
1089#endif
1090
1091 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case NUL:
1093 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 /* For ^@ the trailing ESC will end the insert, unless there is an
1095 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1097 && c != Ctrl_A && !p_im)
1098 goto doESCkey; /* quit insert mode */
1099 inserted_space = FALSE;
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ins_reg();
1104 auto_format(FALSE, TRUE);
1105 inserted_space = FALSE;
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_ctrl_g();
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_HAT: /* switch input mode and/or langmap */
1113 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 break;
1115
1116#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (!p_ari)
1119 goto normalchar;
1120 ins_ctrl_();
1121 break;
1122#endif
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1126 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1127 goto docomplete;
1128#endif
1129 /* FALLTHROUGH */
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132# ifdef FEAT_INS_EXPAND
1133 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 if (has_compl_option(FALSE))
1136 goto docomplete;
1137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139# endif
1140 ins_shift(c, lastc);
1141 auto_format(FALSE, TRUE);
1142 inserted_space = FALSE;
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_KDEL:
1147 ins_del();
1148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case Ctrl_H:
1153 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1159 auto_format(FALSE, TRUE);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001163# ifdef FEAT_COMPL_FUNC
1164 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001166 goto docomplete;
1167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1169 auto_format(FALSE, TRUE);
1170 inserted_space = FALSE;
1171 break;
1172
1173#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_LEFTMOUSE_NM:
1176 case K_LEFTDRAG:
1177 case K_LEFTRELEASE:
1178 case K_LEFTRELEASE_NM:
1179 case K_MIDDLEMOUSE:
1180 case K_MIDDLEDRAG:
1181 case K_MIDDLERELEASE:
1182 case K_RIGHTMOUSE:
1183 case K_RIGHTDRAG:
1184 case K_RIGHTRELEASE:
1185 case K_X1MOUSE:
1186 case K_X1DRAG:
1187 case K_X1RELEASE:
1188 case K_X2MOUSE:
1189 case K_X2DRAG:
1190 case K_X2RELEASE:
1191 ins_mouse(c);
1192 break;
1193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001194 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001195 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001199 ins_mousescroll(MSCR_UP);
1200 break;
1201
1202 case K_MOUSELEFT: /* Scroll wheel left */
1203 ins_mousescroll(MSCR_LEFT);
1204 break;
1205
1206 case K_MOUSERIGHT: /* Scroll wheel right */
1207 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 break;
1209#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001210 case K_PS:
1211 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1212 if (cmdchar == K_PS)
1213 /* invoked from normal mode, bail out */
1214 goto doESCkey;
1215 break;
1216 case K_PE:
1217 /* Got K_PE without K_PS, ignore. */
1218 break;
1219
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001220#ifdef FEAT_GUI_TABLINE
1221 case K_TABLINE:
1222 case K_TABMENU:
1223 ins_tabline(c);
1224 break;
1225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 break;
1229
Bram Moolenaar754b5602006-02-09 23:53:20 +00001230#ifdef FEAT_AUTOCMD
1231 case K_CURSORHOLD: /* Didn't type something for a while. */
1232 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1233 did_cursorhold = TRUE;
1234 break;
1235#endif
1236
Bram Moolenaar4770d092006-01-12 23:22:24 +00001237#ifdef FEAT_GUI_W32
1238 /* On Win32 ignore <M-F4>, we get it when closing the window was
1239 * cancelled. */
1240 case K_F4:
1241 if (mod_mask != MOD_MASK_ALT)
1242 goto normalchar;
1243 break;
1244#endif
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#ifdef FEAT_GUI
1247 case K_VER_SCROLLBAR:
1248 ins_scroll();
1249 break;
1250
1251 case K_HOR_SCROLLBAR:
1252 ins_horscroll();
1253 break;
1254#endif
1255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 case K_S_HOME:
1259 case K_C_HOME:
1260 ins_home(c);
1261 break;
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_S_END:
1266 case K_C_END:
1267 ins_end(c);
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001271 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1272 ins_s_left();
1273 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001274 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 break;
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 case K_C_LEFT:
1279 ins_s_left();
1280 break;
1281
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001282 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001283 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1284 ins_s_right();
1285 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001286 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 case K_C_RIGHT:
1291 ins_s_right();
1292 break;
1293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001295#ifdef FEAT_INS_EXPAND
1296 if (pum_visible())
1297 goto docomplete;
1298#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001299 if (mod_mask & MOD_MASK_SHIFT)
1300 ins_pageup();
1301 else
1302 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 break;
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 case K_PAGEUP:
1307 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001308#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001309 if (pum_visible())
1310 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ins_pageup();
1313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001316#ifdef FEAT_INS_EXPAND
1317 if (pum_visible())
1318 goto docomplete;
1319#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001320 if (mod_mask & MOD_MASK_SHIFT)
1321 ins_pagedown();
1322 else
1323 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 break;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 case K_PAGEDOWN:
1328 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001329#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001330 if (pum_visible())
1331 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 ins_pagedown();
1334 break;
1335
1336#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 ins_drop();
1339 break;
1340#endif
1341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001342 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 c = TAB;
1344 /* FALLTHROUGH */
1345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001346 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1348 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1349 goto docomplete;
1350#endif
1351 inserted_space = FALSE;
1352 if (ins_tab())
1353 goto normalchar; /* insert TAB as a normal char */
1354 auto_format(FALSE, TRUE);
1355 break;
1356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001357 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 c = CAR;
1359 /* FALLTHROUGH */
1360 case CAR:
1361 case NL:
1362#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1363 /* In a quickfix window a <CR> jumps to the error under the
1364 * cursor. */
1365 if (bt_quickfix(curbuf) && c == CAR)
1366 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001367 if (curwin->w_llist_ref == NULL) /* quickfix window */
1368 do_cmdline_cmd((char_u *)".cc");
1369 else /* location list window */
1370 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 break;
1372 }
1373#endif
1374#ifdef FEAT_CMDWIN
1375 if (cmdwin_type != 0)
1376 {
1377 /* Execute the command in the cmdline window. */
1378 cmdwin_result = CAR;
1379 goto doESCkey;
1380 }
1381#endif
1382 if (ins_eol(c) && !p_im)
1383 goto doESCkey; /* out of memory */
1384 auto_format(FALSE, FALSE);
1385 inserted_space = FALSE;
1386 break;
1387
Bram Moolenaar860cae12010-06-05 23:22:07 +02001388#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001389 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390# ifdef FEAT_INS_EXPAND
1391 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1392 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001393 if (has_compl_option(TRUE))
1394 goto docomplete;
1395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397# endif
1398# ifdef FEAT_DIGRAPHS
1399 c = ins_digraph();
1400 if (c == NUL)
1401 break;
1402# endif
1403 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001407 case Ctrl_X: /* Enter CTRL-X mode */
1408 ins_ctrl_x();
1409 break;
1410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (ctrl_x_mode != CTRL_X_TAGS)
1413 goto normalchar;
1414 goto docomplete;
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (ctrl_x_mode != CTRL_X_FILES)
1418 goto normalchar;
1419 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001420
1421 case 's': /* Spelling completion after ^X */
1422 case Ctrl_S:
1423 if (ctrl_x_mode != CTRL_X_SPELL)
1424 goto normalchar;
1425 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#endif
1427
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001428 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429#ifdef FEAT_INS_EXPAND
1430 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1431#endif
1432 {
1433 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1434 if (p_im)
1435 {
1436 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1437 break;
1438 goto doESCkey;
1439 }
1440 goto normalchar;
1441 }
1442#ifdef FEAT_INS_EXPAND
1443 /* FALLTHROUGH */
1444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 case Ctrl_N:
1447 /* if 'complete' is empty then plain ^P is no longer special,
1448 * but it is under other ^X modes */
1449 if (*curbuf->b_p_cpt == NUL
1450 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 goto normalchar;
1453
1454docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001455 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001456#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001457 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001458#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001459 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001460 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001461#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001462 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001463#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001464 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466#endif /* FEAT_INS_EXPAND */
1467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 case Ctrl_Y: /* copy from previous line or scroll down */
1469 case Ctrl_E: /* copy from next line or scroll up */
1470 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 break;
1472
1473 default:
1474#ifdef UNIX
1475 if (c == intr_char) /* special interrupt char */
1476 goto do_intr;
1477#endif
1478
Bram Moolenaare659c952011-05-19 17:25:41 +02001479normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001481 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001483#ifdef FEAT_AUTOCMD
1484 if (!p_paste)
1485 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001486 /* Trigger InsertCharPre. */
1487 char_u *str = do_insert_char_pre(c);
1488 char_u *p;
1489
1490 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001491 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001492 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001493 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001494 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001495 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001496 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001497 c = PTR2CHAR(p);
1498 if (c == CAR || c == K_KENTER || c == NL)
1499 ins_eol(c);
1500 else
1501 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001502 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001503 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001504 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001505 vim_free(str);
1506 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001507 }
1508
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001509 /* If the new value is already inserted or an empty string
1510 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001511 if (c == NUL)
1512 break;
1513 }
1514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#ifdef FEAT_SMARTINDENT
1516 /* Try to perform smart-indenting. */
1517 ins_try_si(c);
1518#endif
1519
1520 if (c == ' ')
1521 {
1522 inserted_space = TRUE;
1523#ifdef FEAT_CINDENT
1524 if (inindent(0))
1525 can_cindent = FALSE;
1526#endif
1527 if (Insstart_blank_vcol == MAXCOL
1528 && curwin->w_cursor.lnum == Insstart.lnum)
1529 Insstart_blank_vcol = get_nolist_virtcol();
1530 }
1531
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001532 /* Insert a normal character and check for abbreviations on a
1533 * special character. Let CTRL-] expand abbreviations without
1534 * inserting it. */
1535 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#ifdef FEAT_MBYTE
1537 /* Add ABBR_OFF for characters above 0x100, this is
1538 * what check_abbr() expects. */
1539 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1540#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001541 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 insert_special(c, FALSE, FALSE);
1544#ifdef FEAT_RIGHTLEFT
1545 revins_legal++;
1546 revins_chars++;
1547#endif
1548 }
1549
1550 auto_format(FALSE, TRUE);
1551
1552#ifdef FEAT_FOLDING
1553 /* When inserting a character the cursor line must never be in a
1554 * closed fold. */
1555 foldOpenCursor();
1556#endif
1557 break;
1558 } /* end of switch (c) */
1559
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001560#ifdef FEAT_AUTOCMD
1561 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001562 if (c != K_CURSORHOLD
1563# ifdef FEAT_COMPL_FUNC
1564 /* but not in CTRL-X mode, a script can't restore the state */
1565 && ctrl_x_mode == 0
1566# endif
1567 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001568 did_cursorhold = FALSE;
1569#endif
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 /* If the cursor was moved we didn't just insert a space */
1572 if (arrow_used)
1573 inserted_space = FALSE;
1574
1575#ifdef FEAT_CINDENT
1576 if (can_cindent && cindent_on()
1577# ifdef FEAT_INS_EXPAND
1578 && ctrl_x_mode == 0
1579# endif
1580 )
1581 {
1582force_cindent:
1583 /*
1584 * Indent now if a key was typed that is in 'cinkeys'.
1585 */
1586 if (in_cinkeys(c, ' ', line_is_white))
1587 {
1588 if (stop_arrow() == OK)
1589 /* re-indent the current line */
1590 do_c_expr_indent();
1591 }
1592 }
1593#endif /* FEAT_CINDENT */
1594
1595 } /* for (;;) */
1596 /* NOTREACHED */
1597}
1598
1599/*
1600 * Redraw for Insert mode.
1601 * This is postponed until getting the next character to make '$' in the 'cpo'
1602 * option work correctly.
1603 * Only redraw when there are no characters available. This speeds up
1604 * inserting sequences of characters (e.g., for CTRL-R).
1605 */
1606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607ins_redraw(
1608 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001610#ifdef FEAT_CONCEAL
1611 linenr_T conceal_old_cursor_line = 0;
1612 linenr_T conceal_new_cursor_line = 0;
1613 int conceal_update_lines = FALSE;
1614#endif
1615
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001616 if (char_avail())
1617 return;
1618
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001619#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1621 * visible, the command might delete it. */
1622 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001623# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001625# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001626# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001628# endif
1629# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001630 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001631# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001633# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001634 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001635# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001636# ifdef FEAT_INS_EXPAND
1637 && !pum_visible()
1638# endif
1639 )
1640 {
1641# ifdef FEAT_SYN_HL
1642 /* Need to update the screen first, to make sure syntax
1643 * highlighting is correct after making a change (e.g., inserting
1644 * a "(". The autocommand may also require a redraw, so it's done
1645 * again below, unfortunately. */
1646 if (syntax_present(curwin) && must_redraw)
1647 update_screen(0);
1648# endif
1649# ifdef FEAT_AUTOCMD
1650 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001651 {
1652 /* Make sure curswant is correct, an autocommand may call
1653 * getcurpos(). */
1654 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001655 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001656 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001657# endif
1658# ifdef FEAT_CONCEAL
1659 if (curwin->w_p_cole > 0)
1660 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001661# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001662 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001663# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664 conceal_new_cursor_line = curwin->w_cursor.lnum;
1665 conceal_update_lines = TRUE;
1666 }
1667# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001668# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 }
1672#endif
1673
1674#ifdef FEAT_AUTOCMD
1675 /* Trigger TextChangedI if b_changedtick differs. */
1676 if (ready && has_textchangedI()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001677 && last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001678# ifdef FEAT_INS_EXPAND
1679 && !pum_visible()
1680# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681 )
1682 {
1683 if (last_changedtick_buf == curbuf)
1684 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1685 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001686 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001688#endif
1689
1690 if (must_redraw)
1691 update_screen(0);
1692 else if (clear_cmdline || redraw_cmdline)
1693 showmode(); /* clear cmdline and show mode */
1694# if defined(FEAT_CONCEAL)
1695 if ((conceal_update_lines
1696 && (conceal_old_cursor_line != conceal_new_cursor_line
1697 || conceal_cursor_line(curwin)))
1698 || need_cursor_line_redraw)
1699 {
1700 if (conceal_old_cursor_line != conceal_new_cursor_line)
1701 update_single_line(curwin, conceal_old_cursor_line);
1702 update_single_line(curwin, conceal_new_cursor_line == 0
1703 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1704 curwin->w_valid &= ~VALID_CROW;
1705 }
1706# endif
1707 showruler(FALSE);
1708 setcursor();
1709 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710}
1711
1712/*
1713 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1714 */
1715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001716ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001719 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001722 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001727 did_putchar = TRUE;
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1730
1731#ifdef FEAT_CMDL_INFO
1732 add_to_showcmd_c(Ctrl_V);
1733#endif
1734
1735 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001736 if (did_putchar)
1737 /* when the line fits in 'columns' the '^' is at the start of the next
1738 * line and will not removed by the redraw */
1739 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740#ifdef FEAT_CMDL_INFO
1741 clear_showcmd();
1742#endif
1743 insert_special(c, FALSE, TRUE);
1744#ifdef FEAT_RIGHTLEFT
1745 revins_chars++;
1746 revins_legal++;
1747#endif
1748}
1749
1750/*
1751 * Put a character directly onto the screen. It's not stored in a buffer.
1752 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1753 */
1754static int pc_status;
1755#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1756#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1757#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1758#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760static int pc_attr;
1761static int pc_row;
1762static int pc_col;
1763
1764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 int attr;
1768
1769 if (ScreenLines != NULL)
1770 {
1771 update_topline(); /* just in case w_topline isn't valid */
1772 validate_cursor();
1773 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001774 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 else
1776 attr = 0;
1777 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1778 pc_col = W_WINCOL(curwin);
1779#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1780 pc_status = PC_STATUS_UNSET;
1781#endif
1782#ifdef FEAT_RIGHTLEFT
1783 if (curwin->w_p_rl)
1784 {
1785 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1786# ifdef FEAT_MBYTE
1787 if (has_mbyte)
1788 {
1789 int fix_col = mb_fix_col(pc_col, pc_row);
1790
1791 if (fix_col != pc_col)
1792 {
1793 screen_putchar(' ', pc_row, fix_col, attr);
1794 --curwin->w_wcol;
1795 pc_status = PC_STATUS_RIGHT;
1796 }
1797 }
1798# endif
1799 }
1800 else
1801#endif
1802 {
1803 pc_col += curwin->w_wcol;
1804#ifdef FEAT_MBYTE
1805 if (mb_lefthalve(pc_row, pc_col))
1806 pc_status = PC_STATUS_LEFT;
1807#endif
1808 }
1809
1810 /* save the character to be able to put it back */
1811#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1812 if (pc_status == PC_STATUS_UNSET)
1813#endif
1814 {
1815 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1816 pc_status = PC_STATUS_SET;
1817 }
1818 screen_putchar(c, pc_row, pc_col, attr);
1819 }
1820}
1821
1822/*
1823 * Undo the previous edit_putchar().
1824 */
1825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1829 {
1830#if defined(FEAT_MBYTE)
1831 if (pc_status == PC_STATUS_RIGHT)
1832 ++curwin->w_wcol;
1833 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1834 redrawWinline(curwin->w_cursor.lnum, FALSE);
1835 else
1836#endif
1837 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1838 }
1839}
1840
1841/*
1842 * Called when p_dollar is set: display a '$' at the end of the changed text
1843 * Only works when cursor is in the line that changes.
1844 */
1845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 colnr_T save_col;
1849
1850 if (!redrawing())
1851 return;
1852
1853 cursor_off();
1854 save_col = curwin->w_cursor.col;
1855 curwin->w_cursor.col = col;
1856#ifdef FEAT_MBYTE
1857 if (has_mbyte)
1858 {
1859 char_u *p;
1860
1861 /* If on the last byte of a multi-byte move to the first byte. */
1862 p = ml_get_curline();
1863 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1864 }
1865#endif
1866 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1867 if (curwin->w_wcol < W_WIDTH(curwin))
1868 {
1869 edit_putchar('$', FALSE);
1870 dollar_vcol = curwin->w_virtcol;
1871 }
1872 curwin->w_cursor.col = save_col;
1873}
1874
1875/*
1876 * Call this function before moving the cursor from the normal insert position
1877 * in insert mode.
1878 */
1879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001880undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001882 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001884 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 redrawWinline(curwin->w_cursor.lnum, FALSE);
1886 }
1887}
1888
1889/*
1890 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1891 * Keep the cursor on the same character.
1892 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1893 * type == INDENT_DEC decrease indent (for CTRL-D)
1894 * type == INDENT_SET set indent to "amount"
1895 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1896 */
1897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898change_indent(
1899 int type,
1900 int amount,
1901 int round,
1902 int replaced, /* replaced character, put on replace stack */
1903 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
1905 int vcol;
1906 int last_vcol;
1907 int insstart_less; /* reduction for Insstart.col */
1908 int new_cursor_col;
1909 int i;
1910 char_u *ptr;
1911 int save_p_list;
1912 int start_col;
1913 colnr_T vc;
1914#ifdef FEAT_VREPLACE
1915 colnr_T orig_col = 0; /* init for GCC */
1916 char_u *new_line, *orig_line = NULL; /* init for GCC */
1917
1918 /* VREPLACE mode needs to know what the line was like before changing */
1919 if (State & VREPLACE_FLAG)
1920 {
1921 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1922 orig_col = curwin->w_cursor.col;
1923 }
1924#endif
1925
1926 /* for the following tricks we don't want list mode */
1927 save_p_list = curwin->w_p_list;
1928 curwin->w_p_list = FALSE;
1929 vc = getvcol_nolist(&curwin->w_cursor);
1930 vcol = vc;
1931
1932 /*
1933 * For Replace mode we need to fix the replace stack later, which is only
1934 * possible when the cursor is in the indent. Remember the number of
1935 * characters before the cursor if it's possible.
1936 */
1937 start_col = curwin->w_cursor.col;
1938
1939 /* determine offset from first non-blank */
1940 new_cursor_col = curwin->w_cursor.col;
1941 beginline(BL_WHITE);
1942 new_cursor_col -= curwin->w_cursor.col;
1943
1944 insstart_less = curwin->w_cursor.col;
1945
1946 /*
1947 * If the cursor is in the indent, compute how many screen columns the
1948 * cursor is to the left of the first non-blank.
1949 */
1950 if (new_cursor_col < 0)
1951 vcol = get_indent() - vcol;
1952
1953 if (new_cursor_col > 0) /* can't fix replace stack */
1954 start_col = -1;
1955
1956 /*
1957 * Set the new indent. The cursor will be put on the first non-blank.
1958 */
1959 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001960 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 else
1962 {
1963#ifdef FEAT_VREPLACE
1964 int save_State = State;
1965
1966 /* Avoid being called recursively. */
1967 if (State & VREPLACE_FLAG)
1968 State = INSERT;
1969#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001970 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#ifdef FEAT_VREPLACE
1972 State = save_State;
1973#endif
1974 }
1975 insstart_less -= curwin->w_cursor.col;
1976
1977 /*
1978 * Try to put cursor on same character.
1979 * If the cursor is at or after the first non-blank in the line,
1980 * compute the cursor column relative to the column of the first
1981 * non-blank character.
1982 * If we are not in insert mode, leave the cursor on the first non-blank.
1983 * If the cursor is before the first non-blank, position it relative
1984 * to the first non-blank, counted in screen columns.
1985 */
1986 if (new_cursor_col >= 0)
1987 {
1988 /*
1989 * When changing the indent while the cursor is touching it, reset
1990 * Insstart_col to 0.
1991 */
1992 if (new_cursor_col == 0)
1993 insstart_less = MAXCOL;
1994 new_cursor_col += curwin->w_cursor.col;
1995 }
1996 else if (!(State & INSERT))
1997 new_cursor_col = curwin->w_cursor.col;
1998 else
1999 {
2000 /*
2001 * Compute the screen column where the cursor should be.
2002 */
2003 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002004 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006 /*
2007 * Advance the cursor until we reach the right screen column.
2008 */
2009 vcol = last_vcol = 0;
2010 new_cursor_col = -1;
2011 ptr = ml_get_curline();
2012 while (vcol <= (int)curwin->w_virtcol)
2013 {
2014 last_vcol = vcol;
2015#ifdef FEAT_MBYTE
2016 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002017 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else
2019#endif
2020 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002021 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 vcol = last_vcol;
2024
2025 /*
2026 * May need to insert spaces to be able to position the cursor on
2027 * the right screen column.
2028 */
2029 if (vcol != (int)curwin->w_virtcol)
2030 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002031 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002033 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (ptr != NULL)
2035 {
2036 new_cursor_col += i;
2037 ptr[i] = NUL;
2038 while (--i >= 0)
2039 ptr[i] = ' ';
2040 ins_str(ptr);
2041 vim_free(ptr);
2042 }
2043 }
2044
2045 /*
2046 * When changing the indent while the cursor is in it, reset
2047 * Insstart_col to 0.
2048 */
2049 insstart_less = MAXCOL;
2050 }
2051
2052 curwin->w_p_list = save_p_list;
2053
2054 if (new_cursor_col <= 0)
2055 curwin->w_cursor.col = 0;
2056 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002057 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 curwin->w_set_curswant = TRUE;
2059 changed_cline_bef_curs();
2060
2061 /*
2062 * May have to adjust the start of the insert.
2063 */
2064 if (State & INSERT)
2065 {
2066 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2067 {
2068 if ((int)Insstart.col <= insstart_less)
2069 Insstart.col = 0;
2070 else
2071 Insstart.col -= insstart_less;
2072 }
2073 if ((int)ai_col <= insstart_less)
2074 ai_col = 0;
2075 else
2076 ai_col -= insstart_less;
2077 }
2078
2079 /*
2080 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2081 * If the number of characters before the cursor decreased, need to pop a
2082 * few characters from the replace stack.
2083 * If the number of characters before the cursor increased, need to push a
2084 * few NULs onto the replace stack.
2085 */
2086 if (REPLACE_NORMAL(State) && start_col >= 0)
2087 {
2088 while (start_col > (int)curwin->w_cursor.col)
2089 {
2090 replace_join(0); /* remove a NUL from the replace stack */
2091 --start_col;
2092 }
2093 while (start_col < (int)curwin->w_cursor.col || replaced)
2094 {
2095 replace_push(NUL);
2096 if (replaced)
2097 {
2098 replace_push(replaced);
2099 replaced = NUL;
2100 }
2101 ++start_col;
2102 }
2103 }
2104
2105#ifdef FEAT_VREPLACE
2106 /*
2107 * For VREPLACE mode, we also have to fix the replace stack. In this case
2108 * it is always possible because we backspace over the whole line and then
2109 * put it back again the way we wanted it.
2110 */
2111 if (State & VREPLACE_FLAG)
2112 {
2113 /* If orig_line didn't allocate, just return. At least we did the job,
2114 * even if you can't backspace. */
2115 if (orig_line == NULL)
2116 return;
2117
2118 /* Save new line */
2119 new_line = vim_strsave(ml_get_curline());
2120 if (new_line == NULL)
2121 return;
2122
2123 /* We only put back the new line up to the cursor */
2124 new_line[curwin->w_cursor.col] = NUL;
2125
2126 /* Put back original line */
2127 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2128 curwin->w_cursor.col = orig_col;
2129
2130 /* Backspace from cursor to start of line */
2131 backspace_until_column(0);
2132
2133 /* Insert new stuff into line again */
2134 ins_bytes(new_line);
2135
2136 vim_free(new_line);
2137 }
2138#endif
2139}
2140
2141/*
2142 * Truncate the space at the end of a line. This is to be used only in an
2143 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2144 * modes.
2145 */
2146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002147truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int i;
2150
2151 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002152 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 if (State & REPLACE_FLAG)
2155 replace_join(0); /* remove a NUL from the replace stack */
2156 }
2157 line[i + 1] = NUL;
2158}
2159
2160#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2161 || defined(FEAT_COMMENTS) || defined(PROTO)
2162/*
2163 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2164 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002165 * Will attempt not to go before "col" even when there is a composing
2166 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
2168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
2171 while ((int)curwin->w_cursor.col > col)
2172 {
2173 curwin->w_cursor.col--;
2174 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002175 replace_do_bs(col);
2176 else if (!del_char_after_col(col))
2177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179}
2180#endif
2181
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002182/*
2183 * Like del_char(), but make sure not to go before column "limit_col".
2184 * Only matters when there are composing characters.
2185 * Return TRUE when something was deleted.
2186 */
2187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002189{
2190#ifdef FEAT_MBYTE
2191 if (enc_utf8 && limit_col >= 0)
2192 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002193 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002194
2195 /* Make sure the cursor is at the start of a character, but
2196 * skip forward again when going too far back because of a
2197 * composing character. */
2198 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002199 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002200 {
2201 int l = utf_ptr2len(ml_get_cursor());
2202
2203 if (l == 0) /* end of line */
2204 break;
2205 curwin->w_cursor.col += l;
2206 }
2207 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2208 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002209 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002210 }
2211 else
2212#endif
2213 (void)del_char(FALSE);
2214 return TRUE;
2215}
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2218/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002219 * CTRL-X pressed in Insert mode.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002223{
2224 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2225 * CTRL-V works like CTRL-N */
2226 if (ctrl_x_mode != CTRL_X_CMDLINE)
2227 {
2228 /* if the next ^X<> won't ADD nothing, then reset
2229 * compl_cont_status */
2230 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002231 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002232 else
2233 compl_cont_status = 0;
2234 /* We're not sure which CTRL-X mode it will be yet */
2235 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2236 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2237 edit_submode_pre = NULL;
2238 showmode();
2239 }
2240}
2241
2242/*
2243 * Return TRUE if the 'dict' or 'tsr' option can be used.
2244 */
2245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002246has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002247{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002248 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002249# ifdef FEAT_SPELL
2250 && !curwin->w_p_spell
2251# endif
2252 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002253 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2254 {
2255 ctrl_x_mode = 0;
2256 edit_submode = NULL;
2257 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2258 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002259 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002260 if (emsg_silent == 0)
2261 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002262 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002263 setcursor();
2264 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002265#ifdef FEAT_EVAL
2266 if (!get_vim_var_nr(VV_TESTING))
2267#endif
2268 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002269 }
2270 return FALSE;
2271 }
2272 return TRUE;
2273}
2274
2275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2277 * This depends on the current mode.
2278 */
2279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002280vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
2282 /* Always allow ^R - let it's results then be checked */
2283 if (c == Ctrl_R)
2284 return TRUE;
2285
Bram Moolenaare3226be2005-12-18 22:10:00 +00002286 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002287 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002288 return TRUE;
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 switch (ctrl_x_mode)
2291 {
2292 case 0: /* Not in any CTRL-X mode */
2293 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2294 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2297 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2298 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002299 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002300 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 case CTRL_X_SCROLL:
2302 return (c == Ctrl_Y || c == Ctrl_E);
2303 case CTRL_X_WHOLE_LINE:
2304 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2305 case CTRL_X_FILES:
2306 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2307 case CTRL_X_DICTIONARY:
2308 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2309 case CTRL_X_THESAURUS:
2310 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2311 case CTRL_X_TAGS:
2312 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2313#ifdef FEAT_FIND_ID
2314 case CTRL_X_PATH_PATTERNS:
2315 return (c == Ctrl_P || c == Ctrl_N);
2316 case CTRL_X_PATH_DEFINES:
2317 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2318#endif
2319 case CTRL_X_CMDLINE:
2320 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2321 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002322#ifdef FEAT_COMPL_FUNC
2323 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002324 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002325 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002326 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002327#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002328 case CTRL_X_SPELL:
2329 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002330 case CTRL_X_EVAL:
2331 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002333 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 return FALSE;
2335}
2336
2337/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002338 * Return TRUE when character "c" is part of the item currently being
2339 * completed. Used to decide whether to abandon complete mode when the menu
2340 * is visible.
2341 */
2342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002343ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002344{
2345 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2346 /* When expanding an identifier only accept identifier chars. */
2347 return vim_isIDc(c);
2348
2349 switch (ctrl_x_mode)
2350 {
2351 case CTRL_X_FILES:
2352 /* When expanding file name only accept file name chars. But not
2353 * path separators, so that "proto/<Tab>" expands files in
2354 * "proto", not "proto/" as a whole */
2355 return vim_isfilec(c) && !vim_ispathsep(c);
2356
2357 case CTRL_X_CMDLINE:
2358 case CTRL_X_OMNI:
2359 /* Command line and Omni completion can work with just about any
2360 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002361 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002362
2363 case CTRL_X_WHOLE_LINE:
2364 /* For while line completion a space can be part of the line. */
2365 return vim_isprintc(c);
2366 }
2367 return vim_iswordc(c);
2368}
2369
2370/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002371 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002373 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * the rest of the word to be in -- webb
2375 */
2376 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002377ins_compl_add_infercase(
2378 char_u *str,
2379 int len,
2380 int icase,
2381 char_u *fname,
2382 int dir,
2383 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 char_u *p;
2386 int i, c;
2387 int actual_len; /* Take multi-byte characters */
2388 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002389 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002390 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 int has_lower = FALSE;
2392 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002394 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002396 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaara2993e12007-08-12 14:38:46 +00002398 /* Find actual length of completion. */
2399#ifdef FEAT_MBYTE
2400 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002402 p = str;
2403 actual_len = 0;
2404 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002406 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002407 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 else
2411#endif
2412 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaara2993e12007-08-12 14:38:46 +00002414 /* Find actual length of original text. */
2415#ifdef FEAT_MBYTE
2416 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002418 p = compl_orig_text;
2419 actual_compl_length = 0;
2420 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002422 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002423 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002426 else
2427#endif
2428 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002430 /* "actual_len" may be smaller than "actual_compl_length" when using
2431 * thesaurus, only use the minimum when comparing. */
2432 min_len = actual_len < actual_compl_length
2433 ? actual_len : actual_compl_length;
2434
Bram Moolenaara2993e12007-08-12 14:38:46 +00002435 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002436 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002437 if (wca != NULL)
2438 {
2439 p = str;
2440 for (i = 0; i < actual_len; ++i)
2441#ifdef FEAT_MBYTE
2442 if (has_mbyte)
2443 wca[i] = mb_ptr2char_adv(&p);
2444 else
2445#endif
2446 wca[i] = *(p++);
2447
2448 /* Rule 1: Were any chars converted to lower? */
2449 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002450 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002451 {
2452#ifdef FEAT_MBYTE
2453 if (has_mbyte)
2454 c = mb_ptr2char_adv(&p);
2455 else
2456#endif
2457 c = *(p++);
2458 if (MB_ISLOWER(c))
2459 {
2460 has_lower = TRUE;
2461 if (MB_ISUPPER(wca[i]))
2462 {
2463 /* Rule 1 is satisfied. */
2464 for (i = actual_compl_length; i < actual_len; ++i)
2465 wca[i] = MB_TOLOWER(wca[i]);
2466 break;
2467 }
2468 }
2469 }
2470
2471 /*
2472 * Rule 2: No lower case, 2nd consecutive letter converted to
2473 * upper case.
2474 */
2475 if (!has_lower)
2476 {
2477 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002478 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002479 {
2480#ifdef FEAT_MBYTE
2481 if (has_mbyte)
2482 c = mb_ptr2char_adv(&p);
2483 else
2484#endif
2485 c = *(p++);
2486 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2487 {
2488 /* Rule 2 is satisfied. */
2489 for (i = actual_compl_length; i < actual_len; ++i)
2490 wca[i] = MB_TOUPPER(wca[i]);
2491 break;
2492 }
2493 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2494 }
2495 }
2496
2497 /* Copy the original case of the part we typed. */
2498 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002499 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002500 {
2501#ifdef FEAT_MBYTE
2502 if (has_mbyte)
2503 c = mb_ptr2char_adv(&p);
2504 else
2505#endif
2506 c = *(p++);
2507 if (MB_ISLOWER(c))
2508 wca[i] = MB_TOLOWER(wca[i]);
2509 else if (MB_ISUPPER(c))
2510 wca[i] = MB_TOUPPER(wca[i]);
2511 }
2512
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002513 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002514 * Generate encoding specific output from wide character array.
2515 * Multi-byte characters can occupy up to five bytes more than
2516 * ASCII characters, and we also need one byte for NUL, so stay
2517 * six bytes away from the edge of IObuff.
2518 */
2519 p = IObuff;
2520 i = 0;
2521 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2522#ifdef FEAT_MBYTE
2523 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002524 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002525 else
2526#endif
2527 *(p++) = wca[i++];
2528 *p = NUL;
2529
2530 vim_free(wca);
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002533 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2534 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002536 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537}
2538
2539/*
2540 * Add a match to the list of matches.
2541 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002542 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546ins_compl_add(
2547 char_u *str,
2548 int len,
2549 int icase,
2550 char_u *fname,
2551 char_u **cptext, /* extra text for popup menu or NULL */
2552 int cdir,
2553 int flags,
2554 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002556 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 ui_breakcheck();
2560 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (len < 0)
2563 len = (int)STRLEN(str);
2564
2565 /*
2566 * If the same match is already present, don't add it.
2567 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002568 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002570 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 do
2572 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002573 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002574 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002575 && match->cp_str[len] == NUL)
2576 return NOTDONE;
2577 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002578 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002581 /* Remove any popup menu before changing the list of matches. */
2582 ins_compl_del_pum();
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 /*
2585 * Allocate a new match structure.
2586 * Copy the values to the new match structure.
2587 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002588 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002590 return FAIL;
2591 match->cp_number = -1;
2592 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002594 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002599 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002602 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2604 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002605 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002606 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002607 && compl_curr_match->cp_fname != NULL
2608 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002609 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002610 else if (fname != NULL)
2611 {
2612 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002613 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002616 match->cp_fname = NULL;
2617 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002618
2619 if (cptext != NULL)
2620 {
2621 int i;
2622
2623 for (i = 0; i < CPT_COUNT; ++i)
2624 if (cptext[i] != NULL && *cptext[i] != NUL)
2625 match->cp_text[i] = vim_strsave(cptext[i]);
2626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 /*
2629 * Link the new match structure in the list of matches.
2630 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002631 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002632 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 else if (dir == FORWARD)
2634 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002635 match->cp_next = compl_curr_match->cp_next;
2636 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 else /* BACKWARD */
2639 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002640 match->cp_next = compl_curr_match;
2641 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002643 if (match->cp_next)
2644 match->cp_next->cp_prev = match;
2645 if (match->cp_prev)
2646 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002648 compl_first_match = match;
2649 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002651 /*
2652 * Find the longest common string if still doing that.
2653 */
2654 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2655 ins_compl_longest_match(match);
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return OK;
2658}
2659
2660/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002661 * Return TRUE if "str[len]" matches with match->cp_str, considering
2662 * match->cp_icase.
2663 */
2664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002665ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002666{
2667 if (match->cp_icase)
2668 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2669 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2670}
2671
2672/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002673 * Reduce the longest common string for match "match".
2674 */
2675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002676ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002677{
2678 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002679 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002680 int had_match;
2681
2682 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002683 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002684 /* First match, use it as a whole. */
2685 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002686 if (compl_leader != NULL)
2687 {
2688 had_match = (curwin->w_cursor.col > compl_col);
2689 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002690 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002691 ins_redraw(FALSE);
2692
2693 /* When the match isn't there (to avoid matching itself) remove it
2694 * again after redrawing. */
2695 if (!had_match)
2696 ins_compl_delete();
2697 compl_used_match = FALSE;
2698 }
2699 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002700 else
2701 {
2702 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703 p = compl_leader;
2704 s = match->cp_str;
2705 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002706 {
2707#ifdef FEAT_MBYTE
2708 if (has_mbyte)
2709 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002710 c1 = mb_ptr2char(p);
2711 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002712 }
2713 else
2714#endif
2715 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002716 c1 = *p;
2717 c2 = *s;
2718 }
2719 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2720 : (c1 != c2))
2721 break;
2722#ifdef FEAT_MBYTE
2723 if (has_mbyte)
2724 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002725 MB_PTR_ADV(p);
2726 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002727 }
2728 else
2729#endif
2730 {
2731 ++p;
2732 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002733 }
2734 }
2735
2736 if (*p != NUL)
2737 {
2738 /* Leader was shortened, need to change the inserted text. */
2739 *p = NUL;
2740 had_match = (curwin->w_cursor.col > compl_col);
2741 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002742 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002743 ins_redraw(FALSE);
2744
2745 /* When the match isn't there (to avoid matching itself) remove it
2746 * again after redrawing. */
2747 if (!had_match)
2748 ins_compl_delete();
2749 }
2750
2751 compl_used_match = FALSE;
2752 }
2753}
2754
2755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 * Add an array of matches to the list of matches.
2757 * Frees matches[].
2758 */
2759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002760ins_compl_add_matches(
2761 int num_matches,
2762 char_u **matches,
2763 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 int i;
2766 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002767 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar572cb562005-08-05 21:35:02 +00002769 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002770 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002771 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002773 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 FreeWild(num_matches, matches);
2775}
2776
2777/* Make the completion list cyclic.
2778 * Return the number of matches (excluding the original).
2779 */
2780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002781ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002783 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 int count = 0;
2785
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002786 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
2788 /*
2789 * Find the end of the list.
2790 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002791 match = compl_first_match;
2792 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002793 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002795 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 ++count;
2797 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002798 match->cp_next = compl_first_match;
2799 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 return count;
2802}
2803
Bram Moolenaara94bc432006-03-10 21:42:59 +00002804/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002805 * Set variables that store noselect and noinsert behavior from the
2806 * 'completeopt' value.
2807 */
2808 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002809completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002810{
2811 compl_no_insert = FALSE;
2812 compl_no_select = FALSE;
2813 if (strstr((char *)p_cot, "noselect") != NULL)
2814 compl_no_select = TRUE;
2815 if (strstr((char *)p_cot, "noinsert") != NULL)
2816 compl_no_insert = TRUE;
2817}
2818
2819/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002820 * Start completion for the complete() function.
2821 * "startcol" is where the matched text starts (1 is first column).
2822 * "list" is the list of matches.
2823 */
2824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002825set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002826{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002827 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002828 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002829
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830 /* If already doing completions stop it. */
2831 if (ctrl_x_mode != 0)
2832 ins_compl_prep(' ');
2833 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002834 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002835
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002836 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002837 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002838 startcol = curwin->w_cursor.col;
2839 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002840 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002841 /* compl_pattern doesn't need to be set */
2842 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2843 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002844 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002845 return;
2846
Bram Moolenaare4214502015-03-05 18:08:43 +01002847 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002848
2849 ins_compl_add_list(list);
2850 compl_matches = ins_compl_make_cyclic();
2851 compl_started = TRUE;
2852 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002853 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002854
2855 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002856 if (compl_no_insert || compl_no_select)
2857 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002858 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002859 if (compl_no_select)
2860 /* Down/Up has no real effect. */
2861 ins_complete(K_UP, FALSE);
2862 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002863 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002864 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002865 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002866
2867 /* Lazily show the popup menu, unless we got interrupted. */
2868 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002869 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002870 out_flush();
2871}
2872
2873
Bram Moolenaar9372a112005-12-06 19:59:18 +00002874/* "compl_match_array" points the currently displayed list of entries in the
2875 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002876static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002877static int compl_match_arraysize;
2878
2879/*
2880 * Update the screen and when there is any scrolling remove the popup menu.
2881 */
2882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002883ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002884{
2885 int h;
2886
2887 if (compl_match_array != NULL)
2888 {
2889 h = curwin->w_cline_height;
2890 update_screen(0);
2891 if (h != curwin->w_cline_height)
2892 ins_compl_del_pum();
2893 }
2894}
2895
2896/*
2897 * Remove any popup menu.
2898 */
2899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002901{
2902 if (compl_match_array != NULL)
2903 {
2904 pum_undisplay();
2905 vim_free(compl_match_array);
2906 compl_match_array = NULL;
2907 }
2908}
2909
2910/*
2911 * Return TRUE if the popup menu should be displayed.
2912 */
2913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002915{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002916 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002917 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002918 return FALSE;
2919
2920 /* The display looks bad on a B&W display. */
2921 if (t_colors < 8
2922#ifdef FEAT_GUI
2923 && !gui.in_use
2924#endif
2925 )
2926 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002927 return TRUE;
2928}
2929
2930/*
2931 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002932 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002933 */
2934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002936{
2937 compl_T *compl;
2938 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002939
2940 /* Don't display the popup menu if there are no matches or there is only
2941 * one (ignoring the original text). */
2942 compl = compl_first_match;
2943 i = 0;
2944 do
2945 {
2946 if (compl == NULL
2947 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2948 break;
2949 compl = compl->cp_next;
2950 } while (compl != compl_first_match);
2951
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002952 if (strstr((char *)p_cot, "menuone") != NULL)
2953 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002954 return (i >= 2);
2955}
2956
2957/*
2958 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002959 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002960 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002962ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002963{
2964 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002965 compl_T *shown_compl = NULL;
2966 int did_find_shown_match = FALSE;
2967 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002968 int i;
2969 int cur = -1;
2970 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002971 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002972
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002973 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002974 return;
2975
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002976#if defined(FEAT_EVAL)
2977 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2978 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2979#endif
2980
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002981 /* Update the screen before drawing the popup menu over it. */
2982 update_screen(0);
2983
2984 if (compl_match_array == NULL)
2985 {
2986 /* Need to build the popup menu list. */
2987 compl_match_arraysize = 0;
2988 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002989 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002990 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002991 do
2992 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002993 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2994 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002995 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996 ++compl_match_arraysize;
2997 compl = compl->cp_next;
2998 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002999 if (compl_match_arraysize == 0)
3000 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001 compl_match_array = (pumitem_T *)alloc_clear(
3002 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003 * compl_match_arraysize));
3004 if (compl_match_array != NULL)
3005 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003006 /* If the current match is the original text don't find the first
3007 * match after it, don't highlight anything. */
3008 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3009 shown_match_ok = TRUE;
3010
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003011 i = 0;
3012 compl = compl_first_match;
3013 do
3014 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003015 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3016 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003017 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003018 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003019 if (!shown_match_ok)
3020 {
3021 if (compl == compl_shown_match || did_find_shown_match)
3022 {
3023 /* This item is the shown match or this is the
3024 * first displayed item after the shown match. */
3025 compl_shown_match = compl;
3026 did_find_shown_match = TRUE;
3027 shown_match_ok = TRUE;
3028 }
3029 else
3030 /* Remember this displayed match for when the
3031 * shown match is just below it. */
3032 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003033 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003034 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003035
3036 if (compl->cp_text[CPT_ABBR] != NULL)
3037 compl_match_array[i].pum_text =
3038 compl->cp_text[CPT_ABBR];
3039 else
3040 compl_match_array[i].pum_text = compl->cp_str;
3041 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3042 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3043 if (compl->cp_text[CPT_MENU] != NULL)
3044 compl_match_array[i++].pum_extra =
3045 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003046 else
3047 compl_match_array[i++].pum_extra = compl->cp_fname;
3048 }
3049
3050 if (compl == compl_shown_match)
3051 {
3052 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003053
3054 /* When the original text is the shown match don't set
3055 * compl_shown_match. */
3056 if (compl->cp_flags & ORIGINAL_TEXT)
3057 shown_match_ok = TRUE;
3058
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003059 if (!shown_match_ok && shown_compl != NULL)
3060 {
3061 /* The shown match isn't displayed, set it to the
3062 * previously displayed match. */
3063 compl_shown_match = shown_compl;
3064 shown_match_ok = TRUE;
3065 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003066 }
3067 compl = compl->cp_next;
3068 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003069
3070 if (!shown_match_ok) /* no displayed match at all */
3071 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003072 }
3073 }
3074 else
3075 {
3076 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003077 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003078 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3079 || compl_match_array[i].pum_text
3080 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003081 {
3082 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003083 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003084 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003085 }
3086
3087 if (compl_match_array != NULL)
3088 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003089 /* In Replace mode when a $ is displayed at the end of the line only
3090 * part of the screen would be updated. We do need to redraw here. */
3091 dollar_vcol = -1;
3092
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003093 /* Compute the screen column of the start of the completed text.
3094 * Use the cursor to get all wrapping and other settings right. */
3095 col = curwin->w_cursor.col;
3096 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003097 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003098 curwin->w_cursor.col = col;
3099 }
3100}
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#define DICT_FIRST (1) /* use just first element in "dict" */
3103#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003106 * Add any identifiers that match the given pattern in the list of dictionary
3107 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003110ins_compl_dictionaries(
3111 char_u *dict_start,
3112 char_u *pat,
3113 int flags, /* DICT_FIRST and/or DICT_EXACT */
3114 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003116 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 char_u *ptr;
3118 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 char_u **files;
3121 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003123 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
Bram Moolenaar0b238792006-03-02 22:49:12 +00003125 if (*dict == NUL)
3126 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003127#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003128 /* When 'dictionary' is empty and spell checking is enabled use
3129 * "spell". */
3130 if (!thesaurus && curwin->w_p_spell)
3131 dict = (char_u *)"spell";
3132 else
3133#endif
3134 return;
3135 }
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003138 if (buf == NULL)
3139 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003140 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 /* If 'infercase' is set, don't use 'smartcase' here */
3143 save_p_scs = p_scs;
3144 if (curbuf->b_p_inf)
3145 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003146
3147 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3148 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003149 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003150 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003151 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003152 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003153 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003154
3155 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003156 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003157 len = STRLEN(pat_esc) + 10;
3158 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003159 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003160 {
3161 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003162 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003163 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003164 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003165 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3166 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003167 vim_free(ptr);
3168 }
3169 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003170 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003171 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003172 if (regmatch.regprog == NULL)
3173 goto theend;
3174 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3177 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003178 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 /* copy one dictionary file name into buf */
3181 if (flags == DICT_EXACT)
3182 {
3183 count = 1;
3184 files = &dict;
3185 }
3186 else
3187 {
3188 /* Expand wildcards in the dictionary name, but do not allow
3189 * backticks (for security, the 'dict' option may have been set in
3190 * a modeline). */
3191 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003192# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003193 if (!thesaurus && STRCMP(buf, "spell") == 0)
3194 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003195 else
3196# endif
3197 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 || expand_wildcards(1, &buf, &count, &files,
3199 EW_FILE|EW_SILENT) != OK)
3200 count = 0;
3201 }
3202
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003203# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003206 /* Complete from active spelling. Skip "\<" in the pattern, we
3207 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003208 if (pat[0] == '\\' && pat[1] == '<')
3209 ptr = pat + 2;
3210 else
3211 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003212 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003214 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003215# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003216 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003217 {
3218 ins_compl_files(count, files, thesaurus, flags,
3219 &regmatch, buf, &dir);
3220 if (flags != DICT_EXACT)
3221 FreeWild(count, files);
3222 }
3223 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 break;
3225 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003226
3227theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003229 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 vim_free(buf);
3231}
3232
Bram Moolenaar0b238792006-03-02 22:49:12 +00003233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234ins_compl_files(
3235 int count,
3236 char_u **files,
3237 int thesaurus,
3238 int flags,
3239 regmatch_T *regmatch,
3240 char_u *buf,
3241 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003242{
3243 char_u *ptr;
3244 int i;
3245 FILE *fp;
3246 int add_r;
3247
3248 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3249 {
3250 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3251 if (flags != DICT_EXACT)
3252 {
3253 vim_snprintf((char *)IObuff, IOSIZE,
3254 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003255 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003256 }
3257
3258 if (fp != NULL)
3259 {
3260 /*
3261 * Read dictionary file line by line.
3262 * Check each line for a match.
3263 */
3264 while (!got_int && !compl_interrupted
3265 && !vim_fgets(buf, LSIZE, fp))
3266 {
3267 ptr = buf;
3268 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3269 {
3270 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003271 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003272 ptr = find_line_end(ptr);
3273 else
3274 ptr = find_word_end(ptr);
3275 add_r = ins_compl_add_infercase(regmatch->startp[0],
3276 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003277 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003278 if (thesaurus)
3279 {
3280 char_u *wstart;
3281
3282 /*
3283 * Add the other matches on the line
3284 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003285 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286 while (!got_int)
3287 {
3288 /* Find start of the next word. Skip white
3289 * space and punctuation. */
3290 ptr = find_word_start(ptr);
3291 if (*ptr == NUL || *ptr == NL)
3292 break;
3293 wstart = ptr;
3294
Bram Moolenaara2993e12007-08-12 14:38:46 +00003295 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003296#ifdef FEAT_MBYTE
3297 if (has_mbyte)
3298 /* Japanese words may have characters in
3299 * different classes, only separate words
3300 * with single-byte non-word characters. */
3301 while (*ptr != NUL)
3302 {
3303 int l = (*mb_ptr2len)(ptr);
3304
3305 if (l < 2 && !vim_iswordc(*ptr))
3306 break;
3307 ptr += l;
3308 }
3309 else
3310#endif
3311 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003312
3313 /* Add the word. Skip the regexp match. */
3314 if (wstart != regmatch->startp[0])
3315 add_r = ins_compl_add_infercase(wstart,
3316 (int)(ptr - wstart),
3317 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003318 }
3319 }
3320 if (add_r == OK)
3321 /* if dir was BACKWARD then honor it just once */
3322 *dir = FORWARD;
3323 else if (add_r == FAIL)
3324 break;
3325 /* avoid expensive call to vim_regexec() when at end
3326 * of line */
3327 if (*ptr == '\n' || got_int)
3328 break;
3329 }
3330 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003331 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003332 }
3333 fclose(fp);
3334 }
3335 }
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * Find the start of the next word.
3340 * Returns a pointer to the first char of the word. Also stops at a NUL.
3341 */
3342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345#ifdef FEAT_MBYTE
3346 if (has_mbyte)
3347 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003348 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 else
3350#endif
3351 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3352 ++ptr;
3353 return ptr;
3354}
3355
3356/*
3357 * Find the end of the word. Assumes it starts inside a word.
3358 * Returns a pointer to just after the word.
3359 */
3360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003361find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363#ifdef FEAT_MBYTE
3364 int start_class;
3365
3366 if (has_mbyte)
3367 {
3368 start_class = mb_get_class(ptr);
3369 if (start_class > 1)
3370 while (*ptr != NUL)
3371 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003372 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (mb_get_class(ptr) != start_class)
3374 break;
3375 }
3376 }
3377 else
3378#endif
3379 while (vim_iswordc(*ptr))
3380 ++ptr;
3381 return ptr;
3382}
3383
3384/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385 * Find the end of the line, omitting CR and NL at the end.
3386 * Returns a pointer to just after the line.
3387 */
3388 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003390{
3391 char_u *s;
3392
3393 s = ptr + STRLEN(ptr);
3394 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3395 --s;
3396 return s;
3397}
3398
3399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 * Free the list of completions
3401 */
3402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003403ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003405 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003406 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408 vim_free(compl_pattern);
3409 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003410 vim_free(compl_leader);
3411 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003413 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003415
3416 ins_compl_del_pum();
3417 pum_clear();
3418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003419 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 do
3421 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003422 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003423 compl_curr_match = compl_curr_match->cp_next;
3424 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003426 if (match->cp_flags & FREE_FNAME)
3427 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003428 for (i = 0; i < CPT_COUNT; ++i)
3429 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003431 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3432 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003433 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434}
3435
3436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003437ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003439 compl_cont_status = 0;
3440 compl_started = FALSE;
3441 compl_matches = 0;
3442 vim_free(compl_pattern);
3443 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003444 vim_free(compl_leader);
3445 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003447 vim_free(compl_orig_text);
3448 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003449 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003450 /* clear v:completed_item */
3451 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452}
3453
3454/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003455 * Return TRUE when Insert completion is active.
3456 */
3457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003458ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003459{
3460 return compl_started;
3461}
3462
3463/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003464 * Delete one character before the cursor and show the subset of the matches
3465 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003466 * Returns the character to be used, NUL if the work is done and another char
3467 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003468 */
3469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003470ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003471{
3472 char_u *line;
3473 char_u *p;
3474
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003475 line = ml_get_curline();
3476 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003477 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003478
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003479 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003480 * allow the word to be deleted, we won't match everything.
3481 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003482 if ((int)(p - line) - (int)compl_col < 0
3483 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003484 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3485 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3486 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003487 return K_BS;
3488
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003489 /* Deleted more than what was used to find matches or didn't finish
3490 * finding all matches: need to look for matches all over again. */
3491 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003492 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003493 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003494
Bram Moolenaara6557602006-02-04 22:43:20 +00003495 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003497 if (compl_leader != NULL)
3498 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003499 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003500 if (compl_shown_match != NULL)
3501 /* Make sure current match is not a hidden item. */
3502 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003503 return NUL;
3504 }
3505 return K_BS;
3506}
Bram Moolenaara6557602006-02-04 22:43:20 +00003507
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003508/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003509 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3510 * be called.
3511 */
3512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003514{
3515 /* Return TRUE if we didn't complete finding matches or when the
3516 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3517 return compl_was_interrupted
3518 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3519 && compl_opt_refresh_always);
3520}
3521
3522/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003523 * Called after changing "compl_leader".
3524 * Show the popup menu with a different set of matches.
3525 * May also search for matches again if the previous search was interrupted.
3526 */
3527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003528ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529{
3530 ins_compl_del_pum();
3531 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003532 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003533 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003534
3535 if (compl_started)
3536 ins_compl_set_original_text(compl_leader);
3537 else
3538 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003539#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003540 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003541#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003542 /*
3543 * Matches were cleared, need to search for them now. First display
3544 * the changed text before the cursor. Set "compl_restarting" to
3545 * avoid that the first match is inserted.
3546 */
3547 update_screen(0);
3548#ifdef FEAT_GUI
3549 if (gui.in_use)
3550 {
3551 /* Show the cursor after the match, not after the redrawn text. */
3552 setcursor();
3553 out_flush();
3554 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003555 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003556#endif
3557 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003558 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003559 compl_cont_status = 0;
3560 compl_restarting = FALSE;
3561 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003562
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003563 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003564
3565 /* Show the popup menu with a different set of matches. */
3566 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003567
3568 /* Don't let Enter select the original text when there is no popup menu. */
3569 if (compl_match_array == NULL)
3570 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003571}
3572
3573/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003574 * Return the length of the completion, from the completion start column to
3575 * the cursor column. Making sure it never goes below zero.
3576 */
3577 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003579{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003580 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003581
3582 if (off < 0)
3583 return 0;
3584 return off;
3585}
3586
3587/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003588 * Append one character to the match leader. May reduce the number of
3589 * matches.
3590 */
3591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003593{
3594#ifdef FEAT_MBYTE
3595 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003596#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003597
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003598 if (stop_arrow() == FAIL)
3599 return;
3600#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003601 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3602 {
3603 char_u buf[MB_MAXBYTES + 1];
3604
3605 (*mb_char2bytes)(c, buf);
3606 buf[cc] = NUL;
3607 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003608 if (compl_opt_refresh_always)
3609 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003610 }
3611 else
3612#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003613 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003614 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003615 if (compl_opt_refresh_always)
3616 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003617 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003618
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003619 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003620 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003621 ins_compl_restart();
3622
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003623 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003624 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003625 * break redo. */
3626 if (!compl_opt_refresh_always)
3627 {
3628 vim_free(compl_leader);
3629 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003630 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003631 if (compl_leader != NULL)
3632 ins_compl_new_leader();
3633 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003634}
3635
3636/*
3637 * Setup for finding completions again without leaving CTRL-X mode. Used when
3638 * BS or a key was typed while still searching for matches.
3639 */
3640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003642{
3643 ins_compl_free();
3644 compl_started = FALSE;
3645 compl_matches = 0;
3646 compl_cont_status = 0;
3647 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003648}
3649
3650/*
3651 * Set the first match, the original text.
3652 */
3653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003655{
3656 char_u *p;
3657
3658 /* Replace the original text entry. */
3659 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3660 {
3661 p = vim_strsave(str);
3662 if (p != NULL)
3663 {
3664 vim_free(compl_first_match->cp_str);
3665 compl_first_match->cp_str = p;
3666 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003667 }
3668}
3669
3670/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003671 * Append one character to the match leader. May reduce the number of
3672 * matches.
3673 */
3674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003675ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003676{
3677 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003678 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003679 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003680 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003681
3682 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003683 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003684 {
3685 /* When still at the original match use the first entry that matches
3686 * the leader. */
3687 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3688 {
3689 p = NULL;
3690 for (cp = compl_shown_match->cp_next; cp != NULL
3691 && cp != compl_first_match; cp = cp->cp_next)
3692 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003693 if (compl_leader == NULL
3694 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003695 (int)STRLEN(compl_leader)))
3696 {
3697 p = cp->cp_str;
3698 break;
3699 }
3700 }
3701 if (p == NULL || (int)STRLEN(p) <= len)
3702 return;
3703 }
3704 else
3705 return;
3706 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003707 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003708 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003709 ins_compl_addleader(c);
3710}
3711
3712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003714 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003715 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003717 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003718ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
3720 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003722 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 /* Forget any previous 'special' messages if this is actually
3725 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3726 */
3727 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3728 edit_submode_extra = NULL;
3729
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003730 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003731 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3732 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003733 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003735 /* Set "compl_get_longest" when finding the first matches. */
3736 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3737 || (ctrl_x_mode == 0 && !compl_started))
3738 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003739 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003740 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003741
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003742 }
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3745 {
3746 /*
3747 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3748 * it will be yet. Now we decide.
3749 */
3750 switch (c)
3751 {
3752 case Ctrl_E:
3753 case Ctrl_Y:
3754 ctrl_x_mode = CTRL_X_SCROLL;
3755 if (!(State & REPLACE_FLAG))
3756 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3757 else
3758 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3759 edit_submode_pre = NULL;
3760 showmode();
3761 break;
3762 case Ctrl_L:
3763 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3764 break;
3765 case Ctrl_F:
3766 ctrl_x_mode = CTRL_X_FILES;
3767 break;
3768 case Ctrl_K:
3769 ctrl_x_mode = CTRL_X_DICTIONARY;
3770 break;
3771 case Ctrl_R:
3772 /* Simply allow ^R to happen without affecting ^X mode */
3773 break;
3774 case Ctrl_T:
3775 ctrl_x_mode = CTRL_X_THESAURUS;
3776 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003777#ifdef FEAT_COMPL_FUNC
3778 case Ctrl_U:
3779 ctrl_x_mode = CTRL_X_FUNCTION;
3780 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003781 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003782 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003784#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003785 case 's':
3786 case Ctrl_S:
3787 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003788#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003789 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003790 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003791 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003792#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 case Ctrl_RSB:
3795 ctrl_x_mode = CTRL_X_TAGS;
3796 break;
3797#ifdef FEAT_FIND_ID
3798 case Ctrl_I:
3799 case K_S_TAB:
3800 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3801 break;
3802 case Ctrl_D:
3803 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3804 break;
3805#endif
3806 case Ctrl_V:
3807 case Ctrl_Q:
3808 ctrl_x_mode = CTRL_X_CMDLINE;
3809 break;
3810 case Ctrl_P:
3811 case Ctrl_N:
3812 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3813 * just started ^X mode, or there were enough ^X's to cancel
3814 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3815 * do normal expansion when interrupting a different mode (say
3816 * ^X^F^X^P or ^P^X^X^P, see below)
3817 * nothing changes if interrupting mode 0, (eg, the flag
3818 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819 if (!(compl_cont_status & CONT_INTRPT))
3820 compl_cont_status |= CONT_LOCAL;
3821 else if (compl_cont_mode != 0)
3822 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 /* FALLTHROUGH */
3824 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003825 /* If we have typed at least 2 ^X's... for modes != 0, we set
3826 * compl_cont_status = 0 (eg, as if we had just started ^X
3827 * mode).
3828 * For mode 0, we set "compl_cont_mode" to an impossible
3829 * value, in both cases ^X^X can be used to restart the same
3830 * mode (avoiding ADDING mode).
3831 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3832 * 'complete' and local ^P expansions respectively.
3833 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3834 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (c == Ctrl_X)
3836 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 if (compl_cont_mode != 0)
3838 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 ctrl_x_mode = 0;
3843 edit_submode = NULL;
3844 showmode();
3845 break;
3846 }
3847 }
3848 else if (ctrl_x_mode != 0)
3849 {
3850 /* We're already in CTRL-X mode, do we stay in it? */
3851 if (!vim_is_ctrl_x_key(c))
3852 {
3853 if (ctrl_x_mode == CTRL_X_SCROLL)
3854 ctrl_x_mode = 0;
3855 else
3856 ctrl_x_mode = CTRL_X_FINISHED;
3857 edit_submode = NULL;
3858 }
3859 showmode();
3860 }
3861
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003862 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
3864 /* Show error message from attempted keyword completion (probably
3865 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003866 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003868 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3869 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 || ctrl_x_mode == CTRL_X_FINISHED)
3871 {
3872 /* Get here when we have finished typing a sequence of ^N and
3873 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003875 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003878 * If any of the original typed text has been changed, eg when
3879 * ignorecase is set, we must add back-spaces to the redo
3880 * buffer. We add as few as necessary to delete just the part
3881 * of the original text that has changed.
3882 * When using the longest match, edited the match or used
3883 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003885 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3886 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003887 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003888 ptr = NULL;
3889 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891
3892#ifdef FEAT_CINDENT
3893 want_cindent = (can_cindent && cindent_on());
3894#endif
3895 /*
3896 * When completing whole lines: fix indent for 'cindent'.
3897 * Otherwise, break line if it's too long.
3898 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
3901#ifdef FEAT_CINDENT
3902 /* re-indent the current line */
3903 if (want_cindent)
3904 {
3905 do_c_expr_indent();
3906 want_cindent = FALSE; /* don't do it again */
3907 }
3908#endif
3909 }
3910 else
3911 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003912 int prev_col = curwin->w_cursor.col;
3913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003915 if (prev_col > 0)
3916 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003917 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003918 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003920 if (prev_col > 0
3921 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3922 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003925 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003926 * the selection without inserting anything. When
3927 * compl_enter_selects is set the Enter key does the same. */
3928 if ((c == Ctrl_Y || (compl_enter_selects
3929 && (c == CAR || c == K_KENTER || c == NL)))
3930 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003931 retval = TRUE;
3932
Bram Moolenaar47247282016-08-02 22:36:02 +02003933 /* CTRL-E means completion is Ended, go back to the typed text.
3934 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003935 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003936 {
3937 ins_compl_delete();
3938 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003939 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003940 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003941 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003942 retval = TRUE;
3943 }
3944
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003945 auto_format(FALSE, TRUE);
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003948 compl_started = FALSE;
3949 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003950 if (!shortmess(SHM_COMPLETIONMENU))
3951 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003953 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (edit_submode != NULL)
3955 {
3956 edit_submode = NULL;
3957 showmode();
3958 }
3959
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003960#ifdef FEAT_CMDWIN
3961 if (c == Ctrl_C && cmdwin_type != 0)
3962 /* Avoid the popup menu remains displayed when leaving the
3963 * command line window. */
3964 update_screen(0);
3965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#ifdef FEAT_CINDENT
3967 /*
3968 * Indent now if a key was typed that is in 'cinkeys'.
3969 */
3970 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3971 do_c_expr_indent();
3972#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003973#ifdef FEAT_AUTOCMD
3974 /* Trigger the CompleteDone event to give scripts a chance to act
3975 * upon the completion. */
3976 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003980#ifdef FEAT_AUTOCMD
3981 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3982 /* Trigger the CompleteDone event to give scripts a chance to act
3983 * upon the (possibly failed) completion. */
3984 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 /* reset continue_* if we left expansion-mode, if we stay they'll be
3988 * (re)set properly in ins_complete() */
3989 if (!vim_is_ctrl_x_key(c))
3990 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991 compl_cont_status = 0;
3992 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003994
3995 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003999 * Fix the redo buffer for the completion leader replacing some of the typed
4000 * text. This inserts backspaces and appends the changed text.
4001 * "ptr" is the known leader text or NUL.
4002 */
4003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004005{
4006 int len;
4007 char_u *p;
4008 char_u *ptr = ptr_arg;
4009
4010 if (ptr == NULL)
4011 {
4012 if (compl_leader != NULL)
4013 ptr = compl_leader;
4014 else
4015 return; /* nothing to do */
4016 }
4017 if (compl_orig_text != NULL)
4018 {
4019 p = compl_orig_text;
4020 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4021 ;
4022#ifdef FEAT_MBYTE
4023 if (len > 0)
4024 len -= (*mb_head_off)(p, p + len);
4025#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004026 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004027 AppendCharToRedobuff(K_BS);
4028 }
4029 else
4030 len = 0;
4031 if (ptr != NULL)
4032 AppendToRedobuffLit(ptr + len, -1);
4033}
4034
4035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4037 * (depending on flag) starting from buf and looking for a non-scanned
4038 * buffer (other than curbuf). curbuf is special, if it is called with
4039 * buf=curbuf then it has to be the first call for a given flag/expansion.
4040 *
4041 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4042 */
4043 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004044ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046#ifdef FEAT_WINDOWS
4047 static win_T *wp;
4048#endif
4049
4050 if (flag == 'w') /* just windows */
4051 {
4052#ifdef FEAT_WINDOWS
4053 if (buf == curbuf) /* first call for this flag/expansion */
4054 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004055 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 && wp->w_buffer->b_scanned)
4057 ;
4058 buf = wp->w_buffer;
4059#else
4060 buf = curbuf;
4061#endif
4062 }
4063 else
4064 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4065 * (unlisted buffers)
4066 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004067 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 && ((flag == 'U'
4069 ? buf->b_p_bl
4070 : (!buf->b_p_bl
4071 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004072 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 ;
4074 return buf;
4075}
4076
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004077#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004078/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004079 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004080 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004081 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004083expand_by_function(
4084 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4085 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004086{
Bram Moolenaar82139082011-09-14 16:52:09 +02004087 list_T *matchlist = NULL;
4088 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004089 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004090 char_u *funcname;
4091 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004092 win_T *curwin_save;
4093 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004094 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004095
Bram Moolenaare344bea2005-09-01 20:46:49 +00004096 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4097 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004098 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004099
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004100 /* Call 'completefunc' to obtain the list of matches. */
4101 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004102 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004103
Bram Moolenaare344bea2005-09-01 20:46:49 +00004104 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004105 curwin_save = curwin;
4106 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004107
4108 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004109 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004110 {
4111 switch (rettv.v_type)
4112 {
4113 case VAR_LIST:
4114 matchlist = rettv.vval.v_list;
4115 break;
4116 case VAR_DICT:
4117 matchdict = rettv.vval.v_dict;
4118 break;
4119 default:
4120 /* TODO: Give error message? */
4121 clear_tv(&rettv);
4122 break;
4123 }
4124 }
4125
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004126 if (curwin_save != curwin || curbuf_save != curbuf)
4127 {
4128 EMSG(_(e_complwin));
4129 goto theend;
4130 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004131 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004132 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004133 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004134 {
4135 EMSG(_(e_compldel));
4136 goto theend;
4137 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004138
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004139 if (matchlist != NULL)
4140 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004141 else if (matchdict != NULL)
4142 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004143
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004144theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004145 if (matchdict != NULL)
4146 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004147 if (matchlist != NULL)
4148 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004149}
4150#endif /* FEAT_COMPL_FUNC */
4151
Bram Moolenaar39f05632006-03-19 22:15:26 +00004152#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004153/*
4154 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004155 */
4156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004157ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004158{
4159 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004160 int dir = compl_direction;
4161
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004162 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004163 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004164 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004165 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4166 /* if dir was BACKWARD then honor it just once */
4167 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004168 else if (did_emsg)
4169 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004170 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004171}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004172
4173/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004174 * Add completions from a dict.
4175 */
4176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004178{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004179 dictitem_T *di_refresh;
4180 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004181
4182 /* Check for optional "refresh" item. */
4183 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004184 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4185 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004186 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004187 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004188
4189 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4190 compl_opt_refresh_always = TRUE;
4191 }
4192
4193 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004194 di_words = dict_find(dict, (char_u *)"words", 5);
4195 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4196 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004197}
4198
4199/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004200 * Add a match to the list of matches from a typeval_T.
4201 * If the given string is already in the list of completions, then return
4202 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4203 * maybe because alloc() returns NULL, then FAIL is returned.
4204 */
4205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004206ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004207{
4208 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004209 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004210 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004211 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004212 char_u *(cptext[CPT_COUNT]);
4213
4214 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4215 {
4216 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4217 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4218 (char_u *)"abbr", FALSE);
4219 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4220 (char_u *)"menu", FALSE);
4221 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4222 (char_u *)"kind", FALSE);
4223 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4224 (char_u *)"info", FALSE);
4225 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4226 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004227 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004228 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004229 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4230 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004231 }
4232 else
4233 {
4234 word = get_tv_string_chk(tv);
4235 vim_memset(cptext, 0, sizeof(cptext));
4236 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004237 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004238 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004239 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004240}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004241#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243/*
4244 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004245 * The search starts at position "ini" in curbuf and in the direction
4246 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004247 * When "compl_started" is FALSE start at that position, otherwise continue
4248 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 * This may return before finding all the matches.
4250 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 */
4252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004253ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 static pos_T first_match_pos;
4256 static pos_T last_match_pos;
4257 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004258 static int found_all = FALSE; /* Found all matches of a
4259 certain type. */
4260 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
Bram Moolenaar572cb562005-08-05 21:35:02 +00004262 pos_T *pos;
4263 char_u **matches;
4264 int save_p_scs;
4265 int save_p_ws;
4266 int save_p_ic;
4267 int i;
4268 int num_matches;
4269 int len;
4270 int found_new_match;
4271 int type = ctrl_x_mode;
4272 char_u *ptr;
4273 char_u *dict = NULL;
4274 int dict_f = 0;
4275 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004276 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004280 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 ins_buf->b_scanned = 0;
4282 found_all = FALSE;
4283 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004285 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 last_match_pos = first_match_pos = *ini;
4287 }
4288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004290 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4292 for (;;)
4293 {
4294 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004295 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 * or if found_all says this entry is done. For ^X^L only use the
4299 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004300 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
4303 found_all = FALSE;
4304 while (*e_cpt == ',' || *e_cpt == ' ')
4305 e_cpt++;
4306 if (*e_cpt == '.' && !curbuf->b_scanned)
4307 {
4308 ins_buf = curbuf;
4309 first_match_pos = *ini;
4310 /* So that ^N can match word immediately after cursor */
4311 if (ctrl_x_mode == 0)
4312 dec(&first_match_pos);
4313 last_match_pos = first_match_pos;
4314 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004315
4316 /* Remember the first match so that the loop stops when we
4317 * wrap and come back there a second time. */
4318 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4321 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4322 {
4323 /* Scan a buffer, but not the current one. */
4324 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4325 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 first_match_pos.col = last_match_pos.col = 0;
4328 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4329 last_match_pos.lnum = 0;
4330 type = 0;
4331 }
4332 else /* unloaded buffer, scan like dictionary */
4333 {
4334 found_all = TRUE;
4335 if (ins_buf->b_fname == NULL)
4336 continue;
4337 type = CTRL_X_DICTIONARY;
4338 dict = ins_buf->b_fname;
4339 dict_f = DICT_EXACT;
4340 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004341 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 ins_buf->b_fname == NULL
4343 ? buf_spname(ins_buf)
4344 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004345 ? ins_buf->b_fname
4346 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004347 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 else if (*e_cpt == NUL)
4350 break;
4351 else
4352 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004353 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 type = -1;
4355 else if (*e_cpt == 'k' || *e_cpt == 's')
4356 {
4357 if (*e_cpt == 'k')
4358 type = CTRL_X_DICTIONARY;
4359 else
4360 type = CTRL_X_THESAURUS;
4361 if (*++e_cpt != ',' && *e_cpt != NUL)
4362 {
4363 dict = e_cpt;
4364 dict_f = DICT_FIRST;
4365 }
4366 }
4367#ifdef FEAT_FIND_ID
4368 else if (*e_cpt == 'i')
4369 type = CTRL_X_PATH_PATTERNS;
4370 else if (*e_cpt == 'd')
4371 type = CTRL_X_PATH_DEFINES;
4372#endif
4373 else if (*e_cpt == ']' || *e_cpt == 't')
4374 {
4375 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004376 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004377 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 else
4380 type = -1;
4381
4382 /* in any case e_cpt is advanced to the next entry */
4383 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4384
4385 found_all = TRUE;
4386 if (type == -1)
4387 continue;
4388 }
4389 }
4390
4391 switch (type)
4392 {
4393 case -1:
4394 break;
4395#ifdef FEAT_FIND_ID
4396 case CTRL_X_PATH_PATTERNS:
4397 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004398 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4403 (linenr_T)1, (linenr_T)MAXLNUM);
4404 break;
4405#endif
4406
4407 case CTRL_X_DICTIONARY:
4408 case CTRL_X_THESAURUS:
4409 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004410 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 : (type == CTRL_X_THESAURUS
4412 ? (*curbuf->b_p_tsr == NUL
4413 ? p_tsr
4414 : curbuf->b_p_tsr)
4415 : (*curbuf->b_p_dict == NUL
4416 ? p_dict
4417 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004418 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004419 dict != NULL ? dict_f
4420 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 dict = NULL;
4422 break;
4423
4424 case CTRL_X_TAGS:
4425 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4426 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004429 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 * of matches is found when compl_pattern is empty */
4431 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4433 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4434 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4435 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004436 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 p_ic = save_p_ic;
4439 break;
4440
4441 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4444 {
4445
4446 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004448 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 break;
4451
4452 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 if (expand_cmdline(&compl_xp, compl_pattern,
4454 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004456 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 break;
4458
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004459#ifdef FEAT_COMPL_FUNC
4460 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004461 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004462 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004463 break;
4464#endif
4465
Bram Moolenaar488c6512005-08-11 20:09:58 +00004466 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004467#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004468 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004469 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004470 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004471 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004472#endif
4473 break;
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 default: /* normal ^P/^N and ^X^L */
4476 /*
4477 * If 'infercase' is set, don't use 'smartcase' here
4478 */
4479 save_p_scs = p_scs;
4480 if (ins_buf->b_p_inf)
4481 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482
Bram Moolenaar361aa502014-01-23 22:45:58 +01004483 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 * end but never from the middle, thus setting nowrapscan in this
4485 * buffers is a good idea, on the other hand, we always set
4486 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4487 save_p_ws = p_ws;
4488 if (ins_buf != curbuf)
4489 p_ws = FALSE;
4490 else if (*e_cpt == '.')
4491 p_ws = TRUE;
4492 for (;;)
4493 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004494 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004496 ++msg_silent; /* Don't want messages for wrapscan. */
4497
Bram Moolenaare4214502015-03-05 18:08:43 +01004498 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4499 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004500 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004501 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004504 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004506 found_new_match = searchit(NULL, ins_buf, pos,
4507 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004509 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004510 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004511 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004513 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 first_match_pos = *pos;
4516 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004517 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004520 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 found_new_match = FAIL;
4522 if (found_new_match == FAIL)
4523 {
4524 if (ins_buf == curbuf)
4525 found_all = TRUE;
4526 break;
4527 }
4528
4529 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 && ini->lnum == pos->lnum
4532 && ini->col == pos->col)
4533 continue;
4534 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004535 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
4539 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4540 continue;
4541 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4542 if (!p_paste)
4543 ptr = skipwhite(ptr);
4544 }
4545 len = (int)STRLEN(ptr);
4546 }
4547 else
4548 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549 char_u *tmp_ptr = ptr;
4550
4551 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /* Skip if already inside a word. */
4555 if (vim_iswordp(tmp_ptr))
4556 continue;
4557 /* Find start of next word. */
4558 tmp_ptr = find_word_start(tmp_ptr);
4559 }
4560 /* Find end of this word. */
4561 tmp_ptr = find_word_end(tmp_ptr);
4562 len = (int)(tmp_ptr - ptr);
4563
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 if ((compl_cont_status & CONT_ADDING)
4565 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4568 {
4569 /* Try next line, if any. the new word will be
4570 * "join" as if the normal command "J" was used.
4571 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 * works -- Acevedo */
4574 STRNCPY(IObuff, ptr, len);
4575 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4576 tmp_ptr = ptr = skipwhite(ptr);
4577 /* Find start of next word. */
4578 tmp_ptr = find_word_start(tmp_ptr);
4579 /* Find end of next word. */
4580 tmp_ptr = find_word_end(tmp_ptr);
4581 if (tmp_ptr > ptr)
4582 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004583 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004585 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 IObuff[len++] = ' ';
4587 /* IObuf =~ "\k.* ", thus len >= 2 */
4588 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004589 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 || (vim_strchr(p_cpo, CPO_JOINSP)
4591 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004592 && (IObuff[len - 2] == '?'
4593 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 IObuff[len++] = ' ';
4595 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004596 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (tmp_ptr - ptr >= IOSIZE - len)
4598 tmp_ptr = ptr + IOSIZE - len - 1;
4599 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4600 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004601 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 IObuff[len] = NUL;
4604 ptr = IObuff;
4605 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 continue;
4608 }
4609 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004610 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004611 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004612 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 found_new_match = OK;
4615 break;
4616 }
4617 }
4618 p_scs = save_p_scs;
4619 p_ws = save_p_ws;
4620 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004622 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004623 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004624 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 found_new_match = OK;
4626
4627 /* break the loop for specialized modes (use 'complete' just for the
4628 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004629 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004631 {
4632 if (got_int)
4633 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004634 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004635 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004636 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004637
Bram Moolenaare4214502015-03-05 18:08:43 +01004638 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004639 || compl_interrupted)
4640 break;
4641 compl_started = TRUE;
4642 }
4643 else
4644 {
4645 /* Mark a buffer scanned when it has been scanned completely */
4646 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4647 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004649 compl_started = FALSE;
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
Bram Moolenaare4214502015-03-05 18:08:43 +01004654 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 && *e_cpt == NUL) /* Got to end of 'complete' */
4656 found_new_match = FAIL;
4657
4658 i = -1; /* total of matches, unknown */
4659 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004660 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 i = ins_compl_make_cyclic();
4662
4663 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004664 * just been made cyclic then we have to move compl_curr_match to the next
4665 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004666 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4667 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 if (compl_curr_match == NULL)
4669 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return i;
4671}
4672
4673/* Delete the old text being completed. */
4674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004675ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004677 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /*
4680 * In insert mode: Delete the typed part.
4681 * In replace mode: Put the old characters back, if any.
4682 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004683 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4684 if ((int)curwin->w_cursor.col > col)
4685 {
4686 if (stop_arrow() == FAIL)
4687 return;
4688 backspace_until_column(col);
4689 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004690
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004691 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4692 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004694 /* clear v:completed_item */
4695 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696}
4697
Bram Moolenaar472e8592016-10-15 17:06:47 +02004698/*
4699 * Insert the new text being completed.
4700 * "in_compl_func" is TRUE when called from complete_check().
4701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004703ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004705 dict_T *dict;
4706
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004707 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004708 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4709 compl_used_match = FALSE;
4710 else
4711 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004712
4713 /* Set completed item. */
4714 /* { word, abbr, menu, kind, info } */
4715 dict = dict_alloc();
4716 if (dict != NULL)
4717 {
4718 dict_add_nr_str(dict, "word", 0L,
4719 EMPTY_IF_NULL(compl_shown_match->cp_str));
4720 dict_add_nr_str(dict, "abbr", 0L,
4721 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4722 dict_add_nr_str(dict, "menu", 0L,
4723 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4724 dict_add_nr_str(dict, "kind", 0L,
4725 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4726 dict_add_nr_str(dict, "info", 0L,
4727 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4728 }
4729 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004730 if (!in_compl_func)
4731 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732}
4733
4734/*
4735 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004736 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4737 * get more completions. If it is FALSE, then we just do nothing when there
4738 * are no more completions in a given direction. The latter case is used when
4739 * we are still in the middle of finding completions, to allow browsing
4740 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 * Return the total number of matches, or -1 if still unknown -- webb.
4742 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004743 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4744 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 *
4746 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004747 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4748 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 */
4750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004751ins_compl_next(
4752 int allow_get_expansion,
4753 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004754 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004755 int insert_match, /* Insert the newly selected match */
4756 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 int num_matches = -1;
4759 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004760 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004761 compl_T *found_compl = NULL;
4762 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004763 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004764 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004766 /* When user complete function return -1 for findstart which is next
4767 * time of 'always', compl_shown_match become NULL. */
4768 if (compl_shown_match == NULL)
4769 return -1;
4770
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004771 if (compl_leader != NULL
4772 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004774 /* Set "compl_shown_match" to the actually shown match, it may differ
4775 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004776 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004777 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004778 && compl_shown_match->cp_next != NULL
4779 && compl_shown_match->cp_next != compl_first_match)
4780 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004781
4782 /* If we didn't find it searching forward, and compl_shows_dir is
4783 * backward, find the last match. */
4784 if (compl_shows_dir == BACKWARD
4785 && !ins_compl_equal(compl_shown_match,
4786 compl_leader, (int)STRLEN(compl_leader))
4787 && (compl_shown_match->cp_next == NULL
4788 || compl_shown_match->cp_next == compl_first_match))
4789 {
4790 while (!ins_compl_equal(compl_shown_match,
4791 compl_leader, (int)STRLEN(compl_leader))
4792 && compl_shown_match->cp_prev != NULL
4793 && compl_shown_match->cp_prev != compl_first_match)
4794 compl_shown_match = compl_shown_match->cp_prev;
4795 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004796 }
4797
4798 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004799 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 /* Delete old text to be replaced */
4801 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004802
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004803 /* When finding the longest common text we stick at the original text,
4804 * don't let CTRL-N or CTRL-P move to the first match. */
4805 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4806
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004807 /* When restarting the search don't insert the first match either. */
4808 if (compl_restarting)
4809 {
4810 advance = FALSE;
4811 compl_restarting = FALSE;
4812 }
4813
Bram Moolenaare3226be2005-12-18 22:10:00 +00004814 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4815 * around. */
4816 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004818 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004820 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004821 found_end = (compl_first_match != NULL
4822 && (compl_shown_match->cp_next == compl_first_match
4823 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004824 }
4825 else if (compl_shows_dir == BACKWARD
4826 && compl_shown_match->cp_prev != NULL)
4827 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004828 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004829 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004830 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004833 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004834 if (!allow_get_expansion)
4835 {
4836 if (advance)
4837 {
4838 if (compl_shows_dir == BACKWARD)
4839 compl_pending -= todo + 1;
4840 else
4841 compl_pending += todo + 1;
4842 }
4843 return -1;
4844 }
4845
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004846 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004847 {
4848 if (compl_shows_dir == BACKWARD)
4849 --compl_pending;
4850 else
4851 ++compl_pending;
4852 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004853
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004854 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004855 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004856
4857 /* handle any pending completions */
4858 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004859 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004860 {
4861 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4862 {
4863 compl_shown_match = compl_shown_match->cp_next;
4864 --compl_pending;
4865 }
4866 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4867 {
4868 compl_shown_match = compl_shown_match->cp_prev;
4869 ++compl_pending;
4870 }
4871 else
4872 break;
4873 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004874 found_end = FALSE;
4875 }
4876 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4877 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004878 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004879 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004880 ++todo;
4881 else
4882 /* Remember a matching item. */
4883 found_compl = compl_shown_match;
4884
4885 /* Stop at the end of the list when we found a usable match. */
4886 if (found_end)
4887 {
4888 if (found_compl != NULL)
4889 {
4890 compl_shown_match = found_compl;
4891 break;
4892 }
4893 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004897 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004898 if (compl_no_insert && !started)
4899 {
4900 ins_bytes(compl_orig_text + ins_compl_len());
4901 compl_used_match = FALSE;
4902 }
4903 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004904 {
4905 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004906 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004907 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004908 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004909 }
4910 else
4911 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913 if (!allow_get_expansion)
4914 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004915 /* may undisplay the popup menu first */
4916 ins_compl_upd_pum();
4917
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004918 /* redraw to show the user what was inserted */
4919 update_screen(0);
4920
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004921 /* display the updated popup menu */
4922 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004923#ifdef FEAT_GUI
4924 if (gui.in_use)
4925 {
4926 /* Show the cursor after the match, not after the redrawn text. */
4927 setcursor();
4928 out_flush();
4929 gui_update_cursor(FALSE, FALSE);
4930 }
4931#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 /* Delete old text to be replaced, since we're still searching and
4934 * don't want to match ourselves! */
4935 ins_compl_delete();
4936 }
4937
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004938 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004939 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004940 if (compl_no_insert && !started)
4941 compl_enter_selects = TRUE;
4942 else
4943 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 /*
4946 * Show the file name for the match (if any)
4947 * Truncate the file name to avoid a wait for return.
4948 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004949 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004952 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (i <= 0)
4954 i = 0;
4955 else
4956 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004957 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 msg(IObuff);
4959 redraw_cmdline = FALSE; /* don't overwrite! */
4960 }
4961
4962 return num_matches;
4963}
4964
4965/*
4966 * Call this while finding completions, to check whether the user has hit a key
4967 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004968 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004970 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004971 * "in_compl_func" is TRUE when called from complete_check(), don't set
4972 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
4974 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004975ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 static int count = 0;
4978
4979 int c;
4980
4981 /* Don't check when reading keys from a script. That would break the test
4982 * scripts */
4983 if (using_script())
4984 return;
4985
4986 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004987 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 return;
4989 count = 0;
4990
Bram Moolenaara260a972006-06-23 19:36:29 +00004991 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4992 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if (c != NUL)
4995 {
4996 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4997 {
4998 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004999 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005000 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005001 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005003 else
5004 {
5005 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005006 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005007 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005008 if (c != K_IGNORE)
5009 {
5010 /* Don't interrupt completion when the character wasn't typed,
5011 * e.g., when doing @q to replay keys. */
5012 if (c != Ctrl_R && KeyTyped)
5013 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005014
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005015 vungetc(c);
5016 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005019 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005020 {
5021 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5022
5023 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005024 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005025 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005026}
5027
5028/*
5029 * Decide the direction of Insert mode complete from the key typed.
5030 * Returns BACKWARD or FORWARD.
5031 */
5032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005034{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005035 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005036 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005037 return BACKWARD;
5038 return FORWARD;
5039}
5040
5041/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005042 * Return TRUE for keys that are used for completion only when the popup menu
5043 * is visible.
5044 */
5045 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005046ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005047{
5048 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005049 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5050 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005051}
5052
5053/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005054 * Decide the number of completions to move forward.
5055 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5056 */
5057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005058ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005059{
5060 int h;
5061
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005062 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005063 {
5064 h = pum_get_height();
5065 if (h > 3)
5066 h -= 2; /* keep some context */
5067 return h;
5068 }
5069 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070}
5071
5072/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005073 * Return TRUE if completion with "c" should insert the match, FALSE if only
5074 * to change the currently selected completion.
5075 */
5076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005078{
5079 switch (c)
5080 {
5081 case K_UP:
5082 case K_DOWN:
5083 case K_PAGEDOWN:
5084 case K_KPAGEDOWN:
5085 case K_S_DOWN:
5086 case K_PAGEUP:
5087 case K_KPAGEUP:
5088 case K_S_UP:
5089 return FALSE;
5090 }
5091 return TRUE;
5092}
5093
5094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * Do Insert mode completion.
5096 * Called when character "c" was typed, which has a meaning for completion.
5097 * Returns OK if completion was done, FAIL if something failed (out of mem).
5098 */
5099 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005100ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 char_u *line;
5103 int startcol = 0; /* column where searched text starts */
5104 colnr_T curs_col; /* cursor column */
5105 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005106 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005107 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005108 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005109 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaare3226be2005-12-18 22:10:00 +00005111 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005112 insert_match = ins_compl_use_match(c);
5113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 /* First time we hit ^N or ^P (in a row, I mean) */
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 did_ai = FALSE;
5119#ifdef FEAT_SMARTINDENT
5120 did_si = FALSE;
5121 can_si = FALSE;
5122 can_si_back = FALSE;
5123#endif
5124 if (stop_arrow() == FAIL)
5125 return FAIL;
5126
5127 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005129 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005131 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 * "compl_startpos" to the cursor as a pattern to add a new word
5133 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005134 * "compl_startpos" is not in the same line as the cursor then fix it
5135 * (the line has been split because it was longer than 'tw'). if SOL
5136 * is set then skip the previous pattern, a word at the beginning of
5137 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005138 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5139 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 /*
5142 * it is a continued search
5143 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5146 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 /* line (probably) wrapped, set compl_startpos to the
5151 * first non_blank in the line, if it is not a wordchar
5152 * include it to get a better pattern, but then we don't
5153 * want the "\\<" prefix, check it bellow */
5154 compl_col = (colnr_T)(skipwhite(line) - line);
5155 compl_startpos.col = compl_col;
5156 compl_startpos.lnum = curwin->w_cursor.lnum;
5157 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 else
5160 {
5161 /* S_IPOS was set when we inserted a word that was at the
5162 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 * mode but first we need to redefine compl_startpos */
5164 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005166 compl_cont_status |= CONT_SOL;
5167 compl_startpos.col = (colnr_T)(skipwhite(
5168 line + compl_length
5169 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005174 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005175 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_cont_status &= ~CONT_SOL;
5180 compl_length = (IOSIZE - MIN_SPACE);
5181 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5184 if (compl_length < 1)
5185 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005187 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 compl_cont_status = 0;
5200 compl_cont_status |= CONT_N_ADDS;
5201 compl_startpos = curwin->w_cursor;
5202 startcol = (int)curs_col;
5203 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205
5206 /* Work out completion pattern and original text -- webb */
5207 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5211 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 compl_col += ++startcol;
5217 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 compl_pattern = str_foldcase(line + compl_col,
5221 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_pattern = vim_strnsave(line + compl_col,
5224 compl_length);
5225 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return FAIL;
5227 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 char_u *prefix = (char_u *)"\\<";
5231
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005232 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005234 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005237 if (!vim_iswordp(line + compl_col)
5238 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 && (
5240#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245 )))
5246 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 STRCPY((char *)compl_pattern, prefix);
5248 (void)quote_meta(compl_pattern + STRLEN(prefix),
5249 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005251 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005255 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
5257 )
5258 {
5259 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5261 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005263 compl_col += curs_col;
5264 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 else
5267 {
5268#ifdef FEAT_MBYTE
5269 /* Search the point of change class of multibyte character
5270 * or not a word single byte character backward. */
5271 if (has_mbyte)
5272 {
5273 int base_class;
5274 int head_off;
5275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 startcol -= (*mb_head_off)(line, line + startcol);
5277 base_class = mb_get_class(line + startcol);
5278 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 head_off = (*mb_head_off)(line, line + startcol);
5281 if (base_class != mb_get_class(line + startcol
5282 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 }
5287 else
5288#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 compl_col += ++startcol;
5292 compl_length = (int)curs_col - startcol;
5293 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 /* Only match word with at least two chars -- webb
5296 * there's no need to call quote_meta,
5297 * alloc(7) is enough -- Acevedo
5298 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 compl_pattern = alloc(7);
5300 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 STRCPY((char *)compl_pattern, "\\<");
5303 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5304 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 else
5307 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005309 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005312 STRCPY((char *)compl_pattern, "\\<");
5313 (void)quote_meta(compl_pattern + 2, line + compl_col,
5314 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 }
5317 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005318 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005320 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 compl_length = (int)curs_col - (int)compl_col;
5322 if (compl_length < 0) /* cursor in indent: empty pattern */
5323 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_pattern = str_foldcase(line + compl_col, compl_length,
5326 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5329 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 return FAIL;
5331 }
5332 else if (ctrl_x_mode == CTRL_X_FILES)
5333 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005334 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005335 if (startcol > 0)
5336 {
5337 char_u *p = line + startcol;
5338
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005339 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005340 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005341 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005342 if (p == line && vim_isfilec(PTR2CHAR(p)))
5343 startcol = 0;
5344 else
5345 startcol = (int)(p - line) + 1;
5346 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005347
Bram Moolenaar0300e462013-09-08 16:03:45 +02005348 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_length = (int)curs_col - startcol;
5350 compl_pattern = addstar(line + compl_col, compl_length,
5351 EXPAND_FILES);
5352 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 return FAIL;
5354 }
5355 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5356 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 compl_pattern = vim_strnsave(line, curs_col);
5358 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005361 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5363 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005364 /* No completion possible, use an empty pattern to get a
5365 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005366 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005367 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005368 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5369 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005371 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005372 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005373#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005374 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005375 * Call user defined function 'completefunc' with "a:findstart"
5376 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005377 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005378 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005379 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005380 char_u *funcname;
5381 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005382 win_T *curwin_save;
5383 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005384
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005385 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005386 * string */
5387 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5388 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5389 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005390 {
5391 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5392 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005393 /* restore did_ai, so that adding comment leader works */
5394 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005395 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005396 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005397
5398 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005399 args[1] = NULL;
5400 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005401 curwin_save = curwin;
5402 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005403 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005404 if (curwin_save != curwin || curbuf_save != curbuf)
5405 {
5406 EMSG(_(e_complwin));
5407 return FAIL;
5408 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005409 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005410 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005411 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005412 {
5413 EMSG(_(e_compldel));
5414 return FAIL;
5415 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005416
Bram Moolenaar6110a002012-01-26 18:58:38 +01005417 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005418 * cancel the complete without an error.
5419 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005420 if (col == -2)
5421 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005422 if (col == -3)
5423 {
5424 ctrl_x_mode = 0;
5425 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005426 if (!shortmess(SHM_COMPLETIONMENU))
5427 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005428 return FAIL;
5429 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005430
Bram Moolenaar82139082011-09-14 16:52:09 +02005431 /*
5432 * Reset extended parameters of completion, when start new
5433 * completion.
5434 */
5435 compl_opt_refresh_always = FALSE;
5436
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005437 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005438 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005439 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005440 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005441 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 /* Setup variables for completion. Need to obtain "line" again,
5444 * it may have become invalid. */
5445 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005446 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5448 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005449#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 return FAIL;
5451 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005452 else if (ctrl_x_mode == CTRL_X_SPELL)
5453 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005454#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005455 if (spell_bad_len > 0)
5456 compl_col = curs_col - spell_bad_len;
5457 else
5458 compl_col = spell_word_start(startcol);
5459 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005460 {
5461 compl_length = 0;
5462 compl_col = curs_col;
5463 }
5464 else
5465 {
5466 spell_expand_check_cap(compl_col);
5467 compl_length = (int)curs_col - compl_col;
5468 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005469 /* Need to obtain "line" again, it may have become invalid. */
5470 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005471 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5472 if (compl_pattern == NULL)
5473#endif
5474 return FAIL;
5475 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 else
5477 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005478 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 return FAIL;
5480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005482 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005485 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 /* Insert a new line, keep indentation but ignore 'comments' */
5488#ifdef FEAT_COMMENTS
5489 char_u *old = curbuf->b_p_com;
5490
5491 curbuf->b_p_com = (char_u *)"";
5492#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005493 compl_startpos.lnum = curwin->w_cursor.lnum;
5494 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 ins_eol('\r');
5496#ifdef FEAT_COMMENTS
5497 curbuf->b_p_com = old;
5498#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_length = 0;
5500 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 }
5503 else
5504 {
5505 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
5508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 if (compl_cont_status & CONT_LOCAL)
5510 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 else
5512 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5513
Bram Moolenaar62951b12011-09-21 18:23:05 +02005514 /* If any of the original typed text has been changed we need to fix
5515 * the redo buffer. */
5516 ins_compl_fixRedoBufForLeader(NULL);
5517
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005518 /* Always add completion for the original text. */
5519 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005520 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5521 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005522 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 vim_free(compl_pattern);
5525 compl_pattern = NULL;
5526 vim_free(compl_orig_text);
5527 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 return FAIL;
5529 }
5530
5531 /* showmode might reset the internal line pointers, so it must
5532 * be called before line = ml_get(), or when this address is no
5533 * longer needed. -- Acevedo.
5534 */
5535 edit_submode_extra = (char_u *)_("-- Searching...");
5536 edit_submode_highl = HLF_COUNT;
5537 showmode();
5538 edit_submode_extra = NULL;
5539 out_flush();
5540 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005541 else if (insert_match && stop_arrow() == FAIL)
5542 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005544 compl_shown_match = compl_curr_match;
5545 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005548 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005550 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005551 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005552 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005554 /* may undisplay the popup menu */
5555 ins_compl_upd_pum();
5556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005557 if (n > 1) /* all matches have been found */
5558 compl_matches = n;
5559 compl_curr_match = compl_shown_match;
5560 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaard68071d2006-05-02 22:08:30 +00005562 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5563 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 if (got_int && !global_busy)
5565 {
5566 (void)vgetc();
5567 got_int = FALSE;
5568 }
5569
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005570 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005571 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005573 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5574 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5576 edit_submode_highl = HLF_E;
5577 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5578 * because we couldn't expand anything at first place, but if we used
5579 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5580 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005581 if ( compl_length > 1
5582 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 || (ctrl_x_mode != 0
5584 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5585 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005586 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588
Bram Moolenaar572cb562005-08-05 21:35:02 +00005589 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005590 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005592 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
5594 if (edit_submode_extra == NULL)
5595 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005596 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
5598 edit_submode_extra = (char_u *)_("Back at original");
5599 edit_submode_highl = HLF_W;
5600 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005601 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
5603 edit_submode_extra = (char_u *)_("Word from other line");
5604 edit_submode_highl = HLF_COUNT;
5605 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005606 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 edit_submode_extra = (char_u *)_("The only match");
5609 edit_submode_highl = HLF_COUNT;
5610 }
5611 else
5612 {
5613 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005614 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005616 int number = 0;
5617 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005619 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 /* search backwards for the first valid (!= -1) number.
5622 * This should normally succeed already at the first loop
5623 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005624 for (match = compl_curr_match->cp_prev; match != NULL
5625 && match != compl_first_match;
5626 match = match->cp_prev)
5627 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005629 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 break;
5631 }
5632 if (match != NULL)
5633 /* go up and assign all numbers which are not assigned
5634 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005635 for (match = match->cp_next;
5636 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005637 match = match->cp_next)
5638 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
5640 else /* BACKWARD */
5641 {
5642 /* search forwards (upwards) for the first valid (!= -1)
5643 * number. This should normally succeed already at the
5644 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005645 for (match = compl_curr_match->cp_next; match != NULL
5646 && match != compl_first_match;
5647 match = match->cp_next)
5648 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005650 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 break;
5652 }
5653 if (match != NULL)
5654 /* go down and assign all numbers which are not
5655 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005656 for (match = match->cp_prev; match
5657 && match->cp_number == -1;
5658 match = match->cp_prev)
5659 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 }
5662
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005663 /* The match should always have a sequence number now, this is
5664 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005665 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005667 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5668 * Translations may need more than twice that. */
5669 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005671 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005672 vim_snprintf((char *)match_ref, sizeof(match_ref),
5673 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005674 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005676 vim_snprintf((char *)match_ref, sizeof(match_ref),
5677 _("match %d"),
5678 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 edit_submode_extra = match_ref;
5680 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005681 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 curs_columns(FALSE);
5683 }
5684 }
5685 }
5686
5687 /* Show a message about what (completion) mode we're in. */
5688 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005689 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005691 if (edit_submode_extra != NULL)
5692 {
5693 if (!p_smd)
5694 msg_attr(edit_submode_extra,
5695 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005696 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005697 }
5698 else
5699 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
Bram Moolenaard68071d2006-05-02 22:08:30 +00005702 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005703 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005704 show_pum(save_w_wrow, save_w_leftcol);
5705
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005706 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005707 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 return OK;
5710}
5711
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005712 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005713show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005714{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005715 /* RedrawingDisabled may be set when invoked through complete(). */
5716 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005717
Bram Moolenaarcb036422017-03-01 12:29:10 +01005718 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005719
Bram Moolenaarcb036422017-03-01 12:29:10 +01005720 /* If the cursor moved or the display scrolled we need to remove the pum
5721 * first. */
5722 setcursor();
5723 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5724 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005725
Bram Moolenaarcb036422017-03-01 12:29:10 +01005726 ins_compl_show_pum();
5727 setcursor();
5728 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005729}
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731/*
5732 * Looks in the first "len" chars. of "src" for search-metachars.
5733 * If dest is not NULL the chars. are copied there quoting (with
5734 * a backslash) the metachars, and dest would be NUL terminated.
5735 * Returns the length (needed) of dest
5736 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005737 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005740 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005742 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 switch (*src)
5745 {
5746 case '.':
5747 case '*':
5748 case '[':
5749 if (ctrl_x_mode == CTRL_X_DICTIONARY
5750 || ctrl_x_mode == CTRL_X_THESAURUS)
5751 break;
5752 case '~':
5753 if (!p_magic) /* quote these only if magic is set */
5754 break;
5755 case '\\':
5756 if (ctrl_x_mode == CTRL_X_DICTIONARY
5757 || ctrl_x_mode == CTRL_X_THESAURUS)
5758 break;
5759 case '^': /* currently it's not needed. */
5760 case '$':
5761 m++;
5762 if (dest != NULL)
5763 *dest++ = '\\';
5764 break;
5765 }
5766 if (dest != NULL)
5767 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005768# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 /* Copy remaining bytes of a multibyte character. */
5770 if (has_mbyte)
5771 {
5772 int i, mb_len;
5773
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005774 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (mb_len > 0 && len >= mb_len)
5776 for (i = 0; i < mb_len; ++i)
5777 {
5778 --len;
5779 ++src;
5780 if (dest != NULL)
5781 *dest++ = *src;
5782 }
5783 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786 if (dest != NULL)
5787 *dest = NUL;
5788
5789 return m;
5790}
5791#endif /* FEAT_INS_EXPAND */
5792
5793/*
5794 * Next character is interpreted literally.
5795 * A one, two or three digit decimal number is interpreted as its byte value.
5796 * If one or two digits are entered, the next character is given to vungetc().
5797 * For Unicode a character > 255 may be returned.
5798 */
5799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005800get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802 int cc;
5803 int nc;
5804 int i;
5805 int hex = FALSE;
5806 int octal = FALSE;
5807#ifdef FEAT_MBYTE
5808 int unicode = 0;
5809#endif
5810
5811 if (got_int)
5812 return Ctrl_C;
5813
5814#ifdef FEAT_GUI
5815 /*
5816 * In GUI there is no point inserting the internal code for a special key.
5817 * It is more useful to insert the string "<KEY>" instead. This would
5818 * probably be useful in a text window too, but it would not be
5819 * vi-compatible (maybe there should be an option for it?) -- webb
5820 */
5821 if (gui.in_use)
5822 ++allow_keys;
5823#endif
5824#ifdef USE_ON_FLY_SCROLL
5825 dont_scroll = TRUE; /* disallow scrolling here */
5826#endif
5827 ++no_mapping; /* don't map the next key hits */
5828 cc = 0;
5829 i = 0;
5830 for (;;)
5831 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005832 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#ifdef FEAT_CMDL_INFO
5834 if (!(State & CMDLINE)
5835# ifdef FEAT_MBYTE
5836 && MB_BYTE2LEN_CHECK(nc) == 1
5837# endif
5838 )
5839 add_to_showcmd(nc);
5840#endif
5841 if (nc == 'x' || nc == 'X')
5842 hex = TRUE;
5843 else if (nc == 'o' || nc == 'O')
5844 octal = TRUE;
5845#ifdef FEAT_MBYTE
5846 else if (nc == 'u' || nc == 'U')
5847 unicode = nc;
5848#endif
5849 else
5850 {
5851 if (hex
5852#ifdef FEAT_MBYTE
5853 || unicode != 0
5854#endif
5855 )
5856 {
5857 if (!vim_isxdigit(nc))
5858 break;
5859 cc = cc * 16 + hex2nr(nc);
5860 }
5861 else if (octal)
5862 {
5863 if (nc < '0' || nc > '7')
5864 break;
5865 cc = cc * 8 + nc - '0';
5866 }
5867 else
5868 {
5869 if (!VIM_ISDIGIT(nc))
5870 break;
5871 cc = cc * 10 + nc - '0';
5872 }
5873
5874 ++i;
5875 }
5876
5877 if (cc > 255
5878#ifdef FEAT_MBYTE
5879 && unicode == 0
5880#endif
5881 )
5882 cc = 255; /* limit range to 0-255 */
5883 nc = 0;
5884
5885 if (hex) /* hex: up to two chars */
5886 {
5887 if (i >= 2)
5888 break;
5889 }
5890#ifdef FEAT_MBYTE
5891 else if (unicode) /* Unicode: up to four or eight chars */
5892 {
5893 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5894 break;
5895 }
5896#endif
5897 else if (i >= 3) /* decimal or octal: up to three chars */
5898 break;
5899 }
5900 if (i == 0) /* no number entered */
5901 {
5902 if (nc == K_ZERO) /* NUL is stored as NL */
5903 {
5904 cc = '\n';
5905 nc = 0;
5906 }
5907 else
5908 {
5909 cc = nc;
5910 nc = 0;
5911 }
5912 }
5913
5914 if (cc == 0) /* NUL is stored as NL */
5915 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005916#ifdef FEAT_MBYTE
5917 if (enc_dbcs && (cc & 0xff) == 0)
5918 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5919 second byte will cause trouble! */
5920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922 --no_mapping;
5923#ifdef FEAT_GUI
5924 if (gui.in_use)
5925 --allow_keys;
5926#endif
5927 if (nc)
5928 vungetc(nc);
5929 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5930 return cc;
5931}
5932
5933/*
5934 * Insert character, taking care of special keys and mod_mask
5935 */
5936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937insert_special(
5938 int c,
5939 int allow_modmask,
5940 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 char_u *p;
5943 int len;
5944
5945 /*
5946 * Special function key, translate into "<Key>". Up to the last '>' is
5947 * inserted with ins_str(), so as not to replace characters in replace
5948 * mode.
5949 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5950 * unless 'allow_modmask' is TRUE.
5951 */
5952#ifdef MACOS
5953 /* Command-key never produces a normal key */
5954 if (mod_mask & MOD_MASK_CMD)
5955 allow_modmask = TRUE;
5956#endif
5957 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5958 {
5959 p = get_special_key_name(c, mod_mask);
5960 len = (int)STRLEN(p);
5961 c = p[len - 1];
5962 if (len > 2)
5963 {
5964 if (stop_arrow() == FAIL)
5965 return;
5966 p[len - 1] = NUL;
5967 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005968 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 ctrlv = FALSE;
5970 }
5971 }
5972 if (stop_arrow() == OK)
5973 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5974}
5975
5976/*
5977 * Special characters in this context are those that need processing other
5978 * than the simple insertion that can be performed here. This includes ESC
5979 * which terminates the insert, and CR/NL which need special processing to
5980 * open up a new line. This routine tries to optimize insertions performed by
5981 * the "redo", "undo" or "put" commands, so it needs to know when it should
5982 * stop and defer processing to the "normal" mechanism.
5983 * '0' and '^' are special, because they can be followed by CTRL-D.
5984 */
5985#ifdef EBCDIC
5986# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5987#else
5988# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5989#endif
5990
5991#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01005992# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01005994# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995#endif
5996
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005997/*
5998 * "flags": INSCHAR_FORMAT - force formatting
5999 * INSCHAR_CTRLV - char typed just after CTRL-V
6000 * INSCHAR_NO_FEX - don't use 'formatexpr'
6001 *
6002 * NOTE: passes the flags value straight through to internal_format() which,
6003 * beside INSCHAR_FORMAT (above), is also looking for these:
6004 * INSCHAR_DO_COM - format comments
6005 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008insertchar(
6009 int c, /* character to insert or NUL */
6010 int flags, /* INSCHAR_FORMAT, etc. */
6011 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 int textwidth;
6014#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006018 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006020 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
6023 /*
6024 * Try to break the line in two or more pieces when:
6025 * - Always do this if we have been called to do formatting only.
6026 * - Always do this when 'formatoptions' has the 'a' flag and the line
6027 * ends in white space.
6028 * - Otherwise:
6029 * - Don't do this if inserting a blank
6030 * - Don't do this if an existing character is being replaced, unless
6031 * we're in VREPLACE mode.
6032 * - Do this if the cursor is not on the line where insert started
6033 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6034 * before the insert.
6035 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6036 * before 'textwidth'
6037 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006039 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006040 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 && !((State & REPLACE_FLAG)
6042#ifdef FEAT_VREPLACE
6043 && !(State & VREPLACE_FLAG)
6044#endif
6045 && *ml_get_cursor() != NUL)
6046 && (curwin->w_cursor.lnum != Insstart.lnum
6047 || ((!has_format_option(FO_INS_LONG)
6048 || Insstart_textlen <= (colnr_T)textwidth)
6049 && (!fo_ins_blank
6050 || Insstart_blank_vcol <= (colnr_T)textwidth
6051 ))))))
6052 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006053 /* Format with 'formatexpr' when it's set. Use internal formatting
6054 * when 'formatexpr' isn't set or it returns non-zero. */
6055#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006056 int do_internal = TRUE;
6057 colnr_T virtcol = get_nolist_virtcol()
6058 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006059
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006060 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6061 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006062 {
6063 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6064 /* It may be required to save for undo again, e.g. when setline()
6065 * was called. */
6066 ins_need_undo = TRUE;
6067 }
6068 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006070 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (c == NUL) /* only formatting was wanted */
6074 return;
6075
6076#ifdef FEAT_COMMENTS
6077 /* Check whether this character should end a comment. */
6078 if (did_ai && (int)c == end_comment_pending)
6079 {
6080 char_u *line;
6081 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6082 int middle_len, end_len;
6083 int i;
6084
6085 /*
6086 * Need to remove existing (middle) comment leader and insert end
6087 * comment leader. First, check what comment leader we can find.
6088 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006089 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6091 {
6092 /* Skip middle-comment string */
6093 while (*p && p[-1] != ':') /* find end of middle flags */
6094 ++p;
6095 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6096 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006097 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 --middle_len;
6099
6100 /* Find the end-comment string */
6101 while (*p && p[-1] != ':') /* find end of end flags */
6102 ++p;
6103 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6104
6105 /* Skip white space before the cursor */
6106 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006107 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 ;
6109 i++;
6110
6111 /* Skip to before the middle leader */
6112 i -= middle_len;
6113
6114 /* Check some expected things before we go on */
6115 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6116 {
6117 /* Backspace over all the stuff we want to replace */
6118 backspace_until_column(i);
6119
6120 /*
6121 * Insert the end-comment string, except for the last
6122 * character, which will get inserted as normal later.
6123 */
6124 ins_bytes_len(lead_end, end_len - 1);
6125 }
6126 }
6127 }
6128 end_comment_pending = NUL;
6129#endif
6130
6131 did_ai = FALSE;
6132#ifdef FEAT_SMARTINDENT
6133 did_si = FALSE;
6134 can_si = FALSE;
6135 can_si_back = FALSE;
6136#endif
6137
6138 /*
6139 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6140 * This speeds up normal text input considerably.
6141 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6142 * need to re-indent at a ':', or any other character (but not what
6143 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006144 * Don't do this when there an InsertCharPre autocommand is defined,
6145 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 */
6147#ifdef USE_ON_FLY_SCROLL
6148 dont_scroll = FALSE; /* allow scrolling here */
6149#endif
6150
6151 if ( !ISSPECIAL(c)
6152#ifdef FEAT_MBYTE
6153 && (!has_mbyte || (*mb_char2len)(c) == 1)
6154#endif
6155 && vpeekc() != NUL
6156 && !(State & REPLACE_FLAG)
6157#ifdef FEAT_CINDENT
6158 && !cindent_on()
6159#endif
6160#ifdef FEAT_RIGHTLEFT
6161 && !p_ri
6162#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006163#ifdef FEAT_AUTOCMD
6164 && !has_insertcharpre()
6165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 )
6167 {
6168#define INPUT_BUFLEN 100
6169 char_u buf[INPUT_BUFLEN + 1];
6170 int i;
6171 colnr_T virtcol = 0;
6172
6173 buf[0] = c;
6174 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 virtcol = get_nolist_virtcol();
6177 /*
6178 * Stop the string when:
6179 * - no more chars available
6180 * - finding a special character (command key)
6181 * - buffer is full
6182 * - running into the 'textwidth' boundary
6183 * - need to check for abbreviation: A non-word char after a word-char
6184 */
6185 while ( (c = vpeekc()) != NUL
6186 && !ISSPECIAL(c)
6187#ifdef FEAT_MBYTE
6188 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6189#endif
6190 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006191# ifdef FEAT_FKMAP
6192 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 && (textwidth == 0
6195 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6196 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6197 {
6198#ifdef FEAT_RIGHTLEFT
6199 c = vgetc();
6200 if (p_hkmap && KeyTyped)
6201 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 buf[i++] = c;
6203#else
6204 buf[i++] = vgetc();
6205#endif
6206 }
6207
6208#ifdef FEAT_DIGRAPHS
6209 do_digraph(-1); /* clear digraphs */
6210 do_digraph(buf[i-1]); /* may be the start of a digraph */
6211#endif
6212 buf[i] = NUL;
6213 ins_str(buf);
6214 if (flags & INSCHAR_CTRLV)
6215 {
6216 redo_literal(*buf);
6217 i = 1;
6218 }
6219 else
6220 i = 0;
6221 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006222 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224 else
6225 {
6226#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227 int cc;
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6230 {
6231 char_u buf[MB_MAXBYTES + 1];
6232
6233 (*mb_char2bytes)(c, buf);
6234 buf[cc] = NUL;
6235 ins_char_bytes(buf, cc);
6236 AppendCharToRedobuff(c);
6237 }
6238 else
6239#endif
6240 {
6241 ins_char(c);
6242 if (flags & INSCHAR_CTRLV)
6243 redo_literal(c);
6244 else
6245 AppendCharToRedobuff(c);
6246 }
6247 }
6248}
6249
6250/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006252 *
6253 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6254 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255 */
6256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257internal_format(
6258 int textwidth,
6259 int second_indent,
6260 int flags,
6261 int format_only,
6262 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263{
6264 int cc;
6265 int save_char = NUL;
6266 int haveto_redraw = FALSE;
6267 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6268#ifdef FEAT_MBYTE
6269 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6270#endif
6271 int fo_white_par = has_format_option(FO_WHITE_PAR);
6272 int first_line = TRUE;
6273#ifdef FEAT_COMMENTS
6274 colnr_T leader_len;
6275 int no_leader = FALSE;
6276 int do_comments = (flags & INSCHAR_DO_COM);
6277#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006278#ifdef FEAT_LINEBREAK
6279 int has_lbr = curwin->w_p_lbr;
6280
6281 /* make sure win_lbr_chartabsize() counts correctly */
6282 curwin->w_p_lbr = FALSE;
6283#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284
6285 /*
6286 * When 'ai' is off we don't want a space under the cursor to be
6287 * deleted. Replace it with an 'x' temporarily.
6288 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006289 if (!curbuf->b_p_ai
6290#ifdef FEAT_VREPLACE
6291 && !(State & VREPLACE_FLAG)
6292#endif
6293 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006294 {
6295 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006296 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006297 {
6298 save_char = cc;
6299 pchar_cursor('x');
6300 }
6301 }
6302
6303 /*
6304 * Repeat breaking lines, until the current line is not too long.
6305 */
6306 while (!got_int)
6307 {
6308 int startcol; /* Cursor column at entry */
6309 int wantcol; /* column at textwidth border */
6310 int foundcol; /* column for start of spaces */
6311 int end_foundcol = 0; /* column for start of word */
6312 colnr_T len;
6313 colnr_T virtcol;
6314#ifdef FEAT_VREPLACE
6315 int orig_col = 0;
6316 char_u *saved_text = NULL;
6317#endif
6318 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006319 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006320
Bram Moolenaar97b98102009-11-17 16:41:01 +00006321 virtcol = get_nolist_virtcol()
6322 + char2cells(c != NUL ? c : gchar_cursor());
6323 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324 break;
6325
6326#ifdef FEAT_COMMENTS
6327 if (no_leader)
6328 do_comments = FALSE;
6329 else if (!(flags & INSCHAR_FORMAT)
6330 && has_format_option(FO_WRAP_COMS))
6331 do_comments = TRUE;
6332
6333 /* Don't break until after the comment leader */
6334 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006335 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006336 else
6337 leader_len = 0;
6338
6339 /* If the line doesn't start with a comment leader, then don't
6340 * start one in a following broken line. Avoids that a %word
6341 * moved to the start of the next line causes all following lines
6342 * to start with %. */
6343 if (leader_len == 0)
6344 no_leader = TRUE;
6345#endif
6346 if (!(flags & INSCHAR_FORMAT)
6347#ifdef FEAT_COMMENTS
6348 && leader_len == 0
6349#endif
6350 && !has_format_option(FO_WRAP))
6351
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006352 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006353 if ((startcol = curwin->w_cursor.col) == 0)
6354 break;
6355
6356 /* find column of textwidth border */
6357 coladvance((colnr_T)textwidth);
6358 wantcol = curwin->w_cursor.col;
6359
Bram Moolenaar97b98102009-11-17 16:41:01 +00006360 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 foundcol = 0;
6362
6363 /*
6364 * Find position to break at.
6365 * Stop at first entered white when 'formatoptions' has 'v'
6366 */
6367 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006368 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006369 || curwin->w_cursor.lnum != Insstart.lnum
6370 || curwin->w_cursor.col >= Insstart.col)
6371 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006372 if (curwin->w_cursor.col == startcol && c != NUL)
6373 cc = c;
6374 else
6375 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376 if (WHITECHAR(cc))
6377 {
6378 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006379 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380
6381 /* find start of sequence of blanks */
6382 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6383 {
6384 dec_cursor();
6385 cc = gchar_cursor();
6386 }
6387 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6388 break; /* only spaces in front of text */
6389#ifdef FEAT_COMMENTS
6390 /* Don't break until after the comment leader */
6391 if (curwin->w_cursor.col < leader_len)
6392 break;
6393#endif
6394 if (has_format_option(FO_ONE_LETTER))
6395 {
6396 /* do not break after one-letter words */
6397 if (curwin->w_cursor.col == 0)
6398 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006399#ifdef FEAT_COMMENTS
6400 /* do not break "#a b" when 'tw' is 2 */
6401 if (curwin->w_cursor.col <= leader_len)
6402 break;
6403#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404 col = curwin->w_cursor.col;
6405 dec_cursor();
6406 cc = gchar_cursor();
6407
6408 if (WHITECHAR(cc))
6409 continue; /* one-letter, continue */
6410 curwin->w_cursor.col = col;
6411 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006412
6413 inc_cursor();
6414
6415 end_foundcol = end_col + 1;
6416 foundcol = curwin->w_cursor.col;
6417 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006418 break;
6419 }
6420#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006421 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006422 {
6423 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006424 if (curwin->w_cursor.col != startcol)
6425 {
6426#ifdef FEAT_COMMENTS
6427 /* Don't break until after the comment leader */
6428 if (curwin->w_cursor.col < leader_len)
6429 break;
6430#endif
6431 col = curwin->w_cursor.col;
6432 inc_cursor();
6433 /* Don't change end_foundcol if already set. */
6434 if (foundcol != curwin->w_cursor.col)
6435 {
6436 foundcol = curwin->w_cursor.col;
6437 end_foundcol = foundcol;
6438 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6439 break;
6440 }
6441 curwin->w_cursor.col = col;
6442 }
6443
6444 if (curwin->w_cursor.col == 0)
6445 break;
6446
6447 col = curwin->w_cursor.col;
6448
6449 dec_cursor();
6450 cc = gchar_cursor();
6451
6452 if (WHITECHAR(cc))
6453 continue; /* break with space */
6454#ifdef FEAT_COMMENTS
6455 /* Don't break until after the comment leader */
6456 if (curwin->w_cursor.col < leader_len)
6457 break;
6458#endif
6459
6460 curwin->w_cursor.col = col;
6461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006462 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006464 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6465 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466 }
6467#endif
6468 if (curwin->w_cursor.col == 0)
6469 break;
6470 dec_cursor();
6471 }
6472
6473 if (foundcol == 0) /* no spaces, cannot break line */
6474 {
6475 curwin->w_cursor.col = startcol;
6476 break;
6477 }
6478
6479 /* Going to break the line, remove any "$" now. */
6480 undisplay_dollar();
6481
6482 /*
6483 * Offset between cursor position and line break is used by replace
6484 * stack functions. VREPLACE does not use this, and backspaces
6485 * over the text instead.
6486 */
6487#ifdef FEAT_VREPLACE
6488 if (State & VREPLACE_FLAG)
6489 orig_col = startcol; /* Will start backspacing from here */
6490 else
6491#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006492 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493
6494 /*
6495 * adjust startcol for spaces that will be deleted and
6496 * characters that will remain on top line
6497 */
6498 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006499 while ((cc = gchar_cursor(), WHITECHAR(cc))
6500 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006501 inc_cursor();
6502 startcol -= curwin->w_cursor.col;
6503 if (startcol < 0)
6504 startcol = 0;
6505
6506#ifdef FEAT_VREPLACE
6507 if (State & VREPLACE_FLAG)
6508 {
6509 /*
6510 * In VREPLACE mode, we will backspace over the text to be
6511 * wrapped, so save a copy now to put on the next line.
6512 */
6513 saved_text = vim_strsave(ml_get_cursor());
6514 curwin->w_cursor.col = orig_col;
6515 if (saved_text == NULL)
6516 break; /* Can't do it, out of memory */
6517 saved_text[startcol] = NUL;
6518
6519 /* Backspace over characters that will move to the next line */
6520 if (!fo_white_par)
6521 backspace_until_column(foundcol);
6522 }
6523 else
6524#endif
6525 {
6526 /* put cursor after pos. to break line */
6527 if (!fo_white_par)
6528 curwin->w_cursor.col = foundcol;
6529 }
6530
6531 /*
6532 * Split the line just before the margin.
6533 * Only insert/delete lines, but don't really redraw the window.
6534 */
6535 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6536 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6537#ifdef FEAT_COMMENTS
6538 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006539 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006541 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6542 if (!(flags & INSCHAR_COM_LIST))
6543 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006544
6545 replace_offset = 0;
6546 if (first_line)
6547 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006548 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006549 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006550 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006551 * This section is for auto-wrap of numeric lists. When not
6552 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6553 * flag will be set and open_line() will handle it (as seen
6554 * above). The code here (and in get_number_indent()) will
6555 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006556 */
6557 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006558 second_indent =
6559 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006560 if (second_indent >= 0)
6561 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006562#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006563 if (State & VREPLACE_FLAG)
6564 change_indent(INDENT_SET, second_indent,
6565 FALSE, NUL, TRUE);
6566 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006567#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006568#ifdef FEAT_COMMENTS
6569 if (leader_len > 0 && second_indent - leader_len > 0)
6570 {
6571 int i;
6572 int padding = second_indent - leader_len;
6573
6574 /* We started at the first_line of a numbered list
6575 * that has a comment. the open_line() function has
6576 * inserted the proper comment leader and positioned
6577 * the cursor at the end of the split line. Now we
6578 * add the additional whitespace needed after the
6579 * comment leader for the numbered list. */
6580 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006581 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006582 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006583 }
6584 else
6585 {
6586#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006587 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006588#ifdef FEAT_COMMENTS
6589 }
6590#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006591 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 }
6593 first_line = FALSE;
6594 }
6595
6596#ifdef FEAT_VREPLACE
6597 if (State & VREPLACE_FLAG)
6598 {
6599 /*
6600 * In VREPLACE mode we have backspaced over the text to be
6601 * moved, now we re-insert it into the new line.
6602 */
6603 ins_bytes(saved_text);
6604 vim_free(saved_text);
6605 }
6606 else
6607#endif
6608 {
6609 /*
6610 * Check if cursor is not past the NUL off the line, cindent
6611 * may have added or removed indent.
6612 */
6613 curwin->w_cursor.col += startcol;
6614 len = (colnr_T)STRLEN(ml_get_curline());
6615 if (curwin->w_cursor.col > len)
6616 curwin->w_cursor.col = len;
6617 }
6618
6619 haveto_redraw = TRUE;
6620#ifdef FEAT_CINDENT
6621 can_cindent = TRUE;
6622#endif
6623 /* moved the cursor, don't autoindent or cindent now */
6624 did_ai = FALSE;
6625#ifdef FEAT_SMARTINDENT
6626 did_si = FALSE;
6627 can_si = FALSE;
6628 can_si_back = FALSE;
6629#endif
6630 line_breakcheck();
6631 }
6632
6633 if (save_char != NUL) /* put back space after cursor */
6634 pchar_cursor(save_char);
6635
Bram Moolenaar0026d472014-09-09 16:32:39 +02006636#ifdef FEAT_LINEBREAK
6637 curwin->w_p_lbr = has_lbr;
6638#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006639 if (!format_only && haveto_redraw)
6640 {
6641 update_topline();
6642 redraw_curbuf_later(VALID);
6643 }
6644}
6645
6646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 * Called after inserting or deleting text: When 'formatoptions' includes the
6648 * 'a' flag format from the current line until the end of the paragraph.
6649 * Keep the cursor at the same position relative to the text.
6650 * The caller must have saved the cursor line for undo, following ones will be
6651 * saved here.
6652 */
6653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006654auto_format(
6655 int trailblank, /* when TRUE also format with trailing blank */
6656 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 pos_T pos;
6659 colnr_T len;
6660 char_u *old;
6661 char_u *new, *pnew;
6662 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006663 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
6665 if (!has_format_option(FO_AUTO))
6666 return;
6667
6668 pos = curwin->w_cursor;
6669 old = ml_get_curline();
6670
6671 /* may remove added space */
6672 check_auto_format(FALSE);
6673
6674 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6675 * user might insert normal text next. Also skip formatting when "1" is
6676 * in 'formatoptions' and there is a single character before the cursor.
6677 * Otherwise the line would be broken and when typing another non-white
6678 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006679 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (*old != NUL && !trailblank && wasatend)
6681 {
6682 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006683 cc = gchar_cursor();
6684 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6685 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006687 cc = gchar_cursor();
6688 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {
6690 curwin->w_cursor = pos;
6691 return;
6692 }
6693 curwin->w_cursor = pos;
6694 }
6695
6696#ifdef FEAT_COMMENTS
6697 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6698 * comments. */
6699 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006700 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 return;
6702#endif
6703
6704 /*
6705 * May start formatting in a previous line, so that after "x" a word is
6706 * moved to the previous line if it fits there now. Only when this is not
6707 * the start of a paragraph.
6708 */
6709 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6710 {
6711 --curwin->w_cursor.lnum;
6712 if (u_save_cursor() == FAIL)
6713 return;
6714 }
6715
6716 /*
6717 * Do the formatting and restore the cursor position. "saved_cursor" will
6718 * be adjusted for the text formatting.
6719 */
6720 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006721 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 curwin->w_cursor = saved_cursor;
6723 saved_cursor.lnum = 0;
6724
6725 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6726 {
6727 /* "cannot happen" */
6728 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6729 coladvance((colnr_T)MAXCOL);
6730 }
6731 else
6732 check_cursor_col();
6733
6734 /* Insert mode: If the cursor is now after the end of the line while it
6735 * previously wasn't, the line was broken. Because of the rule above we
6736 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6737 * formatted. */
6738 if (!wasatend && has_format_option(FO_WHITE_PAR))
6739 {
6740 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006741 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (curwin->w_cursor.col == len)
6743 {
6744 pnew = vim_strnsave(new, len + 2);
6745 pnew[len] = ' ';
6746 pnew[len + 1] = NUL;
6747 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6748 /* remove the space later */
6749 did_add_space = TRUE;
6750 }
6751 else
6752 /* may remove added space */
6753 check_auto_format(FALSE);
6754 }
6755
6756 check_cursor();
6757}
6758
6759/*
6760 * When an extra space was added to continue a paragraph for auto-formatting,
6761 * delete it now. The space must be under the cursor, just after the insert
6762 * position.
6763 */
6764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765check_auto_format(
6766 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006769 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
6771 if (did_add_space)
6772 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006773 cc = gchar_cursor();
6774 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 /* Somehow the space was removed already. */
6776 did_add_space = FALSE;
6777 else
6778 {
6779 if (!end_insert)
6780 {
6781 inc_cursor();
6782 c = gchar_cursor();
6783 dec_cursor();
6784 }
6785 if (c != NUL)
6786 {
6787 /* The space is no longer at the end of the line, delete it. */
6788 del_char(FALSE);
6789 did_add_space = FALSE;
6790 }
6791 }
6792 }
6793}
6794
6795/*
6796 * Find out textwidth to be used for formatting:
6797 * if 'textwidth' option is set, use it
6798 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6799 * if invalid value, use 0.
6800 * Set default to window width (maximum 79) for "gq" operator.
6801 */
6802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006803comp_textwidth(
6804 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int textwidth;
6807
6808 textwidth = curbuf->b_p_tw;
6809 if (textwidth == 0 && curbuf->b_p_wm)
6810 {
6811 /* The width is the window width minus 'wrapmargin' minus all the
6812 * things that add to the margin. */
6813 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6814#ifdef FEAT_CMDWIN
6815 if (cmdwin_type != 0)
6816 textwidth -= 1;
6817#endif
6818#ifdef FEAT_FOLDING
6819 textwidth -= curwin->w_p_fdc;
6820#endif
6821#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006822 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 textwidth -= 1;
6824#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006825 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 textwidth -= 8;
6827 }
6828 if (textwidth < 0)
6829 textwidth = 0;
6830 if (ff && textwidth == 0)
6831 {
6832 textwidth = W_WIDTH(curwin) - 1;
6833 if (textwidth > 79)
6834 textwidth = 79;
6835 }
6836 return textwidth;
6837}
6838
6839/*
6840 * Put a character in the redo buffer, for when just after a CTRL-V.
6841 */
6842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006843redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 char_u buf[10];
6846
6847 /* Only digits need special treatment. Translate them into a string of
6848 * three digits. */
6849 if (VIM_ISDIGIT(c))
6850 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006851 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 AppendToRedobuff(buf);
6853 }
6854 else
6855 AppendCharToRedobuff(c);
6856}
6857
6858/*
6859 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006860 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 */
6862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006863start_arrow(
6864 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006866 start_arrow_common(end_insert_pos, TRUE);
6867}
6868
6869/*
6870 * Like start_arrow() but with end_change argument.
6871 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6872 */
6873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006874start_arrow_with_change(
6875 pos_T *end_insert_pos, /* can be NULL */
6876 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006877{
6878 start_arrow_common(end_insert_pos, end_change);
6879 if (!end_change)
6880 {
6881 AppendCharToRedobuff(Ctrl_G);
6882 AppendCharToRedobuff('U');
6883 }
6884}
6885
6886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006887start_arrow_common(
6888 pos_T *end_insert_pos, /* can be NULL */
6889 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006890{
6891 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
6893 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006894 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 arrow_used = TRUE; /* this means we stopped the current insert */
6896 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006897#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006898 check_spell_redraw();
6899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900}
6901
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006902#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006903/*
6904 * If we skipped highlighting word at cursor, do it now.
6905 * It may be skipped again, thus reset spell_redraw_lnum first.
6906 */
6907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006908check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006909{
6910 if (spell_redraw_lnum != 0)
6911 {
6912 linenr_T lnum = spell_redraw_lnum;
6913
6914 spell_redraw_lnum = 0;
6915 redrawWinline(lnum, FALSE);
6916 }
6917}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006918
6919/*
6920 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6921 * spelled word, if there is one.
6922 */
6923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006924spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006925{
6926 pos_T tpos = curwin->w_cursor;
6927
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006928 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006929 if (curwin->w_cursor.col != tpos.col)
6930 start_arrow(&tpos);
6931}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006932#endif
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934/*
6935 * stop_arrow() is called before a change is made in insert mode.
6936 * If an arrow key has been used, start a new insertion.
6937 * Returns FAIL if undo is impossible, shouldn't insert then.
6938 */
6939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006940stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
6942 if (arrow_used)
6943 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006944 Insstart = curwin->w_cursor; /* new insertion starts here */
6945 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6946 /* Don't update the original insert position when moved to the
6947 * right, except when nothing was inserted yet. */
6948 update_Insstart_orig = FALSE;
6949 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (u_save_cursor() == OK)
6952 {
6953 arrow_used = FALSE;
6954 ins_need_undo = FALSE;
6955 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 ai_col = 0;
6958#ifdef FEAT_VREPLACE
6959 if (State & VREPLACE_FLAG)
6960 {
6961 orig_line_count = curbuf->b_ml.ml_line_count;
6962 vr_lines_changed = 1;
6963 }
6964#endif
6965 ResetRedobuff();
6966 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006967 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969 else if (ins_need_undo)
6970 {
6971 if (u_save_cursor() == OK)
6972 ins_need_undo = FALSE;
6973 }
6974
6975#ifdef FEAT_FOLDING
6976 /* Always open fold at the cursor line when inserting something. */
6977 foldOpenCursor();
6978#endif
6979
6980 return (arrow_used || ins_need_undo ? FAIL : OK);
6981}
6982
6983/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006984 * Do a few things to stop inserting.
6985 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6986 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 */
6988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989stop_insert(
6990 pos_T *end_insert_pos,
6991 int esc, /* called by ins_esc() */
6992 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006994 int cc;
6995 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
6997 stop_redo_ins();
6998 replace_flush(); /* abandon replace stack */
6999
7000 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007001 * Save the inserted text for later redo with ^@ and CTRL-A.
7002 * Don't do it when "restart_edit" was set and nothing was inserted,
7003 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007005 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007006 if (did_restart_edit == 0 || (ptr != NULL
7007 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007008 {
7009 vim_free(last_insert);
7010 last_insert = ptr;
7011 last_insert_skip = new_insert_skip;
7012 }
7013 else
7014 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007016 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {
7018 /* Auto-format now. It may seem strange to do this when stopping an
7019 * insertion (or moving the cursor), but it's required when appending
7020 * a line and having it end in a space. But only do it when something
7021 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007022 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007024 pos_T tpos = curwin->w_cursor;
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 /* When the cursor is at the end of the line after a space the
7027 * formatting will move it to the following word. Avoid that by
7028 * moving the cursor onto the space. */
7029 cc = 'x';
7030 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7031 {
7032 dec_cursor();
7033 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007034 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007035 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037
7038 auto_format(TRUE, FALSE);
7039
Bram Moolenaar1c465442017-03-12 20:10:05 +01007040 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007041 {
7042 if (gchar_cursor() != NUL)
7043 inc_cursor();
7044#ifdef FEAT_VIRTUALEDIT
7045 /* If the cursor is still at the same character, also keep
7046 * the "coladd". */
7047 if (gchar_cursor() == NUL
7048 && curwin->w_cursor.lnum == tpos.lnum
7049 && curwin->w_cursor.col == tpos.col)
7050 curwin->w_cursor.coladd = tpos.coladd;
7051#endif
7052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
7054
7055 /* If a space was inserted for auto-formatting, remove it now. */
7056 check_auto_format(TRUE);
7057
7058 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007059 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007060 * Do this when ESC was used or moving the cursor up/down.
7061 * Check for the old position still being valid, just in case the text
7062 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007063 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007064 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7065 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007067 pos_T tpos = curwin->w_cursor;
7068
7069 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007070 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007071 for (;;)
7072 {
7073 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7074 --curwin->w_cursor.col;
7075 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007076 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007077 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007078 if (del_char(TRUE) == FAIL)
7079 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007080 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007081 if (curwin->w_cursor.lnum != tpos.lnum)
7082 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007083 else
7084 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007085 /* reset tpos, could have been invalidated in the loop above */
7086 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007087 tpos.col++;
7088 if (cc != NUL && gchar_pos(&tpos) == NUL)
7089 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 /* <C-S-Right> may have started Visual mode, adjust the position for
7093 * deleted characters. */
7094 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7095 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007096 int len = (int)STRLEN(ml_get_curline());
7097
7098 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007100 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007101#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 }
7105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 }
7107 }
7108 did_ai = FALSE;
7109#ifdef FEAT_SMARTINDENT
7110 did_si = FALSE;
7111 can_si = FALSE;
7112 can_si_back = FALSE;
7113#endif
7114
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007115 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7116 * now in a different buffer. */
7117 if (end_insert_pos != NULL)
7118 {
7119 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007120 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007121 curbuf->b_op_end = *end_insert_pos;
7122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123}
7124
7125/*
7126 * Set the last inserted text to a single character.
7127 * Used for the replace command.
7128 */
7129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007130set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 char_u *s;
7133
7134 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (last_insert != NULL)
7137 {
7138 s = last_insert;
7139 /* Use the CTRL-V only when entering a special char */
7140 if (c < ' ' || c == DEL)
7141 *s++ = Ctrl_V;
7142 s = add_char2buf(c, s);
7143 *s++ = ESC;
7144 *s++ = NUL;
7145 last_insert_skip = 0;
7146 }
7147}
7148
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007149#if defined(EXITFREE) || defined(PROTO)
7150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007151free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007152{
7153 vim_free(last_insert);
7154 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007155# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007156 vim_free(compl_orig_text);
7157 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007158# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007159}
7160#endif
7161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162/*
7163 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7164 * and CSI. Handle multi-byte characters.
7165 * Returns a pointer to after the added bytes.
7166 */
7167 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007168add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007171 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 int i;
7173 int len;
7174
7175 len = (*mb_char2bytes)(c, temp);
7176 for (i = 0; i < len; ++i)
7177 {
7178 c = temp[i];
7179#endif
7180 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7181 if (c == K_SPECIAL)
7182 {
7183 *s++ = K_SPECIAL;
7184 *s++ = KS_SPECIAL;
7185 *s++ = KE_FILLER;
7186 }
7187#ifdef FEAT_GUI
7188 else if (c == CSI)
7189 {
7190 *s++ = CSI;
7191 *s++ = KS_EXTRA;
7192 *s++ = (int)KE_CSI;
7193 }
7194#endif
7195 else
7196 *s++ = c;
7197#ifdef FEAT_MBYTE
7198 }
7199#endif
7200 return s;
7201}
7202
7203/*
7204 * move cursor to start of line
7205 * if flags & BL_WHITE move to first non-white
7206 * if flags & BL_SOL move to first non-white if startofline is set,
7207 * otherwise keep "curswant" column
7208 * if flags & BL_FIX don't leave the cursor on a NUL.
7209 */
7210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007211beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 if ((flags & BL_SOL) && !p_sol)
7214 coladvance(curwin->w_curswant);
7215 else
7216 {
7217 curwin->w_cursor.col = 0;
7218#ifdef FEAT_VIRTUALEDIT
7219 curwin->w_cursor.coladd = 0;
7220#endif
7221
7222 if (flags & (BL_WHITE | BL_SOL))
7223 {
7224 char_u *ptr;
7225
Bram Moolenaar1c465442017-03-12 20:10:05 +01007226 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7228 ++curwin->w_cursor.col;
7229 }
7230 curwin->w_set_curswant = TRUE;
7231 }
7232}
7233
7234/*
7235 * oneright oneleft cursor_down cursor_up
7236 *
7237 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007238 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 * Return OK when successful, FAIL when we hit a line of file boundary.
7240 */
7241
7242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
7248#ifdef FEAT_VIRTUALEDIT
7249 if (virtual_active())
7250 {
7251 pos_T prevpos = curwin->w_cursor;
7252
7253 /* Adjust for multi-wide char (excluding TAB) */
7254 ptr = ml_get_cursor();
7255 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007256# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007258# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 ))
7262 ? ptr2cells(ptr) : 1));
7263 curwin->w_set_curswant = TRUE;
7264 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7265 return (prevpos.col != curwin->w_cursor.col
7266 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7267 }
7268#endif
7269
7270 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007271 if (*ptr == NUL)
7272 return FAIL; /* already at the very end */
7273
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007275 if (has_mbyte)
7276 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
7278#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007279 l = 1;
7280
7281 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7282 * contains "onemore". */
7283 if (ptr[l] == NUL
7284#ifdef FEAT_VIRTUALEDIT
7285 && (ve_flags & VE_ONEMORE) == 0
7286#endif
7287 )
7288 return FAIL;
7289 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291 curwin->w_set_curswant = TRUE;
7292 return OK;
7293}
7294
7295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
7298#ifdef FEAT_VIRTUALEDIT
7299 if (virtual_active())
7300 {
7301 int width;
7302 int v = getviscol();
7303
7304 if (v == 0)
7305 return FAIL;
7306
7307# ifdef FEAT_LINEBREAK
7308 /* We might get stuck on 'showbreak', skip over it. */
7309 width = 1;
7310 for (;;)
7311 {
7312 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007313 /* getviscol() is slow, skip it when 'showbreak' is empty,
7314 * 'breakindent' is not set and there are no multi-byte
7315 * characters */
7316 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317# ifdef FEAT_MBYTE
7318 && !has_mbyte
7319# endif
7320 ) || getviscol() < v)
7321 break;
7322 ++width;
7323 }
7324# else
7325 coladvance(v - 1);
7326# endif
7327
7328 if (curwin->w_cursor.coladd == 1)
7329 {
7330 char_u *ptr;
7331
7332 /* Adjust for multi-wide char (not a TAB) */
7333 ptr = ml_get_cursor();
7334 if (*ptr != TAB && vim_isprintc(
7335# ifdef FEAT_MBYTE
7336 (*mb_ptr2char)(ptr)
7337# else
7338 *ptr
7339# endif
7340 ) && ptr2cells(ptr) > 1)
7341 curwin->w_cursor.coladd = 0;
7342 }
7343
7344 curwin->w_set_curswant = TRUE;
7345 return OK;
7346 }
7347#endif
7348
7349 if (curwin->w_cursor.col == 0)
7350 return FAIL;
7351
7352 curwin->w_set_curswant = TRUE;
7353 --curwin->w_cursor.col;
7354
7355#ifdef FEAT_MBYTE
7356 /* if the character on the left of the current cursor is a multi-byte
7357 * character, move to its first byte */
7358 if (has_mbyte)
7359 mb_adjust_cursor();
7360#endif
7361 return OK;
7362}
7363
7364 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007365cursor_up(
7366 long n,
7367 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369 linenr_T lnum;
7370
7371 if (n > 0)
7372 {
7373 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007374 /* This fails if the cursor is already in the first line or the count
7375 * is larger than the line number and '-' is in 'cpoptions' */
7376 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 return FAIL;
7378 if (n >= lnum)
7379 lnum = 1;
7380 else
7381#ifdef FEAT_FOLDING
7382 if (hasAnyFolding(curwin))
7383 {
7384 /*
7385 * Count each sequence of folded lines as one logical line.
7386 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007387 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 (void)hasFolding(lnum, &lnum, NULL);
7389
7390 while (n--)
7391 {
7392 /* move up one line */
7393 --lnum;
7394 if (lnum <= 1)
7395 break;
7396 /* If we entered a fold, move to the beginning, unless in
7397 * Insert mode or when 'foldopen' contains "all": it will open
7398 * in a moment. */
7399 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7400 (void)hasFolding(lnum, &lnum, NULL);
7401 }
7402 if (lnum < 1)
7403 lnum = 1;
7404 }
7405 else
7406#endif
7407 lnum -= n;
7408 curwin->w_cursor.lnum = lnum;
7409 }
7410
7411 /* try to advance to the column we want to be at */
7412 coladvance(curwin->w_curswant);
7413
7414 if (upd_topline)
7415 update_topline(); /* make sure curwin->w_topline is valid */
7416
7417 return OK;
7418}
7419
7420/*
7421 * Cursor down a number of logical lines.
7422 */
7423 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424cursor_down(
7425 long n,
7426 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 linenr_T lnum;
7429
7430 if (n > 0)
7431 {
7432 lnum = curwin->w_cursor.lnum;
7433#ifdef FEAT_FOLDING
7434 /* Move to last line of fold, will fail if it's the end-of-file. */
7435 (void)hasFolding(lnum, NULL, &lnum);
7436#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007437 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007438 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007439 if (lnum >= curbuf->b_ml.ml_line_count
7440 || (lnum + n > curbuf->b_ml.ml_line_count
7441 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 return FAIL;
7443 if (lnum + n >= curbuf->b_ml.ml_line_count)
7444 lnum = curbuf->b_ml.ml_line_count;
7445 else
7446#ifdef FEAT_FOLDING
7447 if (hasAnyFolding(curwin))
7448 {
7449 linenr_T last;
7450
7451 /* count each sequence of folded lines as one logical line */
7452 while (n--)
7453 {
7454 if (hasFolding(lnum, NULL, &last))
7455 lnum = last + 1;
7456 else
7457 ++lnum;
7458 if (lnum >= curbuf->b_ml.ml_line_count)
7459 break;
7460 }
7461 if (lnum > curbuf->b_ml.ml_line_count)
7462 lnum = curbuf->b_ml.ml_line_count;
7463 }
7464 else
7465#endif
7466 lnum += n;
7467 curwin->w_cursor.lnum = lnum;
7468 }
7469
7470 /* try to advance to the column we want to be at */
7471 coladvance(curwin->w_curswant);
7472
7473 if (upd_topline)
7474 update_topline(); /* make sure curwin->w_topline is valid */
7475
7476 return OK;
7477}
7478
7479/*
7480 * Stuff the last inserted text in the read buffer.
7481 * Last_insert actually is a copy of the redo buffer, so we
7482 * first have to remove the command.
7483 */
7484 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007485stuff_inserted(
7486 int c, /* Command character to be inserted */
7487 long count, /* Repeat this many times */
7488 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 char_u *esc_ptr;
7491 char_u *ptr;
7492 char_u *last_ptr;
7493 char_u last = NUL;
7494
7495 ptr = get_last_insert();
7496 if (ptr == NULL)
7497 {
7498 EMSG(_(e_noinstext));
7499 return FAIL;
7500 }
7501
7502 /* may want to stuff the command character, to start Insert mode */
7503 if (c != NUL)
7504 stuffcharReadbuff(c);
7505 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7506 *esc_ptr = NUL; /* remove the ESC */
7507
7508 /* when the last char is either "0" or "^" it will be quoted if no ESC
7509 * comes after it OR if it will inserted more than once and "ptr"
7510 * starts with ^D. -- Acevedo
7511 */
7512 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7513 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7514 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7515 {
7516 last = *last_ptr;
7517 *last_ptr = NUL;
7518 }
7519
7520 do
7521 {
7522 stuffReadbuff(ptr);
7523 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7524 if (last)
7525 stuffReadbuff((char_u *)(last == '0'
7526 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7527 : IF_EB("\026^", CTRL_V_STR "^")));
7528 }
7529 while (--count > 0);
7530
7531 if (last)
7532 *last_ptr = last;
7533
7534 if (esc_ptr != NULL)
7535 *esc_ptr = ESC; /* put the ESC back */
7536
7537 /* may want to stuff a trailing ESC, to get out of Insert mode */
7538 if (!no_esc)
7539 stuffcharReadbuff(ESC);
7540
7541 return OK;
7542}
7543
7544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 if (last_insert == NULL)
7548 return NULL;
7549 return last_insert + last_insert_skip;
7550}
7551
7552/*
7553 * Get last inserted string, and remove trailing <Esc>.
7554 * Returns pointer to allocated memory (must be freed) or NULL.
7555 */
7556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007557get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
7559 char_u *s;
7560 int len;
7561
7562 if (last_insert == NULL)
7563 return NULL;
7564 s = vim_strsave(last_insert + last_insert_skip);
7565 if (s != NULL)
7566 {
7567 len = (int)STRLEN(s);
7568 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7569 s[len - 1] = NUL;
7570 }
7571 return s;
7572}
7573
7574/*
7575 * Check the word in front of the cursor for an abbreviation.
7576 * Called when the non-id character "c" has been entered.
7577 * When an abbreviation is recognized it is removed from the text and
7578 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7579 */
7580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
7583 /* Don't check for abbreviation in paste mode, when disabled and just
7584 * after moving around with cursor keys. */
7585 if (p_paste || no_abbr || arrow_used)
7586 return FALSE;
7587
7588 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7589 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7590}
7591
7592/*
7593 * replace-stack functions
7594 *
7595 * When replacing characters, the replaced characters are remembered for each
7596 * new character. This is used to re-insert the old text when backspacing.
7597 *
7598 * There is a NUL headed list of characters for each character that is
7599 * currently in the file after the insertion point. When BS is used, one NUL
7600 * headed list is put back for the deleted character.
7601 *
7602 * For a newline, there are two NUL headed lists. One contains the characters
7603 * that the NL replaced. The extra one stores the characters after the cursor
7604 * that were deleted (always white space).
7605 *
7606 * Replace_offset is normally 0, in which case replace_push will add a new
7607 * character at the end of the stack. If replace_offset is not 0, that many
7608 * characters will be left on the stack above the newly inserted character.
7609 */
7610
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007611static char_u *replace_stack = NULL;
7612static long replace_stack_nr = 0; /* next entry in replace stack */
7613static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614
7615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007616replace_push(
7617 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
7619 char_u *p;
7620
7621 if (replace_stack_nr < replace_offset) /* nothing to do */
7622 return;
7623 if (replace_stack_len <= replace_stack_nr)
7624 {
7625 replace_stack_len += 50;
7626 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7627 if (p == NULL) /* out of memory */
7628 {
7629 replace_stack_len -= 50;
7630 return;
7631 }
7632 if (replace_stack != NULL)
7633 {
7634 mch_memmove(p, replace_stack,
7635 (size_t)(replace_stack_nr * sizeof(char_u)));
7636 vim_free(replace_stack);
7637 }
7638 replace_stack = p;
7639 }
7640 p = replace_stack + replace_stack_nr - replace_offset;
7641 if (replace_offset)
7642 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7643 *p = c;
7644 ++replace_stack_nr;
7645}
7646
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007647#if defined(FEAT_MBYTE) || defined(PROTO)
7648/*
7649 * Push a character onto the replace stack. Handles a multi-byte character in
7650 * reverse byte order, so that the first byte is popped off first.
7651 * Return the number of bytes done (includes composing characters).
7652 */
7653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007655{
7656 int l = (*mb_ptr2len)(p);
7657 int j;
7658
7659 for (j = l - 1; j >= 0; --j)
7660 replace_push(p[j]);
7661 return l;
7662}
7663#endif
7664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665/*
7666 * Pop one item from the replace stack.
7667 * return -1 if stack empty
7668 * return replaced character or NUL otherwise
7669 */
7670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 if (replace_stack_nr == 0)
7674 return -1;
7675 return (int)replace_stack[--replace_stack_nr];
7676}
7677
7678/*
7679 * Join the top two items on the replace stack. This removes to "off"'th NUL
7680 * encountered.
7681 */
7682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007683replace_join(
7684 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
7686 int i;
7687
7688 for (i = replace_stack_nr; --i >= 0; )
7689 if (replace_stack[i] == NUL && off-- <= 0)
7690 {
7691 --replace_stack_nr;
7692 mch_memmove(replace_stack + i, replace_stack + i + 1,
7693 (size_t)(replace_stack_nr - i));
7694 return;
7695 }
7696}
7697
7698/*
7699 * Pop bytes from the replace stack until a NUL is found, and insert them
7700 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7701 */
7702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007703replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
7705 int cc;
7706 int oldState = State;
7707
7708 State = NORMAL; /* don't want REPLACE here */
7709 while ((cc = replace_pop()) > 0)
7710 {
7711#ifdef FEAT_MBYTE
7712 mb_replace_pop_ins(cc);
7713#else
7714 ins_char(cc);
7715#endif
7716 dec_cursor();
7717 }
7718 State = oldState;
7719}
7720
7721#ifdef FEAT_MBYTE
7722/*
7723 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7724 * indicates a multi-byte char, pop the other bytes too.
7725 */
7726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007730 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 int i;
7732 int c;
7733
7734 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7735 {
7736 buf[0] = cc;
7737 for (i = 1; i < n; ++i)
7738 buf[i] = replace_pop();
7739 ins_bytes_len(buf, n);
7740 }
7741 else
7742 ins_char(cc);
7743
7744 if (enc_utf8)
7745 /* Handle composing chars. */
7746 for (;;)
7747 {
7748 c = replace_pop();
7749 if (c == -1) /* stack empty */
7750 break;
7751 if ((n = MB_BYTE2LEN(c)) == 1)
7752 {
7753 /* Not a multi-byte char, put it back. */
7754 replace_push(c);
7755 break;
7756 }
7757 else
7758 {
7759 buf[0] = c;
7760 for (i = 1; i < n; ++i)
7761 buf[i] = replace_pop();
7762 if (utf_iscomposing(utf_ptr2char(buf)))
7763 ins_bytes_len(buf, n);
7764 else
7765 {
7766 /* Not a composing char, put it back. */
7767 for (i = n - 1; i >= 0; --i)
7768 replace_push(buf[i]);
7769 break;
7770 }
7771 }
7772 }
7773}
7774#endif
7775
7776/*
7777 * make the replace stack empty
7778 * (called when exiting replace mode)
7779 */
7780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007781replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 vim_free(replace_stack);
7784 replace_stack = NULL;
7785 replace_stack_len = 0;
7786 replace_stack_nr = 0;
7787}
7788
7789/*
7790 * Handle doing a BS for one character.
7791 * cc < 0: replace stack empty, just move cursor
7792 * cc == 0: character was inserted, delete it
7793 * cc > 0: character was replaced, put cc (first byte of original char) back
7794 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007795 * When "limit_col" is >= 0, don't delete before this column. Matters when
7796 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 */
7798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007799replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 int cc;
7802#ifdef FEAT_VREPLACE
7803 int orig_len = 0;
7804 int ins_len;
7805 int orig_vcols = 0;
7806 colnr_T start_vcol;
7807 char_u *p;
7808 int i;
7809 int vcol;
7810#endif
7811
7812 cc = replace_pop();
7813 if (cc > 0)
7814 {
7815#ifdef FEAT_VREPLACE
7816 if (State & VREPLACE_FLAG)
7817 {
7818 /* Get the number of screen cells used by the character we are
7819 * going to delete. */
7820 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7821 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7822 }
7823#endif
7824#ifdef FEAT_MBYTE
7825 if (has_mbyte)
7826 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007827 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828# ifdef FEAT_VREPLACE
7829 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007830 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831# endif
7832 replace_push(cc);
7833 }
7834 else
7835#endif
7836 {
7837 pchar_cursor(cc);
7838#ifdef FEAT_VREPLACE
7839 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007840 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#endif
7842 }
7843 replace_pop_ins();
7844
7845#ifdef FEAT_VREPLACE
7846 if (State & VREPLACE_FLAG)
7847 {
7848 /* Get the number of screen cells used by the inserted characters */
7849 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007850 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 vcol = start_vcol;
7852 for (i = 0; i < ins_len; ++i)
7853 {
7854 vcol += chartabsize(p + i, vcol);
7855#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007856 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857#endif
7858 }
7859 vcol -= start_vcol;
7860
7861 /* Delete spaces that were inserted after the cursor to keep the
7862 * text aligned. */
7863 curwin->w_cursor.col += ins_len;
7864 while (vcol > orig_vcols && gchar_cursor() == ' ')
7865 {
7866 del_char(FALSE);
7867 ++orig_vcols;
7868 }
7869 curwin->w_cursor.col -= ins_len;
7870 }
7871#endif
7872
7873 /* mark the buffer as changed and prepare for displaying */
7874 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7875 }
7876 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007877 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
7880#ifdef FEAT_CINDENT
7881/*
7882 * Return TRUE if C-indenting is on.
7883 */
7884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007885cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 return (!p_paste && (curbuf->b_p_cin
7888# ifdef FEAT_EVAL
7889 || *curbuf->b_p_inde != NUL
7890# endif
7891 ));
7892}
7893#endif
7894
7895#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7896/*
7897 * Re-indent the current line, based on the current contents of it and the
7898 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7899 * confused what all the part that handles Control-T is doing that I'm not.
7900 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7901 */
7902
7903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007904fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007906 int amount = get_the_indent();
7907
7908 if (amount >= 0)
7909 {
7910 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7911 if (linewhite(curwin->w_cursor.lnum))
7912 did_ai = TRUE; /* delete the indent if the line stays empty */
7913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914}
7915
7916 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007917fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 if (p_paste)
7920 return;
7921# ifdef FEAT_LISP
7922 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7923 fixthisline(get_lisp_indent);
7924# endif
7925# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7926 else
7927# endif
7928# ifdef FEAT_CINDENT
7929 if (cindent_on())
7930 do_c_expr_indent();
7931# endif
7932}
7933
7934#endif
7935
7936#ifdef FEAT_CINDENT
7937/*
7938 * return TRUE if 'cinkeys' contains the key "keytyped",
7939 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007940 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7942 *
7943 * "keytyped" can have a few special values:
7944 * KEY_OPEN_FORW
7945 * KEY_OPEN_BACK
7946 * KEY_COMPLETE just finished completion.
7947 *
7948 * If line_is_empty is TRUE accept keys with '0' before them.
7949 */
7950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007951in_cinkeys(
7952 int keytyped,
7953 int when,
7954 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 char_u *look;
7957 int try_match;
7958 int try_match_word;
7959 char_u *p;
7960 char_u *line;
7961 int icase;
7962 int i;
7963
Bram Moolenaar30848942009-12-24 14:46:12 +00007964 if (keytyped == NUL)
7965 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7966 return FALSE;
7967
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968#ifdef FEAT_EVAL
7969 if (*curbuf->b_p_inde != NUL)
7970 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7971 else
7972#endif
7973 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7974 while (*look)
7975 {
7976 /*
7977 * Find out if we want to try a match with this key, depending on
7978 * 'when' and a '*' or '!' before the key.
7979 */
7980 switch (when)
7981 {
7982 case '*': try_match = (*look == '*'); break;
7983 case '!': try_match = (*look == '!'); break;
7984 default: try_match = (*look != '*'); break;
7985 }
7986 if (*look == '*' || *look == '!')
7987 ++look;
7988
7989 /*
7990 * If there is a '0', only accept a match if the line is empty.
7991 * But may still match when typing last char of a word.
7992 */
7993 if (*look == '0')
7994 {
7995 try_match_word = try_match;
7996 if (!line_is_empty)
7997 try_match = FALSE;
7998 ++look;
7999 }
8000 else
8001 try_match_word = FALSE;
8002
8003 /*
8004 * does it look like a control character?
8005 */
8006 if (*look == '^'
8007#ifdef EBCDIC
8008 && (Ctrl_chr(look[1]) != 0)
8009#else
8010 && look[1] >= '?' && look[1] <= '_'
8011#endif
8012 )
8013 {
8014 if (try_match && keytyped == Ctrl_chr(look[1]))
8015 return TRUE;
8016 look += 2;
8017 }
8018 /*
8019 * 'o' means "o" command, open forward.
8020 * 'O' means "O" command, open backward.
8021 */
8022 else if (*look == 'o')
8023 {
8024 if (try_match && keytyped == KEY_OPEN_FORW)
8025 return TRUE;
8026 ++look;
8027 }
8028 else if (*look == 'O')
8029 {
8030 if (try_match && keytyped == KEY_OPEN_BACK)
8031 return TRUE;
8032 ++look;
8033 }
8034
8035 /*
8036 * 'e' means to check for "else" at start of line and just before the
8037 * cursor.
8038 */
8039 else if (*look == 'e')
8040 {
8041 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8042 {
8043 p = ml_get_curline();
8044 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8045 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8046 return TRUE;
8047 }
8048 ++look;
8049 }
8050
8051 /*
8052 * ':' only causes an indent if it is at the end of a label or case
8053 * statement, or when it was before typing the ':' (to fix
8054 * class::method for C++).
8055 */
8056 else if (*look == ':')
8057 {
8058 if (try_match && keytyped == ':')
8059 {
8060 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008061 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008063 /* Need to get the line again after cin_islabel(). */
8064 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (curwin->w_cursor.col > 2
8066 && p[curwin->w_cursor.col - 1] == ':'
8067 && p[curwin->w_cursor.col - 2] == ':')
8068 {
8069 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008070 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008071 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 p = ml_get_curline();
8073 p[curwin->w_cursor.col - 1] = ':';
8074 if (i)
8075 return TRUE;
8076 }
8077 }
8078 ++look;
8079 }
8080
8081
8082 /*
8083 * Is it a key in <>, maybe?
8084 */
8085 else if (*look == '<')
8086 {
8087 if (try_match)
8088 {
8089 /*
8090 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8091 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8092 * >, *, : and ! keys if they really really want to.
8093 */
8094 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8095 && keytyped == look[1])
8096 return TRUE;
8097
8098 if (keytyped == get_special_key_code(look + 1))
8099 return TRUE;
8100 }
8101 while (*look && *look != '>')
8102 look++;
8103 while (*look == '>')
8104 look++;
8105 }
8106
8107 /*
8108 * Is it a word: "=word"?
8109 */
8110 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8111 {
8112 ++look;
8113 if (*look == '~')
8114 {
8115 icase = TRUE;
8116 ++look;
8117 }
8118 else
8119 icase = FALSE;
8120 p = vim_strchr(look, ',');
8121 if (p == NULL)
8122 p = look + STRLEN(look);
8123 if ((try_match || try_match_word)
8124 && curwin->w_cursor.col >= (colnr_T)(p - look))
8125 {
8126 int match = FALSE;
8127
8128#ifdef FEAT_INS_EXPAND
8129 if (keytyped == KEY_COMPLETE)
8130 {
8131 char_u *s;
8132
8133 /* Just completed a word, check if it starts with "look".
8134 * search back for the start of a word. */
8135 line = ml_get_curline();
8136# ifdef FEAT_MBYTE
8137 if (has_mbyte)
8138 {
8139 char_u *n;
8140
8141 for (s = line + curwin->w_cursor.col; s > line; s = n)
8142 {
8143 n = mb_prevptr(line, s);
8144 if (!vim_iswordp(n))
8145 break;
8146 }
8147 }
8148 else
8149# endif
8150 for (s = line + curwin->w_cursor.col; s > line; --s)
8151 if (!vim_iswordc(s[-1]))
8152 break;
8153 if (s + (p - look) <= line + curwin->w_cursor.col
8154 && (icase
8155 ? MB_STRNICMP(s, look, p - look)
8156 : STRNCMP(s, look, p - look)) == 0)
8157 match = TRUE;
8158 }
8159 else
8160#endif
8161 /* TODO: multi-byte */
8162 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8163 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8164 {
8165 line = ml_get_cursor();
8166 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8167 || !vim_iswordc(line[-(p - look) - 1]))
8168 && (icase
8169 ? MB_STRNICMP(line - (p - look), look, p - look)
8170 : STRNCMP(line - (p - look), look, p - look))
8171 == 0)
8172 match = TRUE;
8173 }
8174 if (match && try_match_word && !try_match)
8175 {
8176 /* "0=word": Check if there are only blanks before the
8177 * word. */
8178 line = ml_get_curline();
8179 if ((int)(skipwhite(line) - line) !=
8180 (int)(curwin->w_cursor.col - (p - look)))
8181 match = FALSE;
8182 }
8183 if (match)
8184 return TRUE;
8185 }
8186 look = p;
8187 }
8188
8189 /*
8190 * ok, it's a boring generic character.
8191 */
8192 else
8193 {
8194 if (try_match && *look == keytyped)
8195 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008196 if (*look != NUL)
8197 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199
8200 /*
8201 * Skip over ", ".
8202 */
8203 look = skip_to_option_part(look);
8204 }
8205 return FALSE;
8206}
8207#endif /* FEAT_CINDENT */
8208
8209#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8210/*
8211 * Map Hebrew keyboard when in hkmap mode.
8212 */
8213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008214hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215{
8216 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8217 {
8218 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8219 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8220 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8221 static char_u map[26] =
8222 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8223 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8224 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8225 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8226 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8227 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8228 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8229 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8230 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8231
8232 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8233 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8234 /* '-1'='sofit' */
8235 else if (c == 'x')
8236 return 'X';
8237 else if (c == 'q')
8238 return '\''; /* {geresh}={'} */
8239 else if (c == 246)
8240 return ' '; /* \"o --> ' ' for a german keyboard */
8241 else if (c == 228)
8242 return ' '; /* \"a --> ' ' -- / -- */
8243 else if (c == 252)
8244 return ' '; /* \"u --> ' ' -- / -- */
8245#ifdef EBCDIC
8246 else if (islower(c))
8247#else
8248 /* NOTE: islower() does not do the right thing for us on Linux so we
8249 * do this the same was as 5.7 and previous, so it works correctly on
8250 * all systems. Specifically, the e.g. Delete and Arrow keys are
8251 * munged and won't work if e.g. searching for Hebrew text.
8252 */
8253 else if (c >= 'a' && c <= 'z')
8254#endif
8255 return (int)(map[CharOrdLow(c)] + p_aleph);
8256 else
8257 return c;
8258 }
8259 else
8260 {
8261 switch (c)
8262 {
8263 case '`': return ';';
8264 case '/': return '.';
8265 case '\'': return ',';
8266 case 'q': return '/';
8267 case 'w': return '\'';
8268
8269 /* Hebrew letters - set offset from 'a' */
8270 case ',': c = '{'; break;
8271 case '.': c = 'v'; break;
8272 case ';': c = 't'; break;
8273 default: {
8274 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8275
8276#ifdef EBCDIC
8277 /* see note about islower() above */
8278 if (!islower(c))
8279#else
8280 if (c < 'a' || c > 'z')
8281#endif
8282 return c;
8283 c = str[CharOrdLow(c)];
8284 break;
8285 }
8286 }
8287
8288 return (int)(CharOrdLow(c) + p_aleph);
8289 }
8290}
8291#endif
8292
8293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008294ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 int need_redraw = FALSE;
8297 int regname;
8298 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008299 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
8301 /*
8302 * If we are going to wait for a character, show a '"'.
8303 */
8304 pc_status = PC_STATUS_UNSET;
8305 if (redrawing() && !char_avail())
8306 {
8307 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008308 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
8310 edit_putchar('"', TRUE);
8311#ifdef FEAT_CMDL_INFO
8312 add_to_showcmd_c(Ctrl_R);
8313#endif
8314 }
8315
8316#ifdef USE_ON_FLY_SCROLL
8317 dont_scroll = TRUE; /* disallow scrolling here */
8318#endif
8319
8320 /*
8321 * Don't map the register name. This also prevents the mode message to be
8322 * deleted when ESC is hit.
8323 */
8324 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008325 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8328 {
8329 /* Get a third key for literal register insertion */
8330 literally = regname;
8331#ifdef FEAT_CMDL_INFO
8332 add_to_showcmd_c(literally);
8333#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008334 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
8337 --no_mapping;
8338
8339#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008340 /* Don't call u_sync() while typing the expression or giving an error
8341 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 ++no_u_sync;
8343 if (regname == '=')
8344 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008345# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008347# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008348 /* Sync undo when evaluating the expression calls setline() or
8349 * append(), so that it can be undone separately. */
8350 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008351
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008353# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 /* Restore the Input Method. */
8355 if (im_on)
8356 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008359 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8360 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008361 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 else
8365 {
8366#endif
8367 if (literally == Ctrl_O || literally == Ctrl_P)
8368 {
8369 /* Append the command to the redo buffer. */
8370 AppendCharToRedobuff(Ctrl_R);
8371 AppendCharToRedobuff(literally);
8372 AppendCharToRedobuff(regname);
8373
8374 do_put(regname, BACKWARD, 1L,
8375 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8376 }
8377 else if (insert_reg(regname, literally) == FAIL)
8378 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008379 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 need_redraw = TRUE; /* remove the '"' */
8381 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008382 else if (stop_insert_mode)
8383 /* When the '=' register was used and a function was invoked that
8384 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8385 * insert anything, need to remove the '"' */
8386 need_redraw = TRUE;
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#ifdef FEAT_EVAL
8389 }
8390 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008391 if (u_sync_once == 1)
8392 ins_need_undo = TRUE;
8393 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#endif
8395#ifdef FEAT_CMDL_INFO
8396 clear_showcmd();
8397#endif
8398
8399 /* If the inserted register is empty, we need to remove the '"' */
8400 if (need_redraw || stuff_empty())
8401 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008402
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008403 /* Disallow starting Visual mode here, would get a weird mode. */
8404 if (!vis_active && VIsual_active)
8405 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406}
8407
8408/*
8409 * CTRL-G commands in Insert mode.
8410 */
8411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008412ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
8414 int c;
8415
8416#ifdef FEAT_INS_EXPAND
8417 /* Right after CTRL-X the cursor will be after the ruler. */
8418 setcursor();
8419#endif
8420
8421 /*
8422 * Don't map the second key. This also prevents the mode message to be
8423 * deleted when ESC is hit.
8424 */
8425 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008426 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 --no_mapping;
8428 switch (c)
8429 {
8430 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8431 case K_UP:
8432 case Ctrl_K:
8433 case 'k': ins_up(TRUE);
8434 break;
8435
8436 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8437 case K_DOWN:
8438 case Ctrl_J:
8439 case 'j': ins_down(TRUE);
8440 break;
8441
8442 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008443 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008445
8446 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008447 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008448 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008449 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 break;
8451
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008452 /* CTRL-G U: do not break undo with the next char */
8453 case 'U':
8454 /* Allow one left/right cursor movement with the next char,
8455 * without breaking undo. */
8456 dont_sync_undo = MAYBE;
8457 break;
8458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008460 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
8462}
8463
8464/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008465 * CTRL-^ in Insert mode.
8466 */
8467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008468ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008469{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008470 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008471 {
8472 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8473 if (State & LANGMAP)
8474 {
8475 curbuf->b_p_iminsert = B_IMODE_NONE;
8476 State &= ~LANGMAP;
8477 }
8478 else
8479 {
8480 curbuf->b_p_iminsert = B_IMODE_LMAP;
8481 State |= LANGMAP;
8482#ifdef USE_IM_CONTROL
8483 im_set_active(FALSE);
8484#endif
8485 }
8486 }
8487#ifdef USE_IM_CONTROL
8488 else
8489 {
8490 /* There are no ":lmap" mappings, toggle IM */
8491 if (im_get_status())
8492 {
8493 curbuf->b_p_iminsert = B_IMODE_NONE;
8494 im_set_active(FALSE);
8495 }
8496 else
8497 {
8498 curbuf->b_p_iminsert = B_IMODE_IM;
8499 State &= ~LANGMAP;
8500 im_set_active(TRUE);
8501 }
8502 }
8503#endif
8504 set_iminsert_global();
8505 showmode();
8506#ifdef FEAT_GUI
8507 /* may show different cursor shape or color */
8508 if (gui.in_use)
8509 gui_update_cursor(TRUE, FALSE);
8510#endif
8511#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8512 /* Show/unshow value of 'keymap' in status lines. */
8513 status_redraw_curbuf();
8514#endif
8515}
8516
8517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 * Handle ESC in insert mode.
8519 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8520 * insert.
8521 */
8522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008523ins_esc(
8524 long *count,
8525 int cmdchar,
8526 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527{
8528 int temp;
8529 static int disabled_redraw = FALSE;
8530
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008531#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008532 check_spell_redraw();
8533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534#if defined(FEAT_HANGULIN)
8535# if defined(ESC_CHG_TO_ENG_MODE)
8536 hangul_input_state_set(0);
8537# endif
8538 if (composing_hangul)
8539 {
8540 push_raw_key(composing_hangul_buffer, 2);
8541 composing_hangul = 0;
8542 }
8543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544
8545 temp = curwin->w_cursor.col;
8546 if (disabled_redraw)
8547 {
8548 --RedrawingDisabled;
8549 disabled_redraw = FALSE;
8550 }
8551 if (!arrow_used)
8552 {
8553 /*
8554 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008555 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8556 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 */
8558 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008559 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560
8561 /*
8562 * Repeating insert may take a long time. Check for
8563 * interrupt now and then.
8564 */
8565 if (*count > 0)
8566 {
8567 line_breakcheck();
8568 if (got_int)
8569 *count = 0;
8570 }
8571
8572 if (--*count > 0) /* repeat what was typed */
8573 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008574 /* Vi repeats the insert without replacing characters. */
8575 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8576 State &= ~REPLACE_FLAG;
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 (void)start_redo_ins();
8579 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008580 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 ++RedrawingDisabled;
8582 disabled_redraw = TRUE;
8583 return FALSE; /* repeat the insert */
8584 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008585 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 undisplay_dollar();
8587 }
8588
8589 /* When an autoindent was removed, curswant stays after the
8590 * indent */
8591 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8592 curwin->w_set_curswant = TRUE;
8593
8594 /* Remember the last Insert position in the '^ mark. */
8595 if (!cmdmod.keepjumps)
8596 curbuf->b_last_insert = curwin->w_cursor;
8597
8598 /*
8599 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008600 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008602 if (!nomove
8603 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604#ifdef FEAT_VIRTUALEDIT
8605 || curwin->w_cursor.coladd > 0
8606#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008607 )
8608 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008609 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#ifdef FEAT_RIGHTLEFT
8611 && !revins_on
8612#endif
8613 )
8614 {
8615#ifdef FEAT_VIRTUALEDIT
8616 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8617 {
8618 oneleft();
8619 if (restart_edit != NUL)
8620 ++curwin->w_cursor.coladd;
8621 }
8622 else
8623#endif
8624 {
8625 --curwin->w_cursor.col;
8626#ifdef FEAT_MBYTE
8627 /* Correct cursor for multi-byte character. */
8628 if (has_mbyte)
8629 mb_adjust_cursor();
8630#endif
8631 }
8632 }
8633
8634#ifdef USE_IM_CONTROL
8635 /* Disable IM to allow typing English directly for Normal mode commands.
8636 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8637 * well). */
8638 if (!(State & LANGMAP))
8639 im_save_status(&curbuf->b_p_iminsert);
8640 im_set_active(FALSE);
8641#endif
8642
8643 State = NORMAL;
8644 /* need to position cursor again (e.g. when on a TAB ) */
8645 changed_cline_bef_curs();
8646
8647#ifdef FEAT_MOUSE
8648 setmouse();
8649#endif
8650#ifdef CURSOR_SHAPE
8651 ui_cursor_shape(); /* may show different cursor shape */
8652#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008653 if (!p_ek)
8654 /* Re-enable bracketed paste mode. */
8655 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
8657 /*
8658 * When recording or for CTRL-O, need to display the new mode.
8659 * Otherwise remove the mode message.
8660 */
8661 if (Recording || restart_edit != NUL)
8662 showmode();
8663 else if (p_smd)
8664 MSG("");
8665
8666 return TRUE; /* exit Insert mode */
8667}
8668
8669#ifdef FEAT_RIGHTLEFT
8670/*
8671 * Toggle language: hkmap and revins_on.
8672 * Move to end of reverse inserted text.
8673 */
8674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008675ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 if (revins_on && revins_chars && revins_scol >= 0)
8678 {
8679 while (gchar_cursor() != NUL && revins_chars--)
8680 ++curwin->w_cursor.col;
8681 }
8682 p_ri = !p_ri;
8683 revins_on = (State == INSERT && p_ri);
8684 if (revins_on)
8685 {
8686 revins_scol = curwin->w_cursor.col;
8687 revins_legal++;
8688 revins_chars = 0;
8689 undisplay_dollar();
8690 }
8691 else
8692 revins_scol = -1;
8693#ifdef FEAT_FKMAP
8694 if (p_altkeymap)
8695 {
8696 /*
8697 * to be consistent also for redo command, using '.'
8698 * set arrow_used to true and stop it - causing to redo
8699 * characters entered in one mode (normal/reverse insert).
8700 */
8701 arrow_used = TRUE;
8702 (void)stop_arrow();
8703 p_fkmap = curwin->w_p_rl ^ p_ri;
8704 if (p_fkmap && p_ri)
8705 State = INSERT;
8706 }
8707 else
8708#endif
8709 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8710 showmode();
8711}
8712#endif
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714/*
8715 * If 'keymodel' contains "startsel", may start selection.
8716 * Returns TRUE when a CTRL-O and other keys stuffed.
8717 */
8718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008719ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 if (km_startsel)
8722 switch (c)
8723 {
8724 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 case K_PAGEUP:
8727 case K_KPAGEUP:
8728 case K_PAGEDOWN:
8729 case K_KPAGEDOWN:
8730# ifdef MACOS
8731 case K_LEFT:
8732 case K_RIGHT:
8733 case K_UP:
8734 case K_DOWN:
8735 case K_END:
8736 case K_HOME:
8737# endif
8738 if (!(mod_mask & MOD_MASK_SHIFT))
8739 break;
8740 /* FALLTHROUGH */
8741 case K_S_LEFT:
8742 case K_S_RIGHT:
8743 case K_S_UP:
8744 case K_S_DOWN:
8745 case K_S_END:
8746 case K_S_HOME:
8747 /* Start selection right away, the cursor can move with
8748 * CTRL-O when beyond the end of the line. */
8749 start_selection();
8750
8751 /* Execute the key in (insert) Select mode. */
8752 stuffcharReadbuff(Ctrl_O);
8753 if (mod_mask)
8754 {
8755 char_u buf[4];
8756
8757 buf[0] = K_SPECIAL;
8758 buf[1] = KS_MODIFIER;
8759 buf[2] = mod_mask;
8760 buf[3] = NUL;
8761 stuffReadbuff(buf);
8762 }
8763 stuffcharReadbuff(c);
8764 return TRUE;
8765 }
8766 return FALSE;
8767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008770 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008771 */
8772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008774{
8775#ifdef FEAT_FKMAP
8776 if (p_fkmap && p_ri)
8777 {
8778 beep_flush();
8779 EMSG(farsi_text_3); /* encoded in Farsi */
8780 return;
8781 }
8782#endif
8783
8784#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008785# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008786 set_vim_var_string(VV_INSERTMODE,
8787 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008788# ifdef FEAT_VREPLACE
8789 replaceState == VREPLACE ? "v" :
8790# endif
8791 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008792# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008793 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8794#endif
8795 if (State & REPLACE_FLAG)
8796 State = INSERT | (State & LANGMAP);
8797 else
8798 State = replaceState | (State & LANGMAP);
8799 AppendCharToRedobuff(K_INS);
8800 showmode();
8801#ifdef CURSOR_SHAPE
8802 ui_cursor_shape(); /* may show different cursor shape */
8803#endif
8804}
8805
8806/*
8807 * Pressed CTRL-O in Insert mode.
8808 */
8809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008810ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008811{
8812#ifdef FEAT_VREPLACE
8813 if (State & VREPLACE_FLAG)
8814 restart_edit = 'V';
8815 else
8816#endif
8817 if (State & REPLACE_FLAG)
8818 restart_edit = 'R';
8819 else
8820 restart_edit = 'I';
8821#ifdef FEAT_VIRTUALEDIT
8822 if (virtual_active())
8823 ins_at_eol = FALSE; /* cursor always keeps its column */
8824 else
8825#endif
8826 ins_at_eol = (gchar_cursor() == NUL);
8827}
8828
8829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 * If the cursor is on an indent, ^T/^D insert/delete one
8831 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008832 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 * with vi. But vi only supports ^T and ^D after an
8834 * autoindent, we support it everywhere.
8835 */
8836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008837ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 if (stop_arrow() == FAIL)
8840 return;
8841 AppendCharToRedobuff(c);
8842
8843 /*
8844 * 0^D and ^^D: remove all indent.
8845 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008846 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8847 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
8849 --curwin->w_cursor.col;
8850 (void)del_char(FALSE); /* delete the '^' or '0' */
8851 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8852 if (State & REPLACE_FLAG)
8853 replace_pop_ins();
8854 if (lastc == '^')
8855 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008856 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 }
8858 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008859 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
8861 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8862 did_ai = FALSE;
8863#ifdef FEAT_SMARTINDENT
8864 did_si = FALSE;
8865 can_si = FALSE;
8866 can_si_back = FALSE;
8867#endif
8868#ifdef FEAT_CINDENT
8869 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8870#endif
8871}
8872
8873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008874ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
8876 int temp;
8877
8878 if (stop_arrow() == FAIL)
8879 return;
8880 if (gchar_cursor() == NUL) /* delete newline */
8881 {
8882 temp = curwin->w_cursor.col;
8883 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008884 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008885 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 else
8887 curwin->w_cursor.col = temp;
8888 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008889 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8890 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 did_ai = FALSE;
8892#ifdef FEAT_SMARTINDENT
8893 did_si = FALSE;
8894 can_si = FALSE;
8895 can_si_back = FALSE;
8896#endif
8897 AppendCharToRedobuff(K_DEL);
8898}
8899
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008900static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008901
8902/*
8903 * Delete one character for ins_bs().
8904 */
8905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008906ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008907{
8908 dec_cursor();
8909 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8910 if (State & REPLACE_FLAG)
8911 {
8912 /* Don't delete characters before the insert point when in
8913 * Replace mode */
8914 if (curwin->w_cursor.lnum != Insstart.lnum
8915 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008916 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008917 }
8918 else
8919 (void)del_char(FALSE);
8920}
8921
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922/*
8923 * Handle Backspace, delete-word and delete-line in Insert mode.
8924 * Return TRUE when backspace was actually used.
8925 */
8926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008927ins_bs(
8928 int c,
8929 int mode,
8930 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 linenr_T lnum;
8933 int cc;
8934 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008935 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 colnr_T mincol;
8937 int did_backspace = FALSE;
8938 int in_indent;
8939 int oldState;
8940#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008941 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#endif
8943
8944 /*
8945 * can't delete anything in an empty file
8946 * can't backup past first character in buffer
8947 * can't backup past starting point unless 'backspace' > 1
8948 * can backup to a previous line if 'backspace' == 0
8949 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008950 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 || (
8952#ifdef FEAT_RIGHTLEFT
8953 !revins_on &&
8954#endif
8955 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8956 || (!can_bs(BS_START)
8957 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008958 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8959 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8961 && curwin->w_cursor.col <= ai_col)
8962 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8963 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008964 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 return FALSE;
8966 }
8967
8968 if (stop_arrow() == FAIL)
8969 return FALSE;
8970 in_indent = inindent(0);
8971#ifdef FEAT_CINDENT
8972 if (in_indent)
8973 can_cindent = FALSE;
8974#endif
8975#ifdef FEAT_COMMENTS
8976 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8977#endif
8978#ifdef FEAT_RIGHTLEFT
8979 if (revins_on) /* put cursor after last inserted char */
8980 inc_cursor();
8981#endif
8982
8983#ifdef FEAT_VIRTUALEDIT
8984 /* Virtualedit:
8985 * BACKSPACE_CHAR eats a virtual space
8986 * BACKSPACE_WORD eats all coladd
8987 * BACKSPACE_LINE eats all coladd and keeps going
8988 */
8989 if (curwin->w_cursor.coladd > 0)
8990 {
8991 if (mode == BACKSPACE_CHAR)
8992 {
8993 --curwin->w_cursor.coladd;
8994 return TRUE;
8995 }
8996 if (mode == BACKSPACE_WORD)
8997 {
8998 curwin->w_cursor.coladd = 0;
8999 return TRUE;
9000 }
9001 curwin->w_cursor.coladd = 0;
9002 }
9003#endif
9004
9005 /*
9006 * delete newline!
9007 */
9008 if (curwin->w_cursor.col == 0)
9009 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009010 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009011 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012#ifdef FEAT_RIGHTLEFT
9013 || revins_on
9014#endif
9015 )
9016 {
9017 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9018 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9019 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009020 --Insstart.lnum;
9021 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
9023 /*
9024 * In replace mode:
9025 * cc < 0: NL was inserted, delete it
9026 * cc >= 0: NL was replaced, put original characters back
9027 */
9028 cc = -1;
9029 if (State & REPLACE_FLAG)
9030 cc = replace_pop(); /* returns -1 if NL was inserted */
9031 /*
9032 * In replace mode, in the line we started replacing, we only move the
9033 * cursor.
9034 */
9035 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9036 {
9037 dec_cursor();
9038 }
9039 else
9040 {
9041#ifdef FEAT_VREPLACE
9042 if (!(State & VREPLACE_FLAG)
9043 || curwin->w_cursor.lnum > orig_line_count)
9044#endif
9045 {
9046 temp = gchar_cursor(); /* remember current char */
9047 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009048
9049 /* When "aw" is in 'formatoptions' we must delete the space at
9050 * the end of the line, otherwise the line will be broken
9051 * again when auto-formatting. */
9052 if (has_format_option(FO_AUTO)
9053 && has_format_option(FO_WHITE_PAR))
9054 {
9055 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9056 TRUE);
9057 int len;
9058
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009059 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009060 if (len > 0 && ptr[len - 1] == ' ')
9061 ptr[len - 1] = NUL;
9062 }
9063
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009064 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (temp == NUL && gchar_cursor() != NUL)
9066 inc_cursor();
9067 }
9068#ifdef FEAT_VREPLACE
9069 else
9070 dec_cursor();
9071#endif
9072
9073 /*
9074 * In REPLACE mode we have to put back the text that was replaced
9075 * by the NL. On the replace stack is first a NUL-terminated
9076 * sequence of characters that were deleted and then the
9077 * characters that NL replaced.
9078 */
9079 if (State & REPLACE_FLAG)
9080 {
9081 /*
9082 * Do the next ins_char() in NORMAL state, to
9083 * prevent ins_char() from replacing characters and
9084 * avoiding showmatch().
9085 */
9086 oldState = State;
9087 State = NORMAL;
9088 /*
9089 * restore characters (blanks) deleted after cursor
9090 */
9091 while (cc > 0)
9092 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009093 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094#ifdef FEAT_MBYTE
9095 mb_replace_pop_ins(cc);
9096#else
9097 ins_char(cc);
9098#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009099 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 cc = replace_pop();
9101 }
9102 /* restore the characters that NL replaced */
9103 replace_pop_ins();
9104 State = oldState;
9105 }
9106 }
9107 did_ai = FALSE;
9108 }
9109 else
9110 {
9111 /*
9112 * Delete character(s) before the cursor.
9113 */
9114#ifdef FEAT_RIGHTLEFT
9115 if (revins_on) /* put cursor on last inserted char */
9116 dec_cursor();
9117#endif
9118 mincol = 0;
9119 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009120 if (mode == BACKSPACE_LINE
9121 && (curbuf->b_p_ai
9122#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009123 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009124#endif
9125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126#ifdef FEAT_RIGHTLEFT
9127 && !revins_on
9128#endif
9129 )
9130 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009131 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009133 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009135 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 }
9137
9138 /*
9139 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9140 */
9141 if ( mode == BACKSPACE_CHAR
9142 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009143 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009144 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 && (*(ml_get_cursor() - 1) == TAB
9146 || (*(ml_get_cursor() - 1) == ' '
9147 && (!*inserted_space_p
9148 || arrow_used))))))
9149 {
9150 int ts;
9151 colnr_T vcol;
9152 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009153 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154
9155 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009156 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009157 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009159 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 /* Compute the virtual column where we want to be. Since
9161 * 'showbreak' may get in the way, need to get the last column of
9162 * the previous character. */
9163 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009164 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 dec_cursor();
9166 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9167 inc_cursor();
9168 want_vcol = (want_vcol / ts) * ts;
9169
9170 /* delete characters until we are at or before want_vcol */
9171 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009172 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009173 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
9175 /* insert extra spaces until we are at want_vcol */
9176 while (vcol < want_vcol)
9177 {
9178 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009179 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9180 && curwin->w_cursor.col < Insstart_orig.col)
9181 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183#ifdef FEAT_VREPLACE
9184 if (State & VREPLACE_FLAG)
9185 ins_char(' ');
9186 else
9187#endif
9188 {
9189 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009190 if ((State & REPLACE_FLAG))
9191 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
9193 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9194 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009195
9196 /* If we are now back where we started delete one character. Can
9197 * happen when using 'sts' and 'linebreak'. */
9198 if (vcol >= start_vcol)
9199 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 }
9201
9202 /*
9203 * Delete upto starting point, start of line or previous word.
9204 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009207#ifdef FEAT_MBYTE
9208 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009210 if (has_mbyte)
9211 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009213 do
9214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009216 if (!revins_on) /* put cursor on char to be deleted */
9217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009219
9220 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009222 /* look multi-byte character class */
9223 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009225 prev_cclass = cclass;
9226 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009229
9230 /* start of word? */
9231 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9232 {
9233 mode = BACKSPACE_WORD_NOT_SPACE;
9234 temp = vim_iswordc(cc);
9235 }
9236 /* end of word? */
9237 else if (mode == BACKSPACE_WORD_NOT_SPACE
9238 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9239#ifdef FEAT_MBYTE
9240 || prev_cclass != cclass
9241#endif
9242 ))
9243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009245 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009247 inc_cursor();
9248#ifdef FEAT_RIGHTLEFT
9249 else if (State & REPLACE_FLAG)
9250 dec_cursor();
9251#endif
9252 break;
9253 }
9254 if (State & REPLACE_FLAG)
9255 replace_do_bs(-1);
9256 else
9257 {
9258#ifdef FEAT_MBYTE
9259 if (enc_utf8 && p_deco)
9260 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9261#endif
9262 (void)del_char(FALSE);
9263#ifdef FEAT_MBYTE
9264 /*
9265 * If there are combining characters and 'delcombine' is set
9266 * move the cursor back. Don't back up before the base
9267 * character.
9268 */
9269 if (enc_utf8 && p_deco && cpc[0] != NUL)
9270 inc_cursor();
9271#endif
9272#ifdef FEAT_RIGHTLEFT
9273 if (revins_chars)
9274 {
9275 revins_chars--;
9276 revins_legal++;
9277 }
9278 if (revins_on && gchar_cursor() == NUL)
9279 break;
9280#endif
9281 }
9282 /* Just a single backspace?: */
9283 if (mode == BACKSPACE_CHAR)
9284 break;
9285 } while (
9286#ifdef FEAT_RIGHTLEFT
9287 revins_on ||
9288#endif
9289 (curwin->w_cursor.col > mincol
9290 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9291 || curwin->w_cursor.col != Insstart_orig.col)));
9292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 did_backspace = TRUE;
9294 }
9295#ifdef FEAT_SMARTINDENT
9296 did_si = FALSE;
9297 can_si = FALSE;
9298 can_si_back = FALSE;
9299#endif
9300 if (curwin->w_cursor.col <= 1)
9301 did_ai = FALSE;
9302 /*
9303 * It's a little strange to put backspaces into the redo
9304 * buffer, but it makes auto-indent a lot easier to deal
9305 * with.
9306 */
9307 AppendCharToRedobuff(c);
9308
9309 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009310 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9311 && curwin->w_cursor.col < Insstart_orig.col)
9312 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
9314 /* vi behaviour: the cursor moves backward but the character that
9315 * was there remains visible
9316 * Vim behaviour: the cursor moves backward and the character that
9317 * was there is erased from the screen.
9318 * We can emulate the vi behaviour by pretending there is a dollar
9319 * displayed even when there isn't.
9320 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009321 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 dollar_vcol = curwin->w_virtcol;
9323
Bram Moolenaarce3be472008-01-14 19:12:28 +00009324#ifdef FEAT_FOLDING
9325 /* When deleting a char the cursor line must never be in a closed fold.
9326 * E.g., when 'foldmethod' is indent and deleting the first non-white
9327 * char before a Tab. */
9328 if (did_backspace)
9329 foldOpenCursor();
9330#endif
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 return did_backspace;
9333}
9334
9335#ifdef FEAT_MOUSE
9336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009337ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338{
9339 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009340 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342# ifdef FEAT_GUI
9343 /* When GUI is active, also move/paste when 'mouse' is empty */
9344 if (!gui.in_use)
9345# endif
9346 if (!mouse_has(MOUSE_INSERT))
9347 return;
9348
9349 undisplay_dollar();
9350 tpos = curwin->w_cursor;
9351 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9352 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009353#ifdef FEAT_WINDOWS
9354 win_T *new_curwin = curwin;
9355
9356 if (curwin != old_curwin && win_valid(old_curwin))
9357 {
9358 /* Mouse took us to another window. We need to go back to the
9359 * previous one to stop insert there properly. */
9360 curwin = old_curwin;
9361 curbuf = curwin->w_buffer;
9362 }
9363#endif
9364 start_arrow(curwin == old_curwin ? &tpos : NULL);
9365#ifdef FEAT_WINDOWS
9366 if (curwin != new_curwin && win_valid(new_curwin))
9367 {
9368 curwin = new_curwin;
9369 curbuf = curwin->w_buffer;
9370 }
9371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372# ifdef FEAT_CINDENT
9373 can_cindent = TRUE;
9374# endif
9375 }
9376
9377#ifdef FEAT_WINDOWS
9378 /* redraw status lines (in case another window became active) */
9379 redraw_statuslines();
9380#endif
9381}
9382
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009387# if defined(FEAT_WINDOWS)
9388 win_T *old_curwin = curwin;
9389# endif
9390# ifdef FEAT_INS_EXPAND
9391 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392# endif
9393
9394 tpos = curwin->w_cursor;
9395
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009396# ifdef FEAT_WINDOWS
9397 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 {
9399 int row, col;
9400
9401 row = mouse_row;
9402 col = mouse_col;
9403
9404 /* find the window at the pointer coordinates */
9405 curwin = mouse_find_win(&row, &col);
9406 curbuf = curwin->w_buffer;
9407 }
9408 if (curwin == old_curwin)
9409# endif
9410 undisplay_dollar();
9411
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009412# ifdef FEAT_INS_EXPAND
9413 /* Don't scroll the window in which completion is being done. */
9414 if (!pum_visible()
9415# if defined(FEAT_WINDOWS)
9416 || curwin != old_curwin
9417# endif
9418 )
9419# endif
9420 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009421 if (dir == MSCR_DOWN || dir == MSCR_UP)
9422 {
9423 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9424 scroll_redraw(dir,
9425 (long)(curwin->w_botline - curwin->w_topline));
9426 else
9427 scroll_redraw(dir, 3L);
9428 }
9429#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009430 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009431 {
9432 int val, step = 6;
9433
9434 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9435 step = W_WIDTH(curwin);
9436 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9437 if (val < 0)
9438 val = 0;
9439 gui_do_horiz_scroll(val, TRUE);
9440 }
9441#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009442# ifdef FEAT_INS_EXPAND
9443 did_scroll = TRUE;
9444# endif
9445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009447# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 curwin->w_redr_status = TRUE;
9449
9450 curwin = old_curwin;
9451 curbuf = curwin->w_buffer;
9452# endif
9453
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009454# ifdef FEAT_INS_EXPAND
9455 /* The popup menu may overlay the window, need to redraw it.
9456 * TODO: Would be more efficient to only redraw the windows that are
9457 * overlapped by the popup menu. */
9458 if (pum_visible() && did_scroll)
9459 {
9460 redraw_all_later(NOT_VALID);
9461 ins_compl_show_pum();
9462 }
9463# endif
9464
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009465 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 start_arrow(&tpos);
9468# ifdef FEAT_CINDENT
9469 can_cindent = TRUE;
9470# endif
9471 }
9472}
9473#endif
9474
Bram Moolenaarec2da362017-01-21 20:04:22 +01009475/*
9476 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9477 * P_PE literally.
9478 * When "drop" is TRUE then consume the text and drop it.
9479 */
9480 int
9481bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9482{
9483 int c;
9484 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9485 int idx = 0;
9486 char_u *end = find_termcode((char_u *)"PE");
9487 int ret_char = -1;
9488 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009489 int save_paste = p_paste;
9490 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009491
9492 /* If the end code is too long we can't detect it, read everything. */
9493 if (STRLEN(end) >= NUMBUFLEN)
9494 end = NULL;
9495 ++no_mapping;
9496 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009497 p_paste = TRUE;
9498 curbuf->b_p_ai = FALSE;
9499
Bram Moolenaarec2da362017-01-21 20:04:22 +01009500 for (;;)
9501 {
9502 /* When the end is not defined read everything. */
9503 if (end == NULL && vpeekc() == NUL)
9504 break;
9505 c = plain_vgetc();
9506#ifdef FEAT_MBYTE
9507 if (has_mbyte)
9508 idx += (*mb_char2bytes)(c, buf + idx);
9509 else
9510#endif
9511 buf[idx++] = c;
9512 buf[idx] = NUL;
9513 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9514 {
9515 if (end[idx] == NUL)
9516 break; /* Found the end of paste code. */
9517 continue;
9518 }
9519 if (!drop)
9520 {
9521 switch (mode)
9522 {
9523 case PASTE_CMDLINE:
9524 put_on_cmdline(buf, idx, TRUE);
9525 break;
9526
9527 case PASTE_EX:
9528 if (gap != NULL && ga_grow(gap, idx) == OK)
9529 {
9530 mch_memmove((char *)gap->ga_data + gap->ga_len,
9531 buf, (size_t)idx);
9532 gap->ga_len += idx;
9533 }
9534 break;
9535
9536 case PASTE_INSERT:
9537 if (stop_arrow() == OK)
9538 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009539 c = buf[0];
9540 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9541 ins_eol(c);
9542 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009543 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009544 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009545 AppendToRedobuffLit(buf, idx);
9546 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009547 }
9548 break;
9549
9550 case PASTE_ONE_CHAR:
9551 if (ret_char == -1)
9552 {
9553#ifdef FEAT_MBYTE
9554 if (has_mbyte)
9555 ret_char = (*mb_ptr2char)(buf);
9556 else
9557#endif
9558 ret_char = buf[0];
9559 }
9560 break;
9561 }
9562 }
9563 idx = 0;
9564 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009565
Bram Moolenaarec2da362017-01-21 20:04:22 +01009566 --no_mapping;
9567 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009568 p_paste = save_paste;
9569 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009570
9571 return ret_char;
9572}
9573
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009574#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009576ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009577{
9578 /* We will be leaving the current window, unless closing another tab. */
9579 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9580 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9581 {
9582 undisplay_dollar();
9583 start_arrow(&curwin->w_cursor);
9584# ifdef FEAT_CINDENT
9585 can_cindent = TRUE;
9586# endif
9587 }
9588
9589 if (c == K_TABLINE)
9590 goto_tabpage(current_tab);
9591 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009592 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009593 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009594 redraw_statuslines(); /* will redraw the tabline when needed */
9595 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009596}
9597#endif
9598
9599#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009601ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602{
9603 pos_T tpos;
9604
9605 undisplay_dollar();
9606 tpos = curwin->w_cursor;
9607 if (gui_do_scroll())
9608 {
9609 start_arrow(&tpos);
9610# ifdef FEAT_CINDENT
9611 can_cindent = TRUE;
9612# endif
9613 }
9614}
9615
9616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619 pos_T tpos;
9620
9621 undisplay_dollar();
9622 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009623 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
9625 start_arrow(&tpos);
9626# ifdef FEAT_CINDENT
9627 can_cindent = TRUE;
9628# endif
9629 }
9630}
9631#endif
9632
9633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634ins_left(
9635 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 pos_T tpos;
9638
9639#ifdef FEAT_FOLDING
9640 if ((fdo_flags & FDO_HOR) && KeyTyped)
9641 foldOpenCursor();
9642#endif
9643 undisplay_dollar();
9644 tpos = curwin->w_cursor;
9645 if (oneleft() == OK)
9646 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009647#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9648 /* Only call start_arrow() when not busy with preediting, it will
9649 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9650 if (!im_is_preediting())
9651#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009652 {
9653 start_arrow_with_change(&tpos, end_change);
9654 if (!end_change)
9655 AppendCharToRedobuff(K_LEFT);
9656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657#ifdef FEAT_RIGHTLEFT
9658 /* If exit reversed string, position is fixed */
9659 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9660 revins_legal++;
9661 revins_chars++;
9662#endif
9663 }
9664
9665 /*
9666 * if 'whichwrap' set for cursor in insert mode may go to
9667 * previous line
9668 */
9669 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9670 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009671 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 start_arrow(&tpos);
9673 --(curwin->w_cursor.lnum);
9674 coladvance((colnr_T)MAXCOL);
9675 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9676 }
9677 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009678 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009679 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681
9682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009683ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 pos_T tpos;
9686
9687#ifdef FEAT_FOLDING
9688 if ((fdo_flags & FDO_HOR) && KeyTyped)
9689 foldOpenCursor();
9690#endif
9691 undisplay_dollar();
9692 tpos = curwin->w_cursor;
9693 if (c == K_C_HOME)
9694 curwin->w_cursor.lnum = 1;
9695 curwin->w_cursor.col = 0;
9696#ifdef FEAT_VIRTUALEDIT
9697 curwin->w_cursor.coladd = 0;
9698#endif
9699 curwin->w_curswant = 0;
9700 start_arrow(&tpos);
9701}
9702
9703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009704ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 pos_T tpos;
9707
9708#ifdef FEAT_FOLDING
9709 if ((fdo_flags & FDO_HOR) && KeyTyped)
9710 foldOpenCursor();
9711#endif
9712 undisplay_dollar();
9713 tpos = curwin->w_cursor;
9714 if (c == K_C_END)
9715 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9716 coladvance((colnr_T)MAXCOL);
9717 curwin->w_curswant = MAXCOL;
9718
9719 start_arrow(&tpos);
9720}
9721
9722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009723ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725#ifdef FEAT_FOLDING
9726 if ((fdo_flags & FDO_HOR) && KeyTyped)
9727 foldOpenCursor();
9728#endif
9729 undisplay_dollar();
9730 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9731 {
9732 start_arrow(&curwin->w_cursor);
9733 (void)bck_word(1L, FALSE, FALSE);
9734 curwin->w_set_curswant = TRUE;
9735 }
9736 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009737 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738}
9739
9740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009741ins_right(
9742 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
9744#ifdef FEAT_FOLDING
9745 if ((fdo_flags & FDO_HOR) && KeyTyped)
9746 foldOpenCursor();
9747#endif
9748 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009749 if (gchar_cursor() != NUL
9750#ifdef FEAT_VIRTUALEDIT
9751 || virtual_active()
9752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 )
9754 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009755 start_arrow_with_change(&curwin->w_cursor, end_change);
9756 if (!end_change)
9757 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 curwin->w_set_curswant = TRUE;
9759#ifdef FEAT_VIRTUALEDIT
9760 if (virtual_active())
9761 oneright();
9762 else
9763#endif
9764 {
9765#ifdef FEAT_MBYTE
9766 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009767 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 else
9769#endif
9770 ++curwin->w_cursor.col;
9771 }
9772
9773#ifdef FEAT_RIGHTLEFT
9774 revins_legal++;
9775 if (revins_chars)
9776 revins_chars--;
9777#endif
9778 }
9779 /* if 'whichwrap' set for cursor in insert mode, may move the
9780 * cursor to the next line */
9781 else if (vim_strchr(p_ww, ']') != NULL
9782 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9783 {
9784 start_arrow(&curwin->w_cursor);
9785 curwin->w_set_curswant = TRUE;
9786 ++curwin->w_cursor.lnum;
9787 curwin->w_cursor.col = 0;
9788 }
9789 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009790 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009791 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009795ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
9797#ifdef FEAT_FOLDING
9798 if ((fdo_flags & FDO_HOR) && KeyTyped)
9799 foldOpenCursor();
9800#endif
9801 undisplay_dollar();
9802 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9803 || gchar_cursor() != NUL)
9804 {
9805 start_arrow(&curwin->w_cursor);
9806 (void)fwd_word(1L, FALSE, 0);
9807 curwin->w_set_curswant = TRUE;
9808 }
9809 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009810 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009814ins_up(
9815 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 pos_T tpos;
9818 linenr_T old_topline = curwin->w_topline;
9819#ifdef FEAT_DIFF
9820 int old_topfill = curwin->w_topfill;
9821#endif
9822
9823 undisplay_dollar();
9824 tpos = curwin->w_cursor;
9825 if (cursor_up(1L, TRUE) == OK)
9826 {
9827 if (startcol)
9828 coladvance(getvcol_nolist(&Insstart));
9829 if (old_topline != curwin->w_topline
9830#ifdef FEAT_DIFF
9831 || old_topfill != curwin->w_topfill
9832#endif
9833 )
9834 redraw_later(VALID);
9835 start_arrow(&tpos);
9836#ifdef FEAT_CINDENT
9837 can_cindent = TRUE;
9838#endif
9839 }
9840 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009841 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842}
9843
9844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 pos_T tpos;
9848
9849 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009850
9851#ifdef FEAT_WINDOWS
9852 if (mod_mask & MOD_MASK_CTRL)
9853 {
9854 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009855 if (first_tabpage->tp_next != NULL)
9856 {
9857 start_arrow(&curwin->w_cursor);
9858 goto_tabpage(-1);
9859 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009860 return;
9861 }
9862#endif
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 tpos = curwin->w_cursor;
9865 if (onepage(BACKWARD, 1L) == OK)
9866 {
9867 start_arrow(&tpos);
9868#ifdef FEAT_CINDENT
9869 can_cindent = TRUE;
9870#endif
9871 }
9872 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009873 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874}
9875
9876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877ins_down(
9878 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 pos_T tpos;
9881 linenr_T old_topline = curwin->w_topline;
9882#ifdef FEAT_DIFF
9883 int old_topfill = curwin->w_topfill;
9884#endif
9885
9886 undisplay_dollar();
9887 tpos = curwin->w_cursor;
9888 if (cursor_down(1L, TRUE) == OK)
9889 {
9890 if (startcol)
9891 coladvance(getvcol_nolist(&Insstart));
9892 if (old_topline != curwin->w_topline
9893#ifdef FEAT_DIFF
9894 || old_topfill != curwin->w_topfill
9895#endif
9896 )
9897 redraw_later(VALID);
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_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
9910 pos_T tpos;
9911
9912 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009913
9914#ifdef FEAT_WINDOWS
9915 if (mod_mask & MOD_MASK_CTRL)
9916 {
9917 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009918 if (first_tabpage->tp_next != NULL)
9919 {
9920 start_arrow(&curwin->w_cursor);
9921 goto_tabpage(0);
9922 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009923 return;
9924 }
9925#endif
9926
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 tpos = curwin->w_cursor;
9928 if (onepage(FORWARD, 1L) == OK)
9929 {
9930 start_arrow(&tpos);
9931#ifdef FEAT_CINDENT
9932 can_cindent = TRUE;
9933#endif
9934 }
9935 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009936 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937}
9938
9939#ifdef FEAT_DND
9940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009941ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9944}
9945#endif
9946
9947/*
9948 * Handle TAB in Insert or Replace mode.
9949 * Return TRUE when the TAB needs to be inserted like a normal character.
9950 */
9951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954 int ind;
9955 int i;
9956 int temp;
9957
9958 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9959 Insstart_blank_vcol = get_nolist_virtcol();
9960 if (echeck_abbr(TAB + ABBR_OFF))
9961 return FALSE;
9962
9963 ind = inindent(0);
9964#ifdef FEAT_CINDENT
9965 if (ind)
9966 can_cindent = FALSE;
9967#endif
9968
9969 /*
9970 * When nothing special, insert TAB like a normal character
9971 */
9972 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009973 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009974 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 return TRUE;
9976
9977 if (stop_arrow() == FAIL)
9978 return TRUE;
9979
9980 did_ai = FALSE;
9981#ifdef FEAT_SMARTINDENT
9982 did_si = FALSE;
9983 can_si = FALSE;
9984 can_si_back = FALSE;
9985#endif
9986 AppendToRedobuff((char_u *)"\t");
9987
9988 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009989 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009990 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9991 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 else /* otherwise use 'tabstop' */
9993 temp = (int)curbuf->b_p_ts;
9994 temp -= get_nolist_virtcol() % temp;
9995
9996 /*
9997 * Insert the first space with ins_char(). It will delete one char in
9998 * replace mode. Insert the rest with ins_str(); it will not delete any
9999 * chars. For VREPLACE mode, we use ins_char() for all characters.
10000 */
10001 ins_char(' ');
10002 while (--temp > 0)
10003 {
10004#ifdef FEAT_VREPLACE
10005 if (State & VREPLACE_FLAG)
10006 ins_char(' ');
10007 else
10008#endif
10009 {
10010 ins_str((char_u *)" ");
10011 if (State & REPLACE_FLAG) /* no char replaced */
10012 replace_push(NUL);
10013 }
10014 }
10015
10016 /*
10017 * When 'expandtab' not set: Replace spaces by TABs where possible.
10018 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010019 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 {
10021 char_u *ptr;
10022#ifdef FEAT_VREPLACE
10023 char_u *saved_line = NULL; /* init for GCC */
10024 pos_T pos;
10025#endif
10026 pos_T fpos;
10027 pos_T *cursor;
10028 colnr_T want_vcol, vcol;
10029 int change_col = -1;
10030 int save_list = curwin->w_p_list;
10031
10032 /*
10033 * Get the current line. For VREPLACE mode, don't make real changes
10034 * yet, just work on a copy of the line.
10035 */
10036#ifdef FEAT_VREPLACE
10037 if (State & VREPLACE_FLAG)
10038 {
10039 pos = curwin->w_cursor;
10040 cursor = &pos;
10041 saved_line = vim_strsave(ml_get_curline());
10042 if (saved_line == NULL)
10043 return FALSE;
10044 ptr = saved_line + pos.col;
10045 }
10046 else
10047#endif
10048 {
10049 ptr = ml_get_cursor();
10050 cursor = &curwin->w_cursor;
10051 }
10052
10053 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10054 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10055 curwin->w_p_list = FALSE;
10056
10057 /* Find first white before the cursor */
10058 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010059 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
10061 --fpos.col;
10062 --ptr;
10063 }
10064
10065 /* In Replace mode, don't change characters before the insert point. */
10066 if ((State & REPLACE_FLAG)
10067 && fpos.lnum == Insstart.lnum
10068 && fpos.col < Insstart.col)
10069 {
10070 ptr += Insstart.col - fpos.col;
10071 fpos.col = Insstart.col;
10072 }
10073
10074 /* compute virtual column numbers of first white and cursor */
10075 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10076 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10077
Bram Moolenaar597a4222014-06-25 14:39:50 +020010078 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10079 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010080 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010082 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (vcol + i > want_vcol)
10084 break;
10085 if (*ptr != TAB)
10086 {
10087 *ptr = TAB;
10088 if (change_col < 0)
10089 {
10090 change_col = fpos.col; /* Column of first change */
10091 /* May have to adjust Insstart */
10092 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10093 Insstart.col = fpos.col;
10094 }
10095 }
10096 ++fpos.col;
10097 ++ptr;
10098 vcol += i;
10099 }
10100
10101 if (change_col >= 0)
10102 {
10103 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010104 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
10106 /* Skip over the spaces we need. */
10107 while (vcol < want_vcol && *ptr == ' ')
10108 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010109 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 ++ptr;
10111 ++repl_off;
10112 }
10113 if (vcol > want_vcol)
10114 {
10115 /* Must have a char with 'showbreak' just before it. */
10116 --ptr;
10117 --repl_off;
10118 }
10119 fpos.col += repl_off;
10120
10121 /* Delete following spaces. */
10122 i = cursor->col - fpos.col;
10123 if (i > 0)
10124 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010125 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 /* correct replace stack. */
10127 if ((State & REPLACE_FLAG)
10128#ifdef FEAT_VREPLACE
10129 && !(State & VREPLACE_FLAG)
10130#endif
10131 )
10132 for (temp = i; --temp >= 0; )
10133 replace_join(repl_off);
10134 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010135#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010136 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010137 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010138 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010139 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10140 (char_u *)"\t", 1);
10141 }
10142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 cursor->col -= i;
10144
10145#ifdef FEAT_VREPLACE
10146 /*
10147 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10148 * backspacing over the changed spacing and then inserting the new
10149 * spacing.
10150 */
10151 if (State & VREPLACE_FLAG)
10152 {
10153 /* Backspace from real cursor to change_col */
10154 backspace_until_column(change_col);
10155
10156 /* Insert each char in saved_line from changed_col to
10157 * ptr-cursor */
10158 ins_bytes_len(saved_line + change_col,
10159 cursor->col - change_col);
10160 }
10161#endif
10162 }
10163
10164#ifdef FEAT_VREPLACE
10165 if (State & VREPLACE_FLAG)
10166 vim_free(saved_line);
10167#endif
10168 curwin->w_p_list = save_list;
10169 }
10170
10171 return FALSE;
10172}
10173
10174/*
10175 * Handle CR or NL in insert mode.
10176 * Return TRUE when out of memory or can't undo.
10177 */
10178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010179ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 int i;
10182
10183 if (echeck_abbr(c + ABBR_OFF))
10184 return FALSE;
10185 if (stop_arrow() == FAIL)
10186 return TRUE;
10187 undisplay_dollar();
10188
10189 /*
10190 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10191 * character under the cursor. Only push a NUL on the replace stack,
10192 * nothing to put back when the NL is deleted.
10193 */
10194 if ((State & REPLACE_FLAG)
10195#ifdef FEAT_VREPLACE
10196 && !(State & VREPLACE_FLAG)
10197#endif
10198 )
10199 replace_push(NUL);
10200
10201 /*
10202 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10203 * replacing the next line, so we push all of the characters left on the
10204 * line onto the replace stack. This is not done here though, it is done
10205 * in open_line().
10206 */
10207
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010208#ifdef FEAT_VIRTUALEDIT
10209 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10210 * CTRL-O). */
10211 if (virtual_active() && curwin->w_cursor.coladd > 0)
10212 coladvance(getviscol());
10213#endif
10214
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215#ifdef FEAT_RIGHTLEFT
10216# ifdef FEAT_FKMAP
10217 if (p_altkeymap && p_fkmap)
10218 fkmap(NL);
10219# endif
10220 /* NL in reverse insert will always start in the end of
10221 * current line. */
10222 if (revins_on)
10223 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10224#endif
10225
10226 AppendToRedobuff(NL_STR);
10227 i = open_line(FORWARD,
10228#ifdef FEAT_COMMENTS
10229 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10230#endif
10231 0, old_indent);
10232 old_indent = 0;
10233#ifdef FEAT_CINDENT
10234 can_cindent = TRUE;
10235#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010236#ifdef FEAT_FOLDING
10237 /* When inserting a line the cursor line must never be in a closed fold. */
10238 foldOpenCursor();
10239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 return (!i);
10242}
10243
10244#ifdef FEAT_DIGRAPHS
10245/*
10246 * Handle digraph in insert mode.
10247 * Returns character still to be inserted, or NUL when nothing remaining to be
10248 * done.
10249 */
10250 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010251ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252{
10253 int c;
10254 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010255 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256
10257 pc_status = PC_STATUS_UNSET;
10258 if (redrawing() && !char_avail())
10259 {
10260 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010261 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262
10263 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010264 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265#ifdef FEAT_CMDL_INFO
10266 add_to_showcmd_c(Ctrl_K);
10267#endif
10268 }
10269
10270#ifdef USE_ON_FLY_SCROLL
10271 dont_scroll = TRUE; /* disallow scrolling here */
10272#endif
10273
10274 /* don't map the digraph chars. This also prevents the
10275 * mode message to be deleted when ESC is hit */
10276 ++no_mapping;
10277 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010278 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 --no_mapping;
10280 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010281 if (did_putchar)
10282 /* when the line fits in 'columns' the '?' is at the start of the next
10283 * line and will not be removed by the redraw */
10284 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010285
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 if (IS_SPECIAL(c) || mod_mask) /* special key */
10287 {
10288#ifdef FEAT_CMDL_INFO
10289 clear_showcmd();
10290#endif
10291 insert_special(c, TRUE, FALSE);
10292 return NUL;
10293 }
10294 if (c != ESC)
10295 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010296 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 if (redrawing() && !char_avail())
10298 {
10299 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010300 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301
10302 if (char2cells(c) == 1)
10303 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010304 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010306 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 }
10308#ifdef FEAT_CMDL_INFO
10309 add_to_showcmd_c(c);
10310#endif
10311 }
10312 ++no_mapping;
10313 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010314 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 --no_mapping;
10316 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010317 if (did_putchar)
10318 /* when the line fits in 'columns' the '?' is at the start of the
10319 * next line and will not be removed by a redraw */
10320 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 if (cc != ESC)
10322 {
10323 AppendToRedobuff((char_u *)CTRL_V_STR);
10324 c = getdigraph(c, cc, TRUE);
10325#ifdef FEAT_CMDL_INFO
10326 clear_showcmd();
10327#endif
10328 return c;
10329 }
10330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331#ifdef FEAT_CMDL_INFO
10332 clear_showcmd();
10333#endif
10334 return NUL;
10335}
10336#endif
10337
10338/*
10339 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10340 * Returns the char to be inserted, or NUL if none found.
10341 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010343ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
10345 int c;
10346 int temp;
10347 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010348 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349
10350 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10351 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010352 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 return NUL;
10354 }
10355
10356 /* try to advance to the cursor column */
10357 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010358 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 prev_ptr = ptr;
10360 validate_virtcol();
10361 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10362 {
10363 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010364 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366 if ((colnr_T)temp > curwin->w_virtcol)
10367 ptr = prev_ptr;
10368
10369#ifdef FEAT_MBYTE
10370 c = (*mb_ptr2char)(ptr);
10371#else
10372 c = *ptr;
10373#endif
10374 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010375 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 return c;
10377}
10378
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010379/*
10380 * CTRL-Y or CTRL-E typed in Insert mode.
10381 */
10382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010383ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010384{
10385 int c = tc;
10386
10387#ifdef FEAT_INS_EXPAND
10388 if (ctrl_x_mode == CTRL_X_SCROLL)
10389 {
10390 if (c == Ctrl_Y)
10391 scrolldown_clamp();
10392 else
10393 scrollup_clamp();
10394 redraw_later(VALID);
10395 }
10396 else
10397#endif
10398 {
10399 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10400 if (c != NUL)
10401 {
10402 long tw_save;
10403
10404 /* The character must be taken literally, insert like it
10405 * was typed after a CTRL-V, and pretend 'textwidth'
10406 * wasn't set. Digits, 'o' and 'x' are special after a
10407 * CTRL-V, don't use it for these. */
10408 if (c < 256 && !isalnum(c))
10409 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10410 tw_save = curbuf->b_p_tw;
10411 curbuf->b_p_tw = -1;
10412 insert_special(c, TRUE, FALSE);
10413 curbuf->b_p_tw = tw_save;
10414#ifdef FEAT_RIGHTLEFT
10415 revins_chars++;
10416 revins_legal++;
10417#endif
10418 c = Ctrl_V; /* pretend CTRL-V is last character */
10419 auto_format(FALSE, TRUE);
10420 }
10421 }
10422 return c;
10423}
10424
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425#ifdef FEAT_SMARTINDENT
10426/*
10427 * Try to do some very smart auto-indenting.
10428 * Used when inserting a "normal" character.
10429 */
10430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010431ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 pos_T *pos, old_pos;
10434 char_u *ptr;
10435 int i;
10436 int temp;
10437
10438 /*
10439 * do some very smart indenting when entering '{' or '}'
10440 */
10441 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10442 {
10443 /*
10444 * for '}' set indent equal to indent of line containing matching '{'
10445 */
10446 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10447 {
10448 old_pos = curwin->w_cursor;
10449 /*
10450 * If the matching '{' has a ')' immediately before it (ignoring
10451 * white-space), then line up with the start of the line
10452 * containing the matching '(' if there is one. This handles the
10453 * case where an "if (..\n..) {" statement continues over multiple
10454 * lines -- webb
10455 */
10456 ptr = ml_get(pos->lnum);
10457 i = pos->col;
10458 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010459 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 ;
10461 curwin->w_cursor.lnum = pos->lnum;
10462 curwin->w_cursor.col = i;
10463 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10464 curwin->w_cursor = *pos;
10465 i = get_indent();
10466 curwin->w_cursor = old_pos;
10467#ifdef FEAT_VREPLACE
10468 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010469 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 else
10471#endif
10472 (void)set_indent(i, SIN_CHANGED);
10473 }
10474 else if (curwin->w_cursor.col > 0)
10475 {
10476 /*
10477 * when inserting '{' after "O" reduce indent, but not
10478 * more than indent of previous line
10479 */
10480 temp = TRUE;
10481 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10482 {
10483 old_pos = curwin->w_cursor;
10484 i = get_indent();
10485 while (curwin->w_cursor.lnum > 1)
10486 {
10487 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10488
10489 /* ignore empty lines and lines starting with '#'. */
10490 if (*ptr != '#' && *ptr != NUL)
10491 break;
10492 }
10493 if (get_indent() >= i)
10494 temp = FALSE;
10495 curwin->w_cursor = old_pos;
10496 }
10497 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010498 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 }
10500 }
10501
10502 /*
10503 * set indent of '#' always to 0
10504 */
10505 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10506 {
10507 /* remember current indent for next line */
10508 old_indent = get_indent();
10509 (void)set_indent(0, SIN_CHANGED);
10510 }
10511
10512 /* Adjust ai_col, the char at this position can be deleted. */
10513 if (ai_col > curwin->w_cursor.col)
10514 ai_col = curwin->w_cursor.col;
10515}
10516#endif
10517
10518/*
10519 * Get the value that w_virtcol would have when 'list' is off.
10520 * Unless 'cpo' contains the 'L' flag.
10521 */
10522 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524{
10525 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10526 return getvcol_nolist(&curwin->w_cursor);
10527 validate_virtcol();
10528 return curwin->w_virtcol;
10529}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010530
10531#ifdef FEAT_AUTOCMD
10532/*
10533 * Handle the InsertCharPre autocommand.
10534 * "c" is the character that was typed.
10535 * Return a pointer to allocated memory with the replacement string.
10536 * Return NULL to continue inserting "c".
10537 */
10538 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010539do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010540{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010541 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010542 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010543
10544 /* Return quickly when there is nothing to do. */
10545 if (!has_insertcharpre())
10546 return NULL;
10547
Bram Moolenaar704984a2012-06-01 14:57:51 +020010548#ifdef FEAT_MBYTE
10549 if (has_mbyte)
10550 buf[(*mb_char2bytes)(c, buf)] = NUL;
10551 else
10552#endif
10553 {
10554 buf[0] = c;
10555 buf[1] = NUL;
10556 }
10557
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010558 /* Lock the text to avoid weird things from happening. */
10559 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010560 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010561
Bram Moolenaar704984a2012-06-01 14:57:51 +020010562 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010563 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010564 {
10565 /* Get the value of v:char. It may be empty or more than one
10566 * character. Only use it when changed, otherwise continue with the
10567 * original character to avoid breaking autoindent. */
10568 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10569 res = vim_strsave(get_vim_var_str(VV_CHAR));
10570 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010571
10572 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10573 --textlock;
10574
10575 return res;
10576}
10577#endif