blob: ee8263c8b8f29b7e9d5bc362cc0b6419432860ac [file] [log] [blame]
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
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 * insexpand.c: functions for Insert mode completion
12 */
13
14#include "vim.h"
15
Bram Moolenaar7591bb32019-03-30 13:53:47 +010016/*
17 * Definitions used for CTRL-X submode.
18 * Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
19 * ctrl_x_mode_names[] below.
20 */
21# define CTRL_X_WANT_IDENT 0x100
22
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010023# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024# define CTRL_X_NOT_DEFINED_YET 1
25# define CTRL_X_SCROLL 2
26# define CTRL_X_WHOLE_LINE 3
27# define CTRL_X_FILES 4
28# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
30# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
31# define CTRL_X_FINISHED 8
32# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
33# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
34# define CTRL_X_CMDLINE 11
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010035# define CTRL_X_FUNCTION 12
Bram Moolenaar7591bb32019-03-30 13:53:47 +010036# define CTRL_X_OMNI 13
37# define CTRL_X_SPELL 14
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +010038# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
39# define CTRL_X_EVAL 16 // for builtin function complete()
zeertzjqdca29d92021-08-31 19:12:51 +020040# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
Bram Moolenaar7591bb32019-03-30 13:53:47 +010041
42# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
43
44// Message for CTRL-X mode, index is ctrl_x_mode.
45static char *ctrl_x_msgs[] =
46{
47 N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
48 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
49 NULL, // CTRL_X_SCROLL: depends on state
50 N_(" Whole line completion (^L^N^P)"),
51 N_(" File name completion (^F^N^P)"),
52 N_(" Tag completion (^]^N^P)"),
53 N_(" Path pattern completion (^N^P)"),
54 N_(" Definition completion (^D^N^P)"),
55 NULL, // CTRL_X_FINISHED
56 N_(" Dictionary completion (^K^N^P)"),
57 N_(" Thesaurus completion (^T^N^P)"),
58 N_(" Command-line completion (^V^N^P)"),
59 N_(" User defined completion (^U^N^P)"),
60 N_(" Omni completion (^O^N^P)"),
61 N_(" Spelling suggestion (s^N^P)"),
62 N_(" Keyword Local completion (^N^P)"),
63 NULL, // CTRL_X_EVAL doesn't use msg.
zeertzjqdca29d92021-08-31 19:12:51 +020064 N_(" Command-line completion (^V^N^P)"),
Bram Moolenaar7591bb32019-03-30 13:53:47 +010065};
66
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020067#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +010068static char *ctrl_x_mode_names[] = {
69 "keyword",
70 "ctrl_x",
zeertzjq27fef592021-10-03 12:01:27 +010071 "scroll",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010072 "whole_line",
73 "files",
74 "tags",
75 "path_patterns",
76 "path_defines",
77 "unknown", // CTRL_X_FINISHED
78 "dictionary",
79 "thesaurus",
80 "cmdline",
81 "function",
82 "omni",
83 "spell",
84 NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
zeertzjqdca29d92021-08-31 19:12:51 +020085 "eval",
86 "cmdline",
Bram Moolenaar7591bb32019-03-30 13:53:47 +010087};
Bram Moolenaar9cb698d2019-08-21 15:30:45 +020088#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +010089
90/*
91 * Array indexes used for cp_text[].
92 */
93#define CPT_ABBR 0 // "abbr"
94#define CPT_MENU 1 // "menu"
95#define CPT_KIND 2 // "kind"
96#define CPT_INFO 3 // "info"
Bram Moolenaar08928322020-01-04 14:32:48 +010097#define CPT_COUNT 4 // Number of entries
Bram Moolenaar7591bb32019-03-30 13:53:47 +010098
99/*
100 * Structure used to store one match for insert completion.
101 */
102typedef struct compl_S compl_T;
103struct compl_S
104{
105 compl_T *cp_next;
106 compl_T *cp_prev;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200107 char_u *cp_str; // matched text
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200108 char_u *(cp_text[CPT_COUNT]); // text for the menu
Bram Moolenaarab782c52020-01-04 19:00:11 +0100109#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100110 typval_T cp_user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100111#endif
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200112 char_u *cp_fname; // file containing the match, allocated when
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200113 // cp_flags has CP_FREE_FNAME
114 int cp_flags; // CP_ values
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200115 int cp_number; // sequence number
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100116};
117
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200118// values for cp_flags
119# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
120# define CP_FREE_FNAME 2 // cp_fname is allocated
121# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
122# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
123# define CP_ICASE 16 // ins_compl_equal() ignores case
Bram Moolenaar440cf092021-04-03 20:13:30 +0200124# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100125
126static char e_hitend[] = N_("Hit end of paragraph");
127# ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100128static char e_compldel[] = N_("E840: Completion function deleted text");
129# endif
130
131/*
132 * All the current matches are stored in a list.
133 * "compl_first_match" points to the start of the list.
134 * "compl_curr_match" points to the currently selected entry.
135 * "compl_shown_match" is different from compl_curr_match during
136 * ins_compl_get_exp().
137 */
138static compl_T *compl_first_match = NULL;
139static compl_T *compl_curr_match = NULL;
140static compl_T *compl_shown_match = NULL;
141static compl_T *compl_old_match = NULL;
142
143// After using a cursor key <Enter> selects a match in the popup menu,
144// otherwise it inserts a line break.
145static int compl_enter_selects = FALSE;
146
147// When "compl_leader" is not NULL only matches that start with this string
148// are used.
149static char_u *compl_leader = NULL;
150
151static int compl_get_longest = FALSE; // put longest common string
152 // in compl_leader
153
154static int compl_no_insert = FALSE; // FALSE: select & insert
155 // TRUE: noinsert
156static int compl_no_select = FALSE; // FALSE: select & insert
157 // TRUE: noselect
158
159// Selected one of the matches. When FALSE the match was edited or using the
160// longest common string.
161static int compl_used_match;
162
163// didn't finish finding completions.
164static int compl_was_interrupted = FALSE;
165
166// Set when character typed while looking for matches and it means we should
167// stop looking for matches.
168static int compl_interrupted = FALSE;
169
170static int compl_restarting = FALSE; // don't insert match
171
172// When the first completion is done "compl_started" is set. When it's
173// FALSE the word to be completed must be located.
174static int compl_started = FALSE;
175
176// Which Ctrl-X mode are we in?
177static int ctrl_x_mode = CTRL_X_NORMAL;
178
179static int compl_matches = 0;
180static char_u *compl_pattern = NULL;
181static int compl_direction = FORWARD;
182static int compl_shows_dir = FORWARD;
183static int compl_pending = 0; // > 1 for postponed CTRL-N
184static pos_T compl_startpos;
185static colnr_T compl_col = 0; // column where the text starts
186 // that is being completed
187static char_u *compl_orig_text = NULL; // text as it was before
188 // completion started
189static int compl_cont_mode = 0;
190static expand_T compl_xp;
191
192static int compl_opt_refresh_always = FALSE;
193static int compl_opt_suppress_empty = FALSE;
194
Bram Moolenaar08928322020-01-04 14:32:48 +0100195static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100196static void ins_compl_longest_match(compl_T *match);
197static void ins_compl_del_pum(void);
198static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
199static char_u *find_line_end(char_u *ptr);
200static void ins_compl_free(void);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100201static int ins_compl_need_restart(void);
202static void ins_compl_new_leader(void);
203static int ins_compl_len(void);
204static void ins_compl_restart(void);
205static void ins_compl_set_original_text(char_u *str);
206static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
207# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
208static void ins_compl_add_list(list_T *list);
209static void ins_compl_add_dict(dict_T *dict);
210# endif
211static int ins_compl_key2dir(int c);
212static int ins_compl_pum_key(int c);
213static int ins_compl_key2count(int c);
214static void show_pum(int prev_w_wrow, int prev_w_leftcol);
215static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100216
217#ifdef FEAT_SPELL
218static void spell_back_to_badword(void);
219static int spell_bad_len = 0; // length of located bad word
220#endif
221
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100222/*
223 * CTRL-X pressed in Insert mode.
224 */
225 void
226ins_ctrl_x(void)
227{
zeertzjqdca29d92021-08-31 19:12:51 +0200228 if (!ctrl_x_mode_cmdline())
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100229 {
230 // if the next ^X<> won't ADD nothing, then reset
231 // compl_cont_status
232 if (compl_cont_status & CONT_N_ADDS)
233 compl_cont_status |= CONT_INTRPT;
234 else
235 compl_cont_status = 0;
236 // We're not sure which CTRL-X mode it will be yet
237 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
238 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
239 edit_submode_pre = NULL;
240 showmode();
241 }
zeertzjqdca29d92021-08-31 19:12:51 +0200242 else
243 // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
244 // CTRL-V look like CTRL-N
245 ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100246}
247
248/*
249 * Functions to check the current CTRL-X mode.
250 */
251int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
252int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
253int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
254int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
255int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
256int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
257int ctrl_x_mode_path_patterns(void) {
258 return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
259int ctrl_x_mode_path_defines(void) {
260 return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
261int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
262int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
zeertzjqdca29d92021-08-31 19:12:51 +0200263int ctrl_x_mode_cmdline(void) {
264 return ctrl_x_mode == CTRL_X_CMDLINE
265 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100266int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
267int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
268int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
269int ctrl_x_mode_line_or_eval(void) {
270 return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
271
272/*
273 * Whether other than default completion has been selected.
274 */
275 int
276ctrl_x_mode_not_default(void)
277{
278 return ctrl_x_mode != CTRL_X_NORMAL;
279}
280
281/*
zeertzjqdca29d92021-08-31 19:12:51 +0200282 * Whether CTRL-X was typed without a following character,
283 * not including when in CTRL-X CTRL-V mode.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100284 */
285 int
286ctrl_x_mode_not_defined_yet(void)
287{
288 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
289}
290
291/*
292 * Return TRUE if the 'dict' or 'tsr' option can be used.
293 */
294 int
295has_compl_option(int dict_opt)
296{
297 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200298#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100299 && !curwin->w_p_spell
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200300#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100301 )
302 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
303 {
304 ctrl_x_mode = CTRL_X_NORMAL;
305 edit_submode = NULL;
306 msg_attr(dict_opt ? _("'dictionary' option is empty")
307 : _("'thesaurus' option is empty"),
308 HL_ATTR(HLF_E));
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100309 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100310 {
311 vim_beep(BO_COMPL);
312 setcursor();
313 out_flush();
314#ifdef FEAT_EVAL
315 if (!get_vim_var_nr(VV_TESTING))
316#endif
Bram Moolenaareda1da02019-11-17 17:06:33 +0100317 ui_delay(2004L, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100318 }
319 return FALSE;
320 }
321 return TRUE;
322}
323
324/*
325 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
326 * This depends on the current mode.
327 */
328 int
329vim_is_ctrl_x_key(int c)
330{
331 // Always allow ^R - let its results then be checked
332 if (c == Ctrl_R)
333 return TRUE;
334
335 // Accept <PageUp> and <PageDown> if the popup menu is visible.
336 if (ins_compl_pum_key(c))
337 return TRUE;
338
339 switch (ctrl_x_mode)
340 {
341 case 0: // Not in any CTRL-X mode
342 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
343 case CTRL_X_NOT_DEFINED_YET:
zeertzjqdca29d92021-08-31 19:12:51 +0200344 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100345 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
346 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
347 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
348 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
349 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
zeertzjqdca29d92021-08-31 19:12:51 +0200350 || c == Ctrl_S || c == Ctrl_K || c == 's'
351 || c == Ctrl_Z);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100352 case CTRL_X_SCROLL:
353 return (c == Ctrl_Y || c == Ctrl_E);
354 case CTRL_X_WHOLE_LINE:
355 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
356 case CTRL_X_FILES:
357 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
358 case CTRL_X_DICTIONARY:
359 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
360 case CTRL_X_THESAURUS:
361 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
362 case CTRL_X_TAGS:
363 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
364#ifdef FEAT_FIND_ID
365 case CTRL_X_PATH_PATTERNS:
366 return (c == Ctrl_P || c == Ctrl_N);
367 case CTRL_X_PATH_DEFINES:
368 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
369#endif
370 case CTRL_X_CMDLINE:
371 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
372 || c == Ctrl_X);
373#ifdef FEAT_COMPL_FUNC
374 case CTRL_X_FUNCTION:
375 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
376 case CTRL_X_OMNI:
377 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
378#endif
379 case CTRL_X_SPELL:
380 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
381 case CTRL_X_EVAL:
382 return (c == Ctrl_P || c == Ctrl_N);
383 }
384 internal_error("vim_is_ctrl_x_key()");
385 return FALSE;
386}
387
388/*
389 * Return TRUE when character "c" is part of the item currently being
390 * completed. Used to decide whether to abandon complete mode when the menu
391 * is visible.
392 */
393 int
394ins_compl_accept_char(int c)
395{
396 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
397 // When expanding an identifier only accept identifier chars.
398 return vim_isIDc(c);
399
400 switch (ctrl_x_mode)
401 {
402 case CTRL_X_FILES:
403 // When expanding file name only accept file name chars. But not
404 // path separators, so that "proto/<Tab>" expands files in
405 // "proto", not "proto/" as a whole
406 return vim_isfilec(c) && !vim_ispathsep(c);
407
408 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +0200409 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100410 case CTRL_X_OMNI:
411 // Command line and Omni completion can work with just about any
412 // printable character, but do stop at white space.
413 return vim_isprintc(c) && !VIM_ISWHITE(c);
414
415 case CTRL_X_WHOLE_LINE:
416 // For while line completion a space can be part of the line.
417 return vim_isprintc(c);
418 }
419 return vim_iswordc(c);
420}
421
422/*
423 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
424 * case of the originally typed text is used, and the case of the completed
425 * text is inferred, ie this tries to work out what case you probably wanted
426 * the rest of the word to be in -- webb
427 */
428 int
429ins_compl_add_infercase(
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200430 char_u *str_arg,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100431 int len,
432 int icase,
433 char_u *fname,
434 int dir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200435 int cont_s_ipos) // next ^X<> will set initial_pos
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100436{
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200437 char_u *str = str_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100438 char_u *p;
439 int i, c;
440 int actual_len; // Take multi-byte characters
441 int actual_compl_length; // into account.
442 int min_len;
443 int *wca; // Wide character array.
444 int has_lower = FALSE;
445 int was_letter = FALSE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200446 int flags = 0;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100447
448 if (p_ic && curbuf->b_p_inf && len > 0)
449 {
450 // Infer case of completed part.
451
452 // Find actual length of completion.
453 if (has_mbyte)
454 {
455 p = str;
456 actual_len = 0;
457 while (*p != NUL)
458 {
459 MB_PTR_ADV(p);
460 ++actual_len;
461 }
462 }
463 else
464 actual_len = len;
465
466 // Find actual length of original text.
467 if (has_mbyte)
468 {
469 p = compl_orig_text;
470 actual_compl_length = 0;
471 while (*p != NUL)
472 {
473 MB_PTR_ADV(p);
474 ++actual_compl_length;
475 }
476 }
477 else
478 actual_compl_length = compl_length;
479
480 // "actual_len" may be smaller than "actual_compl_length" when using
481 // thesaurus, only use the minimum when comparing.
482 min_len = actual_len < actual_compl_length
483 ? actual_len : actual_compl_length;
484
485 // Allocate wide character array for the completion and fill it.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200486 wca = ALLOC_MULT(int, actual_len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100487 if (wca != NULL)
488 {
489 p = str;
490 for (i = 0; i < actual_len; ++i)
491 if (has_mbyte)
492 wca[i] = mb_ptr2char_adv(&p);
493 else
494 wca[i] = *(p++);
495
496 // Rule 1: Were any chars converted to lower?
497 p = compl_orig_text;
498 for (i = 0; i < min_len; ++i)
499 {
500 if (has_mbyte)
501 c = mb_ptr2char_adv(&p);
502 else
503 c = *(p++);
504 if (MB_ISLOWER(c))
505 {
506 has_lower = TRUE;
507 if (MB_ISUPPER(wca[i]))
508 {
509 // Rule 1 is satisfied.
510 for (i = actual_compl_length; i < actual_len; ++i)
511 wca[i] = MB_TOLOWER(wca[i]);
512 break;
513 }
514 }
515 }
516
517 // Rule 2: No lower case, 2nd consecutive letter converted to
518 // upper case.
519 if (!has_lower)
520 {
521 p = compl_orig_text;
522 for (i = 0; i < min_len; ++i)
523 {
524 if (has_mbyte)
525 c = mb_ptr2char_adv(&p);
526 else
527 c = *(p++);
528 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
529 {
530 // Rule 2 is satisfied.
531 for (i = actual_compl_length; i < actual_len; ++i)
532 wca[i] = MB_TOUPPER(wca[i]);
533 break;
534 }
535 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
536 }
537 }
538
539 // Copy the original case of the part we typed.
540 p = compl_orig_text;
541 for (i = 0; i < min_len; ++i)
542 {
543 if (has_mbyte)
544 c = mb_ptr2char_adv(&p);
545 else
546 c = *(p++);
547 if (MB_ISLOWER(c))
548 wca[i] = MB_TOLOWER(wca[i]);
549 else if (MB_ISUPPER(c))
550 wca[i] = MB_TOUPPER(wca[i]);
551 }
552
553 // Generate encoding specific output from wide character array.
554 // Multi-byte characters can occupy up to five bytes more than
555 // ASCII characters, and we also need one byte for NUL, so stay
556 // six bytes away from the edge of IObuff.
557 p = IObuff;
558 i = 0;
559 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
560 if (has_mbyte)
561 p += (*mb_char2bytes)(wca[i++], p);
562 else
563 *(p++) = wca[i++];
564 *p = NUL;
565
566 vim_free(wca);
567 }
568
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200569 str = IObuff;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100570 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200571 if (cont_s_ipos)
572 flags |= CP_CONT_S_IPOS;
573 if (icase)
574 flags |= CP_ICASE;
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200575
Bram Moolenaar08928322020-01-04 14:32:48 +0100576 return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100577}
578
579/*
580 * Add a match to the list of matches.
581 * If the given string is already in the list of completions, then return
582 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
583 * maybe because alloc() returns NULL, then FAIL is returned.
584 */
585 static int
586ins_compl_add(
587 char_u *str,
588 int len,
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100589 char_u *fname,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 char_u **cptext, // extra text for popup menu or NULL
591 typval_T *user_data UNUSED, // "user_data" entry or NULL
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100592 int cdir,
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200593 int flags_arg,
Bram Moolenaar08928322020-01-04 14:32:48 +0100594 int adup) // accept duplicate match
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100595{
596 compl_T *match;
597 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200598 int flags = flags_arg;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100599
Bram Moolenaarceb06192021-04-04 15:05:22 +0200600 if (flags & CP_FAST)
601 fast_breakcheck();
602 else
603 ui_breakcheck();
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100604 if (got_int)
605 return FAIL;
606 if (len < 0)
607 len = (int)STRLEN(str);
608
609 // If the same match is already present, don't add it.
610 if (compl_first_match != NULL && !adup)
611 {
612 match = compl_first_match;
613 do
614 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200615 if ( !(match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100616 && STRNCMP(match->cp_str, str, len) == 0
617 && match->cp_str[len] == NUL)
618 return NOTDONE;
619 match = match->cp_next;
620 } while (match != NULL && match != compl_first_match);
621 }
622
623 // Remove any popup menu before changing the list of matches.
624 ins_compl_del_pum();
625
626 // Allocate a new match structure.
627 // Copy the values to the new match structure.
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200628 match = ALLOC_CLEAR_ONE(compl_T);
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100629 if (match == NULL)
630 return FAIL;
631 match->cp_number = -1;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200632 if (flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100633 match->cp_number = 0;
634 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
635 {
636 vim_free(match);
637 return FAIL;
638 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100639
640 // match-fname is:
641 // - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200642 // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100643 // - NULL otherwise. --Acevedo
644 if (fname != NULL
645 && compl_curr_match != NULL
646 && compl_curr_match->cp_fname != NULL
647 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
648 match->cp_fname = compl_curr_match->cp_fname;
649 else if (fname != NULL)
650 {
651 match->cp_fname = vim_strsave(fname);
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200652 flags |= CP_FREE_FNAME;
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100653 }
654 else
655 match->cp_fname = NULL;
656 match->cp_flags = flags;
657
658 if (cptext != NULL)
659 {
660 int i;
661
662 for (i = 0; i < CPT_COUNT; ++i)
663 if (cptext[i] != NULL && *cptext[i] != NUL)
664 match->cp_text[i] = vim_strsave(cptext[i]);
665 }
Bram Moolenaarab782c52020-01-04 19:00:11 +0100666#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +0100667 if (user_data != NULL)
668 match->cp_user_data = *user_data;
Bram Moolenaarab782c52020-01-04 19:00:11 +0100669#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100670
671 // Link the new match structure in the list of matches.
672 if (compl_first_match == NULL)
673 match->cp_next = match->cp_prev = NULL;
674 else if (dir == FORWARD)
675 {
676 match->cp_next = compl_curr_match->cp_next;
677 match->cp_prev = compl_curr_match;
678 }
679 else // BACKWARD
680 {
681 match->cp_next = compl_curr_match;
682 match->cp_prev = compl_curr_match->cp_prev;
683 }
684 if (match->cp_next)
685 match->cp_next->cp_prev = match;
686 if (match->cp_prev)
687 match->cp_prev->cp_next = match;
688 else // if there's nothing before, it is the first match
689 compl_first_match = match;
690 compl_curr_match = match;
691
692 // Find the longest common string if still doing that.
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200693 if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100694 ins_compl_longest_match(match);
695
696 return OK;
697}
698
699/*
700 * Return TRUE if "str[len]" matches with match->cp_str, considering
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200701 * match->cp_flags.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100702 */
703 static int
704ins_compl_equal(compl_T *match, char_u *str, int len)
705{
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200706 if (match->cp_flags & CP_EQUAL)
Bram Moolenaar73655cf2019-04-06 13:45:55 +0200707 return TRUE;
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200708 if (match->cp_flags & CP_ICASE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100709 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
710 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
711}
712
713/*
714 * Reduce the longest common string for match "match".
715 */
716 static void
717ins_compl_longest_match(compl_T *match)
718{
719 char_u *p, *s;
720 int c1, c2;
721 int had_match;
722
723 if (compl_leader == NULL)
724 {
725 // First match, use it as a whole.
726 compl_leader = vim_strsave(match->cp_str);
727 if (compl_leader != NULL)
728 {
729 had_match = (curwin->w_cursor.col > compl_col);
730 ins_compl_delete();
731 ins_bytes(compl_leader + ins_compl_len());
732 ins_redraw(FALSE);
733
734 // When the match isn't there (to avoid matching itself) remove it
735 // again after redrawing.
736 if (!had_match)
737 ins_compl_delete();
738 compl_used_match = FALSE;
739 }
740 }
741 else
742 {
743 // Reduce the text if this match differs from compl_leader.
744 p = compl_leader;
745 s = match->cp_str;
746 while (*p != NUL)
747 {
748 if (has_mbyte)
749 {
750 c1 = mb_ptr2char(p);
751 c2 = mb_ptr2char(s);
752 }
753 else
754 {
755 c1 = *p;
756 c2 = *s;
757 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200758 if ((match->cp_flags & CP_ICASE)
759 ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100760 break;
761 if (has_mbyte)
762 {
763 MB_PTR_ADV(p);
764 MB_PTR_ADV(s);
765 }
766 else
767 {
768 ++p;
769 ++s;
770 }
771 }
772
773 if (*p != NUL)
774 {
775 // Leader was shortened, need to change the inserted text.
776 *p = NUL;
777 had_match = (curwin->w_cursor.col > compl_col);
778 ins_compl_delete();
779 ins_bytes(compl_leader + ins_compl_len());
780 ins_redraw(FALSE);
781
782 // When the match isn't there (to avoid matching itself) remove it
783 // again after redrawing.
784 if (!had_match)
785 ins_compl_delete();
786 }
787
788 compl_used_match = FALSE;
789 }
790}
791
792/*
793 * Add an array of matches to the list of matches.
794 * Frees matches[].
795 */
796 static void
797ins_compl_add_matches(
798 int num_matches,
799 char_u **matches,
800 int icase)
801{
802 int i;
803 int add_r = OK;
804 int dir = compl_direction;
805
806 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar08928322020-01-04 14:32:48 +0100807 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
Bram Moolenaar440cf092021-04-03 20:13:30 +0200808 CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100809 // if dir was BACKWARD then honor it just once
810 dir = FORWARD;
811 FreeWild(num_matches, matches);
812}
813
814/*
815 * Make the completion list cyclic.
816 * Return the number of matches (excluding the original).
817 */
818 static int
819ins_compl_make_cyclic(void)
820{
821 compl_T *match;
822 int count = 0;
823
824 if (compl_first_match != NULL)
825 {
826 // Find the end of the list.
827 match = compl_first_match;
828 // there's always an entry for the compl_orig_text, it doesn't count.
829 while (match->cp_next != NULL && match->cp_next != compl_first_match)
830 {
831 match = match->cp_next;
832 ++count;
833 }
834 match->cp_next = compl_first_match;
835 compl_first_match->cp_prev = match;
836 }
837 return count;
838}
839
840/*
841 * Return whether there currently is a shown match.
842 */
843 int
844ins_compl_has_shown_match(void)
845{
846 return compl_shown_match == NULL
847 || compl_shown_match != compl_shown_match->cp_next;
848}
849
850/*
851 * Return whether the shown match is long enough.
852 */
853 int
854ins_compl_long_shown_match(void)
855{
856 return (int)STRLEN(compl_shown_match->cp_str)
857 > curwin->w_cursor.col - compl_col;
858}
859
860/*
861 * Set variables that store noselect and noinsert behavior from the
862 * 'completeopt' value.
863 */
864 void
865completeopt_was_set(void)
866{
867 compl_no_insert = FALSE;
868 compl_no_select = FALSE;
869 if (strstr((char *)p_cot, "noselect") != NULL)
870 compl_no_select = TRUE;
871 if (strstr((char *)p_cot, "noinsert") != NULL)
872 compl_no_insert = TRUE;
873}
874
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100875
876// "compl_match_array" points the currently displayed list of entries in the
877// popup menu. It is NULL when there is no popup menu.
878static pumitem_T *compl_match_array = NULL;
879static int compl_match_arraysize;
880
881/*
882 * Update the screen and when there is any scrolling remove the popup menu.
883 */
884 static void
885ins_compl_upd_pum(void)
886{
887 int h;
888
889 if (compl_match_array != NULL)
890 {
891 h = curwin->w_cline_height;
892 // Update the screen later, before drawing the popup menu over it.
893 pum_call_update_screen();
894 if (h != curwin->w_cline_height)
895 ins_compl_del_pum();
896 }
897}
898
899/*
900 * Remove any popup menu.
901 */
902 static void
903ins_compl_del_pum(void)
904{
905 if (compl_match_array != NULL)
906 {
907 pum_undisplay();
908 VIM_CLEAR(compl_match_array);
909 }
910}
911
912/*
913 * Return TRUE if the popup menu should be displayed.
914 */
915 int
916pum_wanted(void)
917{
918 // 'completeopt' must contain "menu" or "menuone"
919 if (vim_strchr(p_cot, 'm') == NULL)
920 return FALSE;
921
922 // The display looks bad on a B&W display.
923 if (t_colors < 8
924#ifdef FEAT_GUI
925 && !gui.in_use
926#endif
927 )
928 return FALSE;
929 return TRUE;
930}
931
932/*
933 * Return TRUE if there are two or more matches to be shown in the popup menu.
934 * One if 'completopt' contains "menuone".
935 */
936 static int
937pum_enough_matches(void)
938{
939 compl_T *compl;
940 int i;
941
942 // Don't display the popup menu if there are no matches or there is only
943 // one (ignoring the original text).
944 compl = compl_first_match;
945 i = 0;
946 do
947 {
948 if (compl == NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +0200949 || ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 && ++i == 2))
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100950 break;
951 compl = compl->cp_next;
952 } while (compl != compl_first_match);
953
954 if (strstr((char *)p_cot, "menuone") != NULL)
955 return (i >= 1);
956 return (i >= 2);
957}
958
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200959#ifdef FEAT_EVAL
960/*
961 * Allocate Dict for the completed item.
962 * { word, abbr, menu, kind, info }
963 */
964 static dict_T *
965ins_compl_dict_alloc(compl_T *match)
966{
967 dict_T *dict = dict_alloc_lock(VAR_FIXED);
968
969 if (dict != NULL)
970 {
971 dict_add_string(dict, "word", match->cp_str);
972 dict_add_string(dict, "abbr", match->cp_text[CPT_ABBR]);
973 dict_add_string(dict, "menu", match->cp_text[CPT_MENU]);
974 dict_add_string(dict, "kind", match->cp_text[CPT_KIND]);
975 dict_add_string(dict, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +0100976 if (match->cp_user_data.v_type == VAR_UNKNOWN)
977 dict_add_string(dict, "user_data", (char_u *)"");
978 else
979 dict_add_tv(dict, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200980 }
981 return dict;
982}
983
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200984 static void
985trigger_complete_changed_event(int cur)
986{
987 dict_T *v_event;
988 dict_T *item;
989 static int recursive = FALSE;
990
991 if (recursive)
992 return;
993
994 v_event = get_vim_var_dict(VV_EVENT);
995 if (cur < 0)
996 item = dict_alloc();
997 else
998 item = ins_compl_dict_alloc(compl_curr_match);
999 if (item == NULL)
1000 return;
1001 dict_add_dict(v_event, "completed_item", item);
1002 pum_set_event_info(v_event);
1003 dict_set_items_ro(v_event);
1004
1005 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001006 textwinlock++;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001007 apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001008 textwinlock--;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001009 recursive = FALSE;
1010
1011 dict_free_contents(v_event);
1012 hash_init(&v_event->dv_hashtab);
1013}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001014#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001015
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001016/*
1017 * Show the popup menu for the list of matches.
1018 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
1019 */
1020 void
1021ins_compl_show_pum(void)
1022{
1023 compl_T *compl;
1024 compl_T *shown_compl = NULL;
1025 int did_find_shown_match = FALSE;
1026 int shown_match_ok = FALSE;
1027 int i;
1028 int cur = -1;
1029 colnr_T col;
1030 int lead_len = 0;
1031
1032 if (!pum_wanted() || !pum_enough_matches())
1033 return;
1034
1035#if defined(FEAT_EVAL)
1036 // Dirty hard-coded hack: remove any matchparen highlighting.
Bram Moolenaar179eb562020-12-27 18:03:22 +01001037 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|:3match none|endif");
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001038#endif
1039
1040 // Update the screen later, before drawing the popup menu over it.
1041 pum_call_update_screen();
1042
1043 if (compl_match_array == NULL)
1044 {
1045 // Need to build the popup menu list.
1046 compl_match_arraysize = 0;
1047 compl = compl_first_match;
1048 if (compl_leader != NULL)
1049 lead_len = (int)STRLEN(compl_leader);
1050 do
1051 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001052 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001053 && (compl_leader == NULL
1054 || ins_compl_equal(compl, compl_leader, lead_len)))
1055 ++compl_match_arraysize;
1056 compl = compl->cp_next;
1057 } while (compl != NULL && compl != compl_first_match);
1058 if (compl_match_arraysize == 0)
1059 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001060 compl_match_array = ALLOC_CLEAR_MULT(pumitem_T, compl_match_arraysize);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001061 if (compl_match_array != NULL)
1062 {
1063 // If the current match is the original text don't find the first
1064 // match after it, don't highlight anything.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001065 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001066 shown_match_ok = TRUE;
1067
1068 i = 0;
1069 compl = compl_first_match;
1070 do
1071 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001072 if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001073 && (compl_leader == NULL
1074 || ins_compl_equal(compl, compl_leader, lead_len)))
1075 {
1076 if (!shown_match_ok)
1077 {
1078 if (compl == compl_shown_match || did_find_shown_match)
1079 {
1080 // This item is the shown match or this is the
1081 // first displayed item after the shown match.
1082 compl_shown_match = compl;
1083 did_find_shown_match = TRUE;
1084 shown_match_ok = TRUE;
1085 }
1086 else
1087 // Remember this displayed match for when the
1088 // shown match is just below it.
1089 shown_compl = compl;
1090 cur = i;
1091 }
1092
1093 if (compl->cp_text[CPT_ABBR] != NULL)
1094 compl_match_array[i].pum_text =
1095 compl->cp_text[CPT_ABBR];
1096 else
1097 compl_match_array[i].pum_text = compl->cp_str;
1098 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
1099 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
1100 if (compl->cp_text[CPT_MENU] != NULL)
1101 compl_match_array[i++].pum_extra =
1102 compl->cp_text[CPT_MENU];
1103 else
1104 compl_match_array[i++].pum_extra = compl->cp_fname;
1105 }
1106
1107 if (compl == compl_shown_match)
1108 {
1109 did_find_shown_match = TRUE;
1110
1111 // When the original text is the shown match don't set
1112 // compl_shown_match.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001113 if (compl->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001114 shown_match_ok = TRUE;
1115
1116 if (!shown_match_ok && shown_compl != NULL)
1117 {
1118 // The shown match isn't displayed, set it to the
1119 // previously displayed match.
1120 compl_shown_match = shown_compl;
1121 shown_match_ok = TRUE;
1122 }
1123 }
1124 compl = compl->cp_next;
1125 } while (compl != NULL && compl != compl_first_match);
1126
1127 if (!shown_match_ok) // no displayed match at all
1128 cur = -1;
1129 }
1130 }
1131 else
1132 {
1133 // popup menu already exists, only need to find the current item.
1134 for (i = 0; i < compl_match_arraysize; ++i)
1135 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
1136 || compl_match_array[i].pum_text
1137 == compl_shown_match->cp_text[CPT_ABBR])
1138 {
1139 cur = i;
1140 break;
1141 }
1142 }
1143
1144 if (compl_match_array != NULL)
1145 {
1146 // In Replace mode when a $ is displayed at the end of the line only
1147 // part of the screen would be updated. We do need to redraw here.
1148 dollar_vcol = -1;
1149
1150 // Compute the screen column of the start of the completed text.
1151 // Use the cursor to get all wrapping and other settings right.
1152 col = curwin->w_cursor.col;
1153 curwin->w_cursor.col = compl_col;
1154 pum_display(compl_match_array, compl_match_arraysize, cur);
1155 curwin->w_cursor.col = col;
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001156
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001157#ifdef FEAT_EVAL
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001158 if (has_completechanged())
1159 trigger_complete_changed_event(cur);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001160#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001161 }
1162}
1163
1164#define DICT_FIRST (1) // use just first element in "dict"
1165#define DICT_EXACT (2) // "dict" is the exact name of a file
1166
1167/*
1168 * Add any identifiers that match the given pattern in the list of dictionary
1169 * files "dict_start" to the list of completions.
1170 */
1171 static void
1172ins_compl_dictionaries(
1173 char_u *dict_start,
1174 char_u *pat,
1175 int flags, // DICT_FIRST and/or DICT_EXACT
1176 int thesaurus) // Thesaurus completion
1177{
1178 char_u *dict = dict_start;
1179 char_u *ptr;
1180 char_u *buf;
1181 regmatch_T regmatch;
1182 char_u **files;
1183 int count;
1184 int save_p_scs;
1185 int dir = compl_direction;
1186
1187 if (*dict == NUL)
1188 {
1189#ifdef FEAT_SPELL
1190 // When 'dictionary' is empty and spell checking is enabled use
1191 // "spell".
1192 if (!thesaurus && curwin->w_p_spell)
1193 dict = (char_u *)"spell";
1194 else
1195#endif
1196 return;
1197 }
1198
1199 buf = alloc(LSIZE);
1200 if (buf == NULL)
1201 return;
1202 regmatch.regprog = NULL; // so that we can goto theend
1203
1204 // If 'infercase' is set, don't use 'smartcase' here
1205 save_p_scs = p_scs;
1206 if (curbuf->b_p_inf)
1207 p_scs = FALSE;
1208
1209 // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
1210 // to only match at the start of a line. Otherwise just match the
1211 // pattern. Also need to double backslashes.
1212 if (ctrl_x_mode_line_or_eval())
1213 {
1214 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
1215 size_t len;
1216
1217 if (pat_esc == NULL)
1218 goto theend;
1219 len = STRLEN(pat_esc) + 10;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001220 ptr = alloc(len);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001221 if (ptr == NULL)
1222 {
1223 vim_free(pat_esc);
1224 goto theend;
1225 }
1226 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
1227 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
1228 vim_free(pat_esc);
1229 vim_free(ptr);
1230 }
1231 else
1232 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001233 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001234 if (regmatch.regprog == NULL)
1235 goto theend;
1236 }
1237
1238 // ignore case depends on 'ignorecase', 'smartcase' and "pat"
1239 regmatch.rm_ic = ignorecase(pat);
1240 while (*dict != NUL && !got_int && !compl_interrupted)
1241 {
1242 // copy one dictionary file name into buf
1243 if (flags == DICT_EXACT)
1244 {
1245 count = 1;
1246 files = &dict;
1247 }
1248 else
1249 {
1250 // Expand wildcards in the dictionary name, but do not allow
1251 // backticks (for security, the 'dict' option may have been set in
1252 // a modeline).
1253 copy_option_part(&dict, buf, LSIZE, ",");
1254# ifdef FEAT_SPELL
1255 if (!thesaurus && STRCMP(buf, "spell") == 0)
1256 count = -1;
1257 else
1258# endif
1259 if (vim_strchr(buf, '`') != NULL
1260 || expand_wildcards(1, &buf, &count, &files,
1261 EW_FILE|EW_SILENT) != OK)
1262 count = 0;
1263 }
1264
1265# ifdef FEAT_SPELL
1266 if (count == -1)
1267 {
1268 // Complete from active spelling. Skip "\<" in the pattern, we
1269 // don't use it as a RE.
1270 if (pat[0] == '\\' && pat[1] == '<')
1271 ptr = pat + 2;
1272 else
1273 ptr = pat;
1274 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
1275 }
1276 else
1277# endif
1278 if (count > 0) // avoid warning for using "files" uninit
1279 {
1280 ins_compl_files(count, files, thesaurus, flags,
1281 &regmatch, buf, &dir);
1282 if (flags != DICT_EXACT)
1283 FreeWild(count, files);
1284 }
1285 if (flags != 0)
1286 break;
1287 }
1288
1289theend:
1290 p_scs = save_p_scs;
1291 vim_regfree(regmatch.regprog);
1292 vim_free(buf);
1293}
1294
1295 static void
1296ins_compl_files(
1297 int count,
1298 char_u **files,
1299 int thesaurus,
1300 int flags,
1301 regmatch_T *regmatch,
1302 char_u *buf,
1303 int *dir)
1304{
1305 char_u *ptr;
1306 int i;
1307 FILE *fp;
1308 int add_r;
1309
1310 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
1311 {
1312 fp = mch_fopen((char *)files[i], "r"); // open dictionary file
1313 if (flags != DICT_EXACT)
1314 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01001315 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001316 vim_snprintf((char *)IObuff, IOSIZE,
1317 _("Scanning dictionary: %s"), (char *)files[i]);
1318 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
1319 }
1320
1321 if (fp != NULL)
1322 {
1323 // Read dictionary file line by line.
1324 // Check each line for a match.
1325 while (!got_int && !compl_interrupted
1326 && !vim_fgets(buf, LSIZE, fp))
1327 {
1328 ptr = buf;
1329 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
1330 {
1331 ptr = regmatch->startp[0];
1332 if (ctrl_x_mode_line_or_eval())
1333 ptr = find_line_end(ptr);
1334 else
1335 ptr = find_word_end(ptr);
1336 add_r = ins_compl_add_infercase(regmatch->startp[0],
1337 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001338 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001339 if (thesaurus)
1340 {
1341 char_u *wstart;
1342
1343 // Add the other matches on the line
1344 ptr = buf;
1345 while (!got_int)
1346 {
1347 // Find start of the next word. Skip white
1348 // space and punctuation.
1349 ptr = find_word_start(ptr);
1350 if (*ptr == NUL || *ptr == NL)
1351 break;
1352 wstart = ptr;
1353
1354 // Find end of the word.
1355 if (has_mbyte)
1356 // Japanese words may have characters in
1357 // different classes, only separate words
1358 // with single-byte non-word characters.
1359 while (*ptr != NUL)
1360 {
1361 int l = (*mb_ptr2len)(ptr);
1362
1363 if (l < 2 && !vim_iswordc(*ptr))
1364 break;
1365 ptr += l;
1366 }
1367 else
1368 ptr = find_word_end(ptr);
1369
1370 // Add the word. Skip the regexp match.
1371 if (wstart != regmatch->startp[0])
1372 add_r = ins_compl_add_infercase(wstart,
1373 (int)(ptr - wstart),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001374 p_ic, files[i], *dir, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001375 }
1376 }
1377 if (add_r == OK)
1378 // if dir was BACKWARD then honor it just once
1379 *dir = FORWARD;
1380 else if (add_r == FAIL)
1381 break;
1382 // avoid expensive call to vim_regexec() when at end
1383 // of line
1384 if (*ptr == '\n' || got_int)
1385 break;
1386 }
1387 line_breakcheck();
1388 ins_compl_check_keys(50, FALSE);
1389 }
1390 fclose(fp);
1391 }
1392 }
1393}
1394
1395/*
1396 * Find the start of the next word.
1397 * Returns a pointer to the first char of the word. Also stops at a NUL.
1398 */
1399 char_u *
1400find_word_start(char_u *ptr)
1401{
1402 if (has_mbyte)
1403 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
1404 ptr += (*mb_ptr2len)(ptr);
1405 else
1406 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
1407 ++ptr;
1408 return ptr;
1409}
1410
1411/*
1412 * Find the end of the word. Assumes it starts inside a word.
1413 * Returns a pointer to just after the word.
1414 */
1415 char_u *
1416find_word_end(char_u *ptr)
1417{
1418 int start_class;
1419
1420 if (has_mbyte)
1421 {
1422 start_class = mb_get_class(ptr);
1423 if (start_class > 1)
1424 while (*ptr != NUL)
1425 {
1426 ptr += (*mb_ptr2len)(ptr);
1427 if (mb_get_class(ptr) != start_class)
1428 break;
1429 }
1430 }
1431 else
1432 while (vim_iswordc(*ptr))
1433 ++ptr;
1434 return ptr;
1435}
1436
1437/*
1438 * Find the end of the line, omitting CR and NL at the end.
1439 * Returns a pointer to just after the line.
1440 */
1441 static char_u *
1442find_line_end(char_u *ptr)
1443{
1444 char_u *s;
1445
1446 s = ptr + STRLEN(ptr);
1447 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
1448 --s;
1449 return s;
1450}
1451
1452/*
1453 * Free the list of completions
1454 */
1455 static void
1456ins_compl_free(void)
1457{
1458 compl_T *match;
1459 int i;
1460
1461 VIM_CLEAR(compl_pattern);
1462 VIM_CLEAR(compl_leader);
1463
1464 if (compl_first_match == NULL)
1465 return;
1466
1467 ins_compl_del_pum();
1468 pum_clear();
1469
1470 compl_curr_match = compl_first_match;
1471 do
1472 {
1473 match = compl_curr_match;
1474 compl_curr_match = compl_curr_match->cp_next;
1475 vim_free(match->cp_str);
1476 // several entries may use the same fname, free it just once.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001477 if (match->cp_flags & CP_FREE_FNAME)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001478 vim_free(match->cp_fname);
1479 for (i = 0; i < CPT_COUNT; ++i)
1480 vim_free(match->cp_text[i]);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001481#ifdef FEAT_EVAL
Bram Moolenaar08928322020-01-04 14:32:48 +01001482 clear_tv(&match->cp_user_data);
Bram Moolenaarab782c52020-01-04 19:00:11 +01001483#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001484 vim_free(match);
1485 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
1486 compl_first_match = compl_curr_match = NULL;
1487 compl_shown_match = NULL;
1488 compl_old_match = NULL;
1489}
1490
1491 void
1492ins_compl_clear(void)
1493{
1494 compl_cont_status = 0;
1495 compl_started = FALSE;
1496 compl_matches = 0;
1497 VIM_CLEAR(compl_pattern);
1498 VIM_CLEAR(compl_leader);
1499 edit_submode_extra = NULL;
1500 VIM_CLEAR(compl_orig_text);
1501 compl_enter_selects = FALSE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001502#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001503 // clear v:completed_item
1504 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001505#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001506}
1507
1508/*
1509 * Return TRUE when Insert completion is active.
1510 */
1511 int
1512ins_compl_active(void)
1513{
1514 return compl_started;
1515}
1516
1517/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001518 * Selected one of the matches. When FALSE the match was edited or using the
1519 * longest common string.
1520 */
1521 int
1522ins_compl_used_match(void)
1523{
1524 return compl_used_match;
1525}
1526
1527/*
1528 * Initialize get longest common string.
1529 */
1530 void
1531ins_compl_init_get_longest(void)
1532{
1533 compl_get_longest = FALSE;
1534}
1535
1536/*
1537 * Returns TRUE when insert completion is interrupted.
1538 */
1539 int
1540ins_compl_interrupted(void)
1541{
1542 return compl_interrupted;
1543}
1544
1545/*
1546 * Returns TRUE if the <Enter> key selects a match in the completion popup
1547 * menu.
1548 */
1549 int
1550ins_compl_enter_selects(void)
1551{
1552 return compl_enter_selects;
1553}
1554
1555/*
1556 * Return the column where the text starts that is being completed
1557 */
1558 colnr_T
1559ins_compl_col(void)
1560{
1561 return compl_col;
1562}
1563
1564/*
1565 * Delete one character before the cursor and show the subset of the matches
1566 * that match the word that is now before the cursor.
1567 * Returns the character to be used, NUL if the work is done and another char
1568 * to be got from the user.
1569 */
1570 int
1571ins_compl_bs(void)
1572{
1573 char_u *line;
1574 char_u *p;
1575
1576 line = ml_get_curline();
1577 p = line + curwin->w_cursor.col;
1578 MB_PTR_BACK(line, p);
1579
1580 // Stop completion when the whole word was deleted. For Omni completion
1581 // allow the word to be deleted, we won't match everything.
1582 // Respect the 'backspace' option.
1583 if ((int)(p - line) - (int)compl_col < 0
1584 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar440cf092021-04-03 20:13:30 +02001585 && ctrl_x_mode != CTRL_X_OMNI)
1586 || ctrl_x_mode == CTRL_X_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001587 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
1588 - compl_length < 0))
1589 return K_BS;
1590
1591 // Deleted more than what was used to find matches or didn't finish
1592 // finding all matches: need to look for matches all over again.
1593 if (curwin->w_cursor.col <= compl_col + compl_length
1594 || ins_compl_need_restart())
1595 ins_compl_restart();
1596
1597 vim_free(compl_leader);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001598 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001599 if (compl_leader != NULL)
1600 {
1601 ins_compl_new_leader();
1602 if (compl_shown_match != NULL)
1603 // Make sure current match is not a hidden item.
1604 compl_curr_match = compl_shown_match;
1605 return NUL;
1606 }
1607 return K_BS;
1608}
1609
1610/*
1611 * Return TRUE when we need to find matches again, ins_compl_restart() is to
1612 * be called.
1613 */
1614 static int
1615ins_compl_need_restart(void)
1616{
1617 // Return TRUE if we didn't complete finding matches or when the
1618 // 'completefunc' returned "always" in the "refresh" dictionary item.
1619 return compl_was_interrupted
1620 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
1621 && compl_opt_refresh_always);
1622}
1623
1624/*
1625 * Called after changing "compl_leader".
1626 * Show the popup menu with a different set of matches.
1627 * May also search for matches again if the previous search was interrupted.
1628 */
1629 static void
1630ins_compl_new_leader(void)
1631{
1632 ins_compl_del_pum();
1633 ins_compl_delete();
1634 ins_bytes(compl_leader + ins_compl_len());
1635 compl_used_match = FALSE;
1636
1637 if (compl_started)
1638 ins_compl_set_original_text(compl_leader);
1639 else
1640 {
1641#ifdef FEAT_SPELL
1642 spell_bad_len = 0; // need to redetect bad word
1643#endif
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001644 // Matches were cleared, need to search for them now. Before drawing
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001645 // the popup menu display the changed text before the cursor. Set
1646 // "compl_restarting" to avoid that the first match is inserted.
1647 pum_call_update_screen();
1648#ifdef FEAT_GUI
1649 if (gui.in_use)
1650 {
1651 // Show the cursor after the match, not after the redrawn text.
1652 setcursor();
1653 out_flush_cursor(FALSE, FALSE);
1654 }
1655#endif
1656 compl_restarting = TRUE;
1657 if (ins_complete(Ctrl_N, TRUE) == FAIL)
1658 compl_cont_status = 0;
1659 compl_restarting = FALSE;
1660 }
1661
1662 compl_enter_selects = !compl_used_match;
1663
1664 // Show the popup menu with a different set of matches.
1665 ins_compl_show_pum();
1666
1667 // Don't let Enter select the original text when there is no popup menu.
1668 if (compl_match_array == NULL)
1669 compl_enter_selects = FALSE;
1670}
1671
1672/*
1673 * Return the length of the completion, from the completion start column to
1674 * the cursor column. Making sure it never goes below zero.
1675 */
1676 static int
1677ins_compl_len(void)
1678{
1679 int off = (int)curwin->w_cursor.col - (int)compl_col;
1680
1681 if (off < 0)
1682 return 0;
1683 return off;
1684}
1685
1686/*
1687 * Append one character to the match leader. May reduce the number of
1688 * matches.
1689 */
1690 void
1691ins_compl_addleader(int c)
1692{
1693 int cc;
1694
1695 if (stop_arrow() == FAIL)
1696 return;
1697 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
1698 {
1699 char_u buf[MB_MAXBYTES + 1];
1700
1701 (*mb_char2bytes)(c, buf);
1702 buf[cc] = NUL;
1703 ins_char_bytes(buf, cc);
1704 if (compl_opt_refresh_always)
1705 AppendToRedobuff(buf);
1706 }
1707 else
1708 {
1709 ins_char(c);
1710 if (compl_opt_refresh_always)
1711 AppendCharToRedobuff(c);
1712 }
1713
1714 // If we didn't complete finding matches we must search again.
1715 if (ins_compl_need_restart())
1716 ins_compl_restart();
1717
1718 // When 'always' is set, don't reset compl_leader. While completing,
1719 // cursor doesn't point original position, changing compl_leader would
1720 // break redo.
1721 if (!compl_opt_refresh_always)
1722 {
1723 vim_free(compl_leader);
1724 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001725 curwin->w_cursor.col - compl_col);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001726 if (compl_leader != NULL)
1727 ins_compl_new_leader();
1728 }
1729}
1730
1731/*
1732 * Setup for finding completions again without leaving CTRL-X mode. Used when
1733 * BS or a key was typed while still searching for matches.
1734 */
1735 static void
1736ins_compl_restart(void)
1737{
1738 ins_compl_free();
1739 compl_started = FALSE;
1740 compl_matches = 0;
1741 compl_cont_status = 0;
1742 compl_cont_mode = 0;
1743}
1744
1745/*
1746 * Set the first match, the original text.
1747 */
1748 static void
1749ins_compl_set_original_text(char_u *str)
1750{
1751 char_u *p;
1752
1753 // Replace the original text entry.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001754 // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly be
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001755 // at the last item for backward completion
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001756 if (compl_first_match->cp_flags & CP_ORIGINAL_TEXT) // safety check
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001757 {
1758 p = vim_strsave(str);
1759 if (p != NULL)
1760 {
1761 vim_free(compl_first_match->cp_str);
1762 compl_first_match->cp_str = p;
1763 }
1764 }
1765 else if (compl_first_match->cp_prev != NULL
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001766 && (compl_first_match->cp_prev->cp_flags & CP_ORIGINAL_TEXT))
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001767 {
1768 p = vim_strsave(str);
1769 if (p != NULL)
1770 {
1771 vim_free(compl_first_match->cp_prev->cp_str);
1772 compl_first_match->cp_prev->cp_str = p;
1773 }
1774 }
1775}
1776
1777/*
1778 * Append one character to the match leader. May reduce the number of
1779 * matches.
1780 */
1781 void
1782ins_compl_addfrommatch(void)
1783{
1784 char_u *p;
1785 int len = (int)curwin->w_cursor.col - (int)compl_col;
1786 int c;
1787 compl_T *cp;
1788
1789 p = compl_shown_match->cp_str;
1790 if ((int)STRLEN(p) <= len) // the match is too short
1791 {
1792 // When still at the original match use the first entry that matches
1793 // the leader.
Bram Moolenaard9eefe32019-04-06 14:22:21 +02001794 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001795 {
1796 p = NULL;
1797 for (cp = compl_shown_match->cp_next; cp != NULL
1798 && cp != compl_first_match; cp = cp->cp_next)
1799 {
1800 if (compl_leader == NULL
1801 || ins_compl_equal(cp, compl_leader,
1802 (int)STRLEN(compl_leader)))
1803 {
1804 p = cp->cp_str;
1805 break;
1806 }
1807 }
1808 if (p == NULL || (int)STRLEN(p) <= len)
1809 return;
1810 }
1811 else
1812 return;
1813 }
1814 p += len;
1815 c = PTR2CHAR(p);
1816 ins_compl_addleader(c);
1817}
1818
1819/*
1820 * Prepare for Insert mode completion, or stop it.
1821 * Called just after typing a character in Insert mode.
1822 * Returns TRUE when the character is not to be inserted;
1823 */
1824 int
1825ins_compl_prep(int c)
1826{
1827 char_u *ptr;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001828#ifdef FEAT_CINDENT
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001829 int want_cindent;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001830#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001831 int retval = FALSE;
Bram Moolenaar17e04782020-01-17 18:58:59 +01001832 int prev_mode = ctrl_x_mode;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001833
1834 // Forget any previous 'special' messages if this is actually
1835 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
1836 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
1837 edit_submode_extra = NULL;
1838
1839 // Ignore end of Select mode mapping and mouse scroll buttons.
1840 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar957cf672020-11-12 14:21:06 +01001841 || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_COMMAND)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001842 return retval;
1843
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001844#ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001845 // Ignore mouse events in a popup window
1846 if (is_mouse_key(c))
1847 {
1848 // Ignore drag and release events, the position does not need to be in
1849 // the popup and it may have just closed.
1850 if (c == K_LEFTRELEASE
1851 || c == K_LEFTRELEASE_NM
1852 || c == K_MIDDLERELEASE
1853 || c == K_RIGHTRELEASE
1854 || c == K_X1RELEASE
1855 || c == K_X2RELEASE
1856 || c == K_LEFTDRAG
1857 || c == K_MIDDLEDRAG
1858 || c == K_RIGHTDRAG
1859 || c == K_X1DRAG
1860 || c == K_X2DRAG)
1861 return retval;
1862 if (popup_visible)
1863 {
1864 int row = mouse_row;
1865 int col = mouse_col;
1866 win_T *wp = mouse_find_win(&row, &col, FIND_POPUP);
1867
1868 if (wp != NULL && WIN_IS_POPUP(wp))
1869 return retval;
1870 }
1871 }
1872#endif
1873
zeertzjqdca29d92021-08-31 19:12:51 +02001874 if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X)
1875 {
1876 if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c)
1877 || !vim_is_ctrl_x_key(c))
1878 {
1879 // Not starting another completion mode.
1880 ctrl_x_mode = CTRL_X_CMDLINE;
1881
1882 // CTRL-X CTRL-Z should stop completion without inserting anything
1883 if (c == Ctrl_Z)
1884 retval = TRUE;
1885 }
1886 else
1887 {
1888 ctrl_x_mode = CTRL_X_CMDLINE;
1889
1890 // Other CTRL-X keys first stop completion, then start another
1891 // completion mode.
1892 ins_compl_prep(' ');
1893 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1894 }
1895 }
1896
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001897 // Set "compl_get_longest" when finding the first matches.
1898 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
1899 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
1900 {
1901 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
1902 compl_used_match = TRUE;
1903
1904 }
1905
1906 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
1907 {
1908 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
1909 // it will be yet. Now we decide.
1910 switch (c)
1911 {
1912 case Ctrl_E:
1913 case Ctrl_Y:
1914 ctrl_x_mode = CTRL_X_SCROLL;
1915 if (!(State & REPLACE_FLAG))
1916 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1917 else
1918 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1919 edit_submode_pre = NULL;
1920 showmode();
1921 break;
1922 case Ctrl_L:
1923 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1924 break;
1925 case Ctrl_F:
1926 ctrl_x_mode = CTRL_X_FILES;
1927 break;
1928 case Ctrl_K:
1929 ctrl_x_mode = CTRL_X_DICTIONARY;
1930 break;
1931 case Ctrl_R:
1932 // Simply allow ^R to happen without affecting ^X mode
1933 break;
1934 case Ctrl_T:
1935 ctrl_x_mode = CTRL_X_THESAURUS;
1936 break;
1937#ifdef FEAT_COMPL_FUNC
1938 case Ctrl_U:
1939 ctrl_x_mode = CTRL_X_FUNCTION;
1940 break;
1941 case Ctrl_O:
1942 ctrl_x_mode = CTRL_X_OMNI;
1943 break;
1944#endif
1945 case 's':
1946 case Ctrl_S:
1947 ctrl_x_mode = CTRL_X_SPELL;
1948#ifdef FEAT_SPELL
1949 ++emsg_off; // Avoid getting the E756 error twice.
1950 spell_back_to_badword();
1951 --emsg_off;
1952#endif
1953 break;
1954 case Ctrl_RSB:
1955 ctrl_x_mode = CTRL_X_TAGS;
1956 break;
1957#ifdef FEAT_FIND_ID
1958 case Ctrl_I:
1959 case K_S_TAB:
1960 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
1961 break;
1962 case Ctrl_D:
1963 ctrl_x_mode = CTRL_X_PATH_DEFINES;
1964 break;
1965#endif
1966 case Ctrl_V:
1967 case Ctrl_Q:
1968 ctrl_x_mode = CTRL_X_CMDLINE;
1969 break;
zeertzjqdca29d92021-08-31 19:12:51 +02001970 case Ctrl_Z:
1971 ctrl_x_mode = CTRL_X_NORMAL;
1972 edit_submode = NULL;
1973 showmode();
1974 retval = TRUE;
1975 break;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001976 case Ctrl_P:
1977 case Ctrl_N:
1978 // ^X^P means LOCAL expansion if nothing interrupted (eg we
1979 // just started ^X mode, or there were enough ^X's to cancel
1980 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
1981 // do normal expansion when interrupting a different mode (say
1982 // ^X^F^X^P or ^P^X^X^P, see below)
1983 // nothing changes if interrupting mode 0, (eg, the flag
1984 // doesn't change when going to ADDING mode -- Acevedo
1985 if (!(compl_cont_status & CONT_INTRPT))
1986 compl_cont_status |= CONT_LOCAL;
1987 else if (compl_cont_mode != 0)
1988 compl_cont_status &= ~CONT_LOCAL;
1989 // FALLTHROUGH
1990 default:
1991 // If we have typed at least 2 ^X's... for modes != 0, we set
1992 // compl_cont_status = 0 (eg, as if we had just started ^X
1993 // mode).
1994 // For mode 0, we set "compl_cont_mode" to an impossible
1995 // value, in both cases ^X^X can be used to restart the same
1996 // mode (avoiding ADDING mode).
1997 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
1998 // 'complete' and local ^P expansions respectively.
1999 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2000 // mode -- Acevedo
2001 if (c == Ctrl_X)
2002 {
2003 if (compl_cont_mode != 0)
2004 compl_cont_status = 0;
2005 else
2006 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2007 }
2008 ctrl_x_mode = CTRL_X_NORMAL;
2009 edit_submode = NULL;
2010 showmode();
2011 break;
2012 }
2013 }
2014 else if (ctrl_x_mode != CTRL_X_NORMAL)
2015 {
2016 // We're already in CTRL-X mode, do we stay in it?
2017 if (!vim_is_ctrl_x_key(c))
2018 {
2019 if (ctrl_x_mode == CTRL_X_SCROLL)
2020 ctrl_x_mode = CTRL_X_NORMAL;
2021 else
2022 ctrl_x_mode = CTRL_X_FINISHED;
2023 edit_submode = NULL;
2024 }
2025 showmode();
2026 }
2027
2028 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
2029 {
2030 // Show error message from attempted keyword completion (probably
2031 // 'Pattern not found') until another key is hit, then go back to
2032 // showing what mode we are in.
2033 showmode();
2034 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
2035 && c != Ctrl_R && !ins_compl_pum_key(c))
2036 || ctrl_x_mode == CTRL_X_FINISHED)
2037 {
2038 // Get here when we have finished typing a sequence of ^N and
2039 // ^P or other completion characters in CTRL-X mode. Free up
2040 // memory that was used, and make sure we can redo the insert.
2041 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2042 {
2043 // If any of the original typed text has been changed, eg when
2044 // ignorecase is set, we must add back-spaces to the redo
2045 // buffer. We add as few as necessary to delete just the part
2046 // of the original text that has changed.
2047 // When using the longest match, edited the match or used
2048 // CTRL-E then don't use the current match.
2049 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2050 ptr = compl_curr_match->cp_str;
2051 else
2052 ptr = NULL;
2053 ins_compl_fixRedoBufForLeader(ptr);
2054 }
2055
2056#ifdef FEAT_CINDENT
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002057 want_cindent = (get_can_cindent() && cindent_on());
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002058#endif
2059 // When completing whole lines: fix indent for 'cindent'.
2060 // Otherwise, break line if it's too long.
2061 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2062 {
2063#ifdef FEAT_CINDENT
2064 // re-indent the current line
2065 if (want_cindent)
2066 {
2067 do_c_expr_indent();
2068 want_cindent = FALSE; // don't do it again
2069 }
2070#endif
2071 }
2072 else
2073 {
2074 int prev_col = curwin->w_cursor.col;
2075
2076 // put the cursor on the last char, for 'tw' formatting
2077 if (prev_col > 0)
2078 dec_cursor();
2079 // only format when something was inserted
2080 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2081 insertchar(NUL, 0, -1);
2082 if (prev_col > 0
2083 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2084 inc_cursor();
2085 }
2086
2087 // If the popup menu is displayed pressing CTRL-Y means accepting
2088 // the selection without inserting anything. When
2089 // compl_enter_selects is set the Enter key does the same.
2090 if ((c == Ctrl_Y || (compl_enter_selects
2091 && (c == CAR || c == K_KENTER || c == NL)))
2092 && pum_visible())
2093 retval = TRUE;
2094
2095 // CTRL-E means completion is Ended, go back to the typed text.
2096 // but only do this, if the Popup is still visible
2097 if (c == Ctrl_E)
2098 {
2099 ins_compl_delete();
2100 if (compl_leader != NULL)
2101 ins_bytes(compl_leader + ins_compl_len());
2102 else if (compl_first_match != NULL)
2103 ins_bytes(compl_orig_text + ins_compl_len());
2104 retval = TRUE;
2105 }
2106
2107 auto_format(FALSE, TRUE);
2108
Bram Moolenaar3f169ce2020-01-26 22:43:31 +01002109 // Trigger the CompleteDonePre event to give scripts a chance to
2110 // act upon the completion before clearing the info, and restore
2111 // ctrl_x_mode, so that complete_info() can be used.
Bram Moolenaarda812e22020-01-26 18:35:31 +01002112 ctrl_x_mode = prev_mode;
Bram Moolenaar3f169ce2020-01-26 22:43:31 +01002113 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
Bram Moolenaar17e04782020-01-17 18:58:59 +01002114
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002115 ins_compl_free();
2116 compl_started = FALSE;
2117 compl_matches = 0;
2118 if (!shortmess(SHM_COMPLETIONMENU))
2119 msg_clr_cmdline(); // necessary for "noshowmode"
2120 ctrl_x_mode = CTRL_X_NORMAL;
2121 compl_enter_selects = FALSE;
2122 if (edit_submode != NULL)
2123 {
2124 edit_submode = NULL;
2125 showmode();
2126 }
2127
2128#ifdef FEAT_CMDWIN
2129 if (c == Ctrl_C && cmdwin_type != 0)
2130 // Avoid the popup menu remains displayed when leaving the
2131 // command line window.
2132 update_screen(0);
2133#endif
2134#ifdef FEAT_CINDENT
2135 // Indent now if a key was typed that is in 'cinkeys'.
2136 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2137 do_c_expr_indent();
2138#endif
Bram Moolenaar3f169ce2020-01-26 22:43:31 +01002139 // Trigger the CompleteDone event to give scripts a chance to act
2140 // upon the end of completion.
2141 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002142 }
2143 }
2144 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2145 // Trigger the CompleteDone event to give scripts a chance to act
2146 // upon the (possibly failed) completion.
2147 ins_apply_autocmds(EVENT_COMPLETEDONE);
2148
2149 // reset continue_* if we left expansion-mode, if we stay they'll be
2150 // (re)set properly in ins_complete()
2151 if (!vim_is_ctrl_x_key(c))
2152 {
2153 compl_cont_status = 0;
2154 compl_cont_mode = 0;
2155 }
2156
2157 return retval;
2158}
2159
2160/*
2161 * Fix the redo buffer for the completion leader replacing some of the typed
2162 * text. This inserts backspaces and appends the changed text.
2163 * "ptr" is the known leader text or NUL.
2164 */
2165 static void
2166ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
2167{
2168 int len;
2169 char_u *p;
2170 char_u *ptr = ptr_arg;
2171
2172 if (ptr == NULL)
2173 {
2174 if (compl_leader != NULL)
2175 ptr = compl_leader;
2176 else
2177 return; // nothing to do
2178 }
2179 if (compl_orig_text != NULL)
2180 {
2181 p = compl_orig_text;
2182 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2183 ;
2184 if (len > 0)
2185 len -= (*mb_head_off)(p, p + len);
2186 for (p += len; *p != NUL; MB_PTR_ADV(p))
2187 AppendCharToRedobuff(K_BS);
2188 }
2189 else
2190 len = 0;
2191 if (ptr != NULL)
2192 AppendToRedobuffLit(ptr + len, -1);
2193}
2194
2195/*
2196 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2197 * (depending on flag) starting from buf and looking for a non-scanned
2198 * buffer (other than curbuf). curbuf is special, if it is called with
2199 * buf=curbuf then it has to be the first call for a given flag/expansion.
2200 *
2201 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2202 */
2203 static buf_T *
2204ins_compl_next_buf(buf_T *buf, int flag)
2205{
2206 static win_T *wp = NULL;
2207
2208 if (flag == 'w') // just windows
2209 {
2210 if (buf == curbuf || wp == NULL) // first call for this flag/expansion
2211 wp = curwin;
2212 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2213 && wp->w_buffer->b_scanned)
2214 ;
2215 buf = wp->w_buffer;
2216 }
2217 else
2218 // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2219 // (unlisted buffers)
2220 // When completing whole lines skip unloaded buffers.
2221 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2222 && ((flag == 'U'
2223 ? buf->b_p_bl
2224 : (!buf->b_p_bl
2225 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2226 || buf->b_scanned))
2227 ;
2228 return buf;
2229}
2230
2231#ifdef FEAT_COMPL_FUNC
2232/*
2233 * Execute user defined complete function 'completefunc' or 'omnifunc', and
2234 * get matches in "matches".
2235 */
2236 static void
2237expand_by_function(
2238 int type, // CTRL_X_OMNI or CTRL_X_FUNCTION
2239 char_u *base)
2240{
2241 list_T *matchlist = NULL;
2242 dict_T *matchdict = NULL;
2243 typval_T args[3];
2244 char_u *funcname;
2245 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002246 typval_T rettv;
2247 int save_State = State;
2248
2249 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2250 if (*funcname == NUL)
2251 return;
2252
2253 // Call 'completefunc' to obtain the list of matches.
2254 args[0].v_type = VAR_NUMBER;
2255 args[0].vval.v_number = 0;
2256 args[1].v_type = VAR_STRING;
2257 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
2258 args[2].v_type = VAR_UNKNOWN;
2259
2260 pos = curwin->w_cursor;
Bram Moolenaar28976e22021-01-29 21:07:07 +01002261 // Lock the text to avoid weird things from happening. Also disallow
2262 // switching to another window, it should not be needed and may end up in
2263 // Insert mode in another buffer.
2264 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002265
2266 // Call a function, which returns a list or dict.
2267 if (call_vim_function(funcname, 2, args, &rettv) == OK)
2268 {
2269 switch (rettv.v_type)
2270 {
2271 case VAR_LIST:
2272 matchlist = rettv.vval.v_list;
2273 break;
2274 case VAR_DICT:
2275 matchdict = rettv.vval.v_dict;
2276 break;
2277 case VAR_SPECIAL:
2278 if (rettv.vval.v_number == VVAL_NONE)
2279 compl_opt_suppress_empty = TRUE;
2280 // FALLTHROUGH
2281 default:
2282 // TODO: Give error message?
2283 clear_tv(&rettv);
2284 break;
2285 }
2286 }
Bram Moolenaar28976e22021-01-29 21:07:07 +01002287 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002288
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002289 curwin->w_cursor = pos; // restore the cursor position
2290 validate_cursor();
2291 if (!EQUAL_POS(curwin->w_cursor, pos))
2292 {
2293 emsg(_(e_compldel));
2294 goto theend;
2295 }
2296
2297 if (matchlist != NULL)
2298 ins_compl_add_list(matchlist);
2299 else if (matchdict != NULL)
2300 ins_compl_add_dict(matchdict);
2301
2302theend:
2303 // Restore State, it might have been changed.
2304 State = save_State;
2305
2306 if (matchdict != NULL)
2307 dict_unref(matchdict);
2308 if (matchlist != NULL)
2309 list_unref(matchlist);
2310}
2311#endif // FEAT_COMPL_FUNC
2312
2313#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2314/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002315 * Add a match to the list of matches from a typeval_T.
2316 * If the given string is already in the list of completions, then return
2317 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2318 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar440cf092021-04-03 20:13:30 +02002319 * When "fast" is TRUE use fast_breakcheck() instead of ui_breakcheck().
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002320 */
2321 static int
Bram Moolenaar440cf092021-04-03 20:13:30 +02002322ins_compl_add_tv(typval_T *tv, int dir, int fast)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002323{
2324 char_u *word;
2325 int dup = FALSE;
2326 int empty = FALSE;
Bram Moolenaar440cf092021-04-03 20:13:30 +02002327 int flags = fast ? CP_FAST : 0;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002328 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002329 typval_T user_data;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002330
Bram Moolenaar08928322020-01-04 14:32:48 +01002331 user_data.v_type = VAR_UNKNOWN;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002332 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
2333 {
2334 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
2335 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
2336 (char_u *)"abbr", FALSE);
2337 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
2338 (char_u *)"menu", FALSE);
2339 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
2340 (char_u *)"kind", FALSE);
2341 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
2342 (char_u *)"info", FALSE);
Bram Moolenaar08928322020-01-04 14:32:48 +01002343 dict_get_tv(tv->vval.v_dict, (char_u *)"user_data", &user_data);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002344 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL
2345 && dict_get_number(tv->vval.v_dict, (char_u *)"icase"))
2346 flags |= CP_ICASE;
2347 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
2348 dup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
2349 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
2350 empty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
2351 if (dict_get_string(tv->vval.v_dict, (char_u *)"equal", FALSE) != NULL
2352 && dict_get_number(tv->vval.v_dict, (char_u *)"equal"))
2353 flags |= CP_EQUAL;
2354 }
2355 else
2356 {
2357 word = tv_get_string_chk(tv);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002358 CLEAR_FIELD(cptext);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002359 }
2360 if (word == NULL || (!empty && *word == NUL))
2361 return FAIL;
Bram Moolenaar08928322020-01-04 14:32:48 +01002362 return ins_compl_add(word, -1, NULL, cptext, &user_data, dir, flags, dup);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002363}
2364
2365/*
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002366 * Add completions from a list.
2367 */
2368 static void
2369ins_compl_add_list(list_T *list)
2370{
2371 listitem_T *li;
2372 int dir = compl_direction;
2373
2374 // Go through the List with matches and add each of them.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002375 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002376 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002377 {
Bram Moolenaar440cf092021-04-03 20:13:30 +02002378 if (ins_compl_add_tv(&li->li_tv, dir, TRUE) == OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002379 // if dir was BACKWARD then honor it just once
2380 dir = FORWARD;
2381 else if (did_emsg)
2382 break;
2383 }
2384}
2385
2386/*
2387 * Add completions from a dict.
2388 */
2389 static void
2390ins_compl_add_dict(dict_T *dict)
2391{
2392 dictitem_T *di_refresh;
2393 dictitem_T *di_words;
2394
2395 // Check for optional "refresh" item.
2396 compl_opt_refresh_always = FALSE;
2397 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
2398 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
2399 {
2400 char_u *v = di_refresh->di_tv.vval.v_string;
2401
2402 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
2403 compl_opt_refresh_always = TRUE;
2404 }
2405
2406 // Add completions from a "words" list.
2407 di_words = dict_find(dict, (char_u *)"words", 5);
2408 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
2409 ins_compl_add_list(di_words->di_tv.vval.v_list);
2410}
2411
2412/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002413 * Start completion for the complete() function.
2414 * "startcol" is where the matched text starts (1 is first column).
2415 * "list" is the list of matches.
2416 */
2417 static void
2418set_completion(colnr_T startcol, list_T *list)
2419{
2420 int save_w_wrow = curwin->w_wrow;
2421 int save_w_leftcol = curwin->w_leftcol;
2422 int flags = CP_ORIGINAL_TEXT;
2423
2424 // If already doing completions stop it.
2425 if (ctrl_x_mode != CTRL_X_NORMAL)
2426 ins_compl_prep(' ');
2427 ins_compl_clear();
2428 ins_compl_free();
2429
2430 compl_direction = FORWARD;
2431 if (startcol > curwin->w_cursor.col)
2432 startcol = curwin->w_cursor.col;
2433 compl_col = startcol;
2434 compl_length = (int)curwin->w_cursor.col - (int)startcol;
2435 // compl_pattern doesn't need to be set
2436 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2437 if (p_ic)
2438 flags |= CP_ICASE;
2439 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar440cf092021-04-03 20:13:30 +02002440 -1, NULL, NULL, NULL, 0,
2441 flags | CP_FAST, FALSE) != OK)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002442 return;
2443
2444 ctrl_x_mode = CTRL_X_EVAL;
2445
2446 ins_compl_add_list(list);
2447 compl_matches = ins_compl_make_cyclic();
2448 compl_started = TRUE;
2449 compl_used_match = TRUE;
2450 compl_cont_status = 0;
2451
2452 compl_curr_match = compl_first_match;
2453 if (compl_no_insert || compl_no_select)
2454 {
2455 ins_complete(K_DOWN, FALSE);
2456 if (compl_no_select)
2457 // Down/Up has no real effect.
2458 ins_complete(K_UP, FALSE);
2459 }
2460 else
2461 ins_complete(Ctrl_N, FALSE);
2462 compl_enter_selects = compl_no_insert;
2463
2464 // Lazily show the popup menu, unless we got interrupted.
2465 if (!compl_interrupted)
2466 show_pum(save_w_wrow, save_w_leftcol);
2467 out_flush();
2468}
2469
2470/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002471 * "complete()" function
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002472 */
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002473 void
2474f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002475{
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002476 int startcol;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002477 int save_textlock = textlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002478
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002479 if (in_vim9script()
2480 && (check_for_number_arg(argvars, 0) == FAIL
2481 || check_for_list_arg(argvars, 1) == FAIL))
2482 return;
2483
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002484 if ((State & INSERT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002485 {
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002486 emsg(_("E785: complete() can only be used in Insert mode"));
2487 return;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002488 }
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002489
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002490 // "textlock" is set when evaluating 'completefunc' but we can change
2491 // text here.
Bram Moolenaarff06f282020-04-21 22:01:14 +02002492 textlock = 0;
2493
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002494 // Check for undo allowed here, because if something was already inserted
2495 // the line was already saved for undo and this check isn't done.
2496 if (!undo_allowed())
2497 return;
2498
2499 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002500 emsg(_(e_invarg));
Bram Moolenaarff06f282020-04-21 22:01:14 +02002501 else
2502 {
2503 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
2504 if (startcol > 0)
2505 set_completion(startcol - 1, argvars[1].vval.v_list);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002506 }
Bram Moolenaarff06f282020-04-21 22:01:14 +02002507 textlock = save_textlock;
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002508}
2509
2510/*
2511 * "complete_add()" function
2512 */
2513 void
2514f_complete_add(typval_T *argvars, typval_T *rettv)
2515{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002516 if (in_vim9script() && check_for_string_or_dict_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002517 return;
2518
Bram Moolenaar440cf092021-04-03 20:13:30 +02002519 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, FALSE);
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002520}
2521
2522/*
2523 * "complete_check()" function
2524 */
2525 void
2526f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2527{
2528 int saved = RedrawingDisabled;
2529
2530 RedrawingDisabled = 0;
2531 ins_compl_check_keys(0, TRUE);
2532 rettv->vval.v_number = ins_compl_interrupted();
2533 RedrawingDisabled = saved;
2534}
2535
2536/*
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002537 * Return Insert completion mode name string
2538 */
2539 static char_u *
2540ins_compl_mode(void)
2541{
zeertzjq27fef592021-10-03 12:01:27 +01002542 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || ctrl_x_mode == CTRL_X_SCROLL
2543 || compl_started)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002544 return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
2545
2546 return (char_u *)"";
2547}
2548
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002549 static void
2550ins_compl_update_sequence_numbers()
2551{
2552 int number = 0;
2553 compl_T *match;
2554
2555 if (compl_direction == FORWARD)
2556 {
2557 // search backwards for the first valid (!= -1) number.
2558 // This should normally succeed already at the first loop
2559 // cycle, so it's fast!
2560 for (match = compl_curr_match->cp_prev; match != NULL
2561 && match != compl_first_match;
2562 match = match->cp_prev)
2563 if (match->cp_number != -1)
2564 {
2565 number = match->cp_number;
2566 break;
2567 }
2568 if (match != NULL)
2569 // go up and assign all numbers which are not assigned
2570 // yet
2571 for (match = match->cp_next;
2572 match != NULL && match->cp_number == -1;
2573 match = match->cp_next)
2574 match->cp_number = ++number;
2575 }
2576 else // BACKWARD
2577 {
2578 // search forwards (upwards) for the first valid (!= -1)
2579 // number. This should normally succeed already at the
2580 // first loop cycle, so it's fast!
2581 for (match = compl_curr_match->cp_next; match != NULL
2582 && match != compl_first_match;
2583 match = match->cp_next)
2584 if (match->cp_number != -1)
2585 {
2586 number = match->cp_number;
2587 break;
2588 }
2589 if (match != NULL)
2590 // go down and assign all numbers which are not
2591 // assigned yet
2592 for (match = match->cp_prev; match
2593 && match->cp_number == -1;
2594 match = match->cp_prev)
2595 match->cp_number = ++number;
2596 }
2597}
2598
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002599/*
2600 * Get complete information
2601 */
2602 static void
2603get_complete_info(list_T *what_list, dict_T *retdict)
2604{
2605 int ret = OK;
2606 listitem_T *item;
2607#define CI_WHAT_MODE 0x01
2608#define CI_WHAT_PUM_VISIBLE 0x02
2609#define CI_WHAT_ITEMS 0x04
2610#define CI_WHAT_SELECTED 0x08
2611#define CI_WHAT_INSERTED 0x10
2612#define CI_WHAT_ALL 0xff
2613 int what_flag;
2614
2615 if (what_list == NULL)
2616 what_flag = CI_WHAT_ALL;
2617 else
2618 {
2619 what_flag = 0;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002620 CHECK_LIST_MATERIALIZE(what_list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002621 FOR_ALL_LIST_ITEMS(what_list, item)
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002622 {
2623 char_u *what = tv_get_string(&item->li_tv);
2624
2625 if (STRCMP(what, "mode") == 0)
2626 what_flag |= CI_WHAT_MODE;
2627 else if (STRCMP(what, "pum_visible") == 0)
2628 what_flag |= CI_WHAT_PUM_VISIBLE;
2629 else if (STRCMP(what, "items") == 0)
2630 what_flag |= CI_WHAT_ITEMS;
2631 else if (STRCMP(what, "selected") == 0)
2632 what_flag |= CI_WHAT_SELECTED;
2633 else if (STRCMP(what, "inserted") == 0)
2634 what_flag |= CI_WHAT_INSERTED;
2635 }
2636 }
2637
2638 if (ret == OK && (what_flag & CI_WHAT_MODE))
2639 ret = dict_add_string(retdict, "mode", ins_compl_mode());
2640
2641 if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
2642 ret = dict_add_number(retdict, "pum_visible", pum_visible());
2643
2644 if (ret == OK && (what_flag & CI_WHAT_ITEMS))
2645 {
2646 list_T *li;
2647 dict_T *di;
2648 compl_T *match;
2649
2650 li = list_alloc();
2651 if (li == NULL)
2652 return;
2653 ret = dict_add_list(retdict, "items", li);
2654 if (ret == OK && compl_first_match != NULL)
2655 {
2656 match = compl_first_match;
2657 do
2658 {
2659 if (!(match->cp_flags & CP_ORIGINAL_TEXT))
2660 {
2661 di = dict_alloc();
2662 if (di == NULL)
2663 return;
2664 ret = list_append_dict(li, di);
2665 if (ret != OK)
2666 return;
2667 dict_add_string(di, "word", match->cp_str);
2668 dict_add_string(di, "abbr", match->cp_text[CPT_ABBR]);
2669 dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
2670 dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
2671 dict_add_string(di, "info", match->cp_text[CPT_INFO]);
Bram Moolenaar08928322020-01-04 14:32:48 +01002672 if (match->cp_user_data.v_type == VAR_UNKNOWN)
2673 // Add an empty string for backwards compatibility
2674 dict_add_string(di, "user_data", (char_u *)"");
2675 else
2676 dict_add_tv(di, "user_data", &match->cp_user_data);
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002677 }
2678 match = match->cp_next;
2679 }
2680 while (match != NULL && match != compl_first_match);
2681 }
2682 }
2683
2684 if (ret == OK && (what_flag & CI_WHAT_SELECTED))
Bram Moolenaarf9d51352020-10-26 19:22:42 +01002685 {
2686 if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
2687 ins_compl_update_sequence_numbers();
2688 ret = dict_add_number(retdict, "selected", compl_curr_match != NULL
2689 ? compl_curr_match->cp_number - 1 : -1);
2690 }
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02002691
2692 // TODO
2693 // if (ret == OK && (what_flag & CI_WHAT_INSERTED))
2694}
2695
2696/*
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002697 * "complete_info()" function
2698 */
2699 void
2700f_complete_info(typval_T *argvars, typval_T *rettv)
2701{
2702 list_T *what_list = NULL;
2703
2704 if (rettv_dict_alloc(rettv) != OK)
2705 return;
2706
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002707 if (in_vim9script() && check_for_opt_list_arg(argvars, 0) == FAIL)
2708 return;
2709
Bram Moolenaar9bca58f2019-08-15 21:31:52 +02002710 if (argvars[0].v_type != VAR_UNKNOWN)
2711 {
2712 if (argvars[0].v_type != VAR_LIST)
2713 {
2714 emsg(_(e_listreq));
2715 return;
2716 }
2717 what_list = argvars[0].vval.v_list;
2718 }
2719 get_complete_info(what_list, rettv->vval.v_dict);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002720}
2721#endif
2722
2723/*
2724 * Get the next expansion(s), using "compl_pattern".
2725 * The search starts at position "ini" in curbuf and in the direction
2726 * compl_direction.
2727 * When "compl_started" is FALSE start at that position, otherwise continue
2728 * where we stopped searching before.
2729 * This may return before finding all the matches.
2730 * Return the total number of matches or -1 if still unknown -- Acevedo
2731 */
2732 static int
2733ins_compl_get_exp(pos_T *ini)
2734{
2735 static pos_T first_match_pos;
2736 static pos_T last_match_pos;
2737 static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
2738 static int found_all = FALSE; // Found all matches of a
2739 // certain type.
2740 static buf_T *ins_buf = NULL; // buffer being scanned
2741
2742 pos_T *pos;
2743 char_u **matches;
2744 int save_p_scs;
2745 int save_p_ws;
2746 int save_p_ic;
2747 int i;
2748 int num_matches;
2749 int len;
2750 int found_new_match;
2751 int type = ctrl_x_mode;
2752 char_u *ptr;
2753 char_u *dict = NULL;
2754 int dict_f = 0;
2755 int set_match_pos;
Andy Gozas6a230c62021-08-05 16:23:27 +02002756 pos_T prev_pos = {0, 0, 0};
2757 int looped_around = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002758
2759 if (!compl_started)
2760 {
2761 FOR_ALL_BUFFERS(ins_buf)
2762 ins_buf->b_scanned = 0;
2763 found_all = FALSE;
2764 ins_buf = curbuf;
2765 e_cpt = (compl_cont_status & CONT_LOCAL)
2766 ? (char_u *)"." : curbuf->b_p_cpt;
2767 last_match_pos = first_match_pos = *ini;
2768 }
2769 else if (ins_buf != curbuf && !buf_valid(ins_buf))
2770 ins_buf = curbuf; // In case the buffer was wiped out.
2771
2772 compl_old_match = compl_curr_match; // remember the last current match
2773 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
2774
2775 // For ^N/^P loop over all the flags/windows/buffers in 'complete'.
2776 for (;;)
2777 {
2778 found_new_match = FAIL;
2779 set_match_pos = FALSE;
2780
2781 // For ^N/^P pick a new entry from e_cpt if compl_started is off,
2782 // or if found_all says this entry is done. For ^X^L only use the
2783 // entries from 'complete' that look in loaded buffers.
2784 if ((ctrl_x_mode == CTRL_X_NORMAL
2785 || ctrl_x_mode_line_or_eval())
2786 && (!compl_started || found_all))
2787 {
2788 found_all = FALSE;
2789 while (*e_cpt == ',' || *e_cpt == ' ')
2790 e_cpt++;
2791 if (*e_cpt == '.' && !curbuf->b_scanned)
2792 {
2793 ins_buf = curbuf;
2794 first_match_pos = *ini;
2795 // Move the cursor back one character so that ^N can match the
2796 // word immediately after the cursor.
2797 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
2798 {
2799 // Move the cursor to after the last character in the
2800 // buffer, so that word at start of buffer is found
2801 // correctly.
2802 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
2803 first_match_pos.col =
2804 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
2805 }
2806 last_match_pos = first_match_pos;
2807 type = 0;
2808
2809 // Remember the first match so that the loop stops when we
2810 // wrap and come back there a second time.
2811 set_match_pos = TRUE;
2812 }
2813 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2814 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2815 {
2816 // Scan a buffer, but not the current one.
2817 if (ins_buf->b_ml.ml_mfp != NULL) // loaded buffer
2818 {
2819 compl_started = TRUE;
2820 first_match_pos.col = last_match_pos.col = 0;
2821 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2822 last_match_pos.lnum = 0;
2823 type = 0;
2824 }
2825 else // unloaded buffer, scan like dictionary
2826 {
2827 found_all = TRUE;
2828 if (ins_buf->b_fname == NULL)
2829 continue;
2830 type = CTRL_X_DICTIONARY;
2831 dict = ins_buf->b_fname;
2832 dict_f = DICT_EXACT;
2833 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01002834 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002835 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
2836 ins_buf->b_fname == NULL
2837 ? buf_spname(ins_buf)
2838 : ins_buf->b_sfname == NULL
2839 ? ins_buf->b_fname
2840 : ins_buf->b_sfname);
2841 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2842 }
2843 else if (*e_cpt == NUL)
2844 break;
2845 else
2846 {
2847 if (ctrl_x_mode_line_or_eval())
2848 type = -1;
2849 else if (*e_cpt == 'k' || *e_cpt == 's')
2850 {
2851 if (*e_cpt == 'k')
2852 type = CTRL_X_DICTIONARY;
2853 else
2854 type = CTRL_X_THESAURUS;
2855 if (*++e_cpt != ',' && *e_cpt != NUL)
2856 {
2857 dict = e_cpt;
2858 dict_f = DICT_FIRST;
2859 }
2860 }
2861#ifdef FEAT_FIND_ID
2862 else if (*e_cpt == 'i')
2863 type = CTRL_X_PATH_PATTERNS;
2864 else if (*e_cpt == 'd')
2865 type = CTRL_X_PATH_DEFINES;
2866#endif
2867 else if (*e_cpt == ']' || *e_cpt == 't')
2868 {
Bram Moolenaarcc233582020-12-12 13:32:07 +01002869 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002870 type = CTRL_X_TAGS;
2871 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
2872 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
2873 }
2874 else
2875 type = -1;
2876
2877 // in any case e_cpt is advanced to the next entry
2878 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2879
2880 found_all = TRUE;
2881 if (type == -1)
2882 continue;
2883 }
2884 }
2885
2886 // If complete() was called then compl_pattern has been reset. The
2887 // following won't work then, bail out.
2888 if (compl_pattern == NULL)
2889 break;
2890
2891 switch (type)
2892 {
2893 case -1:
2894 break;
2895#ifdef FEAT_FIND_ID
2896 case CTRL_X_PATH_PATTERNS:
2897 case CTRL_X_PATH_DEFINES:
2898 find_pattern_in_path(compl_pattern, compl_direction,
2899 (int)STRLEN(compl_pattern), FALSE, FALSE,
2900 (type == CTRL_X_PATH_DEFINES
2901 && !(compl_cont_status & CONT_SOL))
2902 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2903 (linenr_T)1, (linenr_T)MAXLNUM);
2904 break;
2905#endif
2906
2907 case CTRL_X_DICTIONARY:
2908 case CTRL_X_THESAURUS:
2909 ins_compl_dictionaries(
2910 dict != NULL ? dict
2911 : (type == CTRL_X_THESAURUS
2912 ? (*curbuf->b_p_tsr == NUL
2913 ? p_tsr
2914 : curbuf->b_p_tsr)
2915 : (*curbuf->b_p_dict == NUL
2916 ? p_dict
2917 : curbuf->b_p_dict)),
2918 compl_pattern,
2919 dict != NULL ? dict_f
2920 : 0, type == CTRL_X_THESAURUS);
2921 dict = NULL;
2922 break;
2923
2924 case CTRL_X_TAGS:
2925 // set p_ic according to p_ic, p_scs and pat for find_tags().
2926 save_p_ic = p_ic;
2927 p_ic = ignorecase(compl_pattern);
2928
2929 // Find up to TAG_MANY matches. Avoids that an enormous number
2930 // of matches is found when compl_pattern is empty
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002931 g_tag_at_cursor = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002932 if (find_tags(compl_pattern, &num_matches, &matches,
2933 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
2934 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
2935 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002936 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002937 g_tag_at_cursor = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002938 p_ic = save_p_ic;
2939 break;
2940
2941 case CTRL_X_FILES:
2942 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
2943 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2944 {
2945
2946 // May change home directory back to "~".
2947 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaarac3150d2019-07-28 16:36:39 +02002948#ifdef BACKSLASH_IN_FILENAME
2949 if (curbuf->b_p_csl[0] != NUL)
2950 {
2951 int i;
2952
2953 for (i = 0; i < num_matches; ++i)
2954 {
2955 char_u *ptr = matches[i];
2956
2957 while (*ptr != NUL)
2958 {
2959 if (curbuf->b_p_csl[0] == 's' && *ptr == '\\')
2960 *ptr = '/';
2961 else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/')
2962 *ptr = '\\';
2963 ptr += (*mb_ptr2len)(ptr);
2964 }
2965 }
2966 }
2967#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002968 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
2969 }
2970 break;
2971
2972 case CTRL_X_CMDLINE:
zeertzjqdca29d92021-08-31 19:12:51 +02002973 case CTRL_X_CMDLINE_CTRL_X:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002974 if (expand_cmdline(&compl_xp, compl_pattern,
2975 (int)STRLEN(compl_pattern),
2976 &num_matches, &matches) == EXPAND_OK)
2977 ins_compl_add_matches(num_matches, matches, FALSE);
2978 break;
2979
2980#ifdef FEAT_COMPL_FUNC
2981 case CTRL_X_FUNCTION:
2982 case CTRL_X_OMNI:
2983 expand_by_function(type, compl_pattern);
2984 break;
2985#endif
2986
2987 case CTRL_X_SPELL:
2988#ifdef FEAT_SPELL
2989 num_matches = expand_spelling(first_match_pos.lnum,
2990 compl_pattern, &matches);
2991 if (num_matches > 0)
2992 ins_compl_add_matches(num_matches, matches, p_ic);
2993#endif
2994 break;
2995
2996 default: // normal ^P/^N and ^X^L
2997 // If 'infercase' is set, don't use 'smartcase' here
2998 save_p_scs = p_scs;
2999 if (ins_buf->b_p_inf)
3000 p_scs = FALSE;
3001
3002 // Buffers other than curbuf are scanned from the beginning or the
3003 // end but never from the middle, thus setting nowrapscan in this
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003004 // buffer is a good idea, on the other hand, we always set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003005 // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
3006 save_p_ws = p_ws;
3007 if (ins_buf != curbuf)
3008 p_ws = FALSE;
3009 else if (*e_cpt == '.')
3010 p_ws = TRUE;
Andy Gozas6a230c62021-08-05 16:23:27 +02003011 looped_around = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003012 for (;;)
3013 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003014 int cont_s_ipos = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003015
3016 ++msg_silent; // Don't want messages for wrapscan.
3017
3018 // ctrl_x_mode_line_or_eval() || word-wise search that
3019 // has added a word that was at the beginning of the line
3020 if (ctrl_x_mode_line_or_eval()
3021 || (compl_cont_status & CONT_SOL))
3022 found_new_match = search_for_exact_line(ins_buf, pos,
3023 compl_direction, compl_pattern);
3024 else
3025 found_new_match = searchit(NULL, ins_buf, pos, NULL,
3026 compl_direction,
3027 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003028 RE_LAST, NULL);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003029 --msg_silent;
3030 if (!compl_started || set_match_pos)
3031 {
3032 // set "compl_started" even on fail
3033 compl_started = TRUE;
3034 first_match_pos = *pos;
3035 last_match_pos = *pos;
3036 set_match_pos = FALSE;
3037 }
3038 else if (first_match_pos.lnum == last_match_pos.lnum
Andy Gozas6a230c62021-08-05 16:23:27 +02003039 && first_match_pos.col == last_match_pos.col)
3040 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003041 found_new_match = FAIL;
Andy Gozas6a230c62021-08-05 16:23:27 +02003042 }
3043 else if ((compl_direction == FORWARD)
3044 && (prev_pos.lnum > pos->lnum
3045 || (prev_pos.lnum == pos->lnum
3046 && prev_pos.col >= pos->col)))
3047 {
3048 if (looped_around)
3049 found_new_match = FAIL;
3050 else
3051 looped_around = TRUE;
3052 }
3053 else if ((compl_direction != FORWARD)
3054 && (prev_pos.lnum < pos->lnum
3055 || (prev_pos.lnum == pos->lnum
3056 && prev_pos.col <= pos->col)))
3057 {
3058 if (looped_around)
3059 found_new_match = FAIL;
3060 else
3061 looped_around = TRUE;
3062 }
3063 prev_pos = *pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003064 if (found_new_match == FAIL)
3065 {
3066 if (ins_buf == curbuf)
3067 found_all = TRUE;
3068 break;
3069 }
3070
3071 // when ADDING, the text before the cursor matches, skip it
3072 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
3073 && ini->lnum == pos->lnum
3074 && ini->col == pos->col)
3075 continue;
3076 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3077 if (ctrl_x_mode_line_or_eval())
3078 {
3079 if (compl_cont_status & CONT_ADDING)
3080 {
3081 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3082 continue;
3083 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3084 if (!p_paste)
3085 ptr = skipwhite(ptr);
3086 }
3087 len = (int)STRLEN(ptr);
3088 }
3089 else
3090 {
3091 char_u *tmp_ptr = ptr;
3092
3093 if (compl_cont_status & CONT_ADDING)
3094 {
3095 tmp_ptr += compl_length;
3096 // Skip if already inside a word.
3097 if (vim_iswordp(tmp_ptr))
3098 continue;
3099 // Find start of next word.
3100 tmp_ptr = find_word_start(tmp_ptr);
3101 }
3102 // Find end of this word.
3103 tmp_ptr = find_word_end(tmp_ptr);
3104 len = (int)(tmp_ptr - ptr);
3105
3106 if ((compl_cont_status & CONT_ADDING)
3107 && len == compl_length)
3108 {
3109 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3110 {
3111 // Try next line, if any. the new word will be
3112 // "join" as if the normal command "J" was used.
3113 // IOSIZE is always greater than
3114 // compl_length, so the next STRNCPY always
3115 // works -- Acevedo
3116 STRNCPY(IObuff, ptr, len);
3117 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3118 tmp_ptr = ptr = skipwhite(ptr);
3119 // Find start of next word.
3120 tmp_ptr = find_word_start(tmp_ptr);
3121 // Find end of next word.
3122 tmp_ptr = find_word_end(tmp_ptr);
3123 if (tmp_ptr > ptr)
3124 {
3125 if (*ptr != ')' && IObuff[len - 1] != TAB)
3126 {
3127 if (IObuff[len - 1] != ' ')
3128 IObuff[len++] = ' ';
3129 // IObuf =~ "\k.* ", thus len >= 2
3130 if (p_js
3131 && (IObuff[len - 2] == '.'
3132 || (vim_strchr(p_cpo, CPO_JOINSP)
3133 == NULL
3134 && (IObuff[len - 2] == '?'
3135 || IObuff[len - 2] == '!'))))
3136 IObuff[len++] = ' ';
3137 }
3138 // copy as much as possible of the new word
3139 if (tmp_ptr - ptr >= IOSIZE - len)
3140 tmp_ptr = ptr + IOSIZE - len - 1;
3141 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3142 len += (int)(tmp_ptr - ptr);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003143 cont_s_ipos = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003144 }
3145 IObuff[len] = NUL;
3146 ptr = IObuff;
3147 }
3148 if (len == compl_length)
3149 continue;
3150 }
3151 }
3152 if (ins_compl_add_infercase(ptr, len, p_ic,
3153 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003154 0, cont_s_ipos) != NOTDONE)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003155 {
3156 found_new_match = OK;
3157 break;
3158 }
3159 }
3160 p_scs = save_p_scs;
3161 p_ws = save_p_ws;
3162 }
3163
3164 // check if compl_curr_match has changed, (e.g. other type of
3165 // expansion added something)
3166 if (type != 0 && compl_curr_match != compl_old_match)
3167 found_new_match = OK;
3168
3169 // break the loop for specialized modes (use 'complete' just for the
3170 // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
3171 // match
3172 if ((ctrl_x_mode != CTRL_X_NORMAL
3173 && !ctrl_x_mode_line_or_eval()) || found_new_match != FAIL)
3174 {
3175 if (got_int)
3176 break;
3177 // Fill the popup menu as soon as possible.
3178 if (type != -1)
3179 ins_compl_check_keys(0, FALSE);
3180
3181 if ((ctrl_x_mode != CTRL_X_NORMAL
3182 && !ctrl_x_mode_line_or_eval()) || compl_interrupted)
3183 break;
3184 compl_started = TRUE;
3185 }
3186 else
3187 {
3188 // Mark a buffer scanned when it has been scanned completely
3189 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3190 ins_buf->b_scanned = TRUE;
3191
3192 compl_started = FALSE;
3193 }
3194 }
3195 compl_started = TRUE;
3196
3197 if ((ctrl_x_mode == CTRL_X_NORMAL || ctrl_x_mode_line_or_eval())
3198 && *e_cpt == NUL) // Got to end of 'complete'
3199 found_new_match = FAIL;
3200
3201 i = -1; // total of matches, unknown
3202 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
3203 && !ctrl_x_mode_line_or_eval()))
3204 i = ins_compl_make_cyclic();
3205
3206 if (compl_old_match != NULL)
3207 {
3208 // If several matches were added (FORWARD) or the search failed and has
3209 // just been made cyclic then we have to move compl_curr_match to the
3210 // next or previous entry (if any) -- Acevedo
3211 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
3212 : compl_old_match->cp_prev;
3213 if (compl_curr_match == NULL)
3214 compl_curr_match = compl_old_match;
3215 }
3216 return i;
3217}
3218
3219/*
3220 * Delete the old text being completed.
3221 */
3222 void
3223ins_compl_delete(void)
3224{
3225 int col;
3226
3227 // In insert mode: Delete the typed part.
3228 // In replace mode: Put the old characters back, if any.
3229 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
3230 if ((int)curwin->w_cursor.col > col)
3231 {
3232 if (stop_arrow() == FAIL)
3233 return;
3234 backspace_until_column(col);
3235 }
3236
3237 // TODO: is this sufficient for redrawing? Redrawing everything causes
3238 // flicker, thus we can't do that.
3239 changed_cline_bef_curs();
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003240#ifdef FEAT_EVAL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003241 // clear v:completed_item
3242 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003243#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003244}
3245
3246/*
3247 * Insert the new text being completed.
3248 * "in_compl_func" is TRUE when called from complete_check().
3249 */
3250 void
3251ins_compl_insert(int in_compl_func)
3252{
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003253 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003254 if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003255 compl_used_match = FALSE;
3256 else
3257 compl_used_match = TRUE;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003258#ifdef FEAT_EVAL
3259 {
3260 dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
3261
3262 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
3263 }
3264#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003265 if (!in_compl_func)
3266 compl_curr_match = compl_shown_match;
3267}
3268
3269/*
3270 * Fill in the next completion in the current direction.
3271 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3272 * get more completions. If it is FALSE, then we just do nothing when there
3273 * are no more completions in a given direction. The latter case is used when
3274 * we are still in the middle of finding completions, to allow browsing
3275 * through the ones found so far.
3276 * Return the total number of matches, or -1 if still unknown -- webb.
3277 *
3278 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3279 * compl_shown_match here.
3280 *
3281 * Note that this function may be called recursively once only. First with
3282 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3283 * calls this function with "allow_get_expansion" FALSE.
3284 */
3285 static int
3286ins_compl_next(
3287 int allow_get_expansion,
3288 int count, // repeat completion this many times; should
3289 // be at least 1
3290 int insert_match, // Insert the newly selected match
3291 int in_compl_func) // called from complete_check()
3292{
3293 int num_matches = -1;
3294 int todo = count;
3295 compl_T *found_compl = NULL;
3296 int found_end = FALSE;
3297 int advance;
3298 int started = compl_started;
3299
3300 // When user complete function return -1 for findstart which is next
3301 // time of 'always', compl_shown_match become NULL.
3302 if (compl_shown_match == NULL)
3303 return -1;
3304
3305 if (compl_leader != NULL
Bram Moolenaar28976e22021-01-29 21:07:07 +01003306 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003307 {
3308 // Set "compl_shown_match" to the actually shown match, it may differ
3309 // when "compl_leader" is used to omit some of the matches.
3310 while (!ins_compl_equal(compl_shown_match,
3311 compl_leader, (int)STRLEN(compl_leader))
3312 && compl_shown_match->cp_next != NULL
3313 && compl_shown_match->cp_next != compl_first_match)
3314 compl_shown_match = compl_shown_match->cp_next;
3315
3316 // If we didn't find it searching forward, and compl_shows_dir is
3317 // backward, find the last match.
3318 if (compl_shows_dir == BACKWARD
3319 && !ins_compl_equal(compl_shown_match,
3320 compl_leader, (int)STRLEN(compl_leader))
3321 && (compl_shown_match->cp_next == NULL
3322 || compl_shown_match->cp_next == compl_first_match))
3323 {
3324 while (!ins_compl_equal(compl_shown_match,
3325 compl_leader, (int)STRLEN(compl_leader))
3326 && compl_shown_match->cp_prev != NULL
3327 && compl_shown_match->cp_prev != compl_first_match)
3328 compl_shown_match = compl_shown_match->cp_prev;
3329 }
3330 }
3331
3332 if (allow_get_expansion && insert_match
3333 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3334 // Delete old text to be replaced
3335 ins_compl_delete();
3336
3337 // When finding the longest common text we stick at the original text,
3338 // don't let CTRL-N or CTRL-P move to the first match.
3339 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3340
3341 // When restarting the search don't insert the first match either.
3342 if (compl_restarting)
3343 {
3344 advance = FALSE;
3345 compl_restarting = FALSE;
3346 }
3347
3348 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3349 // around.
3350 while (--todo >= 0)
3351 {
3352 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3353 {
3354 compl_shown_match = compl_shown_match->cp_next;
3355 found_end = (compl_first_match != NULL
3356 && (compl_shown_match->cp_next == compl_first_match
3357 || compl_shown_match == compl_first_match));
3358 }
3359 else if (compl_shows_dir == BACKWARD
3360 && compl_shown_match->cp_prev != NULL)
3361 {
3362 found_end = (compl_shown_match == compl_first_match);
3363 compl_shown_match = compl_shown_match->cp_prev;
3364 found_end |= (compl_shown_match == compl_first_match);
3365 }
3366 else
3367 {
3368 if (!allow_get_expansion)
3369 {
3370 if (advance)
3371 {
3372 if (compl_shows_dir == BACKWARD)
3373 compl_pending -= todo + 1;
3374 else
3375 compl_pending += todo + 1;
3376 }
3377 return -1;
3378 }
3379
3380 if (!compl_no_select && advance)
3381 {
3382 if (compl_shows_dir == BACKWARD)
3383 --compl_pending;
3384 else
3385 ++compl_pending;
3386 }
3387
3388 // Find matches.
3389 num_matches = ins_compl_get_exp(&compl_startpos);
3390
3391 // handle any pending completions
3392 while (compl_pending != 0 && compl_direction == compl_shows_dir
3393 && advance)
3394 {
3395 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3396 {
3397 compl_shown_match = compl_shown_match->cp_next;
3398 --compl_pending;
3399 }
3400 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
3401 {
3402 compl_shown_match = compl_shown_match->cp_prev;
3403 ++compl_pending;
3404 }
3405 else
3406 break;
3407 }
3408 found_end = FALSE;
3409 }
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003410 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003411 && compl_leader != NULL
3412 && !ins_compl_equal(compl_shown_match,
3413 compl_leader, (int)STRLEN(compl_leader)))
3414 ++todo;
3415 else
3416 // Remember a matching item.
3417 found_compl = compl_shown_match;
3418
3419 // Stop at the end of the list when we found a usable match.
3420 if (found_end)
3421 {
3422 if (found_compl != NULL)
3423 {
3424 compl_shown_match = found_compl;
3425 break;
3426 }
3427 todo = 1; // use first usable match after wrapping around
3428 }
3429 }
3430
3431 // Insert the text of the new completion, or the compl_leader.
3432 if (compl_no_insert && !started)
3433 {
3434 ins_bytes(compl_orig_text + ins_compl_len());
3435 compl_used_match = FALSE;
3436 }
3437 else if (insert_match)
3438 {
3439 if (!compl_get_longest || compl_used_match)
3440 ins_compl_insert(in_compl_func);
3441 else
3442 ins_bytes(compl_leader + ins_compl_len());
3443 }
3444 else
3445 compl_used_match = FALSE;
3446
3447 if (!allow_get_expansion)
3448 {
3449 // may undisplay the popup menu first
3450 ins_compl_upd_pum();
3451
3452 if (pum_enough_matches())
3453 // Will display the popup menu, don't redraw yet to avoid flicker.
3454 pum_call_update_screen();
3455 else
3456 // Not showing the popup menu yet, redraw to show the user what was
3457 // inserted.
3458 update_screen(0);
3459
3460 // display the updated popup menu
3461 ins_compl_show_pum();
3462#ifdef FEAT_GUI
3463 if (gui.in_use)
3464 {
3465 // Show the cursor after the match, not after the redrawn text.
3466 setcursor();
3467 out_flush_cursor(FALSE, FALSE);
3468 }
3469#endif
3470
3471 // Delete old text to be replaced, since we're still searching and
3472 // don't want to match ourselves!
3473 ins_compl_delete();
3474 }
3475
3476 // Enter will select a match when the match wasn't inserted and the popup
3477 // menu is visible.
3478 if (compl_no_insert && !started)
3479 compl_enter_selects = TRUE;
3480 else
3481 compl_enter_selects = !insert_match && compl_match_array != NULL;
3482
3483 // Show the file name for the match (if any)
3484 // Truncate the file name to avoid a wait for return.
3485 if (compl_shown_match->cp_fname != NULL)
3486 {
3487 char *lead = _("match in file");
3488 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3489 char_u *s;
3490 char_u *e;
3491
3492 if (space > 0)
3493 {
3494 // We need the tail that fits. With double-byte encoding going
3495 // back from the end is very slow, thus go from the start and keep
3496 // the text that fits in "space" between "s" and "e".
3497 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3498 {
3499 space -= ptr2cells(e);
3500 while (space < 0)
3501 {
3502 space += ptr2cells(s);
3503 MB_PTR_ADV(s);
3504 }
3505 }
Bram Moolenaarcc233582020-12-12 13:32:07 +01003506 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003507 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3508 s > compl_shown_match->cp_fname ? "<" : "", s);
3509 msg((char *)IObuff);
Bram Moolenaarcc233582020-12-12 13:32:07 +01003510 msg_hist_off = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003511 redraw_cmdline = FALSE; // don't overwrite!
3512 }
3513 }
3514
3515 return num_matches;
3516}
3517
3518/*
3519 * Call this while finding completions, to check whether the user has hit a key
3520 * that should change the currently displayed completion, or exit completion
3521 * mode. Also, when compl_pending is not zero, show a completion as soon as
3522 * possible. -- webb
3523 * "frequency" specifies out of how many calls we actually check.
3524 * "in_compl_func" is TRUE when called from complete_check(), don't set
3525 * compl_curr_match.
3526 */
3527 void
3528ins_compl_check_keys(int frequency, int in_compl_func)
3529{
3530 static int count = 0;
3531 int c;
3532
3533 // Don't check when reading keys from a script, :normal or feedkeys().
3534 // That would break the test scripts. But do check for keys when called
3535 // from complete_check().
3536 if (!in_compl_func && (using_script() || ex_normal_busy))
3537 return;
3538
3539 // Only do this at regular intervals
3540 if (++count < frequency)
3541 return;
3542 count = 0;
3543
3544 // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
3545 // can't do its work correctly.
3546 c = vpeekc_any();
3547 if (c != NUL)
3548 {
3549 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3550 {
3551 c = safe_vgetc(); // Eat the character
3552 compl_shows_dir = ins_compl_key2dir(c);
3553 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3554 c != K_UP && c != K_DOWN, in_compl_func);
3555 }
3556 else
3557 {
3558 // Need to get the character to have KeyTyped set. We'll put it
3559 // back with vungetc() below. But skip K_IGNORE.
3560 c = safe_vgetc();
3561 if (c != K_IGNORE)
3562 {
3563 // Don't interrupt completion when the character wasn't typed,
3564 // e.g., when doing @q to replay keys.
3565 if (c != Ctrl_R && KeyTyped)
3566 compl_interrupted = TRUE;
3567
3568 vungetc(c);
3569 }
3570 }
3571 }
3572 if (compl_pending != 0 && !got_int && !compl_no_insert)
3573 {
3574 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
3575
3576 compl_pending = 0;
3577 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
3578 }
3579}
3580
3581/*
3582 * Decide the direction of Insert mode complete from the key typed.
3583 * Returns BACKWARD or FORWARD.
3584 */
3585 static int
3586ins_compl_key2dir(int c)
3587{
3588 if (c == Ctrl_P || c == Ctrl_L
3589 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
3590 return BACKWARD;
3591 return FORWARD;
3592}
3593
3594/*
3595 * Return TRUE for keys that are used for completion only when the popup menu
3596 * is visible.
3597 */
3598 static int
3599ins_compl_pum_key(int c)
3600{
3601 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3602 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3603 || c == K_UP || c == K_DOWN);
3604}
3605
3606/*
3607 * Decide the number of completions to move forward.
3608 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3609 */
3610 static int
3611ins_compl_key2count(int c)
3612{
3613 int h;
3614
3615 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
3616 {
3617 h = pum_get_height();
3618 if (h > 3)
3619 h -= 2; // keep some context
3620 return h;
3621 }
3622 return 1;
3623}
3624
3625/*
3626 * Return TRUE if completion with "c" should insert the match, FALSE if only
3627 * to change the currently selected completion.
3628 */
3629 static int
3630ins_compl_use_match(int c)
3631{
3632 switch (c)
3633 {
3634 case K_UP:
3635 case K_DOWN:
3636 case K_PAGEDOWN:
3637 case K_KPAGEDOWN:
3638 case K_S_DOWN:
3639 case K_PAGEUP:
3640 case K_KPAGEUP:
3641 case K_S_UP:
3642 return FALSE;
3643 }
3644 return TRUE;
3645}
3646
3647/*
3648 * Do Insert mode completion.
3649 * Called when character "c" was typed, which has a meaning for completion.
3650 * Returns OK if completion was done, FAIL if something failed (out of mem).
3651 */
3652 int
3653ins_complete(int c, int enable_pum)
3654{
3655 char_u *line;
3656 int startcol = 0; // column where searched text starts
3657 colnr_T curs_col; // cursor column
3658 int n;
3659 int save_w_wrow;
3660 int save_w_leftcol;
3661 int insert_match;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003662#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003663 int save_did_ai = did_ai;
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02003664#endif
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003665 int flags = CP_ORIGINAL_TEXT;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003666
3667 compl_direction = ins_compl_key2dir(c);
3668 insert_match = ins_compl_use_match(c);
3669
3670 if (!compl_started)
3671 {
3672 // First time we hit ^N or ^P (in a row, I mean)
3673
3674 did_ai = FALSE;
3675#ifdef FEAT_SMARTINDENT
3676 did_si = FALSE;
3677 can_si = FALSE;
3678 can_si_back = FALSE;
3679#endif
3680 if (stop_arrow() == FAIL)
3681 return FAIL;
3682
3683 line = ml_get(curwin->w_cursor.lnum);
3684 curs_col = curwin->w_cursor.col;
3685 compl_pending = 0;
3686
3687 // If this same ctrl_x_mode has been interrupted use the text from
3688 // "compl_startpos" to the cursor as a pattern to add a new word
3689 // instead of expand the one before the cursor, in word-wise if
3690 // "compl_startpos" is not in the same line as the cursor then fix it
3691 // (the line has been split because it was longer than 'tw'). if SOL
3692 // is set then skip the previous pattern, a word at the beginning of
3693 // the line has been inserted, we'll look for that -- Acevedo.
3694 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3695 && compl_cont_mode == ctrl_x_mode)
3696 {
3697 // it is a continued search
3698 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
3699 if (ctrl_x_mode == CTRL_X_NORMAL
3700 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3701 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3702 {
3703 if (compl_startpos.lnum != curwin->w_cursor.lnum)
3704 {
3705 // line (probably) wrapped, set compl_startpos to the
3706 // first non_blank in the line, if it is not a wordchar
3707 // include it to get a better pattern, but then we don't
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003708 // want the "\\<" prefix, check it below
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003709 compl_col = (colnr_T)getwhitecols(line);
3710 compl_startpos.col = compl_col;
3711 compl_startpos.lnum = curwin->w_cursor.lnum;
3712 compl_cont_status &= ~CONT_SOL; // clear SOL if present
3713 }
3714 else
3715 {
3716 // S_IPOS was set when we inserted a word that was at the
3717 // beginning of the line, which means that we'll go to SOL
3718 // mode but first we need to redefine compl_startpos
3719 if (compl_cont_status & CONT_S_IPOS)
3720 {
3721 compl_cont_status |= CONT_SOL;
3722 compl_startpos.col = (colnr_T)(skipwhite(
3723 line + compl_length
3724 + compl_startpos.col) - line);
3725 }
3726 compl_col = compl_startpos.col;
3727 }
3728 compl_length = curwin->w_cursor.col - (int)compl_col;
3729 // IObuff is used to add a "word from the next line" would we
3730 // have enough space? just being paranoid
3731#define MIN_SPACE 75
3732 if (compl_length > (IOSIZE - MIN_SPACE))
3733 {
3734 compl_cont_status &= ~CONT_SOL;
3735 compl_length = (IOSIZE - MIN_SPACE);
3736 compl_col = curwin->w_cursor.col - compl_length;
3737 }
3738 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3739 if (compl_length < 1)
3740 compl_cont_status &= CONT_LOCAL;
3741 }
3742 else if (ctrl_x_mode_line_or_eval())
3743 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
3744 else
3745 compl_cont_status = 0;
3746 }
3747 else
3748 compl_cont_status &= CONT_LOCAL;
3749
3750 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
3751 {
3752 compl_cont_mode = ctrl_x_mode;
3753 if (ctrl_x_mode != CTRL_X_NORMAL)
3754 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
3755 compl_cont_status = 0;
3756 compl_cont_status |= CONT_N_ADDS;
3757 compl_startpos = curwin->w_cursor;
3758 startcol = (int)curs_col;
3759 compl_col = 0;
3760 }
3761
3762 // Work out completion pattern and original text -- webb
3763 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3764 {
3765 if ((compl_cont_status & CONT_SOL)
3766 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3767 {
3768 if (!(compl_cont_status & CONT_ADDING))
3769 {
3770 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3771 ;
3772 compl_col += ++startcol;
3773 compl_length = curs_col - startcol;
3774 }
3775 if (p_ic)
3776 compl_pattern = str_foldcase(line + compl_col,
3777 compl_length, NULL, 0);
3778 else
3779 compl_pattern = vim_strnsave(line + compl_col,
3780 compl_length);
3781 if (compl_pattern == NULL)
3782 return FAIL;
3783 }
3784 else if (compl_cont_status & CONT_ADDING)
3785 {
3786 char_u *prefix = (char_u *)"\\<";
3787
3788 // we need up to 2 extra chars for the prefix
3789 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3790 compl_length) + 2);
3791 if (compl_pattern == NULL)
3792 return FAIL;
3793 if (!vim_iswordp(line + compl_col)
3794 || (compl_col > 0
3795 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
3796 prefix = (char_u *)"";
3797 STRCPY((char *)compl_pattern, prefix);
3798 (void)quote_meta(compl_pattern + STRLEN(prefix),
3799 line + compl_col, compl_length);
3800 }
3801 else if (--startcol < 0
3802 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
3803 {
3804 // Match any word of at least two chars
3805 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3806 if (compl_pattern == NULL)
3807 return FAIL;
3808 compl_col += curs_col;
3809 compl_length = 0;
3810 }
3811 else
3812 {
3813 // Search the point of change class of multibyte character
3814 // or not a word single byte character backward.
3815 if (has_mbyte)
3816 {
3817 int base_class;
3818 int head_off;
3819
3820 startcol -= (*mb_head_off)(line, line + startcol);
3821 base_class = mb_get_class(line + startcol);
3822 while (--startcol >= 0)
3823 {
3824 head_off = (*mb_head_off)(line, line + startcol);
3825 if (base_class != mb_get_class(line + startcol
3826 - head_off))
3827 break;
3828 startcol -= head_off;
3829 }
3830 }
3831 else
3832 while (--startcol >= 0 && vim_iswordc(line[startcol]))
3833 ;
3834 compl_col += ++startcol;
3835 compl_length = (int)curs_col - startcol;
3836 if (compl_length == 1)
3837 {
3838 // Only match word with at least two chars -- webb
3839 // there's no need to call quote_meta,
3840 // alloc(7) is enough -- Acevedo
3841 compl_pattern = alloc(7);
3842 if (compl_pattern == NULL)
3843 return FAIL;
3844 STRCPY((char *)compl_pattern, "\\<");
3845 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3846 STRCAT((char *)compl_pattern, "\\k");
3847 }
3848 else
3849 {
3850 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3851 compl_length) + 2);
3852 if (compl_pattern == NULL)
3853 return FAIL;
3854 STRCPY((char *)compl_pattern, "\\<");
3855 (void)quote_meta(compl_pattern + 2, line + compl_col,
3856 compl_length);
3857 }
3858 }
3859 }
3860 else if (ctrl_x_mode_line_or_eval())
3861 {
3862 compl_col = (colnr_T)getwhitecols(line);
3863 compl_length = (int)curs_col - (int)compl_col;
3864 if (compl_length < 0) // cursor in indent: empty pattern
3865 compl_length = 0;
3866 if (p_ic)
3867 compl_pattern = str_foldcase(line + compl_col, compl_length,
3868 NULL, 0);
3869 else
3870 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3871 if (compl_pattern == NULL)
3872 return FAIL;
3873 }
3874 else if (ctrl_x_mode == CTRL_X_FILES)
3875 {
3876 // Go back to just before the first filename character.
3877 if (startcol > 0)
3878 {
3879 char_u *p = line + startcol;
3880
3881 MB_PTR_BACK(line, p);
3882 while (p > line && vim_isfilec(PTR2CHAR(p)))
3883 MB_PTR_BACK(line, p);
3884 if (p == line && vim_isfilec(PTR2CHAR(p)))
3885 startcol = 0;
3886 else
3887 startcol = (int)(p - line) + 1;
3888 }
3889
3890 compl_col += startcol;
3891 compl_length = (int)curs_col - startcol;
3892 compl_pattern = addstar(line + compl_col, compl_length,
3893 EXPAND_FILES);
3894 if (compl_pattern == NULL)
3895 return FAIL;
3896 }
3897 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3898 {
3899 compl_pattern = vim_strnsave(line, curs_col);
3900 if (compl_pattern == NULL)
3901 return FAIL;
3902 set_cmd_context(&compl_xp, compl_pattern,
3903 (int)STRLEN(compl_pattern), curs_col, FALSE);
3904 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3905 || compl_xp.xp_context == EXPAND_NOTHING)
3906 // No completion possible, use an empty pattern to get a
3907 // "pattern not found" message.
3908 compl_col = curs_col;
3909 else
3910 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3911 compl_length = curs_col - compl_col;
3912 }
3913 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3914 {
3915#ifdef FEAT_COMPL_FUNC
3916 // Call user defined function 'completefunc' with "a:findstart"
3917 // set to 1 to obtain the length of text to use for completion.
3918 typval_T args[3];
3919 int col;
3920 char_u *funcname;
3921 pos_T pos;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003922 int save_State = State;
3923
3924 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3925 // string
3926 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3927 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3928 if (*funcname == NUL)
3929 {
3930 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3931 ? "completefunc" : "omnifunc");
3932 // restore did_ai, so that adding comment leader works
3933 did_ai = save_did_ai;
3934 return FAIL;
3935 }
3936
3937 args[0].v_type = VAR_NUMBER;
3938 args[0].vval.v_number = 1;
3939 args[1].v_type = VAR_STRING;
3940 args[1].vval.v_string = (char_u *)"";
3941 args[2].v_type = VAR_UNKNOWN;
3942 pos = curwin->w_cursor;
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003943 ++textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003944 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +01003945 --textwinlock;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003946
3947 State = save_State;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003948 curwin->w_cursor = pos; // restore the cursor position
3949 validate_cursor();
3950 if (!EQUAL_POS(curwin->w_cursor, pos))
3951 {
3952 emsg(_(e_compldel));
3953 return FAIL;
3954 }
3955
3956 // Return value -2 means the user complete function wants to
3957 // cancel the complete without an error.
3958 // Return value -3 does the same as -2 and leaves CTRL-X mode.
3959 if (col == -2)
3960 return FAIL;
3961 if (col == -3)
3962 {
3963 ctrl_x_mode = CTRL_X_NORMAL;
3964 edit_submode = NULL;
3965 if (!shortmess(SHM_COMPLETIONMENU))
3966 msg_clr_cmdline();
3967 return FAIL;
3968 }
3969
3970 // Reset extended parameters of completion, when start new
3971 // completion.
3972 compl_opt_refresh_always = FALSE;
3973 compl_opt_suppress_empty = FALSE;
3974
3975 if (col < 0)
3976 col = curs_col;
3977 compl_col = col;
3978 if (compl_col > curs_col)
3979 compl_col = curs_col;
3980
3981 // Setup variables for completion. Need to obtain "line" again,
3982 // it may have become invalid.
3983 line = ml_get(curwin->w_cursor.lnum);
3984 compl_length = curs_col - compl_col;
3985 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3986 if (compl_pattern == NULL)
3987#endif
3988 return FAIL;
3989 }
3990 else if (ctrl_x_mode == CTRL_X_SPELL)
3991 {
3992#ifdef FEAT_SPELL
3993 if (spell_bad_len > 0)
3994 compl_col = curs_col - spell_bad_len;
3995 else
3996 compl_col = spell_word_start(startcol);
3997 if (compl_col >= (colnr_T)startcol)
3998 {
3999 compl_length = 0;
4000 compl_col = curs_col;
4001 }
4002 else
4003 {
4004 spell_expand_check_cap(compl_col);
4005 compl_length = (int)curs_col - compl_col;
4006 }
4007 // Need to obtain "line" again, it may have become invalid.
4008 line = ml_get(curwin->w_cursor.lnum);
4009 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4010 if (compl_pattern == NULL)
4011#endif
4012 return FAIL;
4013 }
4014 else
4015 {
4016 internal_error("ins_complete()");
4017 return FAIL;
4018 }
4019
4020 if (compl_cont_status & CONT_ADDING)
4021 {
4022 edit_submode_pre = (char_u *)_(" Adding");
4023 if (ctrl_x_mode_line_or_eval())
4024 {
4025 // Insert a new line, keep indentation but ignore 'comments'
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004026 char_u *old = curbuf->b_p_com;
4027
4028 curbuf->b_p_com = (char_u *)"";
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004029 compl_startpos.lnum = curwin->w_cursor.lnum;
4030 compl_startpos.col = compl_col;
4031 ins_eol('\r');
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004032 curbuf->b_p_com = old;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004033 compl_length = 0;
4034 compl_col = curwin->w_cursor.col;
4035 }
4036 }
4037 else
4038 {
4039 edit_submode_pre = NULL;
4040 compl_startpos.col = compl_col;
4041 }
4042
4043 if (compl_cont_status & CONT_LOCAL)
4044 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4045 else
4046 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4047
4048 // If any of the original typed text has been changed we need to fix
4049 // the redo buffer.
4050 ins_compl_fixRedoBufForLeader(NULL);
4051
4052 // Always add completion for the original text.
4053 vim_free(compl_orig_text);
4054 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004055 if (p_ic)
4056 flags |= CP_ICASE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004057 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar08928322020-01-04 14:32:48 +01004058 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004059 {
4060 VIM_CLEAR(compl_pattern);
4061 VIM_CLEAR(compl_orig_text);
4062 return FAIL;
4063 }
4064
4065 // showmode might reset the internal line pointers, so it must
4066 // be called before line = ml_get(), or when this address is no
4067 // longer needed. -- Acevedo.
4068 edit_submode_extra = (char_u *)_("-- Searching...");
4069 edit_submode_highl = HLF_COUNT;
4070 showmode();
4071 edit_submode_extra = NULL;
4072 out_flush();
4073 }
4074 else if (insert_match && stop_arrow() == FAIL)
4075 return FAIL;
4076
4077 compl_shown_match = compl_curr_match;
4078 compl_shows_dir = compl_direction;
4079
4080 // Find next match (and following matches).
4081 save_w_wrow = curwin->w_wrow;
4082 save_w_leftcol = curwin->w_leftcol;
4083 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4084
4085 // may undisplay the popup menu
4086 ins_compl_upd_pum();
4087
4088 if (n > 1) // all matches have been found
4089 compl_matches = n;
4090 compl_curr_match = compl_shown_match;
4091 compl_direction = compl_shows_dir;
4092
4093 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4094 // mode.
4095 if (got_int && !global_busy)
4096 {
4097 (void)vgetc();
4098 got_int = FALSE;
4099 }
4100
4101 // we found no match if the list has only the "compl_orig_text"-entry
4102 if (compl_first_match == compl_first_match->cp_next)
4103 {
4104 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4105 && compl_length > 1
4106 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4107 edit_submode_highl = HLF_E;
4108 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4109 // because we couldn't expand anything at first place, but if we used
4110 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4111 // (such as M in M'exico) if not tried already. -- Acevedo
4112 if ( compl_length > 1
4113 || (compl_cont_status & CONT_ADDING)
4114 || (ctrl_x_mode != CTRL_X_NORMAL
4115 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4116 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4117 compl_cont_status &= ~CONT_N_ADDS;
4118 }
4119
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004120 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004121 compl_cont_status |= CONT_S_IPOS;
4122 else
4123 compl_cont_status &= ~CONT_S_IPOS;
4124
4125 if (edit_submode_extra == NULL)
4126 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02004127 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004128 {
4129 edit_submode_extra = (char_u *)_("Back at original");
4130 edit_submode_highl = HLF_W;
4131 }
4132 else if (compl_cont_status & CONT_S_IPOS)
4133 {
4134 edit_submode_extra = (char_u *)_("Word from other line");
4135 edit_submode_highl = HLF_COUNT;
4136 }
4137 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
4138 {
4139 edit_submode_extra = (char_u *)_("The only match");
4140 edit_submode_highl = HLF_COUNT;
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004141 compl_curr_match->cp_number = 1;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004142 }
4143 else
4144 {
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004145#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004146 // Update completion sequence number when needed.
4147 if (compl_curr_match->cp_number == -1)
Bram Moolenaarf9d51352020-10-26 19:22:42 +01004148 ins_compl_update_sequence_numbers();
Bram Moolenaar977fd0b2020-10-27 09:12:45 +01004149#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004150 // The match should always have a sequence number now, this is
4151 // just a safety check.
4152 if (compl_curr_match->cp_number != -1)
4153 {
4154 // Space for 10 text chars. + 2x10-digit no.s = 31.
4155 // Translations may need more than twice that.
4156 static char_u match_ref[81];
4157
4158 if (compl_matches > 0)
4159 vim_snprintf((char *)match_ref, sizeof(match_ref),
4160 _("match %d of %d"),
4161 compl_curr_match->cp_number, compl_matches);
4162 else
4163 vim_snprintf((char *)match_ref, sizeof(match_ref),
4164 _("match %d"),
4165 compl_curr_match->cp_number);
4166 edit_submode_extra = match_ref;
4167 edit_submode_highl = HLF_R;
4168 if (dollar_vcol >= 0)
4169 curs_columns(FALSE);
4170 }
4171 }
4172 }
4173
4174 // Show a message about what (completion) mode we're in.
4175 if (!compl_opt_suppress_empty)
4176 {
4177 showmode();
4178 if (!shortmess(SHM_COMPLETIONMENU))
4179 {
4180 if (edit_submode_extra != NULL)
4181 {
4182 if (!p_smd)
Bram Moolenaarcc233582020-12-12 13:32:07 +01004183 {
4184 msg_hist_off = TRUE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004185 msg_attr((char *)edit_submode_extra,
4186 edit_submode_highl < HLF_COUNT
4187 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarcc233582020-12-12 13:32:07 +01004188 msg_hist_off = FALSE;
4189 }
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004190 }
4191 else
4192 msg_clr_cmdline(); // necessary for "noshowmode"
4193 }
4194 }
4195
4196 // Show the popup menu, unless we got interrupted.
4197 if (enable_pum && !compl_interrupted)
4198 show_pum(save_w_wrow, save_w_leftcol);
4199
4200 compl_was_interrupted = compl_interrupted;
4201 compl_interrupted = FALSE;
4202
4203 return OK;
4204}
4205
4206 static void
4207show_pum(int prev_w_wrow, int prev_w_leftcol)
4208{
4209 // RedrawingDisabled may be set when invoked through complete().
4210 int n = RedrawingDisabled;
4211
4212 RedrawingDisabled = 0;
4213
4214 // If the cursor moved or the display scrolled we need to remove the pum
4215 // first.
4216 setcursor();
4217 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
4218 ins_compl_del_pum();
4219
4220 ins_compl_show_pum();
4221 setcursor();
4222 RedrawingDisabled = n;
4223}
4224
4225/*
4226 * Looks in the first "len" chars. of "src" for search-metachars.
4227 * If dest is not NULL the chars. are copied there quoting (with
4228 * a backslash) the metachars, and dest would be NUL terminated.
4229 * Returns the length (needed) of dest
4230 */
4231 static unsigned
4232quote_meta(char_u *dest, char_u *src, int len)
4233{
4234 unsigned m = (unsigned)len + 1; // one extra for the NUL
4235
4236 for ( ; --len >= 0; src++)
4237 {
4238 switch (*src)
4239 {
4240 case '.':
4241 case '*':
4242 case '[':
4243 if (ctrl_x_mode == CTRL_X_DICTIONARY
4244 || ctrl_x_mode == CTRL_X_THESAURUS)
4245 break;
4246 // FALLTHROUGH
4247 case '~':
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004248 if (!magic_isset()) // quote these only if magic is set
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004249 break;
4250 // FALLTHROUGH
4251 case '\\':
4252 if (ctrl_x_mode == CTRL_X_DICTIONARY
4253 || ctrl_x_mode == CTRL_X_THESAURUS)
4254 break;
4255 // FALLTHROUGH
4256 case '^': // currently it's not needed.
4257 case '$':
4258 m++;
4259 if (dest != NULL)
4260 *dest++ = '\\';
4261 break;
4262 }
4263 if (dest != NULL)
4264 *dest++ = *src;
4265 // Copy remaining bytes of a multibyte character.
4266 if (has_mbyte)
4267 {
4268 int i, mb_len;
4269
4270 mb_len = (*mb_ptr2len)(src) - 1;
4271 if (mb_len > 0 && len >= mb_len)
4272 for (i = 0; i < mb_len; ++i)
4273 {
4274 --len;
4275 ++src;
4276 if (dest != NULL)
4277 *dest++ = *src;
4278 }
4279 }
4280 }
4281 if (dest != NULL)
4282 *dest = NUL;
4283
4284 return m;
4285}
4286
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004287#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004288 void
4289free_insexpand_stuff(void)
4290{
4291 VIM_CLEAR(compl_orig_text);
4292}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004293#endif
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004294
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004295#ifdef FEAT_SPELL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004296/*
4297 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4298 * spelled word, if there is one.
4299 */
4300 static void
4301spell_back_to_badword(void)
4302{
4303 pos_T tpos = curwin->w_cursor;
4304
4305 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
4306 if (curwin->w_cursor.col != tpos.col)
4307 start_arrow(&tpos);
4308}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004309#endif